From 6f3777a68284b37900224e541c9f59c424b35c56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20Tr=C3=A9guier?= Date: Mon, 29 Jul 2024 13:59:49 +0200 Subject: [PATCH] Make shortcuts window-independent --- Fyreplace.xcodeproj/project.pbxproj | 4 ++++ Fyreplace/Commands/DestinationCommands.swift | 13 ++++++------ Fyreplace/Extensions/AnyPublisher.swift | 7 +++++++ Fyreplace/FyreplaceApp.swift | 7 +++++-- Fyreplace/Views/MainView.swift | 22 +++++++++++++++++--- 5 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 Fyreplace/Extensions/AnyPublisher.swift diff --git a/Fyreplace.xcodeproj/project.pbxproj b/Fyreplace.xcodeproj/project.pbxproj index 51f3678..db36032 100644 --- a/Fyreplace.xcodeproj/project.pbxproj +++ b/Fyreplace.xcodeproj/project.pbxproj @@ -52,6 +52,7 @@ 4DCE062B2C08E5E200F69AF1 /* CompactNavigation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DCE062A2C08E5E200F69AF1 /* CompactNavigation.swift */; }; 4DCE062D2C08E65300F69AF1 /* RegularNavigation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DCE062C2C08E65300F69AF1 /* RegularNavigation.swift */; }; 4DE140CD2C52688000A699AE /* DestinationCommands.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DE140CC2C52687F00A699AE /* DestinationCommands.swift */; }; + 4DFB90642C57B98C00D4DABF /* AnyPublisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DFB90632C57B98C00D4DABF /* AnyPublisher.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -134,6 +135,7 @@ 4DCEF8662C452ECC00F53085 /* .env */ = {isa = PBXFileReference; lastKnownFileType = text; path = .env; sourceTree = ""; }; 4DD826FD2BF9FDC500799CEB /* Config.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = Config.sh; sourceTree = ""; }; 4DE140CC2C52687F00A699AE /* DestinationCommands.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DestinationCommands.swift; sourceTree = ""; }; + 4DFB90632C57B98C00D4DABF /* AnyPublisher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnyPublisher.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -338,6 +340,7 @@ isa = PBXGroup; children = ( 4D9B3B412C36E23A00A8F7AD /* Array+RawRepresentable.swift */, + 4DFB90632C57B98C00D4DABF /* AnyPublisher.swift */, 4D9B3B442C36F46F00A8F7AD /* NSTextContentType.swift */, 4D9B3B462C36F50300A8F7AD /* UITextContentType.swift */, ); @@ -573,6 +576,7 @@ 4D9B3B452C36F46F00A8F7AD /* NSTextContentType.swift in Sources */, 4D54C96F2BF4E9DF001DE071 /* DraftsScreen.swift in Sources */, 4DB2E36F2C418F5C007F958D /* DynamicForm.swift in Sources */, + 4DFB90642C57B98C00D4DABF /* AnyPublisher.swift in Sources */, 4DCE062B2C08E5E200F69AF1 /* CompactNavigation.swift in Sources */, 4DCE062D2C08E65300F69AF1 /* RegularNavigation.swift in Sources */, 4D54C9692BF4E8F4001DE071 /* FeedScreen.swift in Sources */, diff --git a/Fyreplace/Commands/DestinationCommands.swift b/Fyreplace/Commands/DestinationCommands.swift index 4c9cdbe..3a8a069 100644 --- a/Fyreplace/Commands/DestinationCommands.swift +++ b/Fyreplace/Commands/DestinationCommands.swift @@ -2,13 +2,15 @@ import Combine import SwiftUI struct DestinationCommands: Commands { + var subject: PassthroughSubject + var body: some Commands { CommandGroup(after: .sidebar) { Divider() ForEach(Destination.all) { destination in Button(destination.titleKey) { - DestinationCommandKey.subject.send(destination) + subject.send(destination) } .keyboardShortcut(destination.keyboardShortcut) } @@ -18,14 +20,13 @@ struct DestinationCommands: Commands { } } -private struct DestinationCommandKey: EnvironmentKey { - static let subject = PassthroughSubject() - static let defaultValue = subject.eraseToAnyPublisher() +struct DestinationCommandEnvironmentKey: EnvironmentKey { + static let defaultValue = AnyPublisher.empty } extension EnvironmentValues { var destinationCommands: AnyPublisher { - get { self[DestinationCommandKey.self] } - set { self[DestinationCommandKey.self] = newValue } + get { self[DestinationCommandEnvironmentKey.self] } + set { self[DestinationCommandEnvironmentKey.self] = newValue } } } diff --git a/Fyreplace/Extensions/AnyPublisher.swift b/Fyreplace/Extensions/AnyPublisher.swift new file mode 100644 index 0000000..e0d8ac1 --- /dev/null +++ b/Fyreplace/Extensions/AnyPublisher.swift @@ -0,0 +1,7 @@ +import Combine + +extension AnyPublisher { + static var empty: Self { + PassthroughSubject().eraseToAnyPublisher() + } +} diff --git a/Fyreplace/FyreplaceApp.swift b/Fyreplace/FyreplaceApp.swift index 1c4aa7e..949ed07 100644 --- a/Fyreplace/FyreplaceApp.swift +++ b/Fyreplace/FyreplaceApp.swift @@ -1,3 +1,4 @@ +import Combine import Sentry import SwiftUI @@ -18,13 +19,15 @@ struct FyreplaceApp: App { } var body: some Scene { + let destinationsSubject = PassthroughSubject() + WindowGroup { - MainView() + MainView(destinationCommands: destinationsSubject.eraseToAnyPublisher()) } .commands { ToolbarCommands() SidebarCommands() - DestinationCommands() + DestinationCommands(subject: destinationsSubject) HelpCommands() } } diff --git a/Fyreplace/Views/MainView.swift b/Fyreplace/Views/MainView.swift index 62925d0..9e879b7 100644 --- a/Fyreplace/Views/MainView.swift +++ b/Fyreplace/Views/MainView.swift @@ -1,15 +1,31 @@ +import Combine import SwiftUI struct MainView: View { + var destinationCommands: AnyPublisher + + #if os(macOS) + @Environment(\.controlActiveState) + private var status + #else + @Environment(\.scenePhase) + private var status + #endif + var body: some View { #if os(macOS) - RegularNavigation() + let navigation = RegularNavigation() #else - DynamicNavigation() + let navigation = DynamicNavigation() #endif + + navigation.environment( + \.destinationCommands, + status == .inactive ? .empty : destinationCommands + ) } } #Preview { - MainView() + MainView(destinationCommands: .empty) }