Skip to content

Commit

Permalink
Reworks
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed Oct 13, 2024
1 parent 5cca38c commit 2a1bdd0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
8 changes: 3 additions & 5 deletions Ice/MenuBar/ItemManagement/MenuBarItemImageCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,9 @@ final class MenuBarItemImageCache: ObservableObject {
return
}

if let lastItemMoveStartDate = await appState.itemManager.lastItemMoveStartDate {
guard Date.now.timeIntervalSince(lastItemMoveStartDate) > 1 else {
logSkippingCache(reason: "an item was recently moved")
return
}
guard await !appState.itemManager.itemHasRecentlyMoved else {
logSkippingCache(reason: "an item was recently moved")
return
}

await updateCacheWithoutChecks(sections: sections)
Expand Down
42 changes: 27 additions & 15 deletions Ice/MenuBar/ItemManagement/MenuBarItemManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,16 @@ final class MenuBarItemManager: ObservableObject {
/// A timer that determines when to rehide the temporarily shown items.
private var tempShownItemsTimer: Timer?

/// The last time an item was moved.
private(set) var lastItemMoveStartDate: Date?
/// The last time a menu bar item was moved.
private var lastItemMoveStartDate: Date?

/// The last time the mouse was moved.
private var lastMouseMoveStartDate: Date?

/// Counter to determine if a menu bar item, or group of menu bar
/// items is being moved.
private var itemMoveCount = 0

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

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

Expand All @@ -142,6 +142,23 @@ final class MenuBarItemManager: ObservableObject {
itemMoveCount > 0
}

/// A Boolean value that indicates whether a menu bar item has
/// recently moved.
var itemHasRecentlyMoved: Bool {
guard let lastItemMoveStartDate else {
return false
}
return Date.now.timeIntervalSince(lastItemMoveStartDate) <= 1
}

/// A Boolean value that indicates whether the mouse has recently moved.
var mouseHasRecentlyMoved: Bool {
guard let lastMouseMoveStartDate else {
return false
}
return Date.now.timeIntervalSince(lastMouseMoveStartDate) <= 1
}

/// Creates a manager with the given app state.
init(appState: AppState) {
self.appState = appState
Expand Down Expand Up @@ -192,10 +209,7 @@ final class MenuBarItemManager: ObservableObject {
}
switch event.type {
case .mouseMoved:
mouseMoveCount += 1
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.mouseMoveCount = max(self.mouseMoveCount - 1, 0)
}
lastMouseMoveStartDate = .now
case .leftMouseDown, .rightMouseDown, .otherMouseDown:
isMouseButtonDown = true
case .leftMouseUp, .rightMouseUp, .otherMouseUp:
Expand Down Expand Up @@ -305,11 +319,9 @@ extension MenuBarItemManager {
logSkippingCache(reason: "an item is currently being moved")
return
} catch {
if let lastItemMoveStartDate {
guard Date.now.timeIntervalSince(lastItemMoveStartDate) > 1 else {
logSkippingCache(reason: "an item was recently moved")
return
}
guard !itemHasRecentlyMoved else {
logSkippingCache(reason: "an item was recently moved")
return
}
}

Expand Down Expand Up @@ -1394,7 +1406,7 @@ extension MenuBarItemManager {
Logger.itemManager.debug("Mouse button is down, so will not enforce control item order")
return
}
guard mouseMoveCount <= 0 else {
guard !mouseHasRecentlyMoved else {
Logger.itemManager.debug("Mouse has recently moved, so will not enforce control item order")
return
}
Expand Down

0 comments on commit 2a1bdd0

Please sign in to comment.