diff --git a/App/AppModel.swift b/App/AppModel.swift index 73e2811..7158ffd 100644 --- a/App/AppModel.swift +++ b/App/AppModel.swift @@ -123,6 +123,25 @@ final class AppModel { isPreview = true updateMenuBarItem() } + + /// Changes the charger, Mac and battery readings every second, as a real Mac's do, so a + /// debug build started with --demo --live-readings shows what live updates cost. Runs + /// until the model goes away. + func varyReadings() { + Task { [weak self] in + while !Task.isCancelled { + try? await Task.sleep(for: .seconds(1)) + guard let self, var battery = self.battery else { return } + let systemPower = Double.random(in: 7...14) + battery.date = .now + battery.systemPower = systemPower + battery.wallPower = systemPower + battery.chargerCurrent = systemPower / (battery.chargerVoltage ?? 20) + battery.temperature = Double.random(in: 28...31) + self.battery = battery + } + } + } #endif var settings: ChargeSettings? { status?.settings } diff --git a/App/ChargectlApp.swift b/App/ChargectlApp.swift index 24f8ccf..a442c4c 100644 --- a/App/ChargectlApp.swift +++ b/App/ChargectlApp.swift @@ -16,7 +16,9 @@ struct ChargectlApp: App { // Debug builds started with --demo show sample data everywhere and never talk to the // helper, so they can be tried without changing how the Mac charges. if Setup.isDemo { - _model = State(initialValue: Sample.model(manyLogs: true)) + let model = Sample.model(manyLogs: true) + if CommandLine.arguments.contains("--live-readings") { model.varyReadings() } + _model = State(initialValue: model) return } #endif diff --git a/App/Dashboard/Components.swift b/App/Dashboard/Components.swift index 22fe2bf..a2085f4 100644 --- a/App/Dashboard/Components.swift +++ b/App/Dashboard/Components.swift @@ -34,10 +34,10 @@ struct InfoRow: View { var body: some View { GridRow { Text(title).foregroundStyle(.secondary) + // Live readings that change every second: no rolling digits. Text(value) .monospacedDigit() .foregroundStyle(tint ?? .primary) - .rollingNumber(value) .gridColumnAlignment(.trailing) } } diff --git a/App/Dashboard/OverviewPage.swift b/App/Dashboard/OverviewPage.swift index bdf3158..e4b062f 100644 --- a/App/Dashboard/OverviewPage.swift +++ b/App/Dashboard/OverviewPage.swift @@ -176,6 +176,8 @@ struct OverviewPage: View { ) .frame(height: 132) let total = max(mac + max(batteryPower, 0), 0.1) + // Not animated: the readings change every second, and easing the width on each + // change lays the whole page out again on every frame. GeometryReader { geometry in HStack(spacing: 2) { Capsule().fill(Theme.mac).frame(width: geometry.size.width * mac / total) @@ -186,7 +188,6 @@ struct OverviewPage: View { } .frame(height: 6) .background(Theme.track, in: Capsule()) - .animation(.easeOut(duration: 0.6), value: mac) HStack(spacing: 16) { legend(Theme.mac, "Mac \(Int((mac / total * 100).rounded()))%") if charging { @@ -248,7 +249,8 @@ struct OverviewPage: View { HealthCell( title: "Temperature", value: battery.temperature.map { String(format: "%.1f", unit.value($0)) } ?? "–", - unit: unit.symbol + unit: unit.symbol, + isLive: true ) { HStack(spacing: 6) { Circle().fill(warm ? Theme.warn : Theme.accent).frame(width: 6, height: 6) @@ -383,7 +385,6 @@ struct OverviewPage: View { Text(Format.watts(app.watts)) .font(.system(size: 12.5)) .monospacedDigit() - .rollingNumber(app.watts) .frame(minWidth: 48, alignment: .trailing) } } @@ -688,6 +689,8 @@ private struct HealthCell: View { let title: String let value: String let unit: String + /// A reading that changes every second, which changes without rolling digits. + var isLive = false @ViewBuilder var footer: Footer var body: some View { @@ -698,7 +701,7 @@ private struct HealthCell: View { .font(.system(size: 24, weight: .medium)) .tracking(-0.5) .monospacedDigit() - .rollingNumber(value) + .rollingNumber(value, isLive: isLive) Text(unit).font(.system(size: 12)).foregroundStyle(.tertiary) } footer @@ -811,7 +814,7 @@ private struct PowerFlow: View { ) .shadow(color: glow ? tint.opacity(0.35) : .clear, radius: 8) let text = VStack(alignment: leading ? .leading : .trailing, spacing: 1) { - Text(value).font(.system(size: 14, weight: .semibold)).monospacedDigit().rollingNumber(value) + Text(value).font(.system(size: 14, weight: .semibold)).monospacedDigit() Text(title).font(.system(size: 11.5)).foregroundStyle(.tertiary) } return HStack(spacing: 10) { diff --git a/App/Design/DesignSystem.swift b/App/Design/DesignSystem.swift index bfbe217..628f484 100644 --- a/App/Design/DesignSystem.swift +++ b/App/Design/DesignSystem.swift @@ -44,17 +44,24 @@ extension Theme { extension View { /// Digits roll whenever the value changes, also when the change came from a background /// refresh rather than an animated action. Not while the window is off screen. - func rollingNumber(_ value: Value) -> some View { - modifier(RollingNumber(value: value)) + /// + /// Only for values that change now and then: the charge, a setting, the cycle count. The roll + /// is drawn on the CPU, so on a live reading that changes every second (watts, volts, amps, + /// temperature) it keeps the app at around half a CPU core. Pass `isLive` for those, or leave + /// the modifier off. + func rollingNumber(_ value: Value, isLive: Bool = false) -> some View { + modifier(RollingNumber(value: value, rolls: !isLive)) } } private struct RollingNumber: ViewModifier { let value: Value + let rolls: Bool @Environment(\.isOnScreen) private var isOnScreen func body(content: Content) -> some View { - content.contentTransition(.numericText()).animation(isOnScreen ? .smooth(duration: 0.45) : nil, value: value) + content.contentTransition(.numericText()) + .animation(rolls && isOnScreen ? .smooth(duration: 0.45) : nil, value: value) } } diff --git a/App/MenuBar/MenuContent.swift b/App/MenuBar/MenuContent.swift index 9ce38ed..a2d58af 100644 --- a/App/MenuBar/MenuContent.swift +++ b/App/MenuBar/MenuContent.swift @@ -113,11 +113,13 @@ struct MenuContent: View { return HStack(spacing: 0) { Metric(title: "Health", value: Format.percent(battery.health), first: true) Metric(title: "Cycles", value: "\(battery.cycleCount)") - Metric(title: "Charger", value: (battery.wallPower ?? 0) < 0.05 ? "0 W" : Format.watts(battery.wallPower)) - Metric(title: "Mac", value: Format.watts(battery.systemPower)) + Metric( + title: "Charger", value: (battery.wallPower ?? 0) < 0.05 ? "0 W" : Format.watts(battery.wallPower), + isLive: true) + Metric(title: "Mac", value: Format.watts(battery.systemPower), isLive: true) Metric( title: "Battery", value: abs(watts) < 0.05 ? "0 W" : Format.signedWatts(watts), - tint: watts > 0.05 ? Theme.accent : watts < -0.05 ? Theme.warn : nil) + tint: watts > 0.05 ? Theme.accent : watts < -0.05 ? Theme.warn : nil, isLive: true) } .padding(.top, 10) .overlay(alignment: .top) { Rectangle().fill(Theme.line).frame(height: 1) } @@ -422,6 +424,8 @@ private struct Metric: View { let title: String let value: String var tint: Color? + /// A reading that changes every second, which changes without rolling digits. + var isLive = false var first = false var body: some View { @@ -433,7 +437,7 @@ private struct Metric: View { .monospacedDigit() .lineLimit(1) .minimumScaleFactor(0.85) - .rollingNumber(value) + .rollingNumber(value, isLive: isLive) } .frame(maxWidth: .infinity, alignment: .leading) .padding(.leading, first ? 0 : 9) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76416e0..83eecfa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,16 @@ sees, with its pull request link. Credit outside contributors with ## [Unreleased] +### Fixed + +- **Chargectl no longer uses CPU while the dashboard is open.** On Overview the + watts, volts, amps and temperature rolled their digits, and the Mac and + battery bar eased to its new width, every time a reading changed. Readings + change every second, so the open window used around half a CPU core, also + behind other apps. Live readings now change in place, and the open dashboard + uses about a tenth of what it did. The charge, the cycle count and your + settings still roll. ([#106](https://github.com/tyldra-org/chargectl/pull/106)) + ## [0.3.1] - 2026-11-25 ### Fixed diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 84f4749..48f392b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -104,6 +104,12 @@ the build is signed ad hoc. Releases are signed with the release certificate. Changes to how charging is controlled must also be tried on a real MacBook. Say in the pull request which Mac and macOS version you used and what you saw. +Changes to the dashboard or the menu bar panel should not raise the app's CPU +use. Run the Debug build with `--demo --live-readings`, which changes the +watts and the temperature every second as a real Mac does, and watch +Chargectl in Activity Monitor with the window open, in front and behind +another app. It should stay at a few percent. + ### Screenshots The README's pictures come from a Debug build with sample data, so they need no