From 3db6489073f598b734a840f217f06fdf2481b746 Mon Sep 17 00:00:00 2001 From: Michael Blauberg <103556269+mblauberg@users.noreply.github.com> Date: Wed, 4 Nov 2026 17:27:16 +1000 Subject: [PATCH] Fix overlapping middle click holds --- Sources/puremouse/NativeButton.swift | 16 +++-- .../MediaAndMiddleClickTests.swift | 64 +++++++++++++++++++ 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/Sources/puremouse/NativeButton.swift b/Sources/puremouse/NativeButton.swift index 03d697f..15b6635 100644 --- a/Sources/puremouse/NativeButton.swift +++ b/Sources/puremouse/NativeButton.swift @@ -107,23 +107,29 @@ struct HeldButtons { /// Presses `cid` as an ordinary mouse button, if it has one. mutating func clickNatively(_ cid: UInt16) { - if post(cid, true) { native.insert(cid) } + guard !native.contains(cid) else { return } + if NativeButton.number(for: cid) == 2 && !middle.isEmpty { + native.insert(cid) + } else if post(cid, true) { + native.insert(cid) + } } /// Presses the middle button for `cid`, held until `cid` is let go. mutating func pressMiddle(_ cid: UInt16) { - if postMiddle(true) { middle.insert(cid) } + guard !middle.contains(cid) else { return } + if !middle.isEmpty || native.contains(0x52) || postMiddle(true) { middle.insert(cid) } } /// Lets go of `cid`: a native click or a held middle click is released; the gesture it held /// is returned. mutating func release(_ cid: UInt16) -> Config.Binding.Gesture? { if native.remove(cid) != nil { - _ = post(cid, false) + if NativeButton.number(for: cid) != 2 || middle.isEmpty { _ = post(cid, false) } return nil } if middle.remove(cid) != nil { - _ = postMiddle(false) + if middle.isEmpty && !native.contains(0x52) { _ = postMiddle(false) } return nil } guard let held = gesture, held.cid == cid else { return nil } @@ -133,7 +139,7 @@ struct HeldButtons { /// Lets go of everything without acting on it. mutating func drop() { - for cid in native { _ = post(cid, false) } + for cid in native where NativeButton.number(for: cid) != 2 || middle.isEmpty { _ = post(cid, false) } native = [] if !middle.isEmpty { _ = postMiddle(false) diff --git a/Tests/PureMouseTests/MediaAndMiddleClickTests.swift b/Tests/PureMouseTests/MediaAndMiddleClickTests.swift index 7c323f9..b4ff963 100644 --- a/Tests/PureMouseTests/MediaAndMiddleClickTests.swift +++ b/Tests/PureMouseTests/MediaAndMiddleClickTests.swift @@ -50,6 +50,70 @@ import PureMouseKit #expect(posts.native.isEmpty) } + @Test func overlappingMiddleHoldsReleaseOnlyAfterLastOwner() { + let posts = Posts() + var h = held(posts) + h.pressMiddle(0x53) + h.pressMiddle(0x53) + h.pressMiddle(0x54) + #expect(posts.middle == [true] && h.middle == [0x53, 0x54]) + _ = h.release(0x53) + #expect(posts.middle == [true]) + _ = h.release(0x54) + #expect(posts.middle == [true, false]) + } + + @Test func nativeWheelAndMiddleClickShareTheHold() { + for nativeFirst in [true, false] { + let posts = Posts() + var h = held(posts) + if nativeFirst { + h.clickNatively(0x52) + h.pressMiddle(0x53) + _ = h.release(0x52) + #expect(posts.native.map(\.1) == [true] && posts.middle.isEmpty) + _ = h.release(0x53) + #expect(posts.middle == [false]) + } else { + h.pressMiddle(0x53) + h.clickNatively(0x52) + _ = h.release(0x53) + #expect(posts.middle == [true] && posts.native.isEmpty) + _ = h.release(0x52) + #expect(posts.native.map(\.1) == [false]) + } + } + } + + @Test func duplicateNativePressDoesNotPostTwice() { + let posts = Posts() + var h = held(posts) + h.clickNatively(0x52) + h.clickNatively(0x52) + _ = h.release(0x52) + #expect(posts.native.map(\.1) == [true, false]) + } + + @Test func failedFirstMiddlePressDoesNotCreateAnOwner() { + var attempts = 0 + var h = HeldButtons(postMiddle: { _ in attempts += 1; return false }) + h.pressMiddle(0x53) + #expect(h.middle.isEmpty && attempts == 1) + _ = h.release(0x53) + #expect(attempts == 1) + } + + @Test func dropReleasesOverlappingMiddleHoldsOnce() { + let posts = Posts() + var h = held(posts) + h.clickNatively(0x52) + h.pressMiddle(0x53) + h.pressMiddle(0x54) + h.drop() + #expect(posts.native.map(\.1) == [true] && posts.middle == [false]) + #expect(h.native.isEmpty && h.middle.isEmpty) + } + @Test func dropReleasesAHeldMiddleClick() { let posts = Posts() var h = held(posts)