From 2a1bdd0bad32b52c4c3c065fd4e47d43d26886d0 Mon Sep 17 00:00:00 2001 From: Jordan Baird Date: Sun, 13 Oct 2024 09:23:17 -0600 Subject: [PATCH] Reworks --- .../MenuBarItemImageCache.swift | 8 ++-- .../ItemManagement/MenuBarItemManager.swift | 42 ++++++++++++------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/Ice/MenuBar/ItemManagement/MenuBarItemImageCache.swift b/Ice/MenuBar/ItemManagement/MenuBarItemImageCache.swift index cdc9da7..1a6857c 100644 --- a/Ice/MenuBar/ItemManagement/MenuBarItemImageCache.swift +++ b/Ice/MenuBar/ItemManagement/MenuBarItemImageCache.swift @@ -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) diff --git a/Ice/MenuBar/ItemManagement/MenuBarItemManager.swift b/Ice/MenuBar/ItemManagement/MenuBarItemManager.swift index d81cbe1..225f97a 100644 --- a/Ice/MenuBar/ItemManagement/MenuBarItemManager.swift +++ b/Ice/MenuBar/ItemManagement/MenuBarItemManager.swift @@ -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 @@ -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 @@ -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: @@ -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 } } @@ -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 }