From 10afec576da03ed2e246cb3563ee466269c62173 Mon Sep 17 00:00:00 2001 From: Takayama Fumihiko Date: Mon, 18 Sep 2023 09:33:07 +0900 Subject: [PATCH] Add EventViewer > ContentMainView --- src/apps/EventViewer/src/AppDelegate.swift | 2 +- .../src/View/ContentMainView.swift | 126 ++++++++++++++++++ .../EventViewer/src/View/ContentView.swift | 116 +--------------- .../src/View/InputMonitoringAlertView.swift | 10 +- .../swift/Views}/SheetCloseButton.swift | 0 5 files changed, 137 insertions(+), 117 deletions(-) create mode 100644 src/apps/EventViewer/src/View/ContentMainView.swift rename src/apps/{SettingsWindow/src/View => share/swift/Views}/SheetCloseButton.swift (100%) diff --git a/src/apps/EventViewer/src/AppDelegate.swift b/src/apps/EventViewer/src/AppDelegate.swift index 0306b5d9e..97f133d00 100644 --- a/src/apps/EventViewer/src/AppDelegate.swift +++ b/src/apps/EventViewer/src/AppDelegate.swift @@ -27,7 +27,7 @@ public class AppDelegate: NSObject, NSApplicationDelegate { defer: false ) window!.title = "Karabiner-EventViewer" - window!.contentView = NSHostingView(rootView: ContentView(window: window)) + window!.contentView = NSHostingView(rootView: ContentView()) window!.center() window!.makeKeyAndOrderFront(self) diff --git a/src/apps/EventViewer/src/View/ContentMainView.swift b/src/apps/EventViewer/src/View/ContentMainView.swift new file mode 100644 index 000000000..40bddcda8 --- /dev/null +++ b/src/apps/EventViewer/src/View/ContentMainView.swift @@ -0,0 +1,126 @@ +import SwiftUI + +enum NavigationTag: String { + case main + case frontmostApplication + case variables + case devices + case systemExtensions + case unknownEvents + case settings +} + +struct ContentMainView: View { + @State private var selection: NavigationTag = .main + + var body: some View { + VStack { + HStack { + VStack(alignment: .leading, spacing: 0) { + Button( + action: { + selection = .main + }, + label: { + SidebarLabelView(text: "Main", systemImage: "magnifyingglass") + } + ) + .sidebarButtonStyle(selected: selection == .main) + + Button( + action: { + selection = .frontmostApplication + }, + label: { + SidebarLabelView(text: "Frontmost Application", systemImage: "triangle.circle") + } + ) + .sidebarButtonStyle(selected: selection == .frontmostApplication) + + Button( + action: { + selection = .variables + }, + label: { + SidebarLabelView(text: "Variables", systemImage: "cube") + } + ) + .sidebarButtonStyle(selected: selection == .variables) + + Button( + action: { + selection = .devices + }, + label: { + SidebarLabelView(text: "Devices", systemImage: "keyboard") + } + ) + .sidebarButtonStyle(selected: selection == .devices) + + Button( + action: { + selection = .systemExtensions + }, + label: { + SidebarLabelView(text: "System Extensions", systemImage: "puzzlepiece") + } + ) + .sidebarButtonStyle(selected: selection == .systemExtensions) + + Button( + action: { + selection = .unknownEvents + }, + label: { + SidebarLabelView(text: "Unknown Events", systemImage: "questionmark.square.dashed") + } + ) + .sidebarButtonStyle(selected: selection == .unknownEvents) + + Button( + action: { + selection = .settings + }, + label: { + SidebarLabelView(text: "Settings", systemImage: "gearshape") + } + ) + .sidebarButtonStyle(selected: selection == .settings) + + Spacer() + } + .frame(width: 250) + + Divider() + + switch selection { + case .main: + MainView() + case .frontmostApplication: + FrontmostApplicationView() + case .variables: + VariablesView() + case .devices: + DevicesView() + case .systemExtensions: + SystemExtensionsView() + case .unknownEvents: + UnknownEventsView() + case .settings: + SettingsView() + } + } + } + .frame( + minWidth: 1100, + maxWidth: .infinity, + minHeight: 650, + maxHeight: .infinity) + } +} + +struct ContentMainView_Previews: PreviewProvider { + static var previews: some View { + ContentMainView() + } +} diff --git a/src/apps/EventViewer/src/View/ContentView.swift b/src/apps/EventViewer/src/View/ContentView.swift index 623956a5b..dcdff08e9 100644 --- a/src/apps/EventViewer/src/View/ContentView.swift +++ b/src/apps/EventViewer/src/View/ContentView.swift @@ -1,122 +1,14 @@ import SwiftUI -enum NavigationTag: String { - case main - case frontmostApplication - case variables - case devices - case systemExtensions - case unknownEvents - case settings -} - struct ContentView: View { - let window: NSWindow? - @ObservedObject var inputMonitoringAlertData = InputMonitoringAlertData.shared - @State private var selection: NavigationTag = .main var body: some View { VStack { - if inputMonitoringAlertData.showing { - InputMonitoringAlertView(window: window) - } else { - HStack { - VStack(alignment: .leading, spacing: 0) { - Button( - action: { - selection = .main - }, - label: { - SidebarLabelView(text: "Main", systemImage: "magnifyingglass") - } - ) - .sidebarButtonStyle(selected: selection == .main) - - Button( - action: { - selection = .frontmostApplication - }, - label: { - SidebarLabelView(text: "Frontmost Application", systemImage: "triangle.circle") - } - ) - .sidebarButtonStyle(selected: selection == .frontmostApplication) - - Button( - action: { - selection = .variables - }, - label: { - SidebarLabelView(text: "Variables", systemImage: "cube") - } - ) - .sidebarButtonStyle(selected: selection == .variables) - - Button( - action: { - selection = .devices - }, - label: { - SidebarLabelView(text: "Devices", systemImage: "keyboard") - } - ) - .sidebarButtonStyle(selected: selection == .devices) - - Button( - action: { - selection = .systemExtensions - }, - label: { - SidebarLabelView(text: "System Extensions", systemImage: "puzzlepiece") - } - ) - .sidebarButtonStyle(selected: selection == .systemExtensions) - - Button( - action: { - selection = .unknownEvents - }, - label: { - SidebarLabelView(text: "Unknown Events", systemImage: "questionmark.square.dashed") - } - ) - .sidebarButtonStyle(selected: selection == .unknownEvents) - - Button( - action: { - selection = .settings - }, - label: { - SidebarLabelView(text: "Settings", systemImage: "gearshape") - } - ) - .sidebarButtonStyle(selected: selection == .settings) - - Spacer() - } - .frame(width: 250) - - Divider() - - switch selection { - case .main: - MainView() - case .frontmostApplication: - FrontmostApplicationView() - case .variables: - VariablesView() - case .devices: - DevicesView() - case .systemExtensions: - SystemExtensionsView() - case .unknownEvents: - UnknownEventsView() - case .settings: - SettingsView() - } + ContentMainView() + .alert(isPresented: inputMonitoringAlertData.showing) { + InputMonitoringAlertView() } - } } .frame( minWidth: 1100, @@ -128,6 +20,6 @@ struct ContentView: View { struct ContentView_Previews: PreviewProvider { static var previews: some View { - ContentView(window: nil) + ContentView() } } diff --git a/src/apps/EventViewer/src/View/InputMonitoringAlertView.swift b/src/apps/EventViewer/src/View/InputMonitoringAlertView.swift index 0aef3268b..da469b3fb 100644 --- a/src/apps/EventViewer/src/View/InputMonitoringAlertView.swift +++ b/src/apps/EventViewer/src/View/InputMonitoringAlertView.swift @@ -7,8 +7,6 @@ class InputMonitoringAlertData: ObservableObject { } struct InputMonitoringAlertView: View { - let window: NSWindow? - var body: some View { VStack(alignment: .center, spacing: 20.0) { Label( @@ -35,6 +33,10 @@ struct InputMonitoringAlertView: View { .aspectRatio(contentMode: .fit) .frame(height: 300.0) .border(Color.gray, width: 1) + + SheetCloseButton { + InputMonitoringAlertData.shared.showing = false + } } .frame(width: 800) .padding() @@ -45,14 +47,14 @@ struct InputMonitoringAlertView: View { string: "x-apple.systempreferences:com.apple.preference.security?Privacy_ListenEvent")! NSWorkspace.shared.open(url) - window?.orderBack(self) + NSApp.miniaturizeAll(nil) } } struct InputMonitoringAlertView_Previews: PreviewProvider { static var previews: some View { Group { - InputMonitoringAlertView(window: nil) + InputMonitoringAlertView() .previewLayout(.sizeThatFits) } } diff --git a/src/apps/SettingsWindow/src/View/SheetCloseButton.swift b/src/apps/share/swift/Views/SheetCloseButton.swift similarity index 100% rename from src/apps/SettingsWindow/src/View/SheetCloseButton.swift rename to src/apps/share/swift/Views/SheetCloseButton.swift