Skip to content

Commit

Permalink
Autorepeat
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Sep 27, 2019
1 parent 1534b6d commit 093ea2e
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 9 deletions.
53 changes: 52 additions & 1 deletion Blink/SmarterKeys/KBView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class KBView: UIView {
private let _scrollViewRightBorder = UIView()
private let _indicatorLeft = UIView()
private let _indicatorRight = UIView()
private var _timer: Timer? = nil
private var _repeatingKeyView: KBKeyView? = nil

var repeatingSequence: String? = nil

var safeBarWidth: CGFloat = 0
var kbDevice: KBDevice = .detect() {
didSet {
Expand Down Expand Up @@ -272,6 +277,36 @@ class KBView: UIView {
}
_untrackedModifiersSet = _onModifiersSet
}

func _startTimer(with view: KBKeyView) {
_repeatingKeyView = view
_timer?.invalidate()
keyViewTriggered(keyView: view, value: view.currentValue)
weak var weakSelf = self
_timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) { _ in
weakSelf?._continueTimer(interval: 0.1)
}
}

func _continueTimer(interval: TimeInterval) {
let repeatingKeyView = _repeatingKeyView
_timer?.invalidate()
_timer = Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { _ in
guard let view = repeatingKeyView
else {
return
}

view.key.sound.playIfPossible()
view.keyDelegate.keyViewTriggered(keyView: view, value: view.currentValue)
}
}

func stopRepeats() {
_timer?.invalidate()
_timer = nil
_repeatingKeyView = nil
}
}

extension KBView: UIScrollViewDelegate {
Expand All @@ -281,20 +316,28 @@ extension KBView: UIScrollViewDelegate {
}

extension KBView: KBKeyViewDelegate {
func keyViewAskedToCancecScroll(keyView: KBKeyView) {

func keyViewAskedToCancelScroll(keyView: KBKeyView) {
_scrollView.panGestureRecognizer.dropTouches()
}

func keyViewOn(keyView: KBKeyView, value: KBKeyValue) {
stopRepeats()

_toggleModifier(kbKeyValue: value, value: true)
if value.isModifier {
_onModifiersSet.insert(keyView)
}
if (keyView.shouldAutoRepeat) {
_startTimer(with: keyView)
}
}


func keyViewOff(keyView: KBKeyView, value: KBKeyValue) {
_toggleModifier(kbKeyValue: value, value: false)
_onModifiersSet.remove(keyView)
stopRepeats()
}

func keyViewCanGoOff(keyView: KBKeyView, value: KBKeyValue) -> Bool {
Expand Down Expand Up @@ -329,9 +372,14 @@ extension KBView: KBKeyViewDelegate {
if value.isModifier {
return
}
if keyView !== _repeatingKeyView {
stopRepeats()
}

if let sequence = value.sequence {
repeatingSequence = sequence
keyInput?.insertText(sequence)
repeatingSequence = nil
}

turnOffUntracked()
Expand All @@ -340,6 +388,9 @@ extension KBView: KBKeyViewDelegate {
func keyViewCancelled(keyView: KBKeyView) {
_untrackedModifiersSet.remove(keyView)
_onModifiersSet.remove(keyView)
if keyView === _repeatingKeyView {
stopRepeats()
}
}

func keyViewTouchesBegin(keyView: KBKeyView, touches: Set<UITouch>) {
Expand Down
5 changes: 4 additions & 1 deletion Blink/SmarterKeys/KeyViews/KBKeyView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import UIKit

protocol KBKeyViewDelegate: class {
func keyViewAskedToCancecScroll(keyView: KBKeyView)
func keyViewAskedToCancelScroll(keyView: KBKeyView)

func keyViewOn(keyView: KBKeyView, value: KBKeyValue)
func keyViewOff(keyView: KBKeyView, value: KBKeyValue)
Expand All @@ -47,6 +47,9 @@ protocol KBKeyViewDelegate: class {
class KBKeyView: UIView {
let key: KBKey
private(set) var trackingTouch: UITouch? = nil

open var shouldAutoRepeat: Bool { false }

var isTracking: Bool {
if let touch = trackingTouch {
return touch.phase != .ended && touch.phase != .cancelled
Expand Down
14 changes: 9 additions & 5 deletions Blink/SmarterKeys/KeyViews/KBKeyViewSymbol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ class KBKeyViewSymbol: KBKeyView {
self.backgroundColor = .white
_imageView.tintColor = UIColor.darkText
}

if key.shape.primaryValue == .esc {
keyDelegate.keyViewTriggered(keyView: self, value: key.shape.primaryValue)
}
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
Expand All @@ -91,12 +87,20 @@ class KBKeyViewSymbol: KBKeyView {
return
}

if key.shape.primaryValue != .esc {
if !shouldAutoRepeat {
keyDelegate.keyViewTriggered(keyView: self, value: key.shape.primaryValue)
}
super.touchesEnded(touches, with: event)
}

override var shouldAutoRepeat: Bool {
switch key.shape.primaryValue {
case .esc, .left, .right, .up, .down, .tab:
return true
default: return super.shouldAutoRepeat
}
}

override func turnOff() {
super.turnOff()
_imageView.tintColor = UIColor.label
Expand Down
2 changes: 1 addition & 1 deletion Blink/SmarterKeys/KeyViews/KBKeyViewTriangle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class KBKeyViewTriangle: KBKeyView {

debugPrint(_progressV)
if _progressV > 0.03 {
keyDelegate.keyViewAskedToCancecScroll(keyView: self)
keyDelegate.keyViewAskedToCancelScroll(keyView: self)
}

CATransaction.begin()
Expand Down
2 changes: 1 addition & 1 deletion Blink/SmarterKeys/KeyViews/KBKeyViewVertical2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class KBKeyViewVertical2: KBKeyView {
_progress = dy / completeDY

if _progress > 0.15 {
keyDelegate.keyViewAskedToCancecScroll(keyView: self)
keyDelegate.keyViewAskedToCancelScroll(keyView: self)
}

CATransaction.begin()
Expand Down
4 changes: 4 additions & 0 deletions Blink/SmarterKeys/SmarterTermInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ class SmarterTermInput: TermInput {
}

override func insertText(_ text: String) {
if text != _kbView.repeatingSequence {
_kbView.stopRepeats()
}

let traits = _kbView.traits
if traits.contains(.cmdOn) && text.count == 1 {
var flags = traits.modifierFlags
Expand Down

0 comments on commit 093ea2e

Please sign in to comment.