From e5363b2e213a16d4a9edd23f1fcd27b3e83eade2 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 3 Apr 2023 23:25:33 -0400 Subject: [PATCH] Fix sidebar key commands not working on macOS Closes #253 --- Tusker/MenuController.swift | 27 ++++-------- .../Main/MainSplitViewController.swift | 44 ++++++++++--------- 2 files changed, 31 insertions(+), 40 deletions(-) diff --git a/Tusker/MenuController.swift b/Tusker/MenuController.swift index 7c1ff0f1..3013b898 100644 --- a/Tusker/MenuController.swift +++ b/Tusker/MenuController.swift @@ -18,33 +18,22 @@ struct MenuController { return UIKeyCommand(title: "Refresh", action: #selector(RefreshableViewController.refresh), input: "r", modifierFlags: .command, discoverabilityTitle: discoverabilityTitle) } - static func sidebarCommand(item: MainSidebarViewController.Item, command: String) -> 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() - } + static func sidebarCommand(item: MainSidebarViewController.Item, command: String, action: Selector) -> UIKeyCommand { return UIKeyCommand( title: item.title, image: UIImage(systemName: item.imageName!), - action: #selector(MainSplitViewController.handleSidebarItemCommand(_:)), + action: action, input: command, - modifierFlags: .command, - propertyList: data + modifierFlags: .command ) } static let sidebarItemKeyCommands: [UIKeyCommand] = [ - sidebarCommand(item: .tab(.timelines), command: "1"), - sidebarCommand(item: .tab(.notifications), command: "2"), - sidebarCommand(item: .explore, command: "3"), - sidebarCommand(item: .bookmarks, command: "4"), - sidebarCommand(item: .tab(.myProfile), command: "5"), + sidebarCommand(item: .tab(.timelines), command: "1", action: #selector(MainSplitViewController.handleSidebarCommandTimelines)), + sidebarCommand(item: .tab(.notifications), command: "2", action: #selector(MainSplitViewController.handleSidebarCommandNotifications)), + sidebarCommand(item: .explore, command: "3", action: #selector(MainSplitViewController.handleSidebarCommandExplore)), + sidebarCommand(item: .bookmarks, command: "4", action: #selector(MainSplitViewController.handleSidebarCommandBookmarks)), + 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]) diff --git a/Tusker/Screens/Main/MainSplitViewController.swift b/Tusker/Screens/Main/MainSplitViewController.swift index 727b32dd..0926867a 100644 --- a/Tusker/Screens/Main/MainSplitViewController.swift +++ b/Tusker/Screens/Main/MainSplitViewController.swift @@ -101,27 +101,29 @@ class MainSplitViewController: UISplitViewController { } } - @objc func handleSidebarItemCommand(_ sender: AnyObject) { - // workaround for crash when sender is not a UICommand, see #253 and FB11804009 - guard let command = sender as? UICommand else { - return - } - let item: MainSidebarViewController.Item - if let index = command.propertyList as? Int { - item = .tab(MainTabBarViewController.Tab(rawValue: index)!) - } else if let str = command.propertyList as? String { - if str == "search" { - item = .explore - } else if str == "bookmarks" { - item = .bookmarks - } else { - fatalError() - } - } else { - fatalError() - } - sidebar.select(item: item, animated: false) - select(item: item) + @objc func handleSidebarCommandTimelines() { + sidebar.select(item: .tab(.timelines), animated: false) + select(item: .tab(.timelines)) + } + + @objc func handleSidebarCommandNotifications() { + sidebar.select(item: .tab(.notifications), animated: false) + select(item: .tab(.notifications)) + } + + @objc func handleSidebarCommandExplore() { + sidebar.select(item: .tab(.explore), animated: false) + select(item: .tab(.explore)) + } + + @objc func handleSidebarCommandBookmarks() { + sidebar.select(item: .bookmarks, animated: false) + select(item: .bookmarks) + } + + @objc func handleSidebarCommandMyProfile() { + sidebar.select(item: .tab(.myProfile), animated: false) + select(item: .tab(.myProfile)) } @objc private func sidebarTapped() {