diff --git a/bin/omarchy-battery-low b/bin/omarchy-battery-low index d107d4f8dc49..9e3cc5d6ca1b 100755 --- a/bin/omarchy-battery-low +++ b/bin/omarchy-battery-low @@ -13,5 +13,8 @@ fi level=$1 +# The title is matched by shell/plugins/services/battery/Service.qml to +# dismiss this toast on plug-in: keep the two in sync (test/shell.d/battery-test.sh +# asserts they match). omarchy-notification-send -g 󱐋 -u critical "Time to recharge!" "Battery is down to ${level}%" -i battery-caution -t 30000 omarchy-hook battery-low "$level" diff --git a/shell/Commons/Color.qml b/shell/Commons/Color.qml index 4fd2452b3b93..794ae0738f9a 100644 --- a/shell/Commons/Color.qml +++ b/shell/Commons/Color.qml @@ -20,6 +20,7 @@ QtObject { property color background: "#101315" property color accent: "#cacccc" property color urgent: "#a55555" + property color warning: "#a58a35" property color muted: "#707880" // Flat dictionary of "section.key" -> raw string from shell.toml. @@ -80,6 +81,7 @@ QtObject { property color background: root.composed("bar.background", "bar.background-alpha", root.background, 1.0) property color text: root.pick("bar.text", root.foreground) property color active: root.pick("bar.active", root.urgent) + property color warning: root.pick("bar.warning", root.warning) } readonly property QtObject popups: QtObject { property color background: root.composed("popups.background", "popups.background-alpha", root.background, 1.0) @@ -163,6 +165,7 @@ QtObject { else if (match[1] === "color7") color7Value = match[2] else if (match[1] === "color8") color8Value = match[2] else if (match[1] === "red" || match[1] === "color1") urgent = match[2] + else if (match[1] === "yellow" || match[1] === "color3") warning = match[2] } if (!loadedBackground && color0Value.length > 0) background = color0Value if (!loadedForeground && color7Value.length > 0) foreground = color7Value diff --git a/shell/Ui/BarIconButton.qml b/shell/Ui/BarIconButton.qml index 32214bb4141d..aa9d40edbc07 100644 --- a/shell/Ui/BarIconButton.qml +++ b/shell/Ui/BarIconButton.qml @@ -37,7 +37,7 @@ WidgetButton { text: root.text fontFamily: root.fontFamily fontSize: root.fontSize - color: root.active && root.useActiveColor ? root.activeColor : root.foreground + color: root.blinkedColor(root.active && root.useActiveColor ? root.activeColor : root.foreground) rotation: root.textRotation debugBounds: root.debugOpticalBounds } @@ -45,6 +45,7 @@ WidgetButton { Loader { anchors.fill: parent visible: root.iconComponent !== null + opacity: root.blinkOpacity sourceComponent: root.iconComponent } diff --git a/shell/Ui/PluginBarApi.qml b/shell/Ui/PluginBarApi.qml index 03c748b142cd..3b3ad99479e8 100644 --- a/shell/Ui/PluginBarApi.qml +++ b/shell/Ui/PluginBarApi.qml @@ -15,6 +15,7 @@ QtObject { property color barForeground: "transparent" property color background: "transparent" property color urgent: "transparent" + property color warning: "transparent" property string fontFamily: "" property string position: "top" property bool vertical: false diff --git a/shell/Ui/WidgetButton.qml b/shell/Ui/WidgetButton.qml index 9267f1d570e1..c5733e7ce5d8 100644 --- a/shell/Ui/WidgetButton.qml +++ b/shell/Ui/WidgetButton.qml @@ -12,6 +12,7 @@ Item { property color foreground: bar ? bar.barForeground : Commons.Color.foreground property color activeColor: bar ? bar.urgent : Commons.Color.urgent property bool active: false + property bool blinking: false property real horizontalMargin: 8.5 property real verticalPadding: 6 property real fixedWidth: -1 @@ -75,6 +76,25 @@ Item { NumberAnimation { duration: Style.duration(140); easing.type: Easing.OutCubic } } + // Text.NativeRendering (below and in BarIconButton's glyph) does not + // composite with an animated ancestor opacity, so the pulse instead fades + // the drawn color's alpha directly. blinkedColor() is exposed so + // BarIconButton can apply the same pulse to its icon glyph, which sets its + // own color and bypasses this Text. + property real blinkOpacity: 1.0 + + function blinkedColor(baseColor) { + return root.blinking ? Util.alpha(baseColor, baseColor.a * root.blinkOpacity) : baseColor + } + + SequentialAnimation on blinkOpacity { + running: root.blinking && !Style.reduceMotion + loops: Animation.Infinite + alwaysRunToEnd: true + NumberAnimation { from: 1.0; to: 0.25; duration: 500; easing.type: Easing.InOutSine } + NumberAnimation { from: 0.25; to: 1.0; duration: 500; easing.type: Easing.InOutSine } + } + Text { id: label textFormat: Text.PlainText @@ -87,7 +107,7 @@ Item { ? 0 : (labelMetrics.descent - labelMetrics.ascent + labelMetrics.tightBoundingRect("0").height) / 2 text: root.text - color: root.active && root.useActiveColor ? root.activeColor : root.foreground + color: root.blinkedColor(root.active && root.useActiveColor ? root.activeColor : root.foreground) font.family: root.fontFamily font.pixelSize: root.fontSize renderType: Text.NativeRendering @@ -95,8 +115,11 @@ Item { horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter + // Disabled while blinking: the pulse re-evaluates color every animation + // frame, and this Behavior would retrigger on each one, damping the + // sine pulse into a muddy fade instead of following it cleanly. Behavior on color { - enabled: !root.bar || root.bar.foregroundAnimationEnabled + enabled: !root.blinking && (!root.bar || root.bar.foregroundAnimationEnabled) ColorAnimation { duration: Style.duration(160) } } } diff --git a/shell/plugins/bar/Bar.qml b/shell/plugins/bar/Bar.qml index 84680cb5b2b4..0e9079535051 100644 --- a/shell/plugins/bar/Bar.qml +++ b/shell/plugins/bar/Bar.qml @@ -78,12 +78,13 @@ Item { property color foreground: themeForeground property color barForeground: useTransparentForeground ? transparentForeground : themeForeground property bool foregroundAnimationEnabled: true - property color background: Commons.Color.bar.background - property color urgent: Commons.Color.bar.active - + property color background: Color.bar.background + property color urgent: Color.bar.active + property color warning: Color.bar.warning Behavior on barForeground { enabled: root.foregroundAnimationEnabled; ColorAnimation { duration: Style.duration(420); easing.type: Easing.OutCubic } } Behavior on background { ColorAnimation { duration: Style.duration(420); easing.type: Easing.OutCubic } } Behavior on urgent { ColorAnimation { duration: Style.duration(420); easing.type: Easing.OutCubic } } + Behavior on warning { ColorAnimation { duration: Style.duration(420); easing.type: Easing.OutCubic } } property var tooltipTarget: null property var pendingTooltipTarget: null property string tooltipText: "" @@ -132,6 +133,7 @@ Item { api.barForeground = Qt.binding(function() { return root.barForeground }) api.background = Qt.binding(function() { return root.background }) api.urgent = Qt.binding(function() { return root.urgent }) + api.warning = Qt.binding(function() { return root.warning }) api.fontFamily = Qt.binding(function() { return root.fontFamily }) api.position = Qt.binding(function() { return root.position }) api.vertical = Qt.binding(function() { return root.vertical }) diff --git a/shell/plugins/bar/README.md b/shell/plugins/bar/README.md index d0934f114536..0bd4b39b8a13 100644 --- a/shell/plugins/bar/README.md +++ b/shell/plugins/bar/README.md @@ -155,7 +155,7 @@ Item { Widgets receive `bar` (the shell root), `moduleName` (string), and `settings` (object) injected at load time. The bar exposes: -- `bar.foreground`, `bar.background`, `bar.urgent` — theme colors (live-updated) +- `bar.foreground`, `bar.background`, `bar.urgent`, `bar.warning` — theme colors (live-updated) - `bar.fontFamily` — current monospace family - `bar.position` — `"top" | "bottom" | "left" | "right"` - `bar.vertical` — boolean shortcut diff --git a/shell/plugins/panels/power/Model.js b/shell/plugins/panels/power/Model.js index 8a11dfe7ee57..74b7c232fac2 100644 --- a/shell/plugins/panels/power/Model.js +++ b/shell/plugins/panels/power/Model.js @@ -78,6 +78,17 @@ function batteryIcon(device, onBattery, states) { return defaultIcons[index] } +// onBattery alone (not device.state) matches root.discharging elsewhere in +// Panel.qml: device.state can briefly lag UPower.onBattery after a plug +// event (see the "stale discharging state" test below), and requiring it +// here too would leave the bar icon uncolored for a beat while the rest of +// the panel already reads as on-battery. +function isBatteryLow(device, onBattery, threshold) { + var d = device || {} + if (!d.isPresent || !onBattery) return false + return Math.round(batteryFraction(d) * 100) <= threshold +} + function modeLabel(device, onBattery, states) { var d = device || {} if (!d.isPresent) return "" @@ -99,6 +110,7 @@ if (typeof module !== "undefined") { batteryFraction: batteryFraction, chargeThresholdActive: chargeThresholdActive, batteryIcon: batteryIcon, + isBatteryLow: isBatteryLow, modeLabel: modeLabel } } diff --git a/shell/plugins/panels/power/Panel.qml b/shell/plugins/panels/power/Panel.qml index 6a69fb267657..027526e6a78b 100644 --- a/shell/plugins/panels/power/Panel.qml +++ b/shell/plugins/panels/power/Panel.qml @@ -30,6 +30,20 @@ Panel { return !!(device && device.isPresent) } + // Three escalating thresholds while discharging: batteryWarning (yellow) + // mirrors the omarchy-battery-low notification threshold in + // shell/plugins/services/battery/Service.qml, batteryDanger (red) is the + // final stretch, and batteryCritical adds a blink once shutdown is close. + // Each is a superset of the next (danger implies warning, critical implies + // both), so the color/blink wiring below only needs to pick the reddest + // active tier. + readonly property int batteryWarningThreshold: 10 + readonly property int batteryDangerThreshold: 5 + readonly property int batteryCriticalThreshold: 2 + readonly property bool batteryWarning: Model.isBatteryLow(UPower.displayDevice, UPower.onBattery, root.batteryWarningThreshold) + readonly property bool batteryDanger: Model.isBatteryLow(UPower.displayDevice, UPower.onBattery, root.batteryDangerThreshold) + readonly property bool batteryCritical: Model.isBatteryLow(UPower.displayDevice, UPower.onBattery, root.batteryCriticalThreshold) + function upowerStates() { return { Charging: UPowerDeviceState.Charging, @@ -282,6 +296,9 @@ Panel { ? Math.round(root.batteryFraction * 100) + "% " + root.batteryIcon() : root.batteryIcon() slotSize: Style.bar.iconSlot * (root.showPercentage && !vertical ? 2 : 1) + active: root.batteryWarning + activeColor: root.batteryDanger ? (root.bar ? root.bar.urgent : Color.urgent) : (root.bar ? root.bar.warning : Color.warning) + blinking: root.batteryCritical tooltipText: "" onPressed: function(b) { if (!root.batteryPresent) return diff --git a/shell/plugins/services/battery/Service.qml b/shell/plugins/services/battery/Service.qml index eb7561f10f91..031f3fb7042d 100644 --- a/shell/plugins/services/battery/Service.qml +++ b/shell/plugins/services/battery/Service.qml @@ -12,6 +12,7 @@ Item { readonly property int batteryThreshold: 10 property string pendingPowerSource: "" + property bool pendingDismiss: false property string activePowerProfile: "" readonly property bool powerSaverOnBattery: UPower.onBattery && activePowerProfile === "power-saver" @@ -44,6 +45,22 @@ Item { warningProcess.running = true } + // Dismiss the toast on plug-in rather than leaving it to its own expiry: + // the warning is moot once the user has already acted on it. The title + // below must match bin/omarchy-battery-low's exactly, or it stops + // matching the live toast; test/shell.d/battery-test.sh asserts the two + // stay in sync. A dismiss only removes toasts that already exist, so a + // request arriving while one is in flight is queued instead of dropped. + function dismissLowBatteryWarning() { + if (dismissProcess.running) { + pendingDismiss = true + return + } + pendingDismiss = false + dismissProcess.command = ["omarchy-notification-dismiss", "Time to recharge!"] + dismissProcess.running = true + } + function applyPowerProfile() { pendingPowerSource = UPower.onBattery ? "battery" : "ac" if (!powerProfileProcess.running) runPendingPowerProfile() @@ -69,7 +86,24 @@ Item { } } - Process { id: warningProcess } + Process { + id: warningProcess + // A warning still being sent when the charger goes in lands after the + // plug-in dismiss has already run, and checkBattery() has cleared + // notifiedLowBattery by then, so nothing else would take it down. + onExited: if (!UPower.onBattery) root.dismissLowBatteryWarning() + } + + Process { + id: dismissProcess + // Replay a queued dismiss only while still on AC: after an unplug the + // toast on screen may be a fresh warning, which has to stay up. + onExited: { + var replay = root.pendingDismiss + root.pendingDismiss = false + if (replay && !UPower.onBattery) root.dismissLowBatteryWarning() + } + } Process { id: powerProfileProcess @@ -116,6 +150,8 @@ Item { Connections { target: UPower function onOnBatteryChanged() { + if (UPower.onBattery) root.pendingDismiss = false + else if (persisted.notifiedLowBattery) root.dismissLowBatteryWarning() root.checkBattery() root.applyPowerProfile() root.refreshPowerProfile() diff --git a/test/shell.d/battery-test.sh b/test/shell.d/battery-test.sh index 0447779a96f1..beb8608eed13 100644 --- a/test/shell.d/battery-test.sh +++ b/test/shell.d/battery-test.sh @@ -28,4 +28,21 @@ assertDeepEqual( { level: 40, notify: false, notifiedLowBattery: false }, 'battery clears notified state after recovery' ) + +const fs = require('fs') +const serviceSource = fs.readFileSync(root + '/shell/plugins/services/battery/Service.qml', 'utf8') +const dismissMatch = serviceSource.match(/dismissProcess\.command = \["omarchy-notification-dismiss", "([^"]+)"\]/) +assert(!!dismissMatch, 'battery service dismisses the low-battery toast through Omarchy command') +assert(/else if \(persisted\.notifiedLowBattery\) root\.dismissLowBatteryWarning\(\)/.test(serviceSource), 'battery service dismisses the low-battery toast on plug-in') +assert(/function onOnBatteryChanged\(\) \{\s*if \(UPower\.onBattery\) root\.pendingDismiss = false/.test(serviceSource), 'battery service drops a queued dismiss on unplug') +assert(/id: warningProcess[\s\S]*?onExited: if \(!UPower\.onBattery\) root\.dismissLowBatteryWarning\(\)/.test(serviceSource), 'battery service dismisses a warning that finished sending after plug-in') +assert(/pendingDismiss = true[\s\S]*?id: dismissProcess[\s\S]*?root\.pendingDismiss = false\s*if \(replay && !UPower\.onBattery\) root\.dismissLowBatteryWarning\(\)/.test(serviceSource), 'battery service replays a queued dismiss only while still on AC') + +// The dismiss above matches by title, independently of the title +// omarchy-battery-low sends: assert they're the same string so the two +// can't silently drift apart. +const sendSource = fs.readFileSync(root + '/bin/omarchy-battery-low', 'utf8') +const sendMatch = sendSource.match(/omarchy-notification-send[^\n]*?"([^"\n]+)"/) +assert(!!sendMatch, 'omarchy-battery-low sends a titled notification') +assertEqual(dismissMatch[1], sendMatch[1], 'battery service dismisses the exact toast title omarchy-battery-low sends') JS diff --git a/test/shell.d/fixtures/manifest-entrypoints/shell.qml b/test/shell.d/fixtures/manifest-entrypoints/shell.qml index 7605eb4ef384..ff96f5cc676e 100644 --- a/test/shell.d/fixtures/manifest-entrypoints/shell.qml +++ b/test/shell.d/fixtures/manifest-entrypoints/shell.qml @@ -161,6 +161,7 @@ ShellRoot { property color foreground: "white" property color background: "black" property color urgent: "red" + property color warning: "yellow" property var shell: mockShell function run(command) {} function showTooltip(target, text) {} diff --git a/test/shell.d/power-test.sh b/test/shell.d/power-test.sh index c367e782d201..b6bbc56c52c4 100644 --- a/test/shell.d/power-test.sh +++ b/test/shell.d/power-test.sh @@ -31,6 +31,17 @@ assertEqual(power.modeLabel({ isPresent: true, percentage: 1, state: states.Full assertEqual(power.modeLabel({ isPresent: true, percentage: 0.5, state: states.Discharging }, true, states), 'On battery', 'power labels battery mode') assertEqual(power.modeLabel({ isPresent: true, percentage: 0.5, state: states.Discharging }, false, states), 'Charging', 'power treats external power as newer than stale discharging state') assert(power.batteryIcon({ isPresent: true, percentage: 0.4, state: states.Charging }, false, states).length > 0, 'power maps battery icons') +assert(power.isBatteryLow({ isPresent: true, percentage: 0.1, state: states.Discharging }, true, 10), 'power flags low battery at the threshold while discharging') +assert(power.isBatteryLow({ isPresent: true, percentage: 0.08, state: states.Discharging }, true, 10), 'power flags low battery under the threshold while discharging') +assert(!power.isBatteryLow({ isPresent: true, percentage: 0.11, state: states.Discharging }, true, 10), 'power does not flag low battery above the threshold') +assert(power.isBatteryLow({ isPresent: true, percentage: 0.104, state: states.Discharging }, true, 10), 'power rounds like the low-battery notification so both switch together at 10.4%') +assert(!power.isBatteryLow({ isPresent: true, percentage: 0.05, state: states.Charging }, false, 10), 'power does not flag low battery while charging') +assert(!power.isBatteryLow({ isPresent: false, percentage: 0.05, state: states.Discharging }, true, 10), 'power does not flag low battery when no battery is present') +assert(power.isBatteryLow({ isPresent: true, percentage: 0.05, state: states.Discharging }, true, 5), 'power flags the danger tier at 5%') +assert(!power.isBatteryLow({ isPresent: true, percentage: 0.06, state: states.Discharging }, true, 5), 'power does not flag the danger tier above 5%') +assert(power.isBatteryLow({ isPresent: true, percentage: 0.02, state: states.Discharging }, true, 2), 'power flags the critical (blinking) tier at 2%') +assert(!power.isBatteryLow({ isPresent: true, percentage: 0.03, state: states.Discharging }, true, 2), 'power does not flag the critical tier above 2%') +assert(power.isBatteryLow({ isPresent: true, percentage: 0.05, state: states.Charging }, true, 10), 'power trusts onBattery over a stale Charging state left over from just before a plug event') assertEqual( power.batteryIcon({ isPresent: true, percentage: 0.4, state: states.Discharging }, false, states), power.batteryIcon({ isPresent: true, percentage: 0.4, state: states.Charging, changeRate: 1.0, timeToFull: 120 }, false, states), @@ -48,4 +59,15 @@ assert(/Math\.round\(root\.batteryFraction \* 100\) \+ "% " \+ root\.batteryIcon assert(/openPanelIndicatorWidth:.*showPercentage.*button\.glyphPaintedWidth : 0/.test(panelSource), 'power spans the open-panel mark across the painted percentage block') assert(/IpcHandler[\s\S]*?function togglePercentage\(\) \{ root\.togglePercentage\(\) \}/.test(panelSource), 'power exposes togglePercentage over IPC') assert(/manageIpc: false/.test(panelSource), 'power owns its IPC handler so it can extend the target methods') +assert(/readonly property bool batteryWarning: Model\.isBatteryLow\(/.test(panelSource), 'power derives batteryWarning from the shared model function') +assert(/readonly property bool batteryDanger: Model\.isBatteryLow\(/.test(panelSource), 'power derives batteryDanger from the shared model function') +assert(/readonly property bool batteryCritical: Model\.isBatteryLow\(/.test(panelSource), 'power derives batteryCritical from the shared model function') +assert(/active: root\.batteryWarning[\s\S]*?activeColor: root\.batteryDanger[\s\S]*?blinking: root\.batteryCritical/.test(panelSource), 'power turns the bar icon yellow at warning, red at danger, and blinking at critical') + +// A cloned power widget gets a PluginBarApi instead of the host bar, so the +// yellow tier needs the facade to carry `warning` alongside `urgent`. +const barApiSource = fs.readFileSync(root + '/shell/Ui/PluginBarApi.qml', 'utf8') +const barSource = fs.readFileSync(root + '/shell/plugins/bar/Bar.qml', 'utf8') +assert(/property color warning:/.test(barApiSource), 'plugin bar api exposes the warning color to cloned widgets') +assert(/api\.warning = Qt\.binding\(function\(\) \{ return root\.warning \}\)/.test(barSource), 'bar mirrors its warning color into the plugin bar api') JS