2019-09-04 23:24:48 +02:00
|
|
|
//
|
|
|
|
// KeyboardManager.swift
|
|
|
|
// NetNewsWire-iOS
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 9/4/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
enum KeyboardType: String {
|
|
|
|
case global = "GlobalKeyboardShortcuts"
|
|
|
|
case sidebar = "SidebarKeyboardShortcuts"
|
|
|
|
case timeline = "TimelineKeyboardShortcuts"
|
|
|
|
case detail = "DetailKeyboardShortcuts"
|
|
|
|
}
|
|
|
|
|
|
|
|
class KeyboardManager {
|
|
|
|
|
|
|
|
private let coordinator: SceneCoordinator
|
|
|
|
private(set) var keyCommands: [UIKeyCommand]?
|
|
|
|
|
|
|
|
init(type: KeyboardType, coordinator: SceneCoordinator) {
|
|
|
|
self.coordinator = coordinator
|
|
|
|
load(type: type)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension KeyboardManager {
|
|
|
|
|
|
|
|
func load(type: KeyboardType) {
|
2019-09-05 18:29:04 +02:00
|
|
|
let file = Bundle.main.path(forResource: type.rawValue, ofType: "plist")!
|
|
|
|
let entries = NSArray(contentsOfFile: file)! as! [[String: Any]]
|
|
|
|
keyCommands = entries.compactMap { createKeyCommand(keyEntry: $0) }
|
2019-09-05 04:06:29 +02:00
|
|
|
|
2019-09-05 20:14:14 +02:00
|
|
|
switch type {
|
|
|
|
case .global:
|
|
|
|
keyCommands?.append(contentsOf: globalAuxilaryKeyCommands())
|
|
|
|
case .sidebar:
|
2019-09-05 18:29:04 +02:00
|
|
|
keyCommands?.append(contentsOf: sidebarAuxilaryKeyCommands())
|
2019-09-05 20:14:14 +02:00
|
|
|
default:
|
|
|
|
break
|
2019-09-05 04:06:29 +02:00
|
|
|
}
|
2019-09-04 23:24:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func createKeyCommand(keyEntry: [String: Any]) -> UIKeyCommand? {
|
|
|
|
guard let input = createKeyCommandInput(keyEntry: keyEntry) else { return nil }
|
|
|
|
let modifiers = createKeyModifierFlags(keyEntry: keyEntry)
|
2019-09-05 04:06:29 +02:00
|
|
|
let action = keyEntry["action"] as! String
|
2019-09-04 23:24:48 +02:00
|
|
|
|
|
|
|
if let title = keyEntry["title"] as? String {
|
2019-09-05 04:06:29 +02:00
|
|
|
return createKeyCommand(title: title, action: action, input: input, modifiers: modifiers)
|
2019-09-04 23:24:48 +02:00
|
|
|
} else {
|
2019-09-05 04:06:29 +02:00
|
|
|
return UIKeyCommand(input: input, modifierFlags: modifiers, action: NSSelectorFromString(action))
|
2019-09-04 23:24:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 04:06:29 +02:00
|
|
|
func createKeyCommand(title: String, action: String, input: String, modifiers: UIKeyModifierFlags) -> UIKeyCommand {
|
|
|
|
let selector = NSSelectorFromString(action)
|
|
|
|
return UIKeyCommand(title: title, image: nil, action: selector, input: input, modifierFlags: modifiers, propertyList: nil, alternates: [], discoverabilityTitle: nil, attributes: [], state: .on)
|
|
|
|
}
|
|
|
|
|
2019-09-04 23:24:48 +02:00
|
|
|
func createKeyCommandInput(keyEntry: [String: Any]) -> String? {
|
|
|
|
guard let key = keyEntry["key"] as? String else { return nil }
|
|
|
|
|
|
|
|
switch(key) {
|
|
|
|
case "[space]":
|
|
|
|
return " "
|
|
|
|
case "[uparrow]":
|
|
|
|
return UIKeyCommand.inputUpArrow
|
|
|
|
case "[downarrow]":
|
|
|
|
return UIKeyCommand.inputDownArrow
|
|
|
|
case "[leftarrow]":
|
|
|
|
return UIKeyCommand.inputLeftArrow
|
|
|
|
case "[rightarrow]":
|
|
|
|
return UIKeyCommand.inputRightArrow
|
|
|
|
case "[return]":
|
|
|
|
return "\r"
|
|
|
|
case "[enter]":
|
|
|
|
return nil
|
|
|
|
case "[delete]":
|
|
|
|
return "\u{8}"
|
|
|
|
case "[deletefunction]":
|
|
|
|
return nil
|
|
|
|
case "[tab]":
|
|
|
|
return "\t"
|
|
|
|
default:
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func createKeyModifierFlags(keyEntry: [String: Any]) -> UIKeyModifierFlags {
|
|
|
|
var flags = UIKeyModifierFlags()
|
|
|
|
|
|
|
|
if keyEntry["shiftModifier"] as? Bool ?? false {
|
|
|
|
flags.insert(.shift)
|
|
|
|
}
|
|
|
|
|
|
|
|
if keyEntry["optionModifier"] as? Bool ?? false {
|
|
|
|
flags.insert(.alternate)
|
|
|
|
}
|
|
|
|
|
|
|
|
if keyEntry["commandModifier"] as? Bool ?? false {
|
|
|
|
flags.insert(.command)
|
|
|
|
}
|
|
|
|
|
|
|
|
if keyEntry["controlModifier"] as? Bool ?? false {
|
|
|
|
flags.insert(.control)
|
|
|
|
}
|
|
|
|
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2019-09-05 20:14:14 +02:00
|
|
|
func globalAuxilaryKeyCommands() -> [UIKeyCommand] {
|
|
|
|
var keys = [UIKeyCommand]()
|
|
|
|
|
|
|
|
let addNewFeedTitle = NSLocalizedString("New Feed", comment: "New Feed")
|
|
|
|
keys.append(createKeyCommand(title: addNewFeedTitle, action: "addNewFeed:", input: "n", modifiers: [.command]))
|
|
|
|
|
|
|
|
let addNewFolderTitle = NSLocalizedString("New Folder", comment: "New Folder")
|
|
|
|
keys.append(createKeyCommand(title: addNewFolderTitle, action: "addNewFolder:", input: "n", modifiers: [.command, .shift]))
|
|
|
|
|
|
|
|
let refreshTitle = NSLocalizedString("Refresh", comment: "Refresh")
|
|
|
|
keys.append(createKeyCommand(title: refreshTitle, action: "refresh:", input: "r", modifiers: [.command]))
|
|
|
|
|
|
|
|
let nextUnreadTitle = NSLocalizedString("Next Unread", comment: "Next Unread")
|
|
|
|
keys.append(createKeyCommand(title: nextUnreadTitle, action: "nextUnread:", input: "/", modifiers: [.command]))
|
|
|
|
|
|
|
|
let goToTodayTitle = NSLocalizedString("Go To Today", comment: "Go To Today")
|
|
|
|
keys.append(createKeyCommand(title: goToTodayTitle, action: "goToToday:", input: "1", modifiers: [.command]))
|
|
|
|
|
|
|
|
let goToAllUnreadTitle = NSLocalizedString("Go To All Unread", comment: "Go To All Unread")
|
|
|
|
keys.append(createKeyCommand(title: goToAllUnreadTitle, action: "goToAllUnread:", input: "2", modifiers: [.command]))
|
|
|
|
|
|
|
|
let goToStarredTitle = NSLocalizedString("Go To Starred", comment: "Go To Starred")
|
|
|
|
keys.append(createKeyCommand(title: goToStarredTitle, action: "goToStarred:", input: "3", modifiers: [.command]))
|
|
|
|
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
2019-09-05 04:06:29 +02:00
|
|
|
func sidebarAuxilaryKeyCommands() -> [UIKeyCommand] {
|
|
|
|
var keys = [UIKeyCommand]()
|
|
|
|
|
|
|
|
let nextUpTitle = NSLocalizedString("Select Next Up", comment: "Select Next Up")
|
|
|
|
keys.append(createKeyCommand(title: nextUpTitle, action: "selectNextUp:", input: UIKeyCommand.inputUpArrow, modifiers: []))
|
|
|
|
|
|
|
|
let nextDownTitle = NSLocalizedString("Select Next Down", comment: "Select Next Down")
|
|
|
|
keys.append(createKeyCommand(title: nextDownTitle, action: "selectNextDown:", input: UIKeyCommand.inputDownArrow, modifiers: []))
|
|
|
|
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
2019-09-04 23:24:48 +02:00
|
|
|
}
|