Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 9 additions & 24 deletions App/MenuBar/MenuBarWidgetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
}
}
}
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion App/Settings/KeepAwakePane.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
16 changes: 13 additions & 3 deletions App/Settings/MenuBarPane.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}
}
2 changes: 1 addition & 1 deletion App/Settings/MenuBarPreview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions App/Settings/SettingsComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ extension MenuBarModule {
switch self {
case .sensors: "thermometer.medium"
case .battery: "battery.75percent"
case .keepAwake: KeepAwake.systemImage
default: DashboardSection(self).symbol
}
}
Expand Down
52 changes: 24 additions & 28 deletions App/StatusItemController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
}
}

Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand All @@ -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<MenuBarModule> {
Expand All @@ -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"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "—"
Expand All @@ -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%")
}
Expand Down Expand Up @@ -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
}
}

Expand All @@ -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
Expand All @@ -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)) } ?? "–:––"
Expand All @@ -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))
}
}

Expand All @@ -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
}
}

Expand All @@ -183,7 +170,6 @@ extension SystemMonitor {
for module: MenuBarModule,
style: MenuBarStyle,
temperatureUnit: TemperatureUnit,
isKeepingAwake: Bool,
locale: Locale = .autoupdatingCurrent
) -> MenuBarWidgetContent {
.make(
Expand All @@ -196,7 +182,6 @@ extension SystemMonitor {
battery: battery,
sensors: sensors,
temperatureUnit: temperatureUnit,
isKeepingAwake: isKeepingAwake,
locale: locale
)
}
Expand Down
Loading
Loading