35 lines
1.1 KiB
Swift
35 lines
1.1 KiB
Swift
import Foundation
|
|
|
|
/// Monotonic timestamps are supplied by the window; tests never wait on a real clock.
|
|
public struct CommandNumberShortcuts: Sendable {
|
|
private var heldSince: TimeInterval?
|
|
public private(set) var isVisible = false
|
|
|
|
public init() {}
|
|
|
|
/// Returns true only when a fresh, eligible Command-only hold begins.
|
|
public mutating func update(commandOnly: Bool, eligible: Bool, at time: TimeInterval) -> Bool {
|
|
guard commandOnly && eligible else { reset(); return false }
|
|
guard heldSince == nil else { return false }
|
|
heldSince = time
|
|
return true
|
|
}
|
|
|
|
public mutating func reveal(at time: TimeInterval) -> Bool {
|
|
guard let heldSince, time - heldSince >= 1 else { return false }
|
|
isVisible = true
|
|
return true
|
|
}
|
|
|
|
public func index(for key: String) -> Int? {
|
|
guard isVisible, key.utf8.count == 1, let digit = key.utf8.first,
|
|
(49...57).contains(digit) else { return nil }
|
|
return Int(digit - 49)
|
|
}
|
|
|
|
public mutating func reset() {
|
|
heldSince = nil
|
|
isVisible = false
|
|
}
|
|
}
|