Tusker/Tusker/Controllers/MenuController.swift

132 lines
4.1 KiB
Swift
Raw Normal View History

2020-11-14 16:10:20 +00:00
//
// MenuController.swift
// Tusker
//
// Created by Shadowfacts on 11/14/20.
// Copyright © 2020 Shadowfacts. All rights reserved.
//
import UIKit
struct MenuController {
2020-11-14 16:55:19 +00:00
static func composeCommand() -> UIKeyCommand {
let selector: Selector
if #available(iOS 14.0, *) {
selector = #selector(MainSplitViewController.presentCompose)
} else {
selector = #selector(MainTabBarViewController.presentCompose)
}
return UIKeyCommand(title: "Compose", action: selector, input: "n", modifierFlags: .command)
}
2020-11-14 16:10:20 +00:00
static func refreshCommand(discoverabilityTitle: String?) -> UIKeyCommand {
return UIKeyCommand(title: "Refresh", action: #selector(RefreshableViewController.refresh), input: "r", modifierFlags: .command, discoverabilityTitle: discoverabilityTitle)
}
2020-11-14 17:15:49 +00:00
@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"),
]
}
2020-11-15 03:26:02 +00:00
static func nextSubTabCommand() -> UIKeyCommand {
return UIKeyCommand(title: "Next Sub Tab", action: #selector(TabbedPageViewController.selectNextPage), input: "]", modifierFlags: [.command, .shift])
}
static func prevSubTabCommand() -> UIKeyCommand {
return UIKeyCommand(title: "Previous Sub Tab", action: #selector(TabbedPageViewController.selectPrevPage), input: "[", modifierFlags: [.command, .shift])
}
2020-11-14 16:10:20 +00:00
static func buildMainMenu(builder: UIMenuBuilder) {
2020-11-14 16:55:19 +00:00
builder.insertChild(buildFileMenu(), atStartOfMenu: .file)
2020-11-15 03:26:02 +00:00
builder.insertChild(buildSubTabMenu(), atStartOfMenu: .view)
builder.insertChild(buildSidebarShortcuts(), atStartOfMenu: .view)
2020-11-14 16:10:20 +00:00
}
2020-11-14 16:55:19 +00:00
private static func buildFileMenu() -> UIMenu {
2020-11-14 16:10:20 +00:00
return UIMenu(
title: "",
image: nil,
identifier: nil,
options: .displayInline,
children: [
2020-11-14 16:55:19 +00:00
composeCommand(),
2020-11-14 16:10:20 +00:00
refreshCommand(discoverabilityTitle: nil),
]
)
}
2020-11-15 03:26:02 +00:00
private static func buildSubTabMenu() -> UIMenu {
return UIMenu(
title: "",
image: nil,
identifier: nil,
options: .displayInline,
children: [
prevSubTabCommand(),
nextSubTabCommand(),
]
)
}
private static func buildSidebarShortcuts() -> UIMenu {
2020-11-14 17:15:49 +00:00
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)
}
}
2020-11-14 16:10:20 +00:00
}