Skip to content

Commit

Permalink
🎨 Move code to process Loop trigger key presses into its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKai77 committed Sep 15, 2023
1 parent 9783d06 commit 8fe8d00
Showing 1 changed file with 39 additions and 35 deletions.
74 changes: 39 additions & 35 deletions Loop/Managers/LoopManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,7 @@ class LoopManager {

func startObservingKeys() {
NSEvent.addGlobalMonitorForEvents(matching: NSEvent.EventTypeMask.flagsChanged) { event in
if event.keyCode == Defaults[.triggerKey].keycode {

let useTriggerDelay = Defaults[.triggerDelay] > 0.1
let useDoubleClickTrigger = Defaults[.doubleClickToTrigger]

if event.modifierFlags.rawValue == 256 {
if useTriggerDelay {
self.cancelTriggerDelayTimer()
}
self.closeLoop()
} else {
if useDoubleClickTrigger {
if abs(self.lastTriggerKeyClick.timeIntervalSinceNow) < NSEvent.doubleClickInterval {
if useTriggerDelay {
if self.triggerDelayTimer == nil {
self.startTriggerDelayTimer(seconds: Defaults[.triggerDelay]) {
self.openLoop()
}
}
} else {
self.openLoop()
}
}
self.lastTriggerKeyClick = Date.now
} else if useTriggerDelay {
if self.triggerDelayTimer == nil {
self.startTriggerDelayTimer(seconds: Defaults[.triggerDelay]) {
self.openLoop()
}
}
} else {
self.openLoop()
}
}
}
self.handleLoopKeypress(event)
}

NotificationCenter.default.addObserver(
Expand Down Expand Up @@ -121,6 +87,44 @@ class LoopManager {
}
}

private func handleLoopKeypress(_ event: NSEvent) {
if event.keyCode == Defaults[.triggerKey].keycode {

let useTriggerDelay = Defaults[.triggerDelay] > 0.1
let useDoubleClickTrigger = Defaults[.doubleClickToTrigger]

if event.modifierFlags.rawValue == 256 {
if useTriggerDelay {
self.cancelTriggerDelayTimer()
}
self.closeLoop()
} else {
if useDoubleClickTrigger {
if abs(self.lastTriggerKeyClick.timeIntervalSinceNow) < NSEvent.doubleClickInterval {
if useTriggerDelay {
if self.triggerDelayTimer == nil {
self.startTriggerDelayTimer(seconds: Defaults[.triggerDelay]) {
self.openLoop()
}
}
} else {
self.openLoop()
}
}
self.lastTriggerKeyClick = Date.now
} else if useTriggerDelay {
if self.triggerDelayTimer == nil {
self.startTriggerDelayTimer(seconds: Defaults[.triggerDelay]) {
self.openLoop()
}
}
} else {
self.openLoop()
}
}
}
}

private func openLoop() {
currentResizingDirection = .noAction
frontmostWindow = nil
Expand Down

0 comments on commit 8fe8d00

Please sign in to comment.