From d30e9e90f3293a4f723c99d88dd330652e06db17 Mon Sep 17 00:00:00 2001 From: Jordan Baird Date: Sun, 13 Oct 2024 20:50:37 -0600 Subject: [PATCH] Refactoring --- .../Appearance/MenuBarAppearanceManager.swift | 26 ++++++++++++------- .../MenuBarManagement/MenuBarManager.swift | 22 ++++++++-------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/Ice/MenuBar/Appearance/MenuBarAppearanceManager.swift b/Ice/MenuBar/Appearance/MenuBarAppearanceManager.swift index 8b20da9..bfcd9eb 100644 --- a/Ice/MenuBar/Appearance/MenuBarAppearanceManager.swift +++ b/Ice/MenuBar/Appearance/MenuBarAppearanceManager.swift @@ -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() + /// 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() + /// The currently managed menu bar overlay panels. private(set) var overlayPanels = Set() + /// 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) { @@ -48,6 +52,7 @@ final class MenuBarAppearanceManager: ObservableObject { } } + /// Configures the internal observers for the manager. private func configureCancellables() { var c = Set() @@ -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) } @@ -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") } diff --git a/Ice/MenuBar/MenuBarManagement/MenuBarManager.swift b/Ice/MenuBar/MenuBarManagement/MenuBarManager.swift index bb6ec14..6e49f7c 100644 --- a/Ice/MenuBar/MenuBarManagement/MenuBarManager.swift +++ b/Ice/MenuBar/MenuBarManagement/MenuBarManager.swift @@ -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() + /// 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) @@ -84,6 +80,7 @@ final class MenuBarManager: ObservableObject { ] } + /// Configures the internal observers for the manager. private func configureCancellables() { var c = Set() @@ -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, @@ -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") }