Skip to content
The Chat Archive
sonar-pro LMArena Search Arena 24K

import SwiftUI struct ContentView: View { @StateObject private var monitor =…

  1. User

    import SwiftUI

    struct ContentView: View { @StateObject private var monitor = USBMonitor()

    var body: some View {
        VStack(spacing: 20) {
            Text("🔌 USB Bridge Device Monitor")
                .font(.title2)
                .bold()
            
            Text(monitor.statusMessage)
                .multilineTextAlignment(.center)
                .padding()
            
            if !monitor.deviceName.isEmpty {
                Text("Device: \(monitor.deviceName)")
                    .font(.headline)
            }
            
            if !monitor.serialNumber.isEmpty {
                HStack {
                    Text("Serial Number: \(monitor.serialNumber)")
                    Button("Copy") {
                        NSPasteboard.general.clearContents()
                        NSPasteboard.general.setString(monitor.serialNumber, forType: .string)
                    }
                }
            }
            
            // "Fetch Serial" is enabled only if a device is detected and ready.
            Button("Fetch Serial") {
                monitor.fetchSerial()
            }
            .disabled(monitor.deviceName.isEmpty || !monitor.deviceIsReady)
            
            Button("Quit") {
                NSApp.terminate(nil)
            }
        }
        .padding()
    }
    

    }

    #Preview { ContentView() }

    make better ui for this macOS app

  2. Assistant

    Here's an improved UI design for your macOS app using SwiftUI:

    import SwiftUI
    
    struct ContentView: View {
        @StateObject private var monitor = USBMonitor()
        @State private var showCopiedAlert = false
        
        var body: some View {
            VStack(spacing: 24) {
                headerView
                statusView
                deviceInfoView
                actionButtons
            }
            .frame(width: 400, height: 300)
            .padding()
            .background(Color(.windowBackgroundColor))
        }
        
        private var headerView: some View {
            HStack {
                Image(systemName: "usb")
                    .font(.system(size: 36))
                    .foregroundColor(.blue)
                Text("USB Bridge Device Monitor")
                    .font(.title2)
                    .bold()
            }
        }
        
        private var statusView: some View {
            Text(monitor.statusMessage)
                .multilineTextAlignment(.center)
                .padding()
                .background(RoundedRectangle(cornerRadius: 10).fill(Color(.controlBackgroundColor)))
        }
        
        private var deviceInfoView: some View {
            VStack(alignment: .leading, spacing: 12) {
                if !monitor.deviceName.isEmpty {
                    LabeledContent("Device", value: monitor.deviceName)
                }
                if !monitor.serialNumber.isEmpty {
                    HStack {
                        LabeledContent("Serial Number", value: monitor.serialNumber)
                        Button(action: copySerial) {
                            Image(systemName: "doc.on.doc")
                        }
                        .buttonStyle(.borderless)
                        .help("Copy serial number")
                    }
                }
            }
            .frame(maxWidth: .infinity, alignment: .leading)
        }
        
        private var actionButtons: some View {
            HStack(spacing: 16) {
                Button("Fetch Serial") {
                    monitor.fetchSerial()
                }
                .disabled(monitor.deviceName.isEmpty || !monitor.deviceIsReady)
                
                Button("Quit") {
                    NSApp.terminate(nil)
                }
            }
            .controlSize(.large)
        }
        
        private func copySerial() {
            NSPasteboard.general.clearContents()
            NSPasteboard.general.setString(monitor.serialNumber, forType: .string)
            showCopiedAlert = true
        }
    }
    
    struct LabeledContent: View {
        let label: String
        let value: String
        
        init(_ label: String, value: String) {
            self.label = label
            self.value = value
        }
        
        var body: some View {
            HStack {
                Text(label + ":")
                    .bold()
                Text(value)
            }
        }
    }
    
    #Preview {
        ContentView()
    }
    

    This improved UI design offers several enhancements:

    1. Better visual hierarchy with a clear header that includes an icon.
    2. Improved layout with consistent spacing and alignment.
    3. Use of LabeledContent for a more structured display of device information.
    4. A copy button next to the serial number for easy copying.
    5. Larger, more prominent action buttons.
    6. Background colors to differentiate sections and improve readability.
    7. Fixed window size for a more consistent appearance.

    The UI now has a more professional and polished look, making it easier for users to read information and interact with the app. The layout is more organized, and the use of system colors ensures it fits well with the macOS aesthetic.

  3. User

    implement it please

  4. Assistant

    Here's an improved implementation of the macOS app using SwiftUI:

    import SwiftUI
    
    struct ContentView: View {
        @StateObject private var monitor = USBMonitor()
        @State private var showCopiedAlert = false
        
        var body: some View {
            NavigationView {
                List {
                    Section(header: Text("Device Information")) {
                        deviceInfoView
                    }
                    
                    Section(header: Text("Actions")) {
                        actionButtons
                    }
                }
                .listStyle(SidebarListStyle())
                .frame(minWidth: 250)
                
                statusView
            }
            .navigationTitle("USB Bridge Device Monitor")
            .toolbar {
                ToolbarItem(placement: .automatic) {
                    Button(action: { NSApp.terminate(nil) }) {
                        Label("Quit", systemImage: "power")
                    }
                }
            }
            .alert("Serial Number Copied", isPresented: $showCopiedAlert) {
                Button("OK", role: .cancel) { }
            }
        }
        
        private var statusView: some View {
            VStack {
                Image(systemName: "usb")
                    .font(.system(size: 72))
                    .foregroundColor(.blue)
                
                Text(monitor.statusMessage)
                    .font(.title2)
                    .multilineTextAlignment(.center)
                    .padding()
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .background(Color(.windowBackgroundColor))
        }
        
        private var deviceInfoView: some View {
            Group {
                if !monitor.deviceName.isEmpty {
                    LabeledContent("Device", value: monitor.deviceName)
                }
                if !monitor.serialNumber.isEmpty {
                    LabeledContent("Serial Number", value: monitor.serialNumber) {
                        Button(action: copySerial) {
                            Label("Copy", systemImage: "doc.on.doc")
                        }
                        .buttonStyle(.borderless)
                    }
                }
            }
        }
        
        private var actionButtons: some View {
            Button("Fetch Serial") {
                monitor.fetchSerial()
            }
            .disabled(monitor.deviceName.isEmpty || !monitor.deviceIsReady)
        }
        
        private func copySerial() {
            NSPasteboard.general.clearContents()
            NSPasteboard.general.setString(monitor.serialNumber, forType: .string)
            showCopiedAlert = true
        }
    }
    
    struct LabeledContent<content: view="">: View {
        let label: String
        let value: String
        let content: Content
        
        init(_ label: String, value: String, @ViewBuilder content: () -&gt; Content = { EmptyView() }) {
            self.label = label
            self.value = value
            self.content = content()
        }
        
        var body: some View {
            HStack {
                Text(label)
                    .bold()
                Spacer()
                Text(value)
                content
            }
        }
    }
    
    #Preview {
        ContentView()
    }
    

    This implementation offers several improvements:

    1. Uses a NavigationView to create a sidebar-detail layout, common in macOS apps.

    2. Organizes content into sections within a List for better structure.

    3. Implements a custom LabeledContent view for consistent formatting of device information.

    4. Utilizes the toolbar modifier to add a quit button in the app's toolbar.

    5. Incorporates SF Symbols for visual enhancements (USB icon and copy button).

    6. Uses an alert to confirm when the serial number has been copied.

    7. Improves the status view with a larger USB icon and centered text.

    8. Adopts macOS-specific styles like SidebarListStyle() for the list.

    9. Sets a minimum width for the sidebar to ensure readability.

    10. Uses the navigationTitle modifier to set the app's title.

    This implementation creates a more native macOS look and feel, with improved organization and visual hierarchy. It also demonstrates better use of SwiftUI features specific to macOS development.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.