From fa716268f7ca7d3d8fb5563ab5655c03478579b9 Mon Sep 17 00:00:00 2001 From: Kai Azim <68963405+MrKai77@users.noreply.github.com> Date: Thu, 7 Sep 2023 18:27:47 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20#25=20Ability=20to=20add=20trigger?= =?UTF-8?q?=20delay?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Loop/Extensions/Defaults+Extensions.swift | 1 + Loop/Managers/LoopManager.swift | 19 +++++++++++++++++-- Loop/Settings/KeybindingSettingsView.swift | 16 ++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/Loop/Extensions/Defaults+Extensions.swift b/Loop/Extensions/Defaults+Extensions.swift index 79c7f17b..28e5d252 100644 --- a/Loop/Extensions/Defaults+Extensions.swift +++ b/Loop/Extensions/Defaults+Extensions.swift @@ -20,6 +20,7 @@ extension Defaults.Keys { static let gradientColor = Key("gradientColor", default: Color(.black)) static let triggerKey = Key("triggerKey", default: .kVK_Function) + static let triggerDelay = Key("triggerDelay", default: 0) static let radialMenuCornerRadius = Key("radialMenuCornerRadius", default: 50) static let radialMenuThickness = Key("radialMenuThickness", default: 22) diff --git a/Loop/Managers/LoopManager.swift b/Loop/Managers/LoopManager.swift index a7038bc0..de633f61 100644 --- a/Loop/Managers/LoopManager.swift +++ b/Loop/Managers/LoopManager.swift @@ -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() + } } } } diff --git a/Loop/Settings/KeybindingSettingsView.swift b/Loop/Settings/KeybindingSettingsView.swift index 92d4e7b6..717867d2 100644 --- a/Loop/Settings/KeybindingSettingsView.swift +++ b/Loop/Settings/KeybindingSettingsView.swift @@ -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 @@ -44,6 +45,21 @@ struct KeybindingSettingsView: View { .onChange(of: triggerKey) { _ in refreshTriggerKeySymbol() } + + HStack { + Stepper( + "Trigger Delay", + value: Binding( + get: { Double(self.triggerDelay) }, + set: { self.triggerDelay = Float($0) } + ), + in: 0...1, + step: 0.1, + format: .number + ) + Text("seconds") + .foregroundColor(.secondary) + } } Section("Instructions") {