diff --git a/App/MenuBar/MenuBarWidgetView.swift b/App/MenuBar/MenuBarWidgetView.swift index 25aaa63..9aa835d 100644 --- a/App/MenuBar/MenuBarWidgetView.swift +++ b/App/MenuBar/MenuBarWidgetView.swift @@ -34,8 +34,9 @@ struct MenuBarWidgetView: View { var body: some View { HStack(spacing: 4) { - // Text readouts carry their own labels, and network's arrows already identify it. - if style == .graphical { + // Text readouts carry their own labels, network's arrows already identify it, + // and the cup identifies Keep Awake. + if style == .graphical, module != .keepAwake { StackedLabel(text: module.label) } switch content { @@ -52,12 +53,11 @@ struct MenuBarWidgetView: View { } } case .battery(let indicator): - if let badge = indicator.badge { - BadgeImage(badge: badge) - .font(.system(size: 10, weight: .semibold)) - } BatteryIcon(fraction: indicator.fraction, glyph: BatteryGlyph(indicator.status)) BatteryText(indicator: indicator) + case .keepAwake: + Image(systemName: KeepAwake.systemImage) + .font(.system(size: 14)) } } } @@ -67,16 +67,6 @@ extension KeepAwake { static let systemImage = "cup.and.heat.waves.fill" } -private struct BadgeImage: View { - let badge: Badge - - var body: some View { - switch badge { - case .keepAwake: Image(systemName: KeepAwake.systemImage) - } - } -} - /// Letters stacked vertically, such as "C P U". private struct StackedLabel: View { let text: String @@ -259,14 +249,9 @@ private struct ReadoutView: View { var body: some View { VStack(spacing: -2) { - HStack(spacing: 1.5) { - if let badge = readout.badge { - BadgeImage(badge: badge) - } - Text(readout.label) - .tracking(0.4) - } - .font(.system(size: 7, weight: .semibold)) + Text(readout.label) + .tracking(0.4) + .font(.system(size: 7, weight: .semibold)) // The hidden template sets a minimum width; longer values still widen the item. ZStack { Text(readout.widthTemplate) diff --git a/App/Settings/KeepAwakePane.swift b/App/Settings/KeepAwakePane.swift index 2831e68..21bd282 100644 --- a/App/Settings/KeepAwakePane.swift +++ b/App/Settings/KeepAwakePane.swift @@ -29,7 +29,7 @@ struct KeepAwakePane: View { .font(.callout) } } footer: { - Text("Turn on Keep Awake with the cup in the dashboard, or ⌥-click the battery item. While it's on, Barometry moves the cursor a point and back after this long without input, so apps that check for activity still see some.") + Text("Turn on Keep Awake with the cup in the dashboard, or ⌥-click any Barometry item in the menu bar. While it's on, Barometry moves the cursor a point and back after this long without input, so apps that check for activity still see some.") .foregroundStyle(.secondary) } } diff --git a/App/Settings/MenuBarPane.swift b/App/Settings/MenuBarPane.swift index 4d4e381..bb30306 100644 --- a/App/Settings/MenuBarPane.swift +++ b/App/Settings/MenuBarPane.swift @@ -44,14 +44,14 @@ struct MenuBarPane: View { }) { SettingsRowLabel( title: module.name, - note: selection.isAvailable(module) ? nil : "Not available on this Mac", + note: note(for: module, in: selection), icon: IconTile(symbol: module.symbol, tint: module.tint, size: 22), isMovable: isMovable ) } .toggleStyle(.switch) - // The last visible module stays so there's always an item to click. - .disabled(!selection.isAvailable(module) || selection.visible == [module]) + // The last module that's always shown stays so there's always an item to click. + .disabled(!selection.isAvailable(module) || !selection.canHide(module)) .moveDisabled(!isMovable) .contextMenu { if isMovable { @@ -76,4 +76,14 @@ struct MenuBarPane: View { .foregroundStyle(.secondary) } } + + private func note(for module: MenuBarModule, in selection: MenuBarModuleSelection) -> String? { + if !selection.isAvailable(module) { + "Not available on this Mac" + } else if module.showsOnlyWhileKeepingAwake { + "Shown while Keep Awake is on" + } else { + nil + } + } } diff --git a/App/Settings/MenuBarPreview.swift b/App/Settings/MenuBarPreview.swift index 31120c8..6046c78 100644 --- a/App/Settings/MenuBarPreview.swift +++ b/App/Settings/MenuBarPreview.swift @@ -11,7 +11,7 @@ struct MenuBarPreview: View { var body: some View { let style = preferences.menuBarStyle - let visible = preferences.menuBarModules.visible + let visible = statusItems.shownModules VStack(spacing: 0) { HStack(spacing: 4) { switch preferences.menuBarLayout { diff --git a/App/Settings/SettingsComponents.swift b/App/Settings/SettingsComponents.swift index e6c9411..2ca809c 100644 --- a/App/Settings/SettingsComponents.swift +++ b/App/Settings/SettingsComponents.swift @@ -97,6 +97,7 @@ extension MenuBarModule { switch self { case .sensors: "thermometer.medium" case .battery: "battery.75percent" + case .keepAwake: KeepAwake.systemImage default: DashboardSection(self).symbol } } diff --git a/App/StatusItemController.swift b/App/StatusItemController.swift index 435bc0e..834b06e 100644 --- a/App/StatusItemController.swift +++ b/App/StatusItemController.swift @@ -41,8 +41,6 @@ final class StatusItemController: NSObject { private let panel: DashboardPanel private var slots: [Slot] = [] private var itemLayout: ItemLayout? - /// Shows Keep Awake when no battery widget is visible to carry its badge. - private var keepAwakeItem: NSStatusItem? init(preferences: Preferences, openSettings: @escaping () -> Void) { self.preferences = preferences @@ -68,11 +66,10 @@ final class StatusItemController: NSObject { } private func applyPreferences() { - let wanted = ItemLayout(layout: preferences.menuBarLayout, style: preferences.menuBarStyle, modules: preferences.menuBarModules.visible) + let wanted = ItemLayout(layout: preferences.menuBarLayout, style: preferences.menuBarStyle, modules: shownModules) if wanted != itemLayout { rebuildItems(wanted) } - updateKeepAwakeItem() keepAwake.cursorNudgeInterval = preferences.movesCursorWhenIdle ? preferences.cursorNudgeInterval : nil let interface = preferences.networkInterface.bsdName if monitor.networkInterface != interface { @@ -85,33 +82,27 @@ final class StatusItemController: NSObject { } private func rebuildItems(_ itemLayout: ItemLayout) { - // Removed items keep their autosaved positions, so rebuilding doesn't undo ⌘-drags. - for slot in slots { - NSStatusBar.system.removeStatusItem(slot.item) - } self.itemLayout = itemLayout let modules = itemLayout.modules - switch itemLayout.layout { + let wanted: [(autosaveName: String, accessibilityLabel: String, modules: [MenuBarModule])] = switch itemLayout.layout { case .individual: // New status items appear to the left of existing ones, so create them in // reverse to get the modules in order. The autosave name keeps ⌘-drag positions. - slots = modules.reversed().map { module in - Slot(item: makeItem(autosaveName: "barometry.\(module.rawValue)", accessibilityLabel: module.name), style: itemLayout.style, modules: [module]) - } + modules.reversed().map { ("barometry.\($0.rawValue)", $0.name, [$0]) } case .combined: - slots = [Slot(item: makeItem(autosaveName: "barometry.combined", accessibilityLabel: "Barometry"), style: itemLayout.style, modules: modules)] + [("barometry.combined", "Barometry", modules)] } - } - - private func updateKeepAwakeItem() { - let isWanted = keepAwake.isEnabled && !preferences.menuBarModules.visible.contains(.battery) - if isWanted, keepAwakeItem == nil { - let item = makeItem(autosaveName: "barometry.keepAwake", accessibilityLabel: "Keep Awake") - item.button?.image = NSImage(systemSymbolName: KeepAwake.systemImage, accessibilityDescription: nil) - keepAwakeItem = item - } else if !isWanted, let item = keepAwakeItem { + // Items still wanted stay put, so Keep Awake coming and going doesn't recreate the rest. + // Removed items keep their autosaved positions, so rebuilding doesn't undo ⌘-drags. + var unused = Dictionary(uniqueKeysWithValues: slots.compactMap { slot in slot.item.autosaveName.map { ($0, slot.item) } }) + slots = wanted.map { wanted in + let item = unused.removeValue(forKey: wanted.autosaveName) + ?? makeItem(autosaveName: wanted.autosaveName, accessibilityLabel: wanted.accessibilityLabel) + // A new slot starts without rendered content, so a style change redraws the item. + return Slot(item: item, style: itemLayout.style, modules: wanted.modules) + } + for item in unused.values { NSStatusBar.system.removeStatusItem(item) - keepAwakeItem = nil } } @@ -130,7 +121,13 @@ final class StatusItemController: NSObject { /// What a module's widget shows now. Reads observable state, so a SwiftUI view that /// calls it updates with each reading. func widgetContent(for module: MenuBarModule, style: MenuBarStyle) -> MenuBarWidgetContent { - monitor.widgetContent(for: module, style: style, temperatureUnit: preferences.temperatureUnit, isKeepingAwake: keepAwake.isEnabled) + monitor.widgetContent(for: module, style: style, temperatureUnit: preferences.temperatureUnit) + } + + /// The modules in the menu bar now, which include Keep Awake only while it's on. + /// Reads observable state, like `widgetContent`. + var shownModules: [MenuBarModule] { + preferences.menuBarModules.shown(isKeepingAwake: keepAwake.isEnabled) } /// Modules sorted by where their individual items sit in the menu bar, left to right, @@ -188,14 +185,13 @@ final class StatusItemController: NSObject { } /// An individual module's item opens with its details pinned; the combined item opens without details. - /// ⌥-clicking an item that shows Keep Awake toggles it instead. + /// ⌥-clicking any item toggles Keep Awake instead. @objc private func togglePanel(_ sender: NSStatusBarButton) { - let modules = slots.first { $0.item.button === sender }?.modules ?? [] - if NSApp.currentEvent?.modifierFlags.contains(.option) == true, - modules.contains(.battery) || sender === keepAwakeItem?.button { + if NSApp.currentEvent?.modifierFlags.contains(.option) == true { keepAwake.toggle() return } + let modules = slots.first { $0.item.button === sender }?.modules ?? [] let section = modules.count == 1 ? modules.first.map(DashboardSection.init) : nil panel.toggle(below: sender, showing: section) } diff --git a/Packages/BarometryKit/Sources/BarometryKit/MenuBar/MenuBarModule.swift b/Packages/BarometryKit/Sources/BarometryKit/MenuBar/MenuBarModule.swift index 2671c71..98f2d36 100644 --- a/Packages/BarometryKit/Sources/BarometryKit/MenuBar/MenuBarModule.swift +++ b/Packages/BarometryKit/Sources/BarometryKit/MenuBar/MenuBarModule.swift @@ -8,6 +8,8 @@ public enum MenuBarModule: String, CaseIterable, Codable, Sendable, Identifiable case network case sensors case battery + /// The Keep Awake cup, shown only while Keep Awake is on. + case keepAwake public var id: String { rawValue } @@ -20,9 +22,15 @@ public enum MenuBarModule: String, CaseIterable, Codable, Sendable, Identifiable case .network: "NET" case .sensors: "SEN" case .battery: "BAT" + case .keepAwake: "AWK" } } + /// Keep Awake comes and goes, so it can't be the one item that's always there to click. + public var showsOnlyWhileKeepingAwake: Bool { + self == .keepAwake + } + /// Modules this Mac lacks the hardware for. Hardware doesn't change while running, /// so check once at launch. public static func unavailableOnThisMac() -> Set { @@ -37,6 +45,7 @@ public enum MenuBarModule: String, CaseIterable, Codable, Sendable, Identifiable case .network: "Network" case .sensors: "Sensors" case .battery: "Battery" + case .keepAwake: "Keep Awake" } } } diff --git a/Packages/BarometryKit/Sources/BarometryKit/MenuBar/MenuBarWidgetContent.swift b/Packages/BarometryKit/Sources/BarometryKit/MenuBar/MenuBarWidgetContent.swift index 8192f94..3112b7e 100644 --- a/Packages/BarometryKit/Sources/BarometryKit/MenuBar/MenuBarWidgetContent.swift +++ b/Packages/BarometryKit/Sources/BarometryKit/MenuBar/MenuBarWidgetContent.swift @@ -17,6 +17,8 @@ public enum MenuBarWidgetContent: Equatable, Sendable { case readouts([Readout]) /// A battery filled to its charge level, beside the time or state over the charge. case battery(BatteryIndicator) + /// The Keep Awake cup. + case keepAwake public static let graphBarCount = 20 static let missing = "—" @@ -31,12 +33,8 @@ public enum MenuBarWidgetContent: Equatable, Sendable { battery: BatterySample?, sensors: SensorSample?, temperatureUnit: TemperatureUnit, - isKeepingAwake: Bool, locale: Locale ) -> MenuBarWidgetContent { - // Keep Awake is marked on the battery, the module most tied to power. - let badge: Badge? = isKeepingAwake ? .keepAwake : nil - func percent(_ value: Percent?) -> Readout { Readout(label: module.label, value: value?.formatted(locale: locale) ?? missing, widthTemplate: "100%") } @@ -79,13 +77,15 @@ public enum MenuBarWidgetContent: Equatable, Sendable { Readout(label: "PWR", value: power(sensors, locale: locale), widthTemplate: "88.8 W"), ]) case (.battery, .graphical): - return .battery(battery.map { BatteryIndicator($0, badge: badge, locale: locale) } ?? .unavailable(badge: badge)) + return .battery(battery.map { BatteryIndicator($0, locale: locale) } ?? .unavailable) case (.battery, .text): let charge = battery?.charge.formatted(locale: locale) ?? missing let isCharging = if case .charging = battery?.state { true } else { false } return .readouts([ - Readout(label: module.label, value: isCharging ? "⚡︎" + charge : charge, widthTemplate: "⚡︎100%", badge: badge), + Readout(label: module.label, value: isCharging ? "⚡︎" + charge : charge, widthTemplate: "⚡︎100%"), ]) + case (.keepAwake, _): + return .keepAwake } } @@ -98,11 +98,6 @@ public enum MenuBarWidgetContent: Equatable, Sendable { } } -/// A symbol drawn beside a widget to show a mode is on. -public enum Badge: Equatable, Sendable { - case keepAwake -} - public struct LineGlyphs: Equatable, Sendable { public let top: String public let bottom: String @@ -125,29 +120,24 @@ public struct BatteryIndicator: Equatable, Sendable { public let fraction: Double public let status: Status public let charge: String - public let badge: Badge? /// The widest expected lines, used to reserve width so the item doesn't shift. public static let widthTemplate = (status: "8:88", charge: "100%") /// An empty battery with dashes, for when no reading is available. - static func unavailable(badge: Badge?) -> BatteryIndicator { - BatteryIndicator( - fraction: 0, - status: .discharging(timeRemaining: MenuBarWidgetContent.missing), - charge: MenuBarWidgetContent.missing, - badge: badge - ) - } + static let unavailable = BatteryIndicator( + fraction: 0, + status: .discharging(timeRemaining: MenuBarWidgetContent.missing), + charge: MenuBarWidgetContent.missing + ) - init(fraction: Double, status: Status, charge: String, badge: Badge? = nil) { + init(fraction: Double, status: Status, charge: String) { self.fraction = fraction self.status = status self.charge = charge - self.badge = badge } - init(_ sample: BatterySample, badge: Badge? = nil, locale: Locale) { + init(_ sample: BatterySample, locale: Locale) { // Estimates are nil while macOS is still calculating. func clock(_ duration: Duration?) -> String { duration.map { $0.formatted(.time(pattern: .hourMinute).locale(locale)) } ?? "–:––" @@ -158,7 +148,7 @@ public struct BatteryIndicator: Equatable, Sendable { case .charged: .charged case .notCharging: .paused } - self.init(fraction: sample.charge.fraction, status: status, charge: sample.charge.formatted(locale: locale), badge: badge) + self.init(fraction: sample.charge.fraction, status: status, charge: sample.charge.formatted(locale: locale)) } } @@ -167,14 +157,11 @@ public struct Readout: Equatable, Sendable { public let value: String /// The widest expected value, used to reserve width so the item doesn't shift. public let widthTemplate: String - /// Drawn before the label. - public let badge: Badge? - init(label: String, value: String, widthTemplate: String, badge: Badge? = nil) { + init(label: String, value: String, widthTemplate: String) { self.label = label self.value = value self.widthTemplate = widthTemplate - self.badge = badge } } @@ -183,7 +170,6 @@ extension SystemMonitor { for module: MenuBarModule, style: MenuBarStyle, temperatureUnit: TemperatureUnit, - isKeepingAwake: Bool, locale: Locale = .autoupdatingCurrent ) -> MenuBarWidgetContent { .make( @@ -196,7 +182,6 @@ extension SystemMonitor { battery: battery, sensors: sensors, temperatureUnit: temperatureUnit, - isKeepingAwake: isKeepingAwake, locale: locale ) } diff --git a/Packages/BarometryKit/Sources/BarometryKit/Settings/Preferences.swift b/Packages/BarometryKit/Sources/BarometryKit/Settings/Preferences.swift index fdf194e..94f822e 100644 --- a/Packages/BarometryKit/Sources/BarometryKit/Settings/Preferences.swift +++ b/Packages/BarometryKit/Sources/BarometryKit/Settings/Preferences.swift @@ -122,8 +122,8 @@ public final class Preferences { } } -/// Every menu bar module exactly once, in display order, with at least one visible so -/// there's always an item to click. +/// Every menu bar module exactly once, in display order, with at least one visible module +/// that doesn't depend on Keep Awake, so there's always an item to click. public struct MenuBarModuleSelection: Equatable, Sendable { public private(set) var order: [MenuBarModule] /// The user's choice, kept separate from `unavailable` so it survives a move to a Mac @@ -138,15 +138,21 @@ public struct MenuBarModuleSelection: Equatable, Sendable { unavailable: Set = [] ) { self.order = Preferences.complete(order) - let unavailable = unavailable.count < MenuBarModule.allCases.count ? unavailable : [] + let unavailable = Self.leavesItemToClick(hidden: [], unavailable: unavailable) ? unavailable : [] self.unavailable = unavailable - self.hidden = hidden.union(unavailable).count < MenuBarModule.allCases.count ? hidden : [] + self.hidden = Self.leavesItemToClick(hidden: hidden, unavailable: unavailable) ? hidden : [] } + /// The modules the user chose to show, including Keep Awake whether or not it's on. public var visible: [MenuBarModule] { order.filter(isVisible) } + /// The modules in the menu bar right now. + public func shown(isKeepingAwake: Bool) -> [MenuBarModule] { + visible.filter { isKeepingAwake || !$0.showsOnlyWhileKeepingAwake } + } + public func isVisible(_ module: MenuBarModule) -> Bool { isAvailable(module) && !hidden.contains(module) } @@ -155,16 +161,27 @@ public struct MenuBarModuleSelection: Equatable, Sendable { !unavailable.contains(module) } - /// Showing an unavailable module and hiding the last visible module are ignored. + /// False for the last visible module that shows regardless of Keep Awake. + public func canHide(_ module: MenuBarModule) -> Bool { + Self.leavesItemToClick(hidden: hidden.union([module]), unavailable: unavailable) + } + + /// Showing an unavailable module and hiding a module that `canHide` refuses are ignored. public mutating func setVisible(_ isVisible: Bool, for module: MenuBarModule) { guard isAvailable(module) else { return } if isVisible { hidden.remove(module) - } else if visible != [module] { + } else if canHide(module) { hidden.insert(module) } } + private static func leavesItemToClick(hidden: Set, unavailable: Set) -> Bool { + MenuBarModule.allCases.contains { module in + !module.showsOnlyWhileKeepingAwake && !hidden.contains(module) && !unavailable.contains(module) + } + } + /// Moves modules the way SwiftUI's `onMove` reports a drag. public mutating func move(fromOffsets source: IndexSet, toOffset destination: Int) { order.move(fromOffsets: source, toOffset: destination) @@ -271,7 +288,7 @@ public enum DashboardSection: String, CaseIterable, Sendable, Identifiable { case .gpu: self = .gpu case .memory: self = .memory case .network: self = .network - case .sensors, .battery: self = .power + case .sensors, .battery, .keepAwake: self = .power } } diff --git a/Packages/BarometryKit/Tests/BarometryKitTests/MenuBarWidgetContentTests.swift b/Packages/BarometryKit/Tests/BarometryKitTests/MenuBarWidgetContentTests.swift index e5947e0..bee4e94 100644 --- a/Packages/BarometryKit/Tests/BarometryKitTests/MenuBarWidgetContentTests.swift +++ b/Packages/BarometryKit/Tests/BarometryKitTests/MenuBarWidgetContentTests.swift @@ -16,8 +16,7 @@ struct MenuBarWidgetContentTests { network: NetworkSample? = nil, battery: BatterySample? = nil, sensors: SensorSample? = nil, - temperatureUnit: TemperatureUnit = .celsius, - isKeepingAwake: Bool = false + temperatureUnit: TemperatureUnit = .celsius ) -> MenuBarWidgetContent { .make( module: module, @@ -29,7 +28,6 @@ struct MenuBarWidgetContentTests { battery: battery, sensors: sensors, temperatureUnit: temperatureUnit, - isKeepingAwake: isKeepingAwake, locale: locale ) } @@ -159,27 +157,8 @@ struct MenuBarWidgetContentTests { #expect(content(.network, style: .text, network: network) == content(.network, network: network)) } - @Test func batteryIsBadgedWhileKeepingAwake() { - let battery = BatterySample(charge: Percent(0.44), state: .discharging(timeRemaining: .seconds(142 * 60))) - - #expect(content(.battery, battery: battery, isKeepingAwake: true) == .battery(BatteryIndicator(fraction: 0.44, status: .discharging(timeRemaining: "2:22"), charge: "44%", badge: .keepAwake))) - } - - @Test func missingBatteryIsStillBadgedWhileKeepingAwake() { - #expect(content(.battery, isKeepingAwake: true) == .battery(BatteryIndicator(fraction: 0, status: .discharging(timeRemaining: "—"), charge: "—", badge: .keepAwake))) - } - - @Test func textBatteryIsBadgedWhileKeepingAwake() { - let battery = BatterySample(charge: Percent(0.87), state: .notCharging) - - #expect(content(.battery, style: .text, battery: battery, isKeepingAwake: true) == .readouts([ - Readout(label: "BAT", value: "87%", widthTemplate: "⚡︎100%", badge: .keepAwake), - ])) - } - - @Test func onlyBatteryIsBadged() { - let history = [CPULoad(user: Percent(0.2), system: Percent(0.02))] - - #expect(content(.cpu, style: .text, cpuHistory: history, isKeepingAwake: true) == .readouts([Readout(label: "CPU", value: "22%", widthTemplate: "100%")])) + @Test(arguments: MenuBarStyle.allCases) + func keepAwakeShowsTheCup(style: MenuBarStyle) { + #expect(content(.keepAwake, style: style) == .keepAwake) } } diff --git a/Packages/BarometryKit/Tests/BarometryKitTests/PreferencesTests.swift b/Packages/BarometryKit/Tests/BarometryKitTests/PreferencesTests.swift index a8ad7d0..71fe6ca 100644 --- a/Packages/BarometryKit/Tests/BarometryKitTests/PreferencesTests.swift +++ b/Packages/BarometryKit/Tests/BarometryKitTests/PreferencesTests.swift @@ -36,7 +36,7 @@ struct PreferencesTests { #expect(reloaded.menuBarLayout == .combined) #expect(reloaded.menuBarStyle == .text) - #expect(reloaded.menuBarModules.visible == [.battery, .cpu, .memory, .network, .sensors]) + #expect(reloaded.menuBarModules.visible == [.battery, .cpu, .memory, .network, .sensors, .keepAwake]) #expect(reloaded.refreshInterval == .fiveSeconds) #expect(reloaded.temperatureUnit == .fahrenheit) #expect(reloaded.networkInterface == .named("en0")) @@ -75,7 +75,7 @@ struct PreferencesTests { let order = Preferences(defaults: defaults).menuBarModules.order - #expect(order == [.battery, .cpu, .gpu, .memory, .network, .sensors]) + #expect(order == [.battery, .cpu, .gpu, .memory, .network, .sensors, .keepAwake]) } @Test func storedDashboardOrderIsRepairedToContainEverySectionOnce() { @@ -89,12 +89,42 @@ struct PreferencesTests { struct MenuBarModuleSelectionTests { @Test func lastVisibleModuleCannotBeHidden() { - var selection = MenuBarModuleSelection(hidden: [.gpu, .memory, .network, .sensors, .battery]) + var selection = MenuBarModuleSelection(hidden: [.gpu, .memory, .network, .sensors, .battery, .keepAwake]) selection.setVisible(false, for: .cpu) #expect(selection.visible == [.cpu]) } + // Keep Awake disappears when it's off, which would leave nothing to click. + @Test func keepAwakeDoesNotCountAsAnItemToClick() { + var selection = MenuBarModuleSelection(hidden: [.gpu, .memory, .network, .sensors, .battery]) + selection.setVisible(false, for: .cpu) + + #expect(!selection.canHide(.cpu)) + #expect(selection.canHide(.keepAwake)) + #expect(selection.visible == [.cpu, .keepAwake]) + } + + @Test func hidingEveryModuleButKeepAwakeShowsThemAll() { + let selection = MenuBarModuleSelection(hidden: [.cpu, .gpu, .memory, .network, .sensors, .battery]) + + #expect(selection.visible == MenuBarModule.allCases) + } + + @Test func keepAwakeIsShownOnlyWhileOn() { + let selection = MenuBarModuleSelection(order: [.keepAwake, .cpu], hidden: [.gpu, .memory, .network, .sensors, .battery]) + + #expect(selection.shown(isKeepingAwake: true) == [.keepAwake, .cpu]) + #expect(selection.shown(isKeepingAwake: false) == [.cpu]) + } + + @Test func keepAwakeIsShownWhereverItsOrdered() { + var selection = MenuBarModuleSelection(unavailable: [.battery]) + selection.move(fromOffsets: [6], toOffset: 1) + + #expect(selection.shown(isKeepingAwake: true) == [.cpu, .keepAwake, .gpu, .memory, .network, .sensors]) + } + @Test func hidingEveryModuleShowsThemAll() { let selection = MenuBarModuleSelection(hidden: Set(MenuBarModule.allCases)) @@ -110,7 +140,7 @@ struct MenuBarModuleSelectionTests { } @Test func lastAvailableModuleCannotBeHidden() { - var selection = MenuBarModuleSelection(hidden: [.gpu, .memory, .network, .sensors], unavailable: [.battery]) + var selection = MenuBarModuleSelection(hidden: [.gpu, .memory, .network, .sensors, .keepAwake], unavailable: [.battery]) selection.setVisible(false, for: .cpu) #expect(selection.visible == [.cpu]) @@ -119,7 +149,7 @@ struct MenuBarModuleSelectionTests { @Test func hidingEveryAvailableModuleShowsThemAll() { let selection = MenuBarModuleSelection(hidden: [.cpu, .gpu, .memory, .network, .sensors], unavailable: [.battery]) - #expect(selection.visible == [.cpu, .gpu, .memory, .network, .sensors]) + #expect(selection.visible == [.cpu, .gpu, .memory, .network, .sensors, .keepAwake]) } // Destinations are indexes before the move, as SwiftUI's onMove reports them. @@ -128,14 +158,14 @@ struct MenuBarModuleSelectionTests { selection.move(fromOffsets: [1], toOffset: 0) selection.move(fromOffsets: [2], toOffset: 4) - #expect(selection.order == [.gpu, .cpu, .network, .memory, .sensors, .battery]) + #expect(selection.order == [.gpu, .cpu, .network, .memory, .sensors, .battery, .keepAwake]) } @Test func moveOfSeveralKeepsTheirOrder() { var selection = MenuBarModuleSelection() - selection.move(fromOffsets: [0, 2], toOffset: 6) + selection.move(fromOffsets: [0, 2], toOffset: 7) - #expect(selection.order == [.gpu, .network, .sensors, .battery, .cpu, .memory]) + #expect(selection.order == [.gpu, .network, .sensors, .battery, .keepAwake, .cpu, .memory]) } @Test func moveIgnoresOutOfRangeOffsets() { diff --git a/README.md b/README.md index 6f2c627..4e1a511 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ In-app updates work only after you copy Barometry to `/Applications`. macOS tran Barometry lives in the menu bar and has no Dock icon. Click a menu bar item to open the dashboard, and hover or click a section to see its details. Choose **Settings…** (⌘,) to pick modules, the layout, and the refresh interval. Quit from the dashboard or with ⌘Q. -Turn on Keep Awake with the cup button in the dashboard's footer, or ⌥-click the battery item. It stays on until you turn it off or quit Barometry. +Turn on Keep Awake with the cup button in the dashboard's footer, or ⌥-click any Barometry item in the menu bar. It stays on until you turn it off or quit Barometry. To have Keep Awake also nudge the cursor when you're idle, turn on **Move cursor when idle** in **Settings → Keep Awake** and allow Barometry in **System Settings → Privacy & Security → Accessibility**, which macOS requires to move the cursor.