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 {
|
|
|
|
|
2020-11-20 20:00:58 +01:00
|
|
|
private(set) var _keyCommands: [UIKeyCommand]
|
|
|
|
var keyCommands: [UIKeyCommand] {
|
|
|
|
guard !UIResponder.isFirstResponderTextField else { return [UIKeyCommand]() }
|
|
|
|
return _keyCommands
|
|
|
|
}
|
2019-09-09 19:08:47 +02:00
|
|
|
|
2019-09-05 21:37:07 +02:00
|
|
|
init(type: KeyboardType) {
|
2020-11-20 20:00:58 +01:00
|
|
|
_keyCommands = KeyboardManager.globalAuxilaryKeyCommands()
|
2019-09-09 19:08:47 +02:00
|
|
|
|
|
|
|
switch type {
|
|
|
|
case .sidebar:
|
2020-11-20 20:00:58 +01:00
|
|
|
_keyCommands.append(contentsOf: KeyboardManager.hardcodeFeedKeyCommands())
|
2019-09-09 19:08:47 +02:00
|
|
|
case .timeline, .detail:
|
2020-11-20 20:00:58 +01:00
|
|
|
_keyCommands.append(contentsOf: KeyboardManager.hardcodeArticleKeyCommands())
|
2019-09-09 19:08:47 +02:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-09-06 14:58:36 +02:00
|
|
|
let globalFile = Bundle.main.path(forResource: KeyboardType.global.rawValue, ofType: "plist")!
|
|
|
|
let globalEntries = NSArray(contentsOfFile: globalFile)! as! [[String: Any]]
|
2019-09-09 19:08:47 +02:00
|
|
|
let globalCommands = globalEntries.compactMap { KeyboardManager.createKeyCommand(keyEntry: $0) }
|
2020-11-20 20:00:58 +01:00
|
|
|
_keyCommands.append(contentsOf: globalCommands)
|
2019-09-06 14:58:36 +02:00
|
|
|
|
|
|
|
let specificFile = Bundle.main.path(forResource: type.rawValue, ofType: "plist")!
|
|
|
|
let specificEntries = NSArray(contentsOfFile: specificFile)! as! [[String: Any]]
|
2020-11-20 20:00:58 +01:00
|
|
|
_keyCommands.append(contentsOf: specificEntries.compactMap { KeyboardManager.createKeyCommand(keyEntry: $0) } )
|
2019-09-09 19:08:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static func createKeyCommand(title: String, action: String, input: String, modifiers: UIKeyModifierFlags) -> UIKeyCommand {
|
|
|
|
let selector = NSSelectorFromString(action)
|
2022-01-24 23:20:07 +01:00
|
|
|
let keyCommand = UIKeyCommand(title: title, image: nil, action: selector, input: input, modifierFlags: modifiers, propertyList: nil, alternates: [], discoverabilityTitle: nil, attributes: [], state: .on)
|
2024-11-12 07:03:32 +01:00
|
|
|
keyCommand.wantsPriorityOverSystemBehavior = true
|
2022-01-24 23:20:07 +01:00
|
|
|
return keyCommand
|
2019-09-04 23:24:48 +02:00
|
|
|
}
|
|
|
|
|
2019-09-05 21:37:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension KeyboardManager {
|
|
|
|
|
2019-09-09 19:08:47 +02:00
|
|
|
static func createKeyCommand(keyEntry: [String: Any]) -> UIKeyCommand? {
|
2019-09-04 23:24:48 +02:00
|
|
|
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-09 19:08:47 +02:00
|
|
|
return KeyboardManager.createKeyCommand(title: title, action: action, input: input, modifiers: modifiers)
|
2019-09-04 23:24:48 +02:00
|
|
|
} else {
|
2022-01-24 23:20:07 +01:00
|
|
|
let keyCommand = UIKeyCommand(input: input, modifierFlags: modifiers, action: NSSelectorFromString(action))
|
2024-11-12 07:03:32 +01:00
|
|
|
keyCommand.wantsPriorityOverSystemBehavior = true
|
2022-01-24 23:20:07 +01:00
|
|
|
return keyCommand
|
2019-09-04 23:24:48 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-09 19:08:47 +02:00
|
|
|
|
|
|
|
static func createKeyCommandInput(keyEntry: [String: Any]) -> String? {
|
2019-09-04 23:24:48 +02:00
|
|
|
guard let key = keyEntry["key"] as? String else { return nil }
|
|
|
|
|
|
|
|
switch(key) {
|
|
|
|
case "[space]":
|
2019-09-06 04:14:19 +02:00
|
|
|
return "\u{0020}"
|
2019-09-04 23:24:48 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-09 19:08:47 +02:00
|
|
|
static func createKeyModifierFlags(keyEntry: [String: Any]) -> UIKeyModifierFlags {
|
2019-09-04 23:24:48 +02:00
|
|
|
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-09 19:08:47 +02:00
|
|
|
static func globalAuxilaryKeyCommands() -> [UIKeyCommand] {
|
2019-09-05 20:14:14 +02:00
|
|
|
var keys = [UIKeyCommand]()
|
|
|
|
|
2023-06-26 00:30:43 +02:00
|
|
|
let addNewFeedTitle = NSLocalizedString("New Feed", comment: "New Feed")
|
2019-09-09 19:08:47 +02:00
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: addNewFeedTitle, action: "addNewFeed:", input: "n", modifiers: [.command]))
|
2019-09-05 20:14:14 +02:00
|
|
|
|
|
|
|
let addNewFolderTitle = NSLocalizedString("New Folder", comment: "New Folder")
|
2019-09-09 19:08:47 +02:00
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: addNewFolderTitle, action: "addNewFolder:", input: "n", modifiers: [.command, .shift]))
|
2019-09-05 20:14:14 +02:00
|
|
|
|
|
|
|
let refreshTitle = NSLocalizedString("Refresh", comment: "Refresh")
|
2019-09-09 19:08:47 +02:00
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: refreshTitle, action: "refresh:", input: "r", modifiers: [.command]))
|
2019-09-05 20:14:14 +02:00
|
|
|
|
|
|
|
let nextUnreadTitle = NSLocalizedString("Next Unread", comment: "Next Unread")
|
2019-09-09 19:08:47 +02:00
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: nextUnreadTitle, action: "nextUnread:", input: "/", modifiers: [.command]))
|
2019-09-05 20:14:14 +02:00
|
|
|
|
|
|
|
let goToTodayTitle = NSLocalizedString("Go To Today", comment: "Go To Today")
|
2019-09-09 19:08:47 +02:00
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: goToTodayTitle, action: "goToToday:", input: "1", modifiers: [.command]))
|
2019-09-05 20:14:14 +02:00
|
|
|
|
|
|
|
let goToAllUnreadTitle = NSLocalizedString("Go To All Unread", comment: "Go To All Unread")
|
2019-09-09 19:08:47 +02:00
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: goToAllUnreadTitle, action: "goToAllUnread:", input: "2", modifiers: [.command]))
|
2019-09-05 20:14:14 +02:00
|
|
|
|
|
|
|
let goToStarredTitle = NSLocalizedString("Go To Starred", comment: "Go To Starred")
|
2019-09-09 19:08:47 +02:00
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: goToStarredTitle, action: "goToStarred:", input: "3", modifiers: [.command]))
|
2019-09-05 20:14:14 +02:00
|
|
|
|
2020-05-13 13:59:59 +02:00
|
|
|
let gotoSettings = NSLocalizedString("Go To Settings", comment: "Go To Settings")
|
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: gotoSettings, action: "goToSettings:", input: ",", modifiers: [.command]))
|
|
|
|
|
2019-09-09 19:08:47 +02:00
|
|
|
let articleSearchTitle = NSLocalizedString("Article Search", comment: "Article Search")
|
2020-03-22 16:18:07 +01:00
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: articleSearchTitle, action: "articleSearch:", input: "f", modifiers: [.command, .alternate]))
|
2019-09-05 22:43:01 +02:00
|
|
|
|
|
|
|
let markAllAsReadTitle = NSLocalizedString("Mark All as Read", comment: "Mark All as Read")
|
2019-09-09 19:08:47 +02:00
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: markAllAsReadTitle, action: "markAllAsRead:", input: "k", modifiers: [.command]))
|
2019-09-05 22:43:01 +02:00
|
|
|
|
2020-03-16 00:15:52 +01:00
|
|
|
let cleanUp = NSLocalizedString("Clean Up", comment: "Clean Up")
|
2020-03-22 16:23:31 +01:00
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: cleanUp, action: "cleanUp:", input: "'", modifiers: [.command]))
|
2020-03-22 16:18:07 +01:00
|
|
|
|
|
|
|
let toggleReadFeedsFilter = NSLocalizedString("Toggle Read Feeds Filter", comment: "Toggle Read Feeds Filter")
|
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: toggleReadFeedsFilter, action: "toggleReadFeedsFilter:", input: "f", modifiers: [.command, .shift]))
|
|
|
|
|
|
|
|
let toggleReadArticlesFilter = NSLocalizedString("Toggle Read Articles Filter", comment: "Toggle Read Articles Filter")
|
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: toggleReadArticlesFilter, action: "toggleReadArticlesFilter:", input: "h", modifiers: [.command, .shift]))
|
2020-03-16 00:15:52 +01:00
|
|
|
|
2019-09-09 19:08:47 +02:00
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
|
|
|
static func hardcodeFeedKeyCommands() -> [UIKeyCommand] {
|
|
|
|
var keys = [UIKeyCommand]()
|
2019-09-05 22:43:01 +02:00
|
|
|
|
2019-09-09 19:08:47 +02:00
|
|
|
let nextUpTitle = NSLocalizedString("Select Next Up", comment: "Select Next Up")
|
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: nextUpTitle, action: "selectNextUp:", input: UIKeyCommand.inputUpArrow, modifiers: []))
|
2019-09-05 22:43:01 +02:00
|
|
|
|
2019-09-09 19:08:47 +02:00
|
|
|
let nextDownTitle = NSLocalizedString("Select Next Down", comment: "Select Next Down")
|
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: nextDownTitle, action: "selectNextDown:", input: UIKeyCommand.inputDownArrow, modifiers: []))
|
2020-05-26 06:51:42 +02:00
|
|
|
|
|
|
|
let getFeedInfo = NSLocalizedString("Get Feed Info", comment: "Get Feed Info")
|
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: getFeedInfo, action: "showFeedInspector:", input: "i", modifiers: .command))
|
2019-09-07 16:01:29 +02:00
|
|
|
|
2019-09-05 20:14:14 +02:00
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
2019-09-09 19:08:47 +02:00
|
|
|
static func hardcodeArticleKeyCommands() -> [UIKeyCommand] {
|
2019-09-05 04:06:29 +02:00
|
|
|
var keys = [UIKeyCommand]()
|
|
|
|
|
2019-09-09 19:08:47 +02:00
|
|
|
let openInBrowserTitle = NSLocalizedString("Open In Browser", comment: "Open In Browser")
|
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: openInBrowserTitle, action: "openInBrowser:", input: UIKeyCommand.inputRightArrow, modifiers: [.command]))
|
2019-09-05 04:06:29 +02:00
|
|
|
|
2019-09-09 19:08:47 +02:00
|
|
|
let toggleReadTitle = NSLocalizedString("Toggle Read Status", comment: "Toggle Read Status")
|
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: toggleReadTitle, action: "toggleRead:", input: "u", modifiers: [.command, .shift]))
|
|
|
|
|
2020-01-04 09:42:29 +01:00
|
|
|
let markAboveAsReadTitle = NSLocalizedString("Mark Above as Read", comment: "Mark Above as Read")
|
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: markAboveAsReadTitle, action: "markAboveAsRead:", input: "k", modifiers: [.command, .control]))
|
|
|
|
|
|
|
|
let markBelowAsReadTitle = NSLocalizedString("Mark Below as Read", comment: "Mark Below as Read")
|
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: markBelowAsReadTitle, action: "markBelowAsRead:", input: "k", modifiers: [.command, .shift]))
|
2019-09-09 19:08:47 +02:00
|
|
|
|
|
|
|
let toggleStarredTitle = NSLocalizedString("Toggle Starred Status", comment: "Toggle Starred Status")
|
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: toggleStarredTitle, action: "toggleStarred:", input: "l", modifiers: [.command, .shift]))
|
2020-05-13 12:13:31 +02:00
|
|
|
|
|
|
|
let findInArticleTitle = NSLocalizedString("Find in Article", comment: "Find in Article")
|
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: findInArticleTitle, action: "beginFind:", input: "f", modifiers: [.command]))
|
2019-09-05 04:06:29 +02:00
|
|
|
|
2020-05-26 06:51:42 +02:00
|
|
|
let getFeedInfo = NSLocalizedString("Get Feed Info", comment: "Get Feed Info")
|
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: getFeedInfo, action: "showFeedInspector:", input: "i", modifiers: .command))
|
2019-09-05 04:06:29 +02:00
|
|
|
|
2020-05-13 13:59:59 +02:00
|
|
|
let toggleSidebar = NSLocalizedString("Toggle Sidebar", comment: "Toggle Sidebar")
|
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: toggleSidebar, action: "toggleSidebar:", input: "s", modifiers: [.command, .control]))
|
|
|
|
|
|
|
|
let toggleReaderView = NSLocalizedString("Toggle Reader View", comment: "Toggle Reader View")
|
|
|
|
keys.append(KeyboardManager.createKeyCommand(title: toggleReaderView, action: "toggleReaderView:", input: "r", modifiers: [.command, .shift]))
|
|
|
|
|
2019-09-05 04:06:29 +02:00
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
2019-09-04 23:24:48 +02:00
|
|
|
}
|