Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed Oct 14, 2024
1 parent 6d0290a commit d30e9e9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
26 changes: 16 additions & 10 deletions Ice/MenuBar/Appearance/MenuBarAppearanceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,39 @@ import Combine
/// A manager for the appearance of the menu bar.
@MainActor
final class MenuBarAppearanceManager: ObservableObject {
/// The current menu bar appearance configuration.
@Published var configuration: MenuBarAppearanceConfigurationV2 = .defaultConfiguration

private var cancellables = Set<AnyCancellable>()
/// The shared app state.
private weak var appState: AppState?

/// Encoder for UserDefaults values.
private let encoder = JSONEncoder()

/// Decoder for UserDefaults values.
private let decoder = JSONDecoder()

private let defaults = UserDefaults.standard

private(set) weak var appState: AppState?
/// Storage for internal observers.
private var cancellables = Set<AnyCancellable>()

/// The currently managed menu bar overlay panels.
private(set) var overlayPanels = Set<MenuBarOverlayPanel>()

/// The amount to inset the menu bar if called for by the configuration.
let menuBarInsetAmount: CGFloat = 5

weak var menuBarManager: MenuBarManager? {
appState?.menuBarManager
}

/// Creates a manager with the given app state.
init(appState: AppState) {
self.appState = appState
}

/// Performs initial setup of the manager.
func performSetup() {
loadInitialState()
configureCancellables()
}

/// Loads the initial values for the configuration.
private func loadInitialState() {
do {
if let data = Defaults.data(forKey: .menuBarAppearanceConfigurationV2) {
Expand All @@ -48,6 +52,7 @@ final class MenuBarAppearanceManager: ObservableObject {
}
}

/// Configures the internal observers for the manager.
private func configureCancellables() {
var c = Set<AnyCancellable>()

Expand Down Expand Up @@ -85,8 +90,8 @@ final class MenuBarAppearanceManager: ObservableObject {
guard let self else {
return
}
// overlay panels may not have been configured yet; since some of the
// properties on the manager might call for them, try to configure now
// The overlay panels may not have been configured yet. Since some of the
// properties on the manager might call for them, try to configure now.
if overlayPanels.isEmpty {
configureOverlayPanels(with: configuration)
}
Expand Down Expand Up @@ -151,5 +156,6 @@ extension MenuBarAppearanceManager: BindingExposable { }

// MARK: - Logger
private extension Logger {
/// The logger to use for the menu bar appearance manager.
static let appearanceManager = Logger(category: "MenuBarAppearanceManager")
}
22 changes: 11 additions & 11 deletions Ice/MenuBar/MenuBarManagement/MenuBarManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,29 @@ final class MenuBarManager: ObservableObject {
@Published private(set) var isMenuBarHiddenBySystemUserDefaults = false

/// The shared app state.
private(set) weak var appState: AppState?
private weak var appState: AppState?

/// Storage for internal observers.
private var cancellables = Set<AnyCancellable>()

/// A Boolean value that indicates whether the application menus are hidden.
private var isHidingApplicationMenus = false

/// The managed sections in the menu bar.
private(set) var sections = [MenuBarSection]()

/// The panel that contains the Ice Bar interface.
let iceBarPanel: IceBarPanel

/// The panel that contains the menu bar search interface.
let searchPanel: MenuBarSearchPanel

private let encoder = JSONEncoder()

private let decoder = JSONDecoder()

/// A Boolean value that indicates whether the application menus are hidden.
private var isHidingApplicationMenus = false

/// A Boolean value that indicates whether the manager can update its stored
/// information for the menu bar's average color.
private var canUpdateAverageColorInfo: Bool {
appState?.settingsWindow?.isVisible == true
}

/// The managed sections in the menu bar.
private(set) var sections = [MenuBarSection]()

/// Initializes a new menu bar manager instance.
init(appState: AppState) {
self.iceBarPanel = IceBarPanel(appState: appState)
Expand Down Expand Up @@ -84,6 +80,7 @@ final class MenuBarManager: ObservableObject {
]
}

/// Configures the internal observers for the manager.
private func configureCancellables() {
var c = Set<AnyCancellable>()

Expand Down Expand Up @@ -226,6 +223,8 @@ final class MenuBarManager: ObservableObject {
cancellables = c
}

/// Updates the ``averageColorInfo`` property with the current average color
/// of the menu bar.
func updateAverageColorInfo() {
guard
canUpdateAverageColorInfo,
Expand Down Expand Up @@ -403,5 +402,6 @@ extension MenuBarManager: BindingExposable { }

// MARK: - Logger
private extension Logger {
/// Logger to use for the menu bar manager.
static let menuBarManager = Logger(category: "MenuBarManager")
}

0 comments on commit d30e9e9

Please sign in to comment.