From 5cbfa28b939fbf44287ce1e2d5dd4d608a247711 Mon Sep 17 00:00:00 2001 From: CMK Date: Wed, 19 May 2021 15:15:19 +0800 Subject: [PATCH] feat: add keyboard shortcuts for tabs --- Localization/app.json | 5 +++- .../xcschemes/xcschememanagement.plist | 4 +-- Mastodon/Generated/Strings.swift | 6 +++++ .../Resources/ar.lproj/Localizable.strings | 1 + .../Resources/en.lproj/Localizable.strings | 1 + .../Scene/MainTab/MainTabBarController.swift | 26 +++++++++++++++++++ 6 files changed, 40 insertions(+), 3 deletions(-) diff --git a/Localization/app.json b/Localization/app.json index f91e428ff..c55b28f21 100644 --- a/Localization/app.json +++ b/Localization/app.json @@ -78,7 +78,10 @@ "home": "Home", "search": "Search", "notification": "Notification", - "profile": "Profile" + "profile": "Profile", + "keyboard": { + "switch_to_tab": "Switch to %s" + } }, "status": { "user_reblogged": "%s reblogged", diff --git a/Mastodon.xcodeproj/xcuserdata/mainasuk.xcuserdatad/xcschemes/xcschememanagement.plist b/Mastodon.xcodeproj/xcuserdata/mainasuk.xcuserdatad/xcschemes/xcschememanagement.plist index 326857269..f092f9734 100644 --- a/Mastodon.xcodeproj/xcuserdata/mainasuk.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/Mastodon.xcodeproj/xcuserdata/mainasuk.xcuserdatad/xcschemes/xcschememanagement.plist @@ -12,7 +12,7 @@ CoreDataStack.xcscheme_^#shared#^_ orderHint - 14 + 15 Mastodon - RTL.xcscheme_^#shared#^_ @@ -32,7 +32,7 @@ NotificationService.xcscheme_^#shared#^_ orderHint - 15 + 14 SuppressBuildableAutocreation diff --git a/Mastodon/Generated/Strings.swift b/Mastodon/Generated/Strings.swift index 5d324c40d..5895b4780 100644 --- a/Mastodon/Generated/Strings.swift +++ b/Mastodon/Generated/Strings.swift @@ -278,6 +278,12 @@ internal enum L10n { internal static let profile = L10n.tr("Localizable", "Common.Controls.Tabs.Profile") /// Search internal static let search = L10n.tr("Localizable", "Common.Controls.Tabs.Search") + internal enum Keyboard { + /// Switch to %@ + internal static func switchToTab(_ p1: Any) -> String { + return L10n.tr("Localizable", "Common.Controls.Tabs.Keyboard.SwitchToTab", String(describing: p1)) + } + } } internal enum Timeline { internal enum Accessibility { diff --git a/Mastodon/Resources/ar.lproj/Localizable.strings b/Mastodon/Resources/ar.lproj/Localizable.strings index 2d3993618..e56fffcd3 100644 --- a/Mastodon/Resources/ar.lproj/Localizable.strings +++ b/Mastodon/Resources/ar.lproj/Localizable.strings @@ -91,6 +91,7 @@ Please check your internet connection."; "Common.Controls.Status.UserReblogged" = "%@ reblogged"; "Common.Controls.Status.UserRepliedTo" = "Replied to %@"; "Common.Controls.Tabs.Home" = "Home"; +"Common.Controls.Tabs.Keyboard.SwitchToTab" = "Switch to %@"; "Common.Controls.Tabs.Notification" = "Notification"; "Common.Controls.Tabs.Profile" = "Profile"; "Common.Controls.Tabs.Search" = "Search"; diff --git a/Mastodon/Resources/en.lproj/Localizable.strings b/Mastodon/Resources/en.lproj/Localizable.strings index 2d3993618..e56fffcd3 100644 --- a/Mastodon/Resources/en.lproj/Localizable.strings +++ b/Mastodon/Resources/en.lproj/Localizable.strings @@ -91,6 +91,7 @@ Please check your internet connection."; "Common.Controls.Status.UserReblogged" = "%@ reblogged"; "Common.Controls.Status.UserRepliedTo" = "Replied to %@"; "Common.Controls.Tabs.Home" = "Home"; +"Common.Controls.Tabs.Keyboard.SwitchToTab" = "Switch to %@"; "Common.Controls.Tabs.Notification" = "Notification"; "Common.Controls.Tabs.Profile" = "Profile"; "Common.Controls.Tabs.Search" = "Search"; diff --git a/Mastodon/Scene/MainTab/MainTabBarController.swift b/Mastodon/Scene/MainTab/MainTabBarController.swift index 8b1701578..fe19640ed 100644 --- a/Mastodon/Scene/MainTab/MainTabBarController.swift +++ b/Mastodon/Scene/MainTab/MainTabBarController.swift @@ -189,3 +189,29 @@ extension MainTabBarController { } } + + +extension MainTabBarController { + + override var keyCommands: [UIKeyCommand]? { + var commands: [UIKeyCommand] = [] + for (i, tab) in Tab.allCases.enumerated() { + let title = L10n.Common.Controls.Tabs.Keyboard.switchToTab(tab.title) + let input = String(i + 1) + let command = UIKeyCommand(title: title, image: nil, action: #selector(MainTabBarController.switchToTab(_:)), input: input, modifierFlags: .control, propertyList: tab.rawValue, alternates: [], discoverabilityTitle: nil, attributes: [], state: .off) + commands.append(command) + + } + return commands + } + + @objc private func switchToTab(_ sender: UIKeyCommand) { + guard let rawValue = sender.propertyList as? Int, + let tab = Tab(rawValue: rawValue) else { return } + os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: %s", ((#file as NSString).lastPathComponent), #line, #function, tab.title) + + guard let index = Tab.allCases.firstIndex(of: tab) else { return } + selectedIndex = index + } + +}