Skip to content
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
16 changes: 11 additions & 5 deletions Sources/puremouse/NativeButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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)
Expand Down
64 changes: 64 additions & 0 deletions Tests/PureMouseTests/MediaAndMiddleClickTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading