Skip to content

Commit

Permalink
Don't cache item images if app isn't frontmost
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed Jul 11, 2024
1 parent aa28264 commit 46f3b44
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
33 changes: 23 additions & 10 deletions Ice/Main/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ final class AppState: ObservableObject {
}
.store(in: &c)

NSWorkspace.shared.publisher(for: \.frontmostApplication)
.receive(on: DispatchQueue.main)
.sink { [weak self] frontmostApplication in
guard let self else {
return
}
navigationState.isAppFrontmost = frontmostApplication == .current
}
.store(in: &c)

if let settingsWindow {
settingsWindow.publisher(for: \.isVisible)
.receive(on: DispatchQueue.main)
Expand All @@ -116,17 +126,20 @@ 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)
}
Publishers.Merge(
navigationState.$isAppFrontmost,
navigationState.$isSettingsPresented
)
.debounce(for: 0.1, scheduler: DispatchQueue.main)
.sink { shouldUpdate in
guard shouldUpdate else {
return
}
.store(in: &c)
Task {
await self.imageCache.updateCacheWithoutChecks(sections: MenuBarSection.Name.allCases)
}
}
.store(in: &c)

menuBarManager.objectWillChange
.sink { [weak self] in
Expand Down
4 changes: 4 additions & 0 deletions Ice/MenuBar/MenuBarItemImageCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ class MenuBarItemImageCache: ObservableObject {
}

if !appState.navigationState.isIceBarPresented {
guard appState.navigationState.isAppFrontmost else {
Logger.imageCache.debug("Skipping image cache as Ice Bar not visible, app not frontmost")
return
}
guard appState.navigationState.isSettingsPresented else {
Logger.imageCache.debug("Skipping image cache as Ice Bar not visible, Settings not visible")
return
Expand Down
1 change: 1 addition & 0 deletions 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 isAppFrontmost = false
@Published var isSettingsPresented = false
@Published var isIceBarPresented = false
@Published var settingsNavigationIdentifier: SettingsNavigationIdentifier = .general
Expand Down

0 comments on commit 46f3b44

Please sign in to comment.