Skip to content

Commit

Permalink
Adjust mouse tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed Sep 30, 2024
1 parent f45203a commit eac90c1
Showing 1 changed file with 10 additions and 30 deletions.
40 changes: 10 additions & 30 deletions Ice/MenuBar/MenuBarItemManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,9 @@ final class MenuBarItemManager: ObservableObject {
/// The last time an item was moved.
private(set) var lastItemMoveStartDate: Date?

/// A Boolean value that indicates whether the mouse button is down.
private var isMouseButtonDown = false

/// Counter for mouse movement.
private var mouseMovedCount = 0

/// Event type mask for tracking mouse events.
private let mouseTrackingMask: NSEvent.EventTypeMask = [
.mouseMoved,
.leftMouseDown,
.rightMouseDown,
.otherMouseDown,
.leftMouseUp,
.rightMouseUp,
.otherMouseUp,
]

/// Creates a manager with the given app state.
init(appState: AppState) {
self.appState = appState
Expand Down Expand Up @@ -161,26 +147,17 @@ final class MenuBarItemManager: ObservableObject {
.store(in: &c)

Publishers.Merge(
UniversalEventMonitor.publisher(for: mouseTrackingMask),
RunLoopLocalEventMonitor.publisher(for: mouseTrackingMask, mode: .eventTracking)
UniversalEventMonitor.publisher(for: .mouseMoved),
RunLoopLocalEventMonitor.publisher(for: .mouseMoved, mode: .eventTracking)
)
.removeDuplicates()
.sink { [weak self] event in
.sink { [weak self] _ in
guard let self else {
return
}
switch event.type {
case .mouseMoved:
mouseMovedCount += 1
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.mouseMovedCount = max(self.mouseMovedCount - 1, 0)
}
case .leftMouseDown, .rightMouseDown, .otherMouseDown:
isMouseButtonDown = true
case .leftMouseUp, .rightMouseUp, .otherMouseUp:
isMouseButtonDown = false
default:
break
mouseMovedCount += 1
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.mouseMovedCount = max(self.mouseMovedCount - 1, 0)
}
}
.store(in: &c)
Expand Down Expand Up @@ -1347,9 +1324,12 @@ extension MenuBarItemManager {
hiddenControlItem: MenuBarItem,
alwaysHiddenControlItem: MenuBarItem
) async throws {
guard !isMouseButtonDown else {
switch NSApp.currentEvent?.type {
case .leftMouseDown, .rightMouseDown, .otherMouseDown:
Logger.itemManager.debug("Mouse button is down, so will not enforce control item order")
return
default:
break
}
guard mouseMovedCount <= 0 else {
Logger.itemManager.debug("Mouse has recently moved, so will not enforce control item order")
Expand Down

0 comments on commit eac90c1

Please sign in to comment.