-
Notifications
You must be signed in to change notification settings - Fork 253
Tap the hold shortcut twice to latch into toggle mode #291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gfrankgva
wants to merge
1
commit into
zachlatta:main
Choose a base branch
from
gfrankgva:double-tap-latch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+244
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| import Foundation | ||
|
|
||
| @main | ||
| struct DictationShortcutSessionControllerTests { | ||
| static func main() { | ||
| testSingleHoldStillStartsAndStopsOnRelease() | ||
| testDoubleTapLatchesIntoToggle() | ||
| testDoubleTapLatchSurvivesTranscriptionOfTheFirstTap() | ||
| testLatchedSessionIsStoppedByTheNextTap() | ||
| testReleaseOfTheSecondTapDoesNotStop() | ||
| testSlowSecondTapIsAnOrdinaryHold() | ||
| testDoubleTapCanBeDisabled() | ||
| testToggleShortcutSessionIsUnaffectedByHoldEvents() | ||
| testHoldThenToggleStillSwitchesToToggle() | ||
| print("DictationShortcutSessionControllerTests passed") | ||
| } | ||
|
|
||
| // MARK: - Existing behaviour must not change | ||
|
|
||
| private static func testSingleHoldStillStartsAndStopsOnRelease() { | ||
| let clock = TestClock() | ||
| let controller = makeController(clock: clock) | ||
|
|
||
| expectEqual(controller.handle(event: .holdActivated, isTranscribing: false), .start(.hold)) | ||
| clock.advance(1.0) | ||
| expectEqual(controller.handle(event: .holdDeactivated, isTranscribing: false), .stop) | ||
| expect(controller.activeMode == nil, "Session should be over after the key is released") | ||
| } | ||
|
|
||
| private static func testToggleShortcutSessionIsUnaffectedByHoldEvents() { | ||
| let clock = TestClock() | ||
| let controller = makeController(clock: clock) | ||
|
|
||
| expectEqual(controller.handle(event: .toggleActivated, isTranscribing: false), .start(.toggle)) | ||
| expectEqual(controller.handle(event: .holdActivated, isTranscribing: false), nil) | ||
| expectEqual(controller.handle(event: .holdDeactivated, isTranscribing: false), nil) | ||
| expect(controller.activeMode == .toggle, "A toggle session must ignore hold events") | ||
|
|
||
| expectEqual(controller.handle(event: .toggleDeactivated, isTranscribing: false), nil) | ||
| expectEqual(controller.handle(event: .toggleActivated, isTranscribing: false), .stop) | ||
| } | ||
|
|
||
| private static func testHoldThenToggleStillSwitchesToToggle() { | ||
| let clock = TestClock() | ||
| let controller = makeController(clock: clock) | ||
|
|
||
| expectEqual(controller.handle(event: .holdActivated, isTranscribing: false), .start(.hold)) | ||
| expectEqual(controller.handle(event: .toggleActivated, isTranscribing: false), .switchedToToggle) | ||
| // Entered from the toggle shortcut, so the hold key must not end it. | ||
| expectEqual(controller.handle(event: .holdDeactivated, isTranscribing: false), nil) | ||
| expect(controller.activeMode == .toggle, "Latched session should still be running") | ||
| } | ||
|
|
||
| // MARK: - Double tap | ||
|
|
||
| private static func testDoubleTapLatchesIntoToggle() { | ||
| let clock = TestClock() | ||
| let controller = makeController(clock: clock) | ||
|
|
||
| expectEqual(controller.handle(event: .holdActivated, isTranscribing: false), .start(.hold)) | ||
| clock.advance(0.09) | ||
| expectEqual(controller.handle(event: .holdDeactivated, isTranscribing: false), .stop) | ||
| clock.advance(0.15) | ||
| expectEqual(controller.handle(event: .holdActivated, isTranscribing: false), .start(.toggle)) | ||
| expect(controller.activeMode == .toggle, "The second tap should latch") | ||
| } | ||
|
|
||
| private static func testDoubleTapLatchSurvivesTranscriptionOfTheFirstTap() { | ||
| let clock = TestClock() | ||
| let controller = makeController(clock: clock) | ||
|
|
||
| _ = controller.handle(event: .holdActivated, isTranscribing: false) | ||
| clock.advance(0.08) | ||
| _ = controller.handle(event: .holdDeactivated, isTranscribing: false) | ||
| // The first tap's near-empty clip is still in flight. The second tap | ||
| // must not be swallowed, or the speaker gets nothing at all. | ||
| clock.advance(0.12) | ||
| expectEqual(controller.handle(event: .holdActivated, isTranscribing: true), .start(.toggle)) | ||
| } | ||
|
|
||
| private static func testLatchedSessionIsStoppedByTheNextTap() { | ||
| let clock = TestClock() | ||
| let controller = makeController(clock: clock) | ||
|
|
||
| _ = controller.handle(event: .holdActivated, isTranscribing: false) | ||
| clock.advance(0.08) | ||
| _ = controller.handle(event: .holdDeactivated, isTranscribing: false) | ||
| clock.advance(0.12) | ||
| expectEqual(controller.handle(event: .holdActivated, isTranscribing: false), .start(.toggle)) | ||
|
|
||
| clock.advance(0.05) | ||
| expectEqual(controller.handle(event: .holdDeactivated, isTranscribing: false), nil) | ||
| clock.advance(12.0) | ||
| expectEqual(controller.handle(event: .holdActivated, isTranscribing: false), .stop) | ||
| expect(controller.activeMode == nil, "The third tap should end the latched session") | ||
| } | ||
|
|
||
| private static func testReleaseOfTheSecondTapDoesNotStop() { | ||
| let clock = TestClock() | ||
| let controller = makeController(clock: clock) | ||
|
|
||
| _ = controller.handle(event: .holdActivated, isTranscribing: false) | ||
| clock.advance(0.08) | ||
| _ = controller.handle(event: .holdDeactivated, isTranscribing: false) | ||
| clock.advance(0.12) | ||
| _ = controller.handle(event: .holdActivated, isTranscribing: false) | ||
|
|
||
| clock.advance(0.06) | ||
| expectEqual(controller.handle(event: .holdDeactivated, isTranscribing: false), nil) | ||
| expect(controller.activeMode == .toggle, "Letting go of the second tap must keep recording") | ||
| } | ||
|
|
||
| private static func testSlowSecondTapIsAnOrdinaryHold() { | ||
| let clock = TestClock() | ||
| let controller = makeController(clock: clock) | ||
|
|
||
| _ = controller.handle(event: .holdActivated, isTranscribing: false) | ||
| clock.advance(0.5) | ||
| _ = controller.handle(event: .holdDeactivated, isTranscribing: false) | ||
| clock.advance(0.9) | ||
| expectEqual(controller.handle(event: .holdActivated, isTranscribing: false), .start(.hold)) | ||
| expect(controller.activeMode == .hold, "A slow second press is just another hold") | ||
| } | ||
|
|
||
| private static func testDoubleTapCanBeDisabled() { | ||
| let clock = TestClock() | ||
| let controller = makeController(clock: clock, enabled: false) | ||
|
|
||
| _ = controller.handle(event: .holdActivated, isTranscribing: false) | ||
| clock.advance(0.08) | ||
| _ = controller.handle(event: .holdDeactivated, isTranscribing: false) | ||
| clock.advance(0.12) | ||
| expectEqual(controller.handle(event: .holdActivated, isTranscribing: false), .start(.hold)) | ||
| } | ||
|
|
||
| // MARK: - Helpers | ||
|
|
||
| private final class TestClock { | ||
| private var current = Date(timeIntervalSince1970: 1_000_000) | ||
| func advance(_ seconds: TimeInterval) { current = current.addingTimeInterval(seconds) } | ||
| func now() -> Date { current } | ||
| } | ||
|
|
||
| private static func makeController(clock: TestClock, enabled: Bool = true) -> DictationShortcutSessionController { | ||
| DictationShortcutSessionController( | ||
| doubleTapLatchEnabled: enabled, | ||
| doubleTapWindow: 0.4, | ||
| now: { clock.now() } | ||
| ) | ||
| } | ||
|
|
||
| private static func expectEqual( | ||
| _ actual: DictationShortcutAction?, | ||
| _ expected: DictationShortcutAction?, | ||
| file: StaticString = #file, | ||
| line: UInt = #line | ||
| ) { | ||
| expect( | ||
| actual == expected, | ||
| "Expected \(String(describing: expected)), got \(String(describing: actual))", | ||
| file: file, | ||
| line: line | ||
| ) | ||
| } | ||
|
|
||
| private static func expect(_ condition: Bool, _ message: String, file: StaticString = #file, line: UInt = #line) { | ||
| if !condition { | ||
| fatalError("\(file):\(line): \(message)") | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Reject negative double-tap intervals.
Dateis a wall clock and can move backward. A negative interval currently satisfies<= doubleTapWindow, so a hold activation can incorrectly enter toggle mode after a clock correction. Require an elapsed interval in the range0...doubleTapWindow.Proposed fix
private func isDoubleTap() -> Bool { guard doubleTapLatchEnabled, let last = lastHoldReleaseAt else { return false } - return now().timeIntervalSince(last) <= doubleTapWindow + let elapsed = now().timeIntervalSince(last) + return elapsed >= 0 && elapsed <= doubleTapWindow }📝 Committable suggestion
🤖 Prompt for AI Agents