Skip to content

Commit

Permalink
fix: rare crash at launch
Browse files Browse the repository at this point in the history
  • Loading branch information
lwouis committed Aug 6, 2020
1 parent a7247da commit 461840e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
16 changes: 6 additions & 10 deletions src/logic/events/MouseEvents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@ class MouseEvents {
observe_()
}

static func disable() {
shouldBeEnabled = false
CGEvent.tapEnable(tap: eventTap, enable: false)
}

static func enable() {
shouldBeEnabled = true
CGEvent.tapEnable(tap: eventTap, enable: true)
static func toggle(_ enabled: Bool) {
shouldBeEnabled = enabled
if let eventTap = eventTap {
CGEvent.tapEnable(tap: eventTap, enable: enabled)
}
}
}


private func observe_() {
let eventMask = [CGEventType.leftMouseDown, CGEventType.leftMouseUp].reduce(CGEventMask(0), { $0 | (1 << $1.rawValue) })
// CGEvent.tapCreate returns null if ensureAccessibilityCheckboxIsChecked() didn't pass
Expand All @@ -32,7 +28,7 @@ private func observe_() {
eventsOfInterest: eventMask,
callback: mouseHandler,
userInfo: nil)
MouseEvents.disable()
MouseEvents.toggle(false)
let runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0)
CFRunLoopAddSource(BackgroundWork.mouseEventsThread.runLoop, runLoopSource, .commonModes)
}
Expand Down
2 changes: 1 addition & 1 deletion src/ui/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class App: AppCenterApplication, NSApplicationDelegate {
debugPrint("hideUi")
appIsBeingUsed = false
isFirstSummon = true
MouseEvents.disable()
MouseEvents.toggle(false)
thumbnailsPanel.orderOut(nil)
}

Expand Down
2 changes: 1 addition & 1 deletion src/ui/main-window/ThumbnailsPanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ThumbnailsPanel: NSPanel {

func show() {
orderFrontRegardless()
MouseEvents.enable()
MouseEvents.toggle(true)
thumbnailsView.scrollView.flashScrollers()
}

Expand Down

0 comments on commit 461840e

Please sign in to comment.