Skip to content

Commit

Permalink
More efficient image cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed Jul 8, 2024
1 parent 2fb6c6d commit 4c51260
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 35 deletions.
4 changes: 0 additions & 4 deletions Ice.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@
71F428112C3B5CE90025706C /* NavigationIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71F428102C3B5CE90025706C /* NavigationIdentifier.swift */; };
71F428132C3B5E030025706C /* IconResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71F428122C3B5E030025706C /* IconResource.swift */; };
71F428152C3B5F3D0025706C /* AppNavigationState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71F428142C3B5F3D0025706C /* AppNavigationState.swift */; };
71F428172C3B60070025706C /* AppNavigationIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71F428162C3B60070025706C /* AppNavigationIdentifier.swift */; };
71FEA2542A8D701B0048341A /* LocalEventMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71FEA2532A8D701B0048341A /* LocalEventMonitor.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -260,7 +259,6 @@
71F428102C3B5CE90025706C /* NavigationIdentifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationIdentifier.swift; sourceTree = "<group>"; };
71F428122C3B5E030025706C /* IconResource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IconResource.swift; sourceTree = "<group>"; };
71F428142C3B5F3D0025706C /* AppNavigationState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppNavigationState.swift; sourceTree = "<group>"; };
71F428162C3B60070025706C /* AppNavigationIdentifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppNavigationIdentifier.swift; sourceTree = "<group>"; };
71FEA2532A8D701B0048341A /* LocalEventMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalEventMonitor.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -788,7 +786,6 @@
71F428182C3B605B0025706C /* NavigationIdentifiers */ = {
isa = PBXGroup;
children = (
71F428162C3B60070025706C /* AppNavigationIdentifier.swift */,
71F428102C3B5CE90025706C /* NavigationIdentifier.swift */,
71F4280E2C3B5C870025706C /* SettingsNavigationIdentifier.swift */,
);
Expand Down Expand Up @@ -967,7 +964,6 @@
173C248C2B8E821C0096F7A1 /* UpdatesSettingsPane.swift in Sources */,
171C6F9C2C0356BC0097A5C8 /* GeneralSettingsPane.swift in Sources */,
7150A7B12AA427F80045EA68 /* KeyCode.swift in Sources */,
71F428172C3B60070025706C /* AppNavigationIdentifier.swift in Sources */,
71829E0A2C2FDCC200503604 /* NSScreen+getMenuBarHeight.swift in Sources */,
17B331D02B9916E70084EBB0 /* SettingsManager.swift in Sources */,
17B331D32B991D290084EBB0 /* GeneralSettingsManager.swift in Sources */,
Expand Down
6 changes: 5 additions & 1 deletion Ice/IceBar/IceBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,11 @@ class IceBarPanel: NSPanel {
return
}

// important that we set the current section before updating the cache
// important that we set the navigation state and current section
// before updating the cache
appState.navigationState.isIceBarPresented = true
currentSection = section

await appState.imageCache.updateCache()

contentView = IceBarHostingView(
Expand All @@ -181,6 +184,7 @@ class IceBarPanel: NSPanel {
super.close()
contentView = nil
currentSection = nil
appState?.navigationState.isIceBarPresented = false
}
}

Expand Down
5 changes: 4 additions & 1 deletion Ice/Main/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ final class AppState: ObservableObject {
settingsWindow.publisher(for: \.isVisible)
.receive(on: DispatchQueue.main)
.sink { [weak self] isVisible in
self?.navigationState.appNavigationIdentifier = if isVisible { .settings } else { .idle }
guard let self else {
return
}
navigationState.isSettingsPresented = isVisible
}
.store(in: &c)
}
Expand Down
35 changes: 17 additions & 18 deletions Ice/MenuBar/MenuBarItemImageCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,6 @@ class MenuBarItemImageCache: ObservableObject {
.store(in: &c)
}

Timer.publish(every: 3, on: .main, in: .default)
.autoconnect()
.sink { [weak self] _ in
guard let self else {
return
}
Task {
await self.updateCache()
}
}
.store(in: &c)

cancellables = c
}

Expand Down Expand Up @@ -181,6 +169,17 @@ class MenuBarItemImageCache: ObservableObject {
guard let appState else {
return
}
if !appState.navigationState.isIceBarPresented {
Logger.imageCache.info("Ice Bar not visible")
guard appState.navigationState.isSettingsPresented else {
Logger.imageCache.info("Settings not visible, so deferring image cache")
return
}
guard case .menuBarItems = appState.navigationState.settingsNavigationIdentifier else {
Logger.imageCache.info("Settings visible but not viewing menu bar items, so deferring image cache")
return
}
}
guard !appState.itemManager.isMovingItem else {
Logger.imageCache.info("Item manager is moving item, so deferring image cache")
return
Expand All @@ -189,12 +188,12 @@ class MenuBarItemImageCache: ObservableObject {
return
}
var sectionsNeedingDisplay = [MenuBarSection.Name]()
if
let settingsWindow = appState.settingsWindow,
settingsWindow.isVisible
{
if appState.navigationState.isSettingsPresented {
sectionsNeedingDisplay = MenuBarSection.Name.allCases
} else if let section = appState.menuBarManager.iceBarPanel.currentSection {
} else if
appState.navigationState.isIceBarPresented,
let section = appState.menuBarManager.iceBarPanel.currentSection
{
sectionsNeedingDisplay.append(section)
}
for section in sectionsNeedingDisplay {
Expand All @@ -203,7 +202,7 @@ class MenuBarItemImageCache: ObservableObject {
}
let sectionImages = await createImages(for: section, screen: screen)
guard !sectionImages.isEmpty else {
Logger.imageCache.warning("Update cache failed for \(section.logString)")
Logger.imageCache.warning("Update image cache failed for \(section.logString)")
continue
}
images.merge(sectionImages) { (_, new) in new }
Expand Down
3 changes: 2 additions & 1 deletion Ice/Navigation/AppNavigationState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Combine
/// The model for app-wide navigation.
@MainActor
final class AppNavigationState: ObservableObject {
@Published var appNavigationIdentifier: AppNavigationIdentifier = .idle
@Published var isSettingsPresented = false
@Published var isIceBarPresented = false
@Published var settingsNavigationIdentifier: SettingsNavigationIdentifier = .general
}
10 changes: 0 additions & 10 deletions Ice/Navigation/NavigationIdentifiers/AppNavigationIdentifier.swift

This file was deleted.

0 comments on commit 4c51260

Please sign in to comment.