Skip to content

Commit

Permalink
Split shortcuts and presses
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Dec 12, 2019
1 parent 9cd385c commit b864fd3
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Blink/SmarterKeys/SmarterTermInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class SmarterTermInput: KBWebView {
override func configure(_ cfg: KBConfig) {
blinkKeyCommands = cfg.shortcuts.map { shortcut in
let cmd = BlinkCommand(
title: shortcut.title,
title: shortcut.action.isCommand ? shortcut.title : "",
image: nil,
action: #selector(SpaceController._onBlinkCommand(_:)),
input: shortcut.input,
Expand Down
33 changes: 27 additions & 6 deletions KB/Native/Model/KeyBindingAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,39 @@ enum KeyBindingAction: Codable, Identifiable {
}
}

var isCommand: Bool {
switch self {
case .command: return true
default: return false
}
}

static func press(_ keyCode: KeyCode, _ mods: UIKeyModifierFlags) -> KeyBindingAction {
KeyBindingAction.press(keyCode, mods: mods.rawValue)
}

static var pressList: [KeyBindingAction] {
[
KeyBindingAction.press(.backquote, mods: UIKeyModifierFlags([]).rawValue),
KeyBindingAction.press(.escape, mods: UIKeyModifierFlags([]).rawValue),
KeyBindingAction.press(.space, mods: UIKeyModifierFlags([.control]).rawValue),
KeyBindingAction.press(.f11, mods: UIKeyModifierFlags([]).rawValue),
KeyBindingAction.press(.f12, mods: UIKeyModifierFlags([]).rawValue),
.press(.escape, []),
.press(.space, [.control]),
.press(.backquote, []),
.press(.f1, []),
.press(.f2, []),
.press(.f3, []),
.press(.f4, []),
.press(.f5, []),
.press(.f6, []),
.press(.f7, []),
.press(.f8, []),
.press(.f9, []),
.press(.f10, []),
.press(.f11, []),
.press(.f12, []),
]
}

static var commandList: [KeyBindingAction] {
Command.allCases.map({KeyBindingAction.command($0) })
Command.allCases.map({ KeyBindingAction.command($0) })
}

var title: String {
Expand Down
1 change: 1 addition & 0 deletions KB/Native/Model/KeyShortcut.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class KeyShortcut: ObservableObject, Codable, Identifiable {
var id: String { "\(action.id)-\(modifiers)-\(input)" }

var title: String { action.title }

var description: String {

var res = modifiers.toSymbols()
Expand Down
14 changes: 12 additions & 2 deletions KB/Native/Views/KBConfigView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@


import SwiftUI
//import Combine

private func _row(_ key: KeyConfig) -> some View {
DefaultRow(title: key.fullName, description: key.description) {
Expand All @@ -58,7 +57,11 @@ struct KBConfigView: View {
List {
Section(header: Text("Blink")) {
DefaultRow(title: "Shortcuts") {
ShortcutsConfigView(config: self.config)
ShortcutsConfigView(
config: self.config,
commandsMode: true
)
.navigationBarTitle("Shortcuts")
}
}
Section(header: Text("Terminal")) {
Expand All @@ -69,6 +72,13 @@ struct KBConfigView: View {
_pairRow(config.command)
_bindingRow(config.fnBinding, title: "Functional Keys", last: "[0-9]")
_bindingRow(config.cursorBinding, title: "Cursor Keys", last: "[Arrow]")
DefaultRow(title: "Custom Presses") {
ShortcutsConfigView(
config: self.config,
commandsMode: false
)
.navigationBarTitle("Presses")
}
}
}
.listStyle(GroupedListStyle())
Expand Down
91 changes: 64 additions & 27 deletions KB/Native/Views/ShortcutsConfigView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,28 @@ import SwiftUI

struct ActionsList: View {
@Binding var action: KeyBindingAction
var commandsMode: Bool
@State private var updatedAt = Date()

var pressList = KeyBindingAction.pressList
var commandList = KeyBindingAction.commandList

var body: some View {
List {
Section(header: Text("Press")) {
ForEach(pressList, id: \.id) { ka in
self._row(action: self.action, value: ka)
if commandsMode {
Section(header: Text("Commands")) {
ForEach(commandList, id: \.id) { ka in
self._row(action: self.action, value: ka)
}
}
}
Section(header: Text("Commands")) {
ForEach(commandList, id: \.id) { ka in
self._row(action: self.action, value: ka)
} else {
Section(header: Text("Press")) {
ForEach(pressList, id: \.id) { ka in
self._row(action: self.action, value: ka)
}
}
}

}
.listStyle(GroupedListStyle())
}
Expand All @@ -74,17 +79,21 @@ struct ShortcutConfigView: View {
@EnvironmentObject var nav: Nav
@ObservedObject var config: KBConfig
@ObservedObject var shortcut: KeyShortcut
var commandsMode: Bool

var body: some View {
List {
Section(header: Text("Combination"), footer: Text("Press keys on external KB to change.")) {
Section(
header: Text("Combination"),
footer: Text("Press keys on external KB to change.")
) {
HStack {
Text(shortcut.description)
}
}
Section(header: Text("Action")) {
DefaultRow(title: shortcut.action.title) {
ActionsList(action: self.$shortcut.action)
ActionsList(action: self.$shortcut.action, commandsMode: self.commandsMode)
}
}
}
Expand All @@ -107,31 +116,59 @@ struct ShortcutConfigView: View {
struct ShortcutsConfigView: View {
@EnvironmentObject var nav: Nav
@ObservedObject var config: KBConfig
var commandsMode: Bool

var body: some View {
List {
ForEach(config.shortcuts, id: \.id) { shortcut in
ForEach(_list, id: \.id) { shortcut in
DefaultRow(title: shortcut.title, description: shortcut.description) {
ShortcutConfigView(config: self.config, shortcut: shortcut)
ShortcutConfigView(
config: self.config,
shortcut: shortcut,
commandsMode: self.commandsMode
)
}
}
.onDelete { offsets in
self.config.shortcuts.remove(atOffsets: offsets)
}
.onDelete(perform: _onDelete)
}
.listStyle(GroupedListStyle())
.navigationBarTitle("Shortcuts")
.navigationBarItems(trailing: Button(
action: {
let nav = self.nav
let shortcut = KeyShortcut(action: .none, modifiers: [], input: "")
self.config.shortcuts.append(shortcut)
self.config.touch()
let rootView = ShortcutConfigView(config: self.config, shortcut: shortcut).environmentObject(nav)
let vc = UIHostingController(rootView: rootView)
nav.navController.pushViewController(vc, animated: true)
},
label: { Text("Add") }
))
.navigationBarItems(
trailing: Button(
action: _addAction,
label: { Text("Add") }
)
)
}

private func _addAction() {
let action: KeyBindingAction = commandsMode ? KeyBindingAction.command(.clipboardCopy) : .none
let shortcut = KeyShortcut(action: action, modifiers: [], input: "")
config.shortcuts.append(shortcut)
config.touch()
let rootView = ShortcutConfigView(
config: config,
shortcut: shortcut,
commandsMode: commandsMode
).environmentObject(nav)
let vc = UIHostingController(rootView: rootView)
nav.navController.pushViewController(vc, animated: true)
}

private var _list: [KeyShortcut] {
config
.shortcuts
.filter({$0.action.isCommand == commandsMode})
.sorted(by: {$0.title < $1.title})
}

private func _onDelete(offsets: IndexSet) {
let list = self._list
var toDelete: [KeyShortcut] = []
for idx in offsets {
toDelete.append(list[idx])
}
for v in toDelete {
self.config.shortcuts.removeAll(where: {$0 === v})
}
}
}

0 comments on commit b864fd3

Please sign in to comment.