Skip to content
Open
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
3 changes: 3 additions & 0 deletions bin/omarchy-battery-low
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 3 additions & 0 deletions shell/Commons/Color.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion shell/Ui/BarIconButton.qml
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ 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
}

Loader {
anchors.fill: parent
visible: root.iconComponent !== null
opacity: root.blinkOpacity
sourceComponent: root.iconComponent
}

Expand Down
1 change: 1 addition & 0 deletions shell/Ui/PluginBarApi.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 25 additions & 2 deletions shell/Ui/WidgetButton.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }
Comment thread
thooams marked this conversation as resolved.
Resolved
}

Text {
id: label
textFormat: Text.PlainText
Expand All @@ -87,16 +107,19 @@ 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
rotation: root.textRotation
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) }
}
}
Expand Down
8 changes: 5 additions & 3 deletions shell/plugins/bar/Bar.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ""
Expand Down Expand Up @@ -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 })
Expand Down
2 changes: 1 addition & 1 deletion shell/plugins/bar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions shell/plugins/panels/power/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand All @@ -99,6 +110,7 @@ if (typeof module !== "undefined") {
batteryFraction: batteryFraction,
chargeThresholdActive: chargeThresholdActive,
batteryIcon: batteryIcon,
isBatteryLow: isBatteryLow,
modeLabel: modeLabel
}
}
17 changes: 17 additions & 0 deletions shell/plugins/panels/power/Panel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
blinking: root.batteryCritical
tooltipText: ""
onPressed: function(b) {
if (!root.batteryPresent) return
Expand Down
38 changes: 37 additions & 1 deletion shell/plugins/services/battery/Service.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
17 changes: 17 additions & 0 deletions test/shell.d/battery-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions test/shell.d/fixtures/manifest-entrypoints/shell.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand Down
22 changes: 22 additions & 0 deletions test/shell.d/power-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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