From 8f003e6eb31e50832d7d367bc177a6c22bb27438 Mon Sep 17 00:00:00 2001 From: Jordan Baird Date: Thu, 26 Sep 2024 05:03:09 -0600 Subject: [PATCH] Update menu bar item image caching --- Ice/MenuBar/MenuBarItemImageCache.swift | 34 +++++++++++++++---------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/Ice/MenuBar/MenuBarItemImageCache.swift b/Ice/MenuBar/MenuBarItemImageCache.swift index 319d74e3..18bf6949 100644 --- a/Ice/MenuBar/MenuBarItemImageCache.swift +++ b/Ice/MenuBar/MenuBarItemImageCache.swift @@ -179,8 +179,7 @@ final class MenuBarItemImageCache: ObservableObject { return images } - /// Updates the cache with the current menu bar item images, without checking whether - /// caching is necessary. + /// Updates the cache for the given sections, without checking whether caching is necessary. func updateCacheWithoutChecks(sections: [MenuBarSection.Name]) async { actor Context { var images = [MenuBarItemInfo: CGImage]() @@ -212,7 +211,8 @@ final class MenuBarItemImageCache: ObservableObject { } let task = Task { @MainActor in - self.images = await context.images + let images = await context.images + self.images.merge(images) { (_, new) in new } } await task.value @@ -220,35 +220,28 @@ final class MenuBarItemImageCache: ObservableObject { self.menuBarHeight = screen.getMenuBarHeight() } - /// Updates the cache with the current menu bar item images, if necessary. - func updateCache() async { + /// Updates the cache for the given sections, if necessary. + func updateCache(sections: [MenuBarSection.Name]) async { guard let appState else { return } let isIceBarPresented = await appState.navigationState.isIceBarPresented let isSearchPresented = await appState.navigationState.isSearchPresented - let isSettingsPresented: Bool if !isIceBarPresented && !isSearchPresented { guard await appState.navigationState.isAppFrontmost else { Logger.imageCache.debug("Skipping image cache as Ice Bar not visible, app not frontmost") return } - - isSettingsPresented = await appState.navigationState.isSettingsPresented - - guard isSettingsPresented else { + guard await appState.navigationState.isSettingsPresented else { Logger.imageCache.debug("Skipping image cache as Ice Bar not visible, Settings not visible") return } - guard case .menuBarLayout = await appState.navigationState.settingsNavigationIdentifier else { Logger.imageCache.debug("Skipping image cache as Ice Bar not visible, Settings visible but not on Menu Bar Layout pane") return } - } else { - isSettingsPresented = await appState.navigationState.isSettingsPresented } if let lastItemMoveStartDate = await appState.itemManager.lastItemMoveStartDate { @@ -258,6 +251,19 @@ final class MenuBarItemImageCache: ObservableObject { } } + await updateCacheWithoutChecks(sections: sections) + } + + /// Updates the cache for all sections, if necessary. + func updateCache() async { + guard let appState else { + return + } + + let isIceBarPresented = await appState.navigationState.isIceBarPresented + let isSearchPresented = await appState.navigationState.isSearchPresented + let isSettingsPresented = await appState.navigationState.isSettingsPresented + var sectionsNeedingDisplay = [MenuBarSection.Name]() if isSettingsPresented || isSearchPresented { sectionsNeedingDisplay = MenuBarSection.Name.allCases @@ -268,7 +274,7 @@ final class MenuBarItemImageCache: ObservableObject { sectionsNeedingDisplay.append(section) } - await updateCacheWithoutChecks(sections: sectionsNeedingDisplay) + await updateCache(sections: sectionsNeedingDisplay) } }