import SwiftUI struct ContentView: View { @StateObject private var monitor =…
- 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
- 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:
- Better visual hierarchy with a clear header that includes an icon.
- Improved layout with consistent spacing and alignment.
- Use of
LabeledContentfor a more structured display of device information. - A copy button next to the serial number for easy copying.
- Larger, more prominent action buttons.
- Background colors to differentiate sections and improve readability.
- 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.
- User
implement it please
- 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: () -> 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:
-
Uses a
NavigationViewto create a sidebar-detail layout, common in macOS apps. -
Organizes content into sections within a
Listfor better structure. -
Implements a custom
LabeledContentview for consistent formatting of device information. -
Utilizes the
toolbarmodifier to add a quit button in the app's toolbar. -
Incorporates SF Symbols for visual enhancements (USB icon and copy button).
-
Uses an alert to confirm when the serial number has been copied.
-
Improves the status view with a larger USB icon and centered text.
-
Adopts macOS-specific styles like
SidebarListStyle()for the list. -
Sets a minimum width for the sidebar to ensure readability.
-
Uses the
navigationTitlemodifier 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.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.