Skip to content

Commit

Permalink
Fix #25
Browse files Browse the repository at this point in the history
✨ Ability to add trigger delay
  • Loading branch information
MrKai77 authored Sep 8, 2023
2 parents 9e33f1b + fa71626 commit b4862a0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions Loop/Extensions/Defaults+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extension Defaults.Keys {
static let gradientColor = Key<Color>("gradientColor", default: Color(.black))

static let triggerKey = Key<CGKeyCode>("triggerKey", default: .kVK_Function)
static let triggerDelay = Key<Float>("triggerDelay", default: 0)
static let radialMenuCornerRadius = Key<CGFloat>("radialMenuCornerRadius", default: 50)
static let radialMenuThickness = Key<CGFloat>("radialMenuThickness", default: 22)

Expand Down
19 changes: 17 additions & 2 deletions Loop/Managers/LoopManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,28 @@ class LoopManager {
private var frontmostWindow: Window?
private var screenWithMouse: NSScreen?

private var timer: DispatchSourceTimer? // Used when user has configured a trigger delay

func startObservingKeys() {
NSEvent.addGlobalMonitorForEvents(matching: NSEvent.EventTypeMask.flagsChanged) { event -> Void in
if event.keyCode == Defaults[.triggerKey] {
if event.modifierFlags.rawValue == 256 {
self.closeLoop()
if self.timer != nil {
self.timer?.cancel()
self.timer = nil
} else {
self.closeLoop()
}
} else {
self.openLoop()
if self.timer == nil {
self.timer = DispatchSource.makeTimerSource(queue: .main)
self.timer!.schedule(deadline: .now() + .milliseconds(Int(Defaults[.triggerDelay]*1000)))
self.timer!.setEventHandler {
self.openLoop()
self.timer = nil
}
self.timer!.resume()
}
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions Loop/Settings/KeybindingSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Defaults
struct KeybindingSettingsView: View {

@Default(.triggerKey) var triggerKey
@Default(.triggerDelay) var triggerDelay
@Default(.useSystemAccentColor) var useSystemAccentColor
@Default(.customAccentColor) var customAccentColor

Expand Down Expand Up @@ -44,6 +45,21 @@ struct KeybindingSettingsView: View {
.onChange(of: triggerKey) { _ in
refreshTriggerKeySymbol()
}

HStack {
Stepper(
"Trigger Delay",
value: Binding<Double>(
get: { Double(self.triggerDelay) },
set: { self.triggerDelay = Float($0) }
),
in: 0...1,
step: 0.1,
format: .number
)
Text("seconds")
.foregroundColor(.secondary)
}
}

Section("Instructions") {
Expand Down

0 comments on commit b4862a0

Please sign in to comment.