Skip to content

Commit

Permalink
Improve smartkb on ipad
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Dec 10, 2019
1 parent 2122eb4 commit 99b61ad
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 157 deletions.
151 changes: 38 additions & 113 deletions Blink/SmarterKeys/SmarterTermInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class SmarterTermInput: KBWebView {
_kbView.keyInput = self
_kbView.lang = textInputMode?.primaryLanguage ?? ""


KBSound.isMutted = BKUserConfigurationManager.userSettingsValue(
forKey: BKUserConfigMuteSmartKeysPlaySound)

Expand All @@ -77,36 +78,9 @@ class SmarterTermInput: KBWebView {
self,
selector: #selector(_inputModeChanged),
name: UITextInputMode.currentInputModeDidChangeNotification, object: nil)

nc.addObserver(
self,
selector: #selector(_keyboardWillChangeFrame(notification:)),
name: UIResponder.keyboardWillChangeFrameNotification, object: nil)

nc.addObserver(
self,
selector: #selector(_keyboardWillHideNotification(notification:)),
name: UIResponder.keyboardWillHideNotification, object: nil)

nc.addObserver(
self,
selector: #selector(_keyboardDidHideNotification(notification:)),
name: UIResponder.keyboardDidHideNotification, object: nil)

nc.addObserver(
self,
selector: #selector(_keyboardWillShowNotification),
name: UIResponder.keyboardWillShowNotification, object: nil)

nc.addObserver(
self,
selector: #selector(_keyboardDidShowNotification),
name: UIResponder.keyboardDidShowNotification, object: nil)



nc.addObserver(self, selector: #selector(_updateSettings), name: NSNotification.Name.BKUserConfigChanged, object: nil)
nc.addObserver(self, selector: #selector(_setKBStyle), name: NSNotification.Name(rawValue: BKAppearanceChanged), object: nil)

}

required init?(coder: NSCoder) {
Expand All @@ -115,12 +89,15 @@ class SmarterTermInput: KBWebView {

override func ready() {
super.ready()
reportLang(_kbView.lang)

if traitCollection.userInterfaceIdiom == .pad {
setupAssistantItem()
} else {
setupAccessoryView()
}
reportLang(_kbView.lang)

reloadInputViews()
}

override func configure(_ cfg: KBConfig) {
Expand Down Expand Up @@ -226,9 +203,9 @@ class SmarterTermInput: KBWebView {

func matchCommand(input: String, flags: UIKeyModifierFlags) -> (UIKeyCommand, UIResponder)? {
var result: (UIKeyCommand, UIResponder)? = nil

var iterator: UIResponder? = self

while let responder = iterator {
if let cmd = responder.keyCommands?.first(
where: { $0.input == input && $0.modifierFlags == flags}),
Expand All @@ -239,7 +216,7 @@ class SmarterTermInput: KBWebView {
}
iterator = responder.next
}

return result
}

Expand All @@ -255,20 +232,6 @@ class SmarterTermInput: KBWebView {
}
}

// override func becomeFirstResponder() -> Bool {
// let res = super.becomeFirstResponder()
// device?.focus()
// _kbView.isHidden = false
// refreshInputViews()
// // TODO: fix
//// if _kbView.traits.isFloatingKB {
//// DispatchQueue.main.async {
//// self.reloadInputViews()
//// }
//// }
// return res
// }

func contentView() -> UIView? {
scrollView.subviews.first
}
Expand Down Expand Up @@ -340,7 +303,6 @@ class SmarterTermInput: KBWebView {
rect.origin.y = minY
}

debugPrint(rect)
rect.size.height = 0
rect.size.width = 0

Expand All @@ -364,57 +326,11 @@ class SmarterTermInput: KBWebView {
return res
}

// override var canResignFirstResponder: Bool {
// let state = window?.windowScene?.activationState
// return state == .foregroundActive || state == .foregroundInactive
// }

// override func insertText(_ text: String) {
// defer {
// _kbView.turnOffUntracked()
// }
//
// if text != _kbView.repeatingSequence {
// _kbView.stopRepeats()
// }
//
// let traits = _kbView.traits
// if traits.contains(.cmdOn) && text.count == 1 {
// var flags = traits.modifierFlags
// var input = text.lowercased()
// if input != text {
// flags.insert(.shift)
// }
// input = _langCharsMap[input] ?? input
//
// if let (cmd, res) = _matchCommand(input: input, flags: flags),
// let action = cmd.action {
// res.perform(action, with: cmd)
// } else {
// switch(input) {
// case "c": copy(self)
// case "x": cut(self)
// case "z": flags.contains(.shift) ? undoManager?.undo() : undoManager?.redo()
// case "v": paste(self)
// default: super.insertText(text);
// }
// }
// } else if traits.contains([.altOn, .ctrlOn]) {
// escCtrlSeq(withInput:text)
// } else if traits.contains(.altOn) {
// escSeq(withInput: text)
// } else if traits.contains(.ctrlOn) {
// ctrlSeq(withInput: text)
// } else {
// super.insertText(text)
// }
// }

func _removeSmartKeys() {
_inputAccessoryView = UIView(frame: .zero)
self.removeAssistantsFromView()
// realInputAssistantItem?.leadingBarButtonGroups = []
// realInputAssistantItem?.trailingBarButtonGroups = []
realInputAssistantItem?.leadingBarButtonGroups = []
realInputAssistantItem?.trailingBarButtonGroups = []
}

func setupAccessoryView() {
Expand All @@ -423,7 +339,9 @@ class SmarterTermInput: KBWebView {
_inputAccessoryView = KBAccessoryView(kbView: kbView)
}

override var inputAccessoryView: UIView? { _inputAccessoryView }
override var inputAccessoryView: UIView? {
return _inputAccessoryView
}

func setupAssistantItem() {
let proxy = KBProxy(kbView: kbView)
Expand All @@ -433,11 +351,11 @@ class SmarterTermInput: KBWebView {
}

var realInputAssistantItem: UITextInputAssistantItem? {
self.scrollView.subviews.first?.inputAssistantItem
scrollView.subviews.first?.inputAssistantItem
}

func _setupWithKBNotification(notification: NSNotification) {

func _setupWithKBNotification(notification: Notification) {
debugPrint(notification)
guard
let userInfo = notification.userInfo,
let kbFrameEnd = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect,
Expand Down Expand Up @@ -506,27 +424,17 @@ class SmarterTermInput: KBWebView {
}

DispatchQueue.main.async {
self.refreshInputViews()
// self.contentView()?.inputAccessoryView?.invalidateIntrinsicContentSize()
// self.contentView()?.reloadInputViews()
}
}

@objc func _keyboardWillShowNotification(notification: NSNotification) {
_setupWithKBNotification(notification: notification)
}

@objc func _keyboardWillHideNotification(notification: NSNotification) {
_setupWithKBNotification(notification: notification)
}

@objc func _keyboardDidHideNotification(notification: NSNotification) {
}

@objc func _keyboardDidShowNotification(notification: NSNotification) {
_keyboardWillChangeFrame(notification: notification)
override func _keyboardDidChangeFrame(_ notification: Notification) {

}

@objc func _keyboardWillChangeFrame(notification: NSNotification) {
override func _keyboardWillChangeFrame(_ notification: Notification) {
guard
let userInfo = notification.userInfo,
let kbFrameEnd = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect,
Expand Down Expand Up @@ -573,6 +481,23 @@ class SmarterTermInput: KBWebView {
LayoutManager.updateMainWindowKBBottomInset(bottomInset);
}

override func _keyboardWillShow(_ notification: Notification) {
_setupWithKBNotification(notification: notification)
}

override func _keyboardWillHide(_ notification: Notification) {
// _setupWithKBNotification(notification: notification)
}

override func _keyboardDidHide(_ notification: Notification) {

}

override func _keyboardDidShow(_ notification: Notification) {
_kbView.invalidateIntrinsicContentSize()
_keyboardWillChangeFrame(notification)
}

override func copy(_ sender: Any?) {
device?.view?.copy(sender)
}
Expand Down
9 changes: 0 additions & 9 deletions Blink/SpaceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,6 @@ public class SpaceController: UIViewController {
object: nil)
}

// public override func resignFirstResponder() -> Bool {
// return super.resignFirstResponder()
// }

@objc func _didBecomeKeyWindow() {
guard
let window = view.window,
Expand Down Expand Up @@ -277,11 +273,6 @@ public class SpaceController: UIViewController {
_attachInputToCurrentTerm()
let input = SmarterTermInput.shared
_ = input.realBecomeFirstResponder()
// if !input.isRealFirstResponder {
// input.realBecomeFirstResponder()
// } else {
// input.refreshInputViews()
// }
// We should make input window key window
if input.window?.isKeyWindow == false {
input.window?.makeKeyAndVisible()
Expand Down
4 changes: 2 additions & 2 deletions KB/JS/dist/kb.js

Large diffs are not rendered by default.

42 changes: 9 additions & 33 deletions KB/JS/src/Keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,33 +134,6 @@ function _blockEvent(e: UIEvent | null) {
}
}

/*
const _keyToCodeMap: {[index: string]: number} = {
KeyC: 67,
c: 67,
KeyI: 73,
i: 73,
KeyH: 72,
h: 72,
KeyM: 77,
m: 77,
BracketLeft: 219,
'[': 77,
KeyN: 78,
n: 78,
KeyU: 85,
u: 85,
KeyE: 69,
e: 69,
KeyV: 86,
v: 86,
KeyW: 87,
w: 87,
KeyQ: 81,
q: 81,
};
*/

function _patchKeyDown(
keyDown: KeyDownType,
keyMap: KeyMap,
Expand Down Expand Up @@ -371,7 +344,6 @@ export default class Keyboard implements IKeyboard {

let keyId = _keyId(event);
this._down.add(keyId);
console.log('down', this._down);

let binding = this._bindings.match(this._downKeysIds());
if (!_holders.has(keyId)) {
Expand Down Expand Up @@ -436,7 +408,6 @@ export default class Keyboard implements IKeyboard {

let keyId = _keyId(e);
this._down.delete(keyId);
console.log('up', keyId, this._down);
let mod = this._mod(this._modsMap[e.code]);
if (mod) {
this._mods[mod].delete(keyId);
Expand Down Expand Up @@ -718,6 +689,9 @@ export default class Keyboard implements IKeyboard {

_output = (data: string | null) => {
this._up.clear();
this.element.value = ' ';
this.element.selectionStart = 1;
this.element.selectionEnd = 1;
if (data) {
op('out', {data});
}
Expand All @@ -733,6 +707,8 @@ export default class Keyboard implements IKeyboard {
Control: new Set(),
};
this.element.value = ' ';
this.element.selectionStart = 1;
this.element.selectionEnd = 1;
};

_handleGuard(up: boolean, char: string) {
Expand Down Expand Up @@ -916,15 +892,15 @@ export default class Keyboard implements IKeyboard {
src: 'toolbar',
};
if (!e) {
let keyId = keyInfo.keyCode + ":" + parts[2]
this._down.add(keyId)
let keyId = keyInfo.keyCode + ':' + parts[2];
this._down.add(keyId);
let binding = this._bindings.match(this._downKeysIds());
this._down.delete(keyId)
this._down.delete(keyId);
if (binding) {
this._execBinding(binding, null);
this._mods = savedMods;
return;
}
}
}
this._handleKeyDownKey(keyInfo, e);
this._mods = savedMods;
Expand Down
4 changes: 4 additions & 0 deletions KB/Native/Views/KBWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ class KBWebView: KBWebViewBase {
loadFileURL(url, allowingReadAccessTo: url.deletingLastPathComponent())
}

public override var editingInteractionConfiguration: UIEditingInteractionConfiguration {
UIEditingInteractionConfiguration.default
}

override func didMoveToSuperview() {
super.didMoveToSuperview()
if window != nil && !_loaded {
Expand Down
8 changes: 8 additions & 0 deletions KB/Native/Views/KBWebViewBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ NS_ASSUME_NONNULL_BEGIN
- (void)onIME:(NSString *)event data:(NSString *)data;
- (void)onCapture:(NSArray<NSString *> *)keys;

- (void)_keyboardDidChangeFrame:(NSNotification *)notification;
- (void)_keyboardWillChangeFrame:(NSNotification *)notification;
- (void)_keyboardWillShow:(NSNotification *)notification;
- (void)_keyboardWillHide:(NSNotification *)notification;
- (void)_keyboardDidHide:(NSNotification *)notification;
- (void)_keyboardDidShow:(NSNotification *)notification;


@end

NS_ASSUME_NONNULL_END
Loading

0 comments on commit 99b61ad

Please sign in to comment.