Add sidebar item key commands

This commit is contained in:
Shadowfacts 2020-11-14 12:15:49 -05:00
parent 72217cde51
commit 1e59f663e5
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 86 additions and 0 deletions

View File

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

View File

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