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
19 changes: 19 additions & 0 deletions App/AppModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 3 additions & 1 deletion App/ChargectlApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion App/Dashboard/Components.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
13 changes: 8 additions & 5 deletions App/Dashboard/OverviewPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -688,6 +689,8 @@ private struct HealthCell<Footer: View>: 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 {
Expand All @@ -698,7 +701,7 @@ private struct HealthCell<Footer: View>: 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
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 10 additions & 3 deletions App/Design/DesignSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: Equatable>(_ 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: Equatable>(_ value: Value, isLive: Bool = false) -> some View {
modifier(RollingNumber(value: value, rolls: !isLive))
}
}

private struct RollingNumber<Value: Equatable>: 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)
}
}

Expand Down
12 changes: 8 additions & 4 deletions App/MenuBar/MenuContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down