Fix sidebar key commands not working on macOS

Closes #253
This commit is contained in:
Shadowfacts 2023-04-03 23:25:33 -04:00
parent d04259b253
commit e5363b2e21
2 changed files with 31 additions and 40 deletions

View File

@ -18,33 +18,22 @@ struct MenuController {
return UIKeyCommand(title: "Refresh", action: #selector(RefreshableViewController.refresh), input: "r", modifierFlags: .command, discoverabilityTitle: discoverabilityTitle) return UIKeyCommand(title: "Refresh", action: #selector(RefreshableViewController.refresh), input: "r", modifierFlags: .command, discoverabilityTitle: discoverabilityTitle)
} }
static func sidebarCommand(item: MainSidebarViewController.Item, command: String) -> UIKeyCommand { static func sidebarCommand(item: MainSidebarViewController.Item, command: String, action: Selector) -> UIKeyCommand {
let data: Any
if case let .tab(tab) = item {
data = tab.rawValue
} else if case .explore = item {
data = "search"
} else if case .bookmarks = item {
data = "bookmarks"
} else {
fatalError()
}
return UIKeyCommand( return UIKeyCommand(
title: item.title, title: item.title,
image: UIImage(systemName: item.imageName!), image: UIImage(systemName: item.imageName!),
action: #selector(MainSplitViewController.handleSidebarItemCommand(_:)), action: action,
input: command, input: command,
modifierFlags: .command, modifierFlags: .command
propertyList: data
) )
} }
static let sidebarItemKeyCommands: [UIKeyCommand] = [ static let sidebarItemKeyCommands: [UIKeyCommand] = [
sidebarCommand(item: .tab(.timelines), command: "1"), sidebarCommand(item: .tab(.timelines), command: "1", action: #selector(MainSplitViewController.handleSidebarCommandTimelines)),
sidebarCommand(item: .tab(.notifications), command: "2"), sidebarCommand(item: .tab(.notifications), command: "2", action: #selector(MainSplitViewController.handleSidebarCommandNotifications)),
sidebarCommand(item: .explore, command: "3"), sidebarCommand(item: .explore, command: "3", action: #selector(MainSplitViewController.handleSidebarCommandExplore)),
sidebarCommand(item: .bookmarks, command: "4"), sidebarCommand(item: .bookmarks, command: "4", action: #selector(MainSplitViewController.handleSidebarCommandBookmarks)),
sidebarCommand(item: .tab(.myProfile), command: "5"), sidebarCommand(item: .tab(.myProfile), command: "5", action: #selector(MainSplitViewController.handleSidebarCommandMyProfile)),
] ]
static let nextSubTabCommand = UIKeyCommand(title: "Next Sub Tab", action: #selector(TabbedPageViewController.selectNextPage), input: "]", modifierFlags: [.command, .shift]) static let nextSubTabCommand = UIKeyCommand(title: "Next Sub Tab", action: #selector(TabbedPageViewController.selectNextPage), input: "]", modifierFlags: [.command, .shift])

View File

@ -101,27 +101,29 @@ class MainSplitViewController: UISplitViewController {
} }
} }
@objc func handleSidebarItemCommand(_ sender: AnyObject) { @objc func handleSidebarCommandTimelines() {
// workaround for crash when sender is not a UICommand, see #253 and FB11804009 sidebar.select(item: .tab(.timelines), animated: false)
guard let command = sender as? UICommand else { select(item: .tab(.timelines))
return }
}
let item: MainSidebarViewController.Item @objc func handleSidebarCommandNotifications() {
if let index = command.propertyList as? Int { sidebar.select(item: .tab(.notifications), animated: false)
item = .tab(MainTabBarViewController.Tab(rawValue: index)!) select(item: .tab(.notifications))
} else if let str = command.propertyList as? String { }
if str == "search" {
item = .explore @objc func handleSidebarCommandExplore() {
} else if str == "bookmarks" { sidebar.select(item: .tab(.explore), animated: false)
item = .bookmarks select(item: .tab(.explore))
} else { }
fatalError()
} @objc func handleSidebarCommandBookmarks() {
} else { sidebar.select(item: .bookmarks, animated: false)
fatalError() select(item: .bookmarks)
} }
sidebar.select(item: item, animated: false)
select(item: item) @objc func handleSidebarCommandMyProfile() {
sidebar.select(item: .tab(.myProfile), animated: false)
select(item: .tab(.myProfile))
} }
@objc private func sidebarTapped() { @objc private func sidebarTapped() {