feat: add keyboard shortcuts for tabs

This commit is contained in:
CMK 2021-05-19 15:15:19 +08:00
parent 11cc256ab1
commit 5cbfa28b93
6 changed files with 40 additions and 3 deletions

View File

@ -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",

View File

@ -12,7 +12,7 @@
<key>CoreDataStack.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>14</integer>
<integer>15</integer>
</dict>
<key>Mastodon - RTL.xcscheme_^#shared#^_</key>
<dict>
@ -32,7 +32,7 @@
<key>NotificationService.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>15</integer>
<integer>14</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>

View File

@ -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 {

View File

@ -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";

View File

@ -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";

View File

@ -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
}
}