Repository navigation
Fix gesture button not releasing trigger, causing stuck drag - #1142
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes a macOS stuck-button condition in GestureButtonTransformer where a trigger button mouseUp could be consumed during the gesture cooldown, leaving the OS/app believing the button remained pressed (e.g., stuck drag / persistent context menus).
Changes:
- Skip re-processing LinearMouse-generated synthetic events in
transform(_:)to avoid re-entrance loops. - During cooldown, detect trigger-button
mouseUpand post a synthetic release event instead of silently consuming it. - Add a helper to synthesize and post a trigger-button
mouseUpvia.cgSessionEventTap.
Comments suppressed due to low confidence (1)
LinearMouse/EventTransformer/GestureButtonTransformer.swift:209
releaseTriggerButton(from:)is invoked in the.triggeredbranch ofhandleButtonUp, buthandleDraggedsetsstate = .triggeredand then immediately overwrites it with.cooldown(...), so.triggeredis never observed here. Consider removing the.triggeredstate/branch or adjusting the state transitions so this branch is reachable (otherwise this call is misleading dead code).
// If we triggered, stay in cooldown and consume the event
if case .triggered = state {
releaseTriggerButton(from: event)
let cooldownNanos = UInt64(cooldownMs) * 1_000_000
state = .cooldown(until: DispatchTime.now().uptimeNanoseconds + cooldownNanos)
return nil
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Check if we're in cooldown | ||
| if case let .cooldown(until) = state { | ||
| if DispatchTime.now().uptimeNanoseconds < until { | ||
| // Still in cooldown - consume our button events | ||
| if matchesTriggerButton(event) { | ||
| // os_log("Event consumed during cooldown", log: Self.log, type: .debug) | ||
| if event.type == mouseUpEventType { | ||
| os_log("Releasing trigger button during cooldown", log: Self.log, type: .debug) | ||
| releaseTriggerButton(from: event) | ||
| state = .idle | ||
| } else { | ||
| os_log("Event consumed during cooldown", log: Self.log, type: .debug) | ||
| } | ||
| return nil |
There was a problem hiding this comment.
In the cooldown path, state is set back to .idle when a trigger-button mouseUp arrives. This ends the configured cooldown early, so subsequent trigger presses can re-enter tracking immediately (and the rest of the cooldown window will no longer suppress re-triggers). If the intent is to keep cooldown semantics after a gesture, keep state in .cooldown(until: ...) and only synthesize the release; optionally track that the release has already been posted to avoid duplicate releases.
lujjjh
left a comment
There was a problem hiding this comment.
Thanks for the contribution!
|
|
||
| extension GestureButtonTransformer: EventTransformer { | ||
| func transform(_ event: CGEvent) -> CGEvent? { | ||
| if event.isLinearMouseSyntheticEvent { |
There was a problem hiding this comment.
This guard looks too broad. isLinearMouseSyntheticEvent is not only used for the synthetic mouse up posted by this transformer, but also for other LinearMouse-generated events such as device-layer synthetic button events. Gesture triggers may legitimately rely on those events, so this early return could bypass valid trigger input.
A safer fix would be either to replace this shared boolean with a more specific synthetic event kind, or to avoid reposting entirely and return the release event directly here instead of post(...) + nil.
There was a problem hiding this comment.
Of course makes sense, when I get some time I’ll put up another update and let you know :)
|
@diosmiodio Thank you for fixing the bug! When you have a moment, could you please merge the fix? It would really help me out. Much appreciated! |
…t guard and preserve cooldown - Remove isLinearMouseSyntheticEvent guard that would block legitimate synthetic button events from InputReportHandler (e.g. Kensington, generic side button devices). Instead, return the release event directly from transform() via makeTriggerButtonRelease(), eliminating re-entrance without filtering unrelated synthetic events. - Preserve cooldown semantics by keeping state as .cooldown(until:, released:) on mouseUp instead of resetting to .idle, so the full cooldown window still suppresses re-triggers. Track whether release has been posted to avoid duplicate releases.
…ouse#1138) The GestureButtonTransformer passed mouseDown through to apps but consumed mouseUp during cooldown after a gesture fired. This left macOS believing the button was still pressed, breaking drag operations in apps like Todoist. Fix: post a synthetic mouseUp (via cgSessionEventTap) when consuming the real mouseUp during cooldown or in the triggered state. An explicit isLinearMouseSyntheticEvent guard prevents re-entrance. The Logitech HID++ path also now clears state on button release during cooldown.
…ng synthetic release Address PR review feedback: 1. Remove overly broad `isLinearMouseSyntheticEvent` guard that would block legitimate synthetic button events from InputReportHandler (Kensington, generic side buttons). 2. Preserve full cooldown window on mouseUp instead of resetting to `.idle` early — track `released: Bool` in cooldown state so rapid re-presses are still suppressed. 3. Return the original mouseUp event (tagged with `isGestureCleanupRelease`) instead of creating a new CGEvent via `makeTriggerButtonRelease()`. Creating a new event and returning it through the event tap callback caused a use-after-free: `Unmanaged.passUnretained` in EventTap.swift does not retain the returned event, so ARC releases the new CGEvent after the callback returns, leaving macOS with a dangling pointer that disables the event tap entirely. 4. Add `isGestureCleanupRelease` flag (dedicated sentinel on eventSourceUserData) so downstream transformers (ButtonActionsTransformer, UniversalBackForwardTransformer) pass the cleanup release through without firing mapped actions or consuming the event.
|
Updated the PR addressing both review comments (broad However, after rebasing I'm hitting a crash that reproduces on latest CrashInvestigation
// ButtonActionsTransformer.swift:25
static let keySimulator = KeySimulator()
// KeyCodeResolver.swift:52
guard let characters = nsEvent.characters, characters.count == 1 else {
continue
}
The same pattern exists in Repro
|
|
|
@chaotianshao, I zipped up my You'll need to right click -> open since I didn't sign this application |
|
Thanks! I’ll fix the crashes later. |
|
Thank you @diosmiodio and @lujjjh for fixing the bug and closing the issue. Really appreciate it! |
Problem
Fixes #1138
After a gesture fires (e.g., Mission Control swipe), the trigger button's
mouseUpevent is consumed during cooldown but no release is ever posted. This leaves macOS believing the button is permanently pressed, causing:Reproduced with Logitech MX Master 3 on macOS 26.x, but affects any mouse using the gesture button feature.
Root Cause
The state machine in
GestureButtonTransformerhas an asymmetry between mouseDown and mouseUp handling:handleButtonDownpasses mouseDown through to the app → app sees "button pressed"handleDraggedconsumes drag events while tracking gesture movement.cooldownreturn nil)handleButtonUpis never reachedFix
Tag and return the original mouseUp event from
transform()instead of consuming it or creating a new event.Key design decisions
Tag original event, don't create a new one: The initial approach (commit
22fe756) usedreleaseTriggerButton()to create a newCGEventand post it viaCGEvent.post(). Review feedback identified two problems: (1) theisLinearMouseSyntheticEventguard needed to prevent re-entrance was too broad, and (2) creating new events risks theUnmanaged.passUnretaineduse-after-free fixed in #1143. This commit tags the original event (which the framework already retains) withisGestureCleanupReleaseand returns it directly — no re-entrance possible, no ownership issues.Preserve cooldown on release: The cooldown state now tracks
released: Boolinstead of resetting to.idleon mouseUp. This preserves the full cooldown window so rapid re-presses are still suppressed.Dedicated cleanup flag: The returned mouseUp flows through the transformer pipeline into
ButtonActionsTransformerandUniversalBackForwardTransformer. Without protection, a completed gesture on a shared trigger button would also fire the mapped action on release. A dedicatedisGestureCleanupReleaseflag (separate from the genericisLinearMouseSyntheticEvent) lets those transformers pass the event through without firing actions.Changes
GestureButtonTransformer.swift: RemoveisLinearMouseSyntheticEventguard. Return original mouseUp tagged withisGestureCleanupReleaseinstead of creating a new event. Trackreleasedin cooldown state. RemovereleaseTriggerButton()method.CGEvent+LinearMouseSynthetic.swift: AddisGestureCleanupReleaseflag with dedicated sentinel value (0x4745_5354_5552).ButtonActionsTransformer.swift: Pass through gesture cleanup releases without firing mapped actions.UniversalBackForwardTransformer.swift: Pass through gesture cleanup releases without consuming them.Evidence
Before (broken) — drag fails after gesture
linearmouse_drag_broken_post_gesture.mov
After (fix) — drag works after gesture
linearmouse_drag_fix_post_gesture.mov
Pre-existing issues (not regressions)
These are pre-existing issues with the "shared trigger" case (same button used for both gesture trigger and button mapping) that predate this PR:
Modifier-hold cleanup on gesture completion: When a gesture fires on a button that also has a modifier-hold mapping,
keySimulator.up()is never called because the mouseUp was consumed during cooldown (even before this PR). This leaves modifier keys stuck.Repeat timer cleanup: Similarly, repeat-enabled mappings sharing the gesture trigger don't get their timer invalidated on gesture completion.
UniversalBackForward conflict on mouseDown: If a back/forward button is used as a gesture trigger with
universalBackForwardenabled, the mouseDown still fires a navigation swipe immediately (before gesture tracking begins).Dead
.triggeredstate: The.triggeredstate is set and immediately overwritten by.cooldownin the same synchronous call, sohandleButtonUpnever observes it. Could be removed as cleanup.Test plan