Skip to content
Open
Show file tree
Hide file tree
Changes from 9 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
27 changes: 25 additions & 2 deletions shell/Ui/WidgetButton.qml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Item {
property color foreground: bar ? bar.barForeground : Color.foreground
property color activeColor: bar ? bar.urgent : 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 @@ -74,6 +75,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 @@ -86,16 +106,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
2 changes: 2 additions & 0 deletions shell/plugins/bar/Bar.qml
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ Item {
property bool foregroundAnimationEnabled: true
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.InOutCubic } }
Behavior on background { ColorAnimation { duration: Style.duration(420); easing.type: Easing.InOutCubic } }
Behavior on urgent { ColorAnimation { duration: Style.duration(420); easing.type: Easing.InOutCubic } }
Behavior on warning { ColorAnimation { duration: Style.duration(420); easing.type: Easing.InOutCubic } }
property var tooltipTarget: null
property var pendingTooltipTarget: null
property string tooltipText: ""
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 @@ -29,6 +29,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 @@ -281,6 +295,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
13 changes: 13 additions & 0 deletions shell/plugins/services/battery/Service.qml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ 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.
function dismissLowBatteryWarning() {
if (dismissProcess.running) return
dismissProcess.command = ["omarchy-notification-dismiss", "Time to recharge!"]
dismissProcess.running = true
}

function applyPowerProfile() {
pendingPowerSource = UPower.onBattery ? "battery" : "ac"
if (!powerProfileProcess.running) runPendingPowerProfile()
Expand All @@ -70,6 +81,7 @@ Item {
}

Process { id: warningProcess }
Process { id: dismissProcess }

Process {
id: powerProfileProcess
Expand Down Expand Up @@ -116,6 +128,7 @@ Item {
Connections {
target: UPower
function onOnBatteryChanged() {
if (!UPower.onBattery && persisted.notifiedLowBattery) root.dismissLowBatteryWarning()
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
root.checkBattery()
root.applyPowerProfile()
root.refreshPowerProfile()
Expand Down
14 changes: 14 additions & 0 deletions test/shell.d/battery-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,18 @@ 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(/if \(!UPower\.onBattery && persisted\.notifiedLowBattery\) root\.dismissLowBatteryWarning\(\)/.test(serviceSource), 'battery service dismisses the low-battery toast on plug-in')

// 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
15 changes: 15 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,8 @@ 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')
JS