From 30922826e8f2f4306a428423dd3e54e66488eb26 Mon Sep 17 00:00:00 2001 From: mrFq1 <1xxbx0il0@mozmail.com> Date: Sat, 27 May 2023 18:14:13 +0800 Subject: [PATCH] feat: toolbar items --- ClashX Dashboard.xcodeproj/project.pbxproj | 4 ++ .../Connections/ConnectionsView.swift | 13 ++-- .../Views/ContentTabs/Logs/LogsView.swift | 23 +++++++ .../ContentTabs/Providers/ProvidersView.swift | 14 +++- .../ContentTabs/Proxies/ProxiesView.swift | 12 ++++ ClashX Dashboard/Views/ContentView.swift | 13 ---- .../Views/SidebarView/SidebarListView.swift | 68 +++++++++++++++++++ .../Views/SidebarView/SidebarView.swift | 41 ++--------- 8 files changed, 134 insertions(+), 54 deletions(-) create mode 100644 ClashX Dashboard/Views/SidebarView/SidebarListView.swift diff --git a/ClashX Dashboard.xcodeproj/project.pbxproj b/ClashX Dashboard.xcodeproj/project.pbxproj index 2d5ea24..db35c82 100644 --- a/ClashX Dashboard.xcodeproj/project.pbxproj +++ b/ClashX Dashboard.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 010F693B29ED639A00BAAFB5 /* ClashServerAppStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 010F693A29ED639A00BAAFB5 /* ClashServerAppStorage.swift */; }; + 01359A392A21D88B00A2B3FB /* SidebarListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01359A382A21D88B00A2B3FB /* SidebarListView.swift */; }; 01505C4A2A147B84001ACC4F /* DBProviderStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01505C492A147B84001ACC4F /* DBProviderStorage.swift */; }; 01505C4C2A14A1A3001ACC4F /* ProviderRowView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01505C4B2A14A1A3001ACC4F /* ProviderRowView.swift */; }; 01505C4E2A14AAEB001ACC4F /* ProviderProxiesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01505C4D2A14AAEB001ACC4F /* ProviderProxiesView.swift */; }; @@ -69,6 +70,7 @@ /* Begin PBXFileReference section */ 010F693A29ED639A00BAAFB5 /* ClashServerAppStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClashServerAppStorage.swift; sourceTree = ""; }; + 01359A382A21D88B00A2B3FB /* SidebarListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SidebarListView.swift; sourceTree = ""; }; 01505C492A147B84001ACC4F /* DBProviderStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DBProviderStorage.swift; sourceTree = ""; }; 01505C4B2A14A1A3001ACC4F /* ProviderRowView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProviderRowView.swift; sourceTree = ""; }; 01505C4D2A14AAEB001ACC4F /* ProviderProxiesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProviderProxiesView.swift; sourceTree = ""; }; @@ -282,6 +284,7 @@ isa = PBXGroup; children = ( 0192317029DD566000539EDD /* SidebarView.swift */, + 01359A382A21D88B00A2B3FB /* SidebarListView.swift */, 0192318429DD7DCD00539EDD /* SidebarItemView.swift */, 0192318229DD70B400539EDD /* SidebarItem.swift */, ); @@ -481,6 +484,7 @@ 01A351A229DD8F440054894E /* RuleItemView.swift in Sources */, 01F885D529E053DE008241EB /* ProxyItemView.swift in Sources */, 0172F1312A1FB06100EE2B6D /* String+Encode.swift in Sources */, + 01359A392A21D88B00A2B3FB /* SidebarListView.swift in Sources */, 01DCEFB12A150E8B00DBBDB3 /* RuleProvidersRowView.swift in Sources */, 01F5E3F42A1E53F4008F3DEB /* ConfigItemView.swift in Sources */, 0172F1352A1FB0B900EE2B6D /* ConfigManager.swift in Sources */, diff --git a/ClashX Dashboard/Views/ContentTabs/Connections/ConnectionsView.swift b/ClashX Dashboard/Views/ContentTabs/Connections/ConnectionsView.swift index 27418d2..bee6d84 100644 --- a/ClashX Dashboard/Views/ContentTabs/Connections/ConnectionsView.swift +++ b/ClashX Dashboard/Views/ContentTabs/Connections/ConnectionsView.swift @@ -18,10 +18,15 @@ struct ConnectionsView: View { filterString: searchString) .background(Color(nsColor: .textBackgroundColor)) .searchable(text: $searchString) - - - - + .toolbar { + ToolbarItem { + Button { + ApiRequest.closeAllConnection() + } label: { + Image(systemName: "stop.circle.fill") + } + } + } } } diff --git a/ClashX Dashboard/Views/ContentTabs/Logs/LogsView.swift b/ClashX Dashboard/Views/ContentTabs/Logs/LogsView.swift index cdf82bc..5b11790 100644 --- a/ClashX Dashboard/Views/ContentTabs/Logs/LogsView.swift +++ b/ClashX Dashboard/Views/ContentTabs/Logs/LogsView.swift @@ -11,6 +11,7 @@ struct LogsView: View { @EnvironmentObject var logStorage: ClashLogStorage @State var searchString: String = "" + @State var logLevel = ConfigManager.selectLoggingApiLevel var logs: [ClashLogStorage.ClashLog] { let logs: [ClashLogStorage.ClashLog] = logStorage.logs.reversed() @@ -45,6 +46,28 @@ struct LogsView: View { TableColumn("", value: \.log) } .searchable(text: $searchString) + .toolbar { + ToolbarItem { + Picker("", selection: $logLevel) { + ForEach([ + ClashLogLevel.silent, + .error, + .warning, + .info, + .debug + ], id: \.self) { + Text($0.rawValue.capitalized).tag($0) + } + } + .pickerStyle(.menu) + .onChange(of: logLevel) { newValue in + guard newValue != ConfigManager.selectLoggingApiLevel else { return } + logStorage.logs.removeAll() + ConfigManager.selectLoggingApiLevel = newValue + ApiRequest.shared.resetLogStreamApi() + } + } + } } } diff --git a/ClashX Dashboard/Views/ContentTabs/Providers/ProvidersView.swift b/ClashX Dashboard/Views/ContentTabs/Providers/ProvidersView.swift index 26bdb07..587096d 100644 --- a/ClashX Dashboard/Views/ContentTabs/Providers/ProvidersView.swift +++ b/ClashX Dashboard/Views/ContentTabs/Providers/ProvidersView.swift @@ -8,9 +8,11 @@ import SwiftUI struct ProvidersView: View { @ObservedObject var providerStorage = DBProviderStorage() - @EnvironmentObject var hideProxyNames: HideProxyNames + @State private var searchString = ProxiesSearchString() + @StateObject private var hideProxyNames = HideProxyNames() + var body: some View { NavigationView { @@ -40,6 +42,16 @@ struct ProvidersView: View { .onAppear { loadProviders() } + .environmentObject(hideProxyNames) + .toolbar { + ToolbarItem { + Button { + hideProxyNames.hide = !hideProxyNames.hide + } label: { + Image(systemName: hideProxyNames.hide ? "eyeglasses" : "wand.and.stars") + } + } + } } func loadProviders() { diff --git a/ClashX Dashboard/Views/ContentTabs/Proxies/ProxiesView.swift b/ClashX Dashboard/Views/ContentTabs/Proxies/ProxiesView.swift index 0d557d2..77ebb09 100644 --- a/ClashX Dashboard/Views/ContentTabs/Proxies/ProxiesView.swift +++ b/ClashX Dashboard/Views/ContentTabs/Proxies/ProxiesView.swift @@ -19,6 +19,8 @@ struct ProxiesView: View { @State private var searchString = ProxiesSearchString() @State private var isGlobalMode = false + @StateObject private var hideProxyNames = HideProxyNames() + var body: some View { NavigationView { List(proxyStorage.groups, id: \.id) { group in @@ -36,6 +38,16 @@ struct ProxiesView: View { .onAppear { loadProxies() } + .environmentObject(hideProxyNames) + .toolbar { + ToolbarItem { + Button { + hideProxyNames.hide = !hideProxyNames.hide + } label: { + Image(systemName: hideProxyNames.hide ? "eyeglasses" : "wand.and.stars") + } + } + } } diff --git a/ClashX Dashboard/Views/ContentView.swift b/ClashX Dashboard/Views/ContentView.swift index cc4085a..dc917d1 100644 --- a/ClashX Dashboard/Views/ContentView.swift +++ b/ClashX Dashboard/Views/ContentView.swift @@ -16,8 +16,6 @@ struct ContentView: View { private let runningState = NotificationCenter.default.publisher(for: .init("ClashRunningStateChanged")) @State private var isRunning = false - @StateObject private var hideProxyNames = HideProxyNames() - var body: some View { Group { if !isRunning { @@ -31,7 +29,6 @@ struct ContentView: View { } } } - .environmentObject(hideProxyNames) .toolbar { ToolbarItem(placement: .navigation) { Button { @@ -40,16 +37,6 @@ struct ContentView: View { Image(systemName: "sidebar.left") } .help("Toggle Sidebar") - .disabled(!isRunning) - } - - ToolbarItem { - Button { - hideProxyNames.hide = !hideProxyNames.hide - } label: { - Image(systemName: hideProxyNames.hide ? "eyeglasses" : "wand.and.stars") - } - .disabled(!isRunning) } } .onReceive(runningState) { _ in diff --git a/ClashX Dashboard/Views/SidebarView/SidebarListView.swift b/ClashX Dashboard/Views/SidebarView/SidebarListView.swift new file mode 100644 index 0000000..80ae113 --- /dev/null +++ b/ClashX Dashboard/Views/SidebarView/SidebarListView.swift @@ -0,0 +1,68 @@ +// +// SidebarListView.swift +// ClashX Dashboard +// +// + +import SwiftUI + +struct SidebarListView: View { + + @Binding var selectionName: String? + + var body: some View { + List { + + NavigationLink(destination: OverviewView(), + tag: "Overview", + selection: $selectionName) { + Label("Overview", systemImage: "chart.bar.xaxis") + } + + NavigationLink(destination: ProxiesView(), + tag: "Proxies", + selection: $selectionName) { + Label("Proxies", systemImage: "globe.asia.australia") + } + + NavigationLink(destination: ProvidersView(), + tag: "Providers", + selection: $selectionName) { + Label("Providers", systemImage: "link.icloud") + } + + NavigationLink(destination: RulesView(), + tag: "Rules", + selection: $selectionName) { + Label("Rules", systemImage: "waveform.and.magnifyingglass") + } + + NavigationLink(destination: ConnectionsView(), + tag: "Conns", + selection: $selectionName) { + Label("Conns", systemImage: "app.connected.to.app.below.fill") + } + + NavigationLink(destination: ConfigView(), + tag: "Config", + selection: $selectionName) { + Label("Config", systemImage: "slider.horizontal.3") + } + + NavigationLink(destination: LogsView(), + tag: "Logs", + selection: $selectionName) { + Label("Logs", systemImage: "wand.and.stars.inverse") + } + + + } + .listStyle(.sidebar) + } +} + +//struct SidebarListView_Previews: PreviewProvider { +// static var previews: some View { +// SidebarListView() +// } +//} diff --git a/ClashX Dashboard/Views/SidebarView/SidebarView.swift b/ClashX Dashboard/Views/SidebarView/SidebarView.swift index 3f633db..33128da 100644 --- a/ClashX Dashboard/Views/SidebarView/SidebarView.swift +++ b/ClashX Dashboard/Views/SidebarView/SidebarView.swift @@ -15,50 +15,19 @@ struct SidebarView: View { @State private var sidebarSelectionName: String? = "Overview" - @State private var sidebarItems = [ - SidebarItem(name: "Overview", - icon: "chart.bar.xaxis", - view: AnyView(OverviewView())), - - SidebarItem(name: "Proxies", - icon: "globe.asia.australia", - view: AnyView(ProxiesView())), - - SidebarItem(name: "Providers", - icon: "link.icloud", - view: AnyView(ProvidersView())), - - SidebarItem(name: "Rules", - icon: "waveform.and.magnifyingglass", - view: AnyView(RulesView())), - - SidebarItem(name: "Conns", - icon: "app.connected.to.app.below.fill", - view: AnyView(ConnectionsView())), - - SidebarItem(name: "Config", - icon: "slider.horizontal.3", - view: AnyView(ConfigView())), - - SidebarItem(name: "Logs", - icon: "wand.and.stars.inverse", - view: AnyView(LogsView())) - ] - var body: some View { ScrollViewReader { scrollViewProxy in - List(sidebarItems, id: \.id) { item in - SidebarItemView(item: item, selectionName: $sidebarSelectionName) - } - .listStyle(.sidebar) + SidebarListView(selectionName: $sidebarSelectionName) } .environmentObject(clashApiDatasStorage.overviewData) .environmentObject(clashApiDatasStorage.logStorage) .environmentObject(clashApiDatasStorage.connsStorage) .onAppear { - ConfigManager.selectLoggingApiLevel = .debug - clashApiDatasStorage.resetStreamApi() + if ConfigManager.selectLoggingApiLevel == .unknow { + ConfigManager.selectLoggingApiLevel = .info + } + clashApiDatasStorage.resetStreamApi() connsQueue.sync { clashApiDatasStorage.connsStorage.conns .removeAll()