From 1e59f663e547693193b6fd86cd5beafa4b09cd5a Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 14 Nov 2020 12:15:49 -0500 Subject: [PATCH] Add sidebar item key commands --- Tusker/Controllers/MenuController.swift | 65 +++++++++++++++++++ .../Main/MainSplitViewController.swift | 21 ++++++ 2 files changed, 86 insertions(+) diff --git a/Tusker/Controllers/MenuController.swift b/Tusker/Controllers/MenuController.swift index e979c1dc..9bf78f37 100644 --- a/Tusker/Controllers/MenuController.swift +++ b/Tusker/Controllers/MenuController.swift @@ -24,8 +24,42 @@ struct MenuController { return UIKeyCommand(title: "Refresh", action: #selector(RefreshableViewController.refresh), input: "r", modifierFlags: .command, discoverabilityTitle: discoverabilityTitle) } + @available(iOS 14.0, *) + static func sidebarCommand(item: MainSidebarViewController.Item, command: String) -> UIKeyCommand { + let data: Any + if case let .tab(tab) = item { + data = tab.rawValue + } else if case .search = item { + data = "search" + } else if case .bookmarks = item { + data = "bookmarks" + } else { + fatalError() + } + return UIKeyCommand( + title: item.title, + image: UIImage(systemName: item.imageName!), + action: #selector(MainSplitViewController.handleSidebarItemCommand(_:)), + input: command, + modifierFlags: .command, + propertyList: data + ) + } + + @available(iOS 14.0, *) + static func sidebarItemKeyCommands() -> [UIKeyCommand] { + return [ + sidebarCommand(item: .tab(.timelines), command: "1"), + sidebarCommand(item: .tab(.notifications), command: "2"), + sidebarCommand(item: .search, command: "3"), + sidebarCommand(item: .bookmarks, command: "4"), + sidebarCommand(item: .tab(.myProfile), command: "5"), + ] + } + static func buildMainMenu(builder: UIMenuBuilder) { builder.insertChild(buildFileMenu(), atStartOfMenu: .file) + builder.insertChild(buildViewMenu(), atStartOfMenu: .view) } private static func buildFileMenu() -> UIMenu { @@ -41,4 +75,35 @@ struct MenuController { ) } + private static func buildViewMenu() -> UIMenu { + let children: [UIMenuElement] + if #available(iOS 14.0, *) { + children = sidebarItemKeyCommands() + } else { + children = [] + } + return UIMenu( + title: "", + image: nil, + identifier: nil, + options: .displayInline, + children: children + ) + } + +} + +extension MenuController { + @available(iOS 14.0, *) + class SidebarItem: NSObject, NSCopying { + let item: MainSidebarViewController.Item + + init(item: MainSidebarViewController.Item) { + self.item = item + } + + func copy(with zone: NSZone? = nil) -> Any { + return SidebarItem(item: self.item) + } + } } diff --git a/Tusker/Screens/Main/MainSplitViewController.swift b/Tusker/Screens/Main/MainSplitViewController.swift index f13415be..bbf667ed 100644 --- a/Tusker/Screens/Main/MainSplitViewController.swift +++ b/Tusker/Screens/Main/MainSplitViewController.swift @@ -58,6 +58,8 @@ class MainSplitViewController: UISplitViewController { setViewController(tabBarViewController, for: .compact) addKeyCommand(MenuController.composeCommand()) + + MenuController.sidebarItemKeyCommands().forEach(addKeyCommand(_:)) } func select(item: MainSidebarViewController.Item) { @@ -74,6 +76,25 @@ class MainSplitViewController: UISplitViewController { return new } } + + @objc func handleSidebarItemCommand(_ command: UICommand) { + 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 = .search + } else if str == "bookmarks" { + item = .bookmarks + } else { + fatalError() + } + } else { + fatalError() + } + sidebar.select(item: item, animated: false) + select(item: item) + } }