Skip to content

Commit

Permalink
Stuck key guard window. Refs blinksh#887
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Dec 23, 2019
1 parent 1dbe3a7 commit 464fd1e
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 34 deletions.
51 changes: 41 additions & 10 deletions Blink/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,40 @@


import Foundation
import SwiftUI

class DummyVC: UIViewController {
override var canBecomeFirstResponder: Bool { true }
override var prefersStatusBarHidden: Bool { true }
public override var prefersHomeIndicatorAutoHidden: Bool { true }
}

struct StuckView: View {
private var _emojies = ["😱", "🤪", "🧐", "🥺", "🤔", "🤭", "🙈", "🙊"]
var keyCode: KeyCode
var dismissAction: () -> ()

init(keyCode: KeyCode, dismissAction: @escaping () -> ()) {
self.keyCode = keyCode
self.dismissAction = dismissAction
}

var body: some View {
VStack {
HStack {
Spacer()
Button(action: dismissAction, label: { Text("Close") })
}.padding()
Spacer()
Text(_emojies.randomElement() ?? "🤥").font(.system(size: 60)).padding(.bottom, 26)
Text("Stuck key detected.").font(.headline).padding(.bottom, 30)
Text("Press \(keyCode.fullName) key").font(.system(size: 30))
Spacer()
Text("Also, please file radar (TODO: link to instructions).").padding()
}
}
}

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow? = nil
var _ctrl = DummyVC()
Expand All @@ -61,24 +88,28 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {

func sceneDidBecomeActive(_ scene: UIScene) {
debugPrint("BK:", "sceneDidBecomeActive")
_setDummyVC()

window?.rootViewController = _spCtrl
guard let term = _spCtrl.currentTerm()
else {
return
}
term.resumeIfNeeded()
term.view?.setNeedsLayout()

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) {
if (scene.activationState == .foregroundActive) {
debugPrint("BK:", "self.window?.rootViewController = self._spCtrl")
self.window?.rootViewController = self._spCtrl
}
}


let input = SmarterTermInput.shared
if let key = input.stuckKey() {
debugPrint("BK:", "stuck!!!")
input.setTrackingModifierFlags([])
let ctrl = UIHostingController(rootView: StuckView(keyCode: key, dismissAction: {
self._spCtrl.onStuckOpCommand()
}))
ctrl.modalPresentationStyle = .formSheet
_spCtrl.stuckKeyCode = key
_spCtrl.present(ctrl, animated: false)
return;
} else {
_spCtrl.stuckKeyCode = nil
}
if
term.termDevice.view?.isFocused() == false,
!input.isRealFirstResponder,
Expand Down
22 changes: 22 additions & 0 deletions Blink/SmarterKeys/SmarterTermInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,28 @@ extension SmarterTermInput {
}
}
}

func stuckKey() -> KeyCode? {
let mods: UIKeyModifierFlags = [.shift, .control, .alternate, .command]
let stuck = mods.intersection(trackingModifierFlags)

if stuck.contains(.shift) {
return KeyCode.shiftLeft
}
if stuck.contains(.control) {
return KeyCode.controlLeft
}

if stuck.contains(.alternate) {
return KeyCode.optionLeft
}

if stuck.contains(.command) {
return KeyCode.commandLeft
}

return nil
}
}
// - MARK: Config

Expand Down
13 changes: 11 additions & 2 deletions Blink/SpaceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class SpaceController: UIViewController {

private var _overlay = UIView()
private var _spaceControllerAnimating: Bool = false
var stuckKeyCode: KeyCode? = nil

public override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
Expand Down Expand Up @@ -459,12 +460,20 @@ extension SpaceController {
public override var keyCommands: [UIKeyCommand]? {
let input = SmarterTermInput.shared
guard foregroundActive else {
return input.noOpKeyCommands
return []
}

if let keyCode = stuckKeyCode {
return [UIKeyCommand(input: "", modifierFlags: keyCode.modifierFlags, action: #selector(onStuckOpCommand))]

}
return input.blinkKeyCommands
}

@objc func _onNoOpCommand(_ cmd: BlinkCommand) {
@objc func onStuckOpCommand() {
stuckKeyCode = nil
presentedViewController?.dismiss(animated: true)
_focusOnShell()
}

@objc func _onBlinkCommand(_ cmd: BlinkCommand) {
Expand Down
11 changes: 11 additions & 0 deletions KB/Native/Model/KeyCode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ enum KeyCode: String, Codable, CaseIterable, Identifiable {

var code: String { rawValue }

var modifierFlags: UIKeyModifierFlags {
switch self {
case .capsLock: return .alphaShift
case .shiftLeft, .shiftRight: return .shift
case .controlLeft, .controlRight: return .control
case .optionLeft, .optionRight: return .alternate
case .commandLeft, .commandRight: return .command
default: return []
}
}

var single: Bool {
switch self {
case .shiftLeft,
Expand Down
14 changes: 0 additions & 14 deletions KB/Native/Views/KBWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class KBWebView: KBWebViewBase {
private var _loaded = false
private(set) var webViewReady = false
private(set) var blinkKeyCommands: [BlinkCommand] = []
private(set) var noOpKeyCommands: [BlinkCommand] = []

func configure(_ cfg: KBConfig) {
_buildCommands(cfg)
Expand All @@ -67,19 +66,6 @@ class KBWebView: KBWebViewBase {
cmd.bindingAction = shortcut.action
return cmd
}

noOpKeyCommands = cfg.shortcuts.map { shortcut in
let cmd = BlinkCommand(
title: shortcut.action.isCommand ? shortcut.title : "", // Show only commands in cmd help view
image: nil,
action: #selector(SpaceController._onNoOpCommand(_:)),
input: shortcut.input,
modifierFlags: shortcut.modifiers,
propertyList: nil
)
cmd.bindingAction = shortcut.action
return cmd
}
}

func matchCommand(input: String, flags: UIKeyModifierFlags) -> (UIKeyCommand, UIResponder)? {
Expand Down
1 change: 1 addition & 0 deletions KB/Native/Views/KBWebViewBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)onOut:(NSString *)data;
- (void)onIME:(NSString *)event data:(NSString *)data;
- (void)onCapture:(NSArray<NSString *> *)keys;
- (void)setTrackingModifierFlags:(UIKeyModifierFlags)trackingModifierFlags;

- (void)_keyboardDidChangeFrame:(NSNotification *)notification;
- (void)_keyboardWillChangeFrame:(NSNotification *)notification;
Expand Down
19 changes: 11 additions & 8 deletions KB/Native/Views/KBWebViewBase.m
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,16 @@ - (void)_removeAssistantsFromView:(UIView *)view {
}
}

- (void)setTrackingModifierFlags:(UIKeyModifierFlags)trackingModifierFlags {
_trackingModifierFlags = trackingModifierFlags;
if (_trackingModifierFlags == 0) {
_activeModsCommand = nil;
} else {
_activeModsCommand = [self _modifiersCommand:_trackingModifierFlags];
}
[self _rebuildKeyCommands];
[self onMods];
}

- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message {
Expand All @@ -315,14 +325,7 @@ - (void)userContentController:(WKUserContentController *)userContentController
});
} else if ([@"mods" isEqual:op]) {
NSNumber *mods = body[@"mods"];
_trackingModifierFlags = (UIKeyModifierFlags)mods.integerValue;
if (_trackingModifierFlags == 0) {
_activeModsCommand = nil;
} else {
_activeModsCommand = [self _modifiersCommand:_trackingModifierFlags];
}
[self _rebuildKeyCommands];
[self onMods];
[self setTrackingModifierFlags:(UIKeyModifierFlags)mods.integerValue];
} else if ([@"ime" isEqual:op]) {
NSString *event = body[@"type"];
NSString *data = body[@"data"];
Expand Down

0 comments on commit 464fd1e

Please sign in to comment.