Skip to content

Commit

Permalink
Make shortcuts window-independent
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentTreguier committed Jul 29, 2024
1 parent 0b1ca09 commit 6f3777a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Fyreplace.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -134,6 +135,7 @@
4DCEF8662C452ECC00F53085 /* .env */ = {isa = PBXFileReference; lastKnownFileType = text; path = .env; sourceTree = "<group>"; };
4DD826FD2BF9FDC500799CEB /* Config.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = Config.sh; sourceTree = "<group>"; };
4DE140CC2C52687F00A699AE /* DestinationCommands.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DestinationCommands.swift; sourceTree = "<group>"; };
4DFB90632C57B98C00D4DABF /* AnyPublisher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnyPublisher.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -338,6 +340,7 @@
isa = PBXGroup;
children = (
4D9B3B412C36E23A00A8F7AD /* Array+RawRepresentable.swift */,
4DFB90632C57B98C00D4DABF /* AnyPublisher.swift */,
4D9B3B442C36F46F00A8F7AD /* NSTextContentType.swift */,
4D9B3B462C36F50300A8F7AD /* UITextContentType.swift */,
);
Expand Down Expand Up @@ -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 */,
Expand Down
13 changes: 7 additions & 6 deletions Fyreplace/Commands/DestinationCommands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import Combine
import SwiftUI

struct DestinationCommands: Commands {
var subject: PassthroughSubject<Destination, Never>

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)
}
Expand All @@ -18,14 +20,13 @@ struct DestinationCommands: Commands {
}
}

private struct DestinationCommandKey: EnvironmentKey {
static let subject = PassthroughSubject<Destination, Never>()
static let defaultValue = subject.eraseToAnyPublisher()
struct DestinationCommandEnvironmentKey: EnvironmentKey {
static let defaultValue = AnyPublisher<Destination, Never>.empty
}

extension EnvironmentValues {
var destinationCommands: AnyPublisher<Destination, Never> {
get { self[DestinationCommandKey.self] }
set { self[DestinationCommandKey.self] = newValue }
get { self[DestinationCommandEnvironmentKey.self] }
set { self[DestinationCommandEnvironmentKey.self] = newValue }
}
}
7 changes: 7 additions & 0 deletions Fyreplace/Extensions/AnyPublisher.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Combine

extension AnyPublisher {
static var empty: Self {
PassthroughSubject<Output, Failure>().eraseToAnyPublisher()
}
}
7 changes: 5 additions & 2 deletions Fyreplace/FyreplaceApp.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Combine
import Sentry
import SwiftUI

Expand All @@ -18,13 +19,15 @@ struct FyreplaceApp: App {
}

var body: some Scene {
let destinationsSubject = PassthroughSubject<Destination, Never>()

WindowGroup {
MainView()
MainView(destinationCommands: destinationsSubject.eraseToAnyPublisher())
}
.commands {
ToolbarCommands()
SidebarCommands()
DestinationCommands()
DestinationCommands(subject: destinationsSubject)
HelpCommands()
}
}
Expand Down
22 changes: 19 additions & 3 deletions Fyreplace/Views/MainView.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
import Combine
import SwiftUI

struct MainView: View {
var destinationCommands: AnyPublisher<Destination, Never>

#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)
}

0 comments on commit 6f3777a

Please sign in to comment.