Skip to content

Commit

Permalink
Add keyboard shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentTreguier committed Jul 26, 2024
1 parent 999c7c7 commit 0b1ca09
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/publishing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
path: |
~/Library/Caches/org.swift.swiftpm
~/Library/Developer/Xcode/DerivedData/**/SourcePackages/checkouts
~/Library/Developer/Xcode/DerivedData/**/SourcePackages/repositories
key: ${{ runner.os }}-${{ matrix.platform }}-spm-${{ hashFiles('**/Package.resolved') }}
restore-keys: |
${{ runner.os }}-${{ matrix.platform }}-spm-
Expand Down
4 changes: 4 additions & 0 deletions Fyreplace.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
4DB2E3792C43E304007F958D /* RegisterScreenTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DB2E3782C43E304007F958D /* RegisterScreenTests.swift */; };
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 */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -132,6 +133,7 @@
4DCEF8652C452EBA00F53085 /* .env-example */ = {isa = PBXFileReference; lastKnownFileType = text; path = ".env-example"; sourceTree = "<group>"; };
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>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -345,6 +347,7 @@
4DB10B4E2C4FEBCE00634BF6 /* Commands */ = {
isa = PBXGroup;
children = (
4DE140CC2C52687F00A699AE /* DestinationCommands.swift */,
4DB10B4F2C4FEBFC00634BF6 /* HelpCommands.swift */,
);
path = Commands;
Expand Down Expand Up @@ -561,6 +564,7 @@
4D5251EC2C1097A600018CD2 /* Destination.swift in Sources */,
4D54C92E2BF2608A001DE071 /* MainView.swift in Sources */,
4D5251F12C109D0D00018CD2 /* Screen.swift in Sources */,
4DE140CD2C52688000A699AE /* DestinationCommands.swift in Sources */,
4D54C96B2BF4E97E001DE071 /* NotificationsScreen.swift in Sources */,
4D9B3B472C36F50300A8F7AD /* UITextContentType.swift in Sources */,
4D9B3B382C334B3A00A8F7AD /* LoginScreen.swift in Sources */,
Expand Down
31 changes: 31 additions & 0 deletions Fyreplace/Commands/DestinationCommands.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Combine
import SwiftUI

struct DestinationCommands: Commands {
var body: some Commands {
CommandGroup(after: .sidebar) {
Divider()

ForEach(Destination.all) { destination in
Button(destination.titleKey) {
DestinationCommandKey.subject.send(destination)
}
.keyboardShortcut(destination.keyboardShortcut)
}

Divider()
}
}
}

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

extension EnvironmentValues {
var destinationCommands: AnyPublisher<Destination, Never> {
get { self[DestinationCommandKey.self] }
set { self[DestinationCommandKey.self] = newValue }
}
}
3 changes: 2 additions & 1 deletion Fyreplace/FyreplaceApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ struct FyreplaceApp: App {
MainView()
}
.commands {
SidebarCommands()
ToolbarCommands()
SidebarCommands()
DestinationCommands()
HelpCommands()
}
}
Expand Down
19 changes: 19 additions & 0 deletions Fyreplace/Views/Navigation/Destination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,24 @@ public enum Destination: String, CaseIterable, Identifiable, Codable {
}
}

var keyboardShortcut: KeyboardShortcut? {
switch self {
case .feed:
.init("1")
case .notifications:
.init("2")
case .archive:
.init("3")
case .drafts:
.init("4")
case .published:
.init("5")
case .settings:
.init("6")
case .login, .register:
nil
}
}

static let all = allCases.filter(\.topLevel)
}
6 changes: 6 additions & 0 deletions Fyreplace/Views/Navigation/RegularNavigation.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import SwiftUI

struct RegularNavigation: View {
@Environment(\.destinationCommands)
private var destinationCommands

#if os(macOS)
@SceneStorage("RegularNavigation.selectedDestination")
private var selectedDestination = Destination.feed
Expand Down Expand Up @@ -32,6 +35,9 @@ struct RegularNavigation: View {
Screen(destination: finalDestination)
}
}
.onReceive(destinationCommands) {
selectedDestination = $0
}
}
}

Expand Down

0 comments on commit 0b1ca09

Please sign in to comment.