Skip to content

Commit

Permalink
Update menu bar item image caching
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed Sep 26, 2024
1 parent c804009 commit 8f003e6
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions Ice/MenuBar/MenuBarItemImageCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]()
Expand Down Expand Up @@ -212,43 +211,37 @@ 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

self.screen = screen
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 {
Expand All @@ -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
Expand All @@ -268,7 +274,7 @@ final class MenuBarItemImageCache: ObservableObject {
sectionsNeedingDisplay.append(section)
}

await updateCacheWithoutChecks(sections: sectionsNeedingDisplay)
await updateCache(sections: sectionsNeedingDisplay)
}
}

Expand Down

0 comments on commit 8f003e6

Please sign in to comment.