diff --git a/Ice/LayoutBar/LayoutBar.swift b/Ice/LayoutBar/LayoutBar.swift index a205755e..0f6d6322 100644 --- a/Ice/LayoutBar/LayoutBar.swift +++ b/Ice/LayoutBar/LayoutBar.swift @@ -21,6 +21,7 @@ struct LayoutBar: View { } @EnvironmentObject var appState: AppState + @EnvironmentObject var imageCache: MenuBarItemImageCache let section: MenuBarSection let spacing: CGFloat @@ -44,7 +45,7 @@ struct LayoutBar: View { @ViewBuilder private var conditionalBody: some View { - if appState.imageCache.cacheFailed(for: section.name) { + if imageCache.cacheFailed(for: section.name) { Text("Unable to display menu bar items") .foregroundStyle(menuBarManager.averageColorInfo?.color.brightness ?? 0 > 0.67 ? .black : .white) } else { diff --git a/Ice/Main/AppState.swift b/Ice/Main/AppState.swift index 270289e4..02640e21 100644 --- a/Ice/Main/AppState.swift +++ b/Ice/Main/AppState.swift @@ -116,6 +116,18 @@ final class AppState: ObservableObject { .store(in: &c) } + navigationState.$isSettingsPresented + .receive(on: DispatchQueue.main) + .sink { isPresented in + guard isPresented else { + return + } + Task { + await self.imageCache.updateCacheWithoutChecks(sections: MenuBarSection.Name.allCases) + } + } + .store(in: &c) + menuBarManager.objectWillChange .sink { [weak self] in self?.objectWillChange.send() diff --git a/Ice/MenuBar/MenuBarItemImageCache.swift b/Ice/MenuBar/MenuBarItemImageCache.swift index acbc15dc..82bef09d 100644 --- a/Ice/MenuBar/MenuBarItemImageCache.swift +++ b/Ice/MenuBar/MenuBarItemImageCache.swift @@ -165,6 +165,30 @@ class MenuBarItemImageCache: ObservableObject { return await tempCache.images } + func updateCacheWithoutChecks(sections: [MenuBarSection.Name]) async { + guard + let appState, + let screen = NSScreen.main + else { + return + } + var images = images + for section in sections { + guard !appState.itemManager.itemCache.allItems(for: section).isEmpty else { + continue + } + let sectionImages = await createImages(for: section, screen: screen) + guard !sectionImages.isEmpty else { + Logger.imageCache.warning("Update image cache failed for \(section.logString)") + continue + } + images.merge(sectionImages) { (_, new) in new } + } + self.images = images + self.screen = screen + self.menuBarHeight = screen.getMenuBarHeight() + } + func updateCache() async { guard let appState else { return @@ -184,9 +208,6 @@ class MenuBarItemImageCache: ObservableObject { Logger.imageCache.info("Item manager is moving item, so deferring image cache") return } - guard let screen = NSScreen.main else { - return - } var sectionsNeedingDisplay = [MenuBarSection.Name]() if appState.navigationState.isSettingsPresented { sectionsNeedingDisplay = MenuBarSection.Name.allCases @@ -196,19 +217,7 @@ class MenuBarItemImageCache: ObservableObject { { sectionsNeedingDisplay.append(section) } - for section in sectionsNeedingDisplay { - guard !appState.itemManager.itemCache.allItems(for: section).isEmpty else { - continue - } - let sectionImages = await createImages(for: section, screen: screen) - guard !sectionImages.isEmpty else { - Logger.imageCache.warning("Update image cache failed for \(section.logString)") - continue - } - images.merge(sectionImages) { (_, new) in new } - } - self.screen = screen - self.menuBarHeight = screen.getMenuBarHeight() + await updateCacheWithoutChecks(sections: sectionsNeedingDisplay) } } diff --git a/Ice/Settings/SettingsPanes/MenuBarItemsSettingsPane.swift b/Ice/Settings/SettingsPanes/MenuBarItemsSettingsPane.swift index fa4124c0..1153c206 100644 --- a/Ice/Settings/SettingsPanes/MenuBarItemsSettingsPane.swift +++ b/Ice/Settings/SettingsPanes/MenuBarItemsSettingsPane.swift @@ -70,6 +70,7 @@ struct MenuBarItemsSettingsPane: View { VStack(alignment: .leading) { Section { LayoutBar(section: section) + .environmentObject(appState.imageCache) } header: { Text(section.name.menuString) } footer: {