Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Mar 24, 2020
1 parent a412ba7 commit 4647e2b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 217 deletions.
98 changes: 46 additions & 52 deletions Blink/KBTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ import Foundation
import UIKit

class KBTracker {

private(set) var hideSmartKeysWithHKB = !BKUserConfigurationManager.userSettingsValue(forKey: BKUserConfigShowSmartKeysWithXKeyBoard)

static let shared = KBTracker()

private(set) var kbTraits = KBTraits.initial
private(set) var kbDevice = KBDevice.detect()

Expand Down Expand Up @@ -81,20 +81,36 @@ class KBTracker {

func attach(input: SmarterTermInput?) {
self.input = input
if let input = input {
kbTraits = input.kbView.traits
}
input?.sync(traits: kbTraits, device: kbDevice, hideSmartKeysWithHKB: hideSmartKeysWithHKB)
}

init() {
let nc = NotificationCenter.default

kbTraits.isHKBAttached = true

nc.addObserver(self, selector: #selector(_keyboardDidChangeFrame(_:)), name: UIResponder.keyboardDidChangeFrameNotification, object: nil)
nc.addObserver(self, selector: #selector(_keyboardWillChangeFrame(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
nc.addObserver(self, selector: #selector(_keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
nc.addObserver(self, selector: #selector(_keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
nc.addObserver(self, selector: #selector(_keyboardDidHide(_:)), name: UIResponder.keyboardDidHideNotification, object: nil)
nc.addObserver(self, selector: #selector(_keyboardDidShow(_:)), name: UIResponder.keyboardDidShowNotification, object: nil)
nc.addObserver(self, selector: #selector(_inputModeChanged), name: UITextInputMode.currentInputModeDidChangeNotification, object: nil)
nc.addObserver(self, selector: #selector(_updateSettings), name: NSNotification.Name.BKUserConfigChanged, object: nil)
}

@objc private func _updateSettings() {
KBSound.isMutted = BKUserConfigurationManager.userSettingsValue(forKey: BKUserConfigMuteSmartKeysPlaySound)
hideSmartKeysWithHKB = !BKUserConfigurationManager.userSettingsValue(forKey: BKUserConfigShowSmartKeysWithXKeyBoard)

input?.sync(traits: kbTraits, device: kbDevice, hideSmartKeysWithHKB: hideSmartKeysWithHKB)
}

@objc func _inputModeChanged() {
let input = self.input
DispatchQueue.main.async {
input?.reportLang()
}
}

private func _setupWithKBNotification(notification: Notification) {
Expand All @@ -110,7 +126,6 @@ class KBTracker {
return
}

var traits = kbTraits
let mainScreen = UIScreen.main
let screenHeight = mainScreen.bounds.height
let isIPad = UIDevice.current.userInterfaceIdiom == .pad
Expand All @@ -125,54 +140,39 @@ class KBTracker {

let isFloatingKB = isIPad && kbFrameEnd.origin.x > 0 && kbFrameEnd.origin.y > 0

let input = self.input

defer {
traits.isFloatingKB = isFloatingKB
traits.isHKBAttached = !isOnScreenKB
input?.kbView.traits = traits
input?.reportLang()
kbTraits.isFloatingKB = isFloatingKB
kbTraits.isHKBAttached = !isOnScreenKB

debugPrint(kbTraits)
input?.sync(traits: kbTraits, device: kbDevice, hideSmartKeysWithHKB: hideSmartKeysWithHKB)
}

if traits.isHKBAttached && isOnScreenKB {
if !kbTraits.isHKBAttached && isOnScreenKB {
if isIPad {
if isFloatingKB {
input?.kbView.kbDevice = .in6_5
traits.isPortrait = true
input?._setupAccessoryView()
kbDevice = .in6_5
kbTraits.isPortrait = true
} else {
input?._setupAssistantItem()
kbDevice = .detect()
}
} else {
input?._setupAccessoryView()
}
} else if !traits.isHKBAttached && !isOnScreenKB {
input?.kbView.kbDevice = .detect()
if hideSmartKeysWithHKB {
input?._removeSmartKeys()
} else if isIPad {
input?._setupAssistantItem()
} else {
input?._setupAccessoryView()
}
} else if !traits.isFloatingKB && isFloatingKB {
return
}

if kbTraits.isHKBAttached && !isOnScreenKB {
kbDevice = .detect()
return
}
if !kbTraits.isFloatingKB && isFloatingKB {
if isFloatingKB {
input?.kbView.kbDevice = .in6_5
traits.isPortrait = true
input?._setupAccessoryView()
} else {
input?._setupAssistantItem()
kbDevice = .in6_5
kbTraits.isPortrait = true
}
} else if traits.isFloatingKB && !isFloatingKB {
input?.kbView.kbDevice = .detect()
input?._removeSmartKeys()
input?._setupAssistantItem()
} else {
return
}

DispatchQueue.main.async {
input?._refreshInputViews()
if kbTraits.isFloatingKB && !isFloatingKB {
kbDevice = .detect()
}
}

Expand Down Expand Up @@ -204,29 +204,19 @@ class KBTracker {
}

let idiom = UIDevice.current.userInterfaceIdiom
let input = self.input

if isLocal && idiom == .pad {
let isFloating = kbFrameEnd.origin.y > 0 && kbFrameEnd.origin.x > 0 || kbFrameEnd == .zero

if !kbTraits.isFloatingKB && isFloating {
kbDevice = .in6_5
kbTraits.isPortrait = true
input?._setupAccessoryView()
DispatchQueue.main.async {
input?.contentView()?.reloadInputViews()
}
} else if kbTraits.isFloatingKB && !isFloating && !kbTraits.isHKBAttached {
kbDevice = .detect()
input?._removeSmartKeys()
input?._setupAssistantItem()
DispatchQueue.main.async {
input?.contentView()?.reloadInputViews()
}
}
kbTraits.isFloatingKB = isFloating
}

// if bottomInset == 0 && _kbTraits.isFloatingKB,
// let safeInsets = superview?.safeAreaInsets {
// bottomInset = _kbView.intrinsicContentSize.height + safeInsets.bottom
Expand All @@ -236,16 +226,20 @@ class KBTracker {
}

@objc private func _keyboardWillShow(_ notification: Notification) {
debugPrint("_keyboardWillShow")
_setupWithKBNotification(notification: notification)
}

@objc private func _keyboardWillHide(_ notification: Notification) {
debugPrint("_keyboardWillHide")
_setupWithKBNotification(notification: notification)
}

@objc private func _keyboardDidHide(_ notification: Notification) {
debugPrint("_keyboardDidHide")
}

@objc private func _keyboardDidShow(_ notification: Notification) {
debugPrint("_keyboardDidShow")
}
}
18 changes: 9 additions & 9 deletions Blink/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {

// 3. Stuck Key Check

let input = SmarterTermInput.shared
if let key = input.stuckKey() {
let input = KBTracker.shared.input
if let key = input?.stuckKey() {
debugPrint("BK:", "stuck!!!")
input.setTrackingModifierFlags([])
input?.setTrackingModifierFlags([])

if input.isHardwareKB && key == .commandLeft {
if input?.isHardwareKB == true && key == .commandLeft {
let ctrl = UIHostingController(rootView: StuckView(keyCode: key, dismissAction: {
spCtrl.onStuckOpCommand()
}))
Expand All @@ -145,19 +145,19 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
// 4. Focus Check

if term.termDevice.view?.isFocused() == false,
!input.isRealFirstResponder,
input.window === window {
input?.isRealFirstResponder == false,
input?.window === window {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
if scene.activationState == .foregroundActive,
!input.isRealFirstResponder {
input?.isRealFirstResponder == false {
spCtrl.focusOnShellAction()
}
}

return
}

if input.window === window {
if input?.window === window {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
if scene.activationState == .foregroundActive,
term.termDevice.view?.isFocused() == false {
Expand All @@ -168,7 +168,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
return
}

SmarterTermInput.shared.reportStateReset()
input?.reportStateReset()
}

func stateRestorationActivity(for scene: UIScene) -> NSUserActivity? {
Expand Down
6 changes: 6 additions & 0 deletions Blink/SmarterKeys/KBTraits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,9 @@ extension KBTraits {
lhs.union(rhs)
}
}

extension KBTraits: CustomDebugStringConvertible {
var debugDescription: String {
"KBTraits - { hkb: \(isHKBAttached), floating: \(isFloatingKB), landscape: \(isLandscape) }"
}
}
Loading

0 comments on commit 4647e2b

Please sign in to comment.