Skip to content
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

✨ #74 Trigger key and conflicts #123

Merged
merged 5 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Loop/Extensions/Defaults+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extension Defaults.Keys {

static let radialMenuCornerRadius = Key<CGFloat>("radialMenuCornerRadius", default: 50)
static let radialMenuThickness = Key<CGFloat>("radialMenuThickness", default: 22)
static let hideUntilDirectionIsChosen = Key<Bool>("hideUntilDirectionIsChosen", default: false)

static let triggerKey = Key<Set<CGKeyCode>>("trigger", default: [.kVK_Function])
static let doubleClickToTrigger = Key<Bool>("doubleClickToTrigger", default: false)
Expand All @@ -37,8 +38,6 @@ extension Defaults.Keys {
static let previewPadding = Key<CGFloat>("previewPadding", default: 10)
static let previewBorderThickness = Key<CGFloat>("previewBorderThickness", default: 5)

static let preferMinimizeWithScrollDown = Key<Bool>("preferMinimizeWithScrollDown", default: false)

static let keybinds = Key<[Keybind]>("keybinds", default: [
Keybind(.maximize, keycode: [.kVK_Space]),
Keybind(.center, keycode: [.kVK_Return]),
Expand Down
59 changes: 31 additions & 28 deletions Loop/Managers/KeybindMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,6 @@ class KeybindMonitor {
KeybindMonitor.shared.pressedKeys = []
}

private func performKeybind(event: NSEvent) {
// If the current key up event is within 100 ms of the last key up event, return.
// This is used when the user is pressing 2+ keys so that it doesn't switch back
// to the one key direction when they're letting go of the keys.
if event.type == .keyUp ||
(event.type == .flagsChanged &&
!event.modifierFlags.intersection(.deviceIndependentFlagsMask).contains(.shift)) {
if (abs(lastKeyReleaseTime.timeIntervalSinceNow)) > 0.1 {
return
}
lastKeyReleaseTime = Date.now
}

if pressedKeys.contains(.kVK_Escape) {
Notification.Name.forceCloseLoop.post()
KeybindMonitor.shared.resetPressedKeys()
return
}

if let newDirection = WindowDirection.getDirection(for: pressedKeys) {
Notification.Name.directionChanged.post(userInfo: ["direction": newDirection])
}
}

func start() {
guard self.eventMonitor == nil,
PermissionsManager.Accessibility.getStatus() else {
Expand All @@ -61,7 +37,7 @@ class KeybindMonitor {
KeybindMonitor.shared.pressedKeys.insert(event.keyCode.baseKey)
}

self.performKeybind(event: event)
return self.performKeybind(event: event) ? nil : Unmanaged.passUnretained(cgEvent)
}

return nil
Expand All @@ -76,9 +52,6 @@ class KeybindMonitor {
self.checkForModifier(event, .kVK_Command, .command)
self.checkForModifier(event, .kVK_Option, .option)
self.checkForModifier(event, .kVK_Function, .function)
self.checkForModifier(event, .kVK_Control, .control)

print(KeybindMonitor.shared.pressedKeys)

self.performKeybind(event: event)
}
Expand All @@ -101,6 +74,36 @@ class KeybindMonitor {
self.flagsEventMonitor = nil
}

@discardableResult
private func performKeybind(event: NSEvent) -> Bool {
// If the current key up event is within 100 ms of the last key up event, return.
// This is used when the user is pressing 2+ keys so that it doesn't switch back
// to the one key direction when they're letting go of the keys.
if event.type == .keyUp ||
(event.type == .flagsChanged &&
!event.modifierFlags.intersection(.deviceIndependentFlagsMask).contains(.shift)) {
if (abs(lastKeyReleaseTime.timeIntervalSinceNow)) > 0.1 {
return true
}
lastKeyReleaseTime = Date.now
}

if pressedKeys.contains(.kVK_Escape) {
Notification.Name.forceCloseLoop.post()
KeybindMonitor.shared.resetPressedKeys()
return true
}

if let newDirection = WindowDirection.getDirection(for: pressedKeys) {
Notification.Name.directionChanged.post(userInfo: ["direction": newDirection])
return true
}

// If this wasn't a valid keybind, return false, which will
// then forward the key event to the frontmost app
return false
}

private func checkForModifier(_ event: NSEvent, _ key: CGKeyCode, _ modifierFlag: NSEvent.ModifierFlags) {
if event.keyCode.baseKey == key {
if event.modifierFlags.intersection(.deviceIndependentFlagsMask).contains(modifierFlag) {
Expand Down
Loading