Skip to content

Commit

Permalink
Improve peek command with a release threshold
Browse files Browse the repository at this point in the history
If you don't release the key for 0.75 seconds, they app won't go hide itself again
  • Loading branch information
zenangst committed Oct 23, 2024
1 parent 4d8233b commit 6008fc1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
22 changes: 19 additions & 3 deletions App/Sources/Core/MachPort/MachPortCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,18 @@ final class MachPortCoordinator: @unchecked Sendable, ObservableObject {
if handleEscapeKeyDownEvent(machPortEvent) { return }
case .keyUp:
if let workflow = previousExactMatch, workflow.machPortConditions.shouldRunOnKeyUp {
machPortEvent.result = nil
Task.detached { [workflowRunner] in
await workflowRunner.run(workflow, machPortEvent: machPortEvent, repeatingEvent: false)
if let previousKeyDownMachPortEvent = PeekApplicationPlugin.peekEvent {
let pressDurtion = timeElapsedInSeconds(
start: previousKeyDownMachPortEvent.event.timestamp,
end: machPortEvent.event.timestamp
)
if pressDurtion < 0.75 {
Task.detached { [workflowRunner] in
await workflowRunner.run(workflow, machPortEvent: machPortEvent, repeatingEvent: false)
}
}
PeekApplicationPlugin.peekEvent = nil
return
}
} else if case .captureKeyDown(let keyCode) = scheduledAction, keyCode == machPortKeyCode {
scheduledAction = nil
Expand Down Expand Up @@ -254,6 +263,13 @@ final class MachPortCoordinator: @unchecked Sendable, ObservableObject {
notifications.notifyBundles(partialMatch)
}

func timeElapsedInSeconds(start: CGEventTimestamp, end: CGEventTimestamp) -> Double {
var timebaseInfo = mach_timebase_info_data_t()
mach_timebase_info(&timebaseInfo)
let elapsedTicks = end - start
return Double(elapsedTicks) / 1_000_000_000
}

@MainActor
private func handleExtactMatch(_ workflow: Workflow, machPortEvent: MachPortEvent, isRepeatingEvent: Bool) {
if workflow.machPortConditions.isPassthrough == true {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ final class ApplicationCommandRunner: @unchecked Sendable {
case .peek:
guard let machPortEvent else { return }

await PeekApplicationPlugin.set(machPortEvent)

if machPortEvent.type == .keyDown {
try await openApplication(command, checkCancellation: checkCancellation)
} else if machPortEvent.type == .keyUp {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Foundation
import MachPort

final class PeekApplicationPlugin: @unchecked Sendable {
@MainActor static var peekEvent: MachPortEvent?

@MainActor static func set(_ peekEvent: MachPortEvent) {
Self.peekEvent = peekEvent
}
}

0 comments on commit 6008fc1

Please sign in to comment.