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
92 changes: 92 additions & 0 deletions bin/omarchy-battery-limit
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/bin/bash

# omarchy:summary=Inspect and control UPower battery charge limits
# omarchy:args=[status|enable|disable] [--shell]
# omarchy:examples=omarchy battery limit | omarchy battery limit enable

case "$*" in
"" | status) action=status; shell_output=false ;;
--shell | "status --shell") action=status; shell_output=true ;;
enable | disable) action="$1"; shell_output=false ;;
*)
echo "Usage: omarchy-battery-limit [status|enable|disable] [--shell]" >&2
exit 2
;;
esac

upower_property() {
local raw value

raw=$(busctl get-property org.freedesktop.UPower "$1" org.freedesktop.UPower.Device "$2" 2>/dev/null) || return 1
read -r _ value <<<"$raw"
value=${value#\"}
value=${value%\"}
printf '%s\n' "$value"
}

declare -a batteries=()
enabled=true
policy=unknown

while IFS= read -r device; do
[[ $device != */DisplayDevice ]] || continue
[[ $(upower_property "$device" ChargeThresholdSupported || true) == true ]] || continue

batteries+=("$device")
[[ $(upower_property "$device" ChargeThresholdEnabled || true) == true ]] || enabled=false

if [[ $policy == unknown ]]; then
settings=$(upower_property "$device" ChargeThresholdSettingsSupported || true)
start=$(upower_property "$device" ChargeStartThreshold || true)
end=$(upower_property "$device" ChargeEndThreshold || true)

if [[ $settings =~ ^[0-9]+$ ]] && (( (settings & 2) != 0 )) && [[ $end =~ ^[0-9]+$ ]]; then
if (( (settings & 1) != 0 )) && [[ $start =~ ^[0-9]+$ ]] && (( start < end )); then
policy="$start-$end"
else
policy="$end"
fi
elif [[ $settings =~ ^[0-9]+$ ]] && (( (settings & 4) != 0 )); then
policy=firmware
fi
fi
done < <(upower -e 2>/dev/null)

if (( ${#batteries[@]} == 0 )); then
state=unsupported
elif [[ $enabled == true ]]; then
state=enabled
else
state=disabled
fi

if [[ $action == status ]]; then
if [[ $shell_output == true ]]; then
printf 'state\t%s\npolicy\t%s\n' "$state" "$policy"
else
case "$state" in
unsupported) echo "Battery protection is not supported" ;;
enabled) echo "Battery protection is enabled" ;;
disabled) echo "Battery protection is disabled" ;;
esac
fi
exit 0
fi

if [[ $state == unsupported ]]; then
echo "Battery charge limits are not supported by UPower on this system" >&2
exit 1
fi

[[ $action == enable ]] && desired=true || desired=false
failed=false

for device in "${batteries[@]}"; do
if ! busctl call org.freedesktop.UPower "$device" org.freedesktop.UPower.Device EnableChargeThreshold b "$desired" >/dev/null; then
echo "Failed to update charge limits for $device" >&2
failed=true
fi
done

[[ $failed == false ]] || exit 1
echo "Battery protection ${action}d"
4 changes: 3 additions & 1 deletion manual/05-the-top-bar.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ The panels aren't read-outs. They're where you actually do the thing:
- **Audio** has a master volume slider, an output-device picker, and a per-app mixer, so you can turn down that one browser tab without touching everything else.
- **Network** scans for Wi-Fi, shows signal strength, connects, and lets you pick a DNS provider.
- **Bluetooth** lists your devices with connect/disconnect and battery levels.
- **Power** shows battery stats, switches power profiles (it remembers a separate choice for battery and AC), and prints some system info.
- **Power** shows battery stats, controls battery-health protection when UPower and the hardware support it, switches power profiles (it remembers a separate choice for battery and AC), and prints some system info.

On compatible laptops, **Preserve battery health** applies the charging policy reported by UPower and keeps it across restarts. The policy commonly stops charging around 80%, but UPower and the hardware choose the actual values rather than Omarchy. The control stays hidden when charging limits are unsupported.
- **Display** carries a brightness slider, text size, monitor scaling presets, and — when you have more than one screen — per-monitor controls. See [monitors](33-monitors.md) for the deeper story.
- **Clock** opens a month grid with ISO week numbers and month stepping.

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 @@ -68,7 +68,7 @@ Example `shell.json` (bar subtree only shown):
| `omarchy.network` | Wi-Fi/Ethernet icon + popup with Wi-Fi scan, signal, connect, DNS provider selection | left = popup |
| `omarchy.tailscale` | Tailscale status, connection switcher, machine browser, and copy actions | left = popup · right = toggle · middle = refresh |
| `omarchy.agents` | AI coding agent limits with pace, today, last week, and all-time model breakdown | left = panel · right = launch agent · middle = next subscription |
| `omarchy.power` | Battery/AC icon + popup with battery stats, power profiles, and system info | left = popup · right = toggle percentage |
| `omarchy.power` | Battery/AC icon + popup with battery stats, battery-health protection, power profiles, and system info | left = popup · right = toggle percentage |
| `omarchy.bluetooth` | Bluetooth icon + popup with device list, connect/disconnect, battery | left = popup · right = toggle radio |
| `omarchy.monitor` | Brightness and laptop display controls | left = popup |

Expand Down
15 changes: 15 additions & 0 deletions shell/plugins/panels/power/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ function parseProfiles(raw, previousIndex) {
}
}

function limitDescription(state, policy) {
var active = state === "enabled"
if (policy === "firmware") return (active ? "Uses" : "Use") + " firmware optimized charging"
if (!policy || policy === "unknown") return (active ? "Uses" : "Use") + " the battery's health-preserving policy"

var thresholds = policy.split("-")
if (thresholds.length === 2) {
return (active ? "Starts charging at " : "Start charging at ") + thresholds[0] + "% and "
+ (active ? "stops" : "stop") + " at " + thresholds[1] + "%"
}

return (active ? "Stops charging at " : "Limit charging to ") + policy + "%"
}

function profileIcon(name) {
if (name === "power-saver") return "󰌪"
if (name === "balanced") return "󰊚"
Expand Down Expand Up @@ -95,6 +109,7 @@ if (typeof module !== "undefined") {
selectProfileIndex: selectProfileIndex,
parseKeyValue: parseKeyValue,
parseProfiles: parseProfiles,
limitDescription: limitDescription,
profileIcon: profileIcon,
batteryFraction: batteryFraction,
chargeThresholdActive: chargeThresholdActive,
Expand Down
122 changes: 117 additions & 5 deletions shell/plugins/panels/power/Panel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ Panel {
// permits — needed for the togglePercentage method below.
manageIpc: false
property var batteryInfo: ({})
property var limitInfo: ({ state: "unsupported", policy: "unknown" })
property var systemInfo: ({})
property var profiles: []
property string activeProfile: ""
property int profileIndex: 0
property bool cursorActive: false
property string cursorSection: "profiles"
property string limitError: ""
readonly property bool limitVisible: limitInfo.state !== "unsupported"
readonly property string limitDescription: Model.limitDescription(limitInfo.state, limitInfo.policy)
readonly property bool showPercentage: setting("showPercentage", false) === true
// With the percentage shown the button paints a text block wider than an
// icon, so the open-panel mark takes the painted width instead of the
Expand Down Expand Up @@ -61,6 +66,37 @@ Panel {
return Model.profileIcon(name)
}

function moveCursor(dx, dy) {
if (!cursorActive) {
cursorActive = true
return
}

if (limitVisible && ((cursorSection === "limit" && dy > 0) || (cursorSection === "profiles" && dy < 0))) {
cursorSection = cursorSection === "limit" ? "profiles" : "limit"
return
}

if (cursorSection === "profiles") selectProfileByDelta(dx !== 0 ? dx : dy)
}

function activateCursor() {
if (!cursorActive) {
cursorActive = true
return
}

if (cursorSection === "profiles") activateSelectedProfile()
else setLimit(limitInfo.state === "enabled" ? "disable" : "enable")
}

function setLimit(action) {
if (!action || limitActionProc.running) return
limitError = ""
limitActionProc.command = ["omarchy-battery-limit", action]
limitActionProc.running = true
}

readonly property bool fullyCharged: {
var device = UPower.displayDevice
return device && device.isPresent && device.state === UPowerDeviceState.FullyCharged && !root.chargeThresholdActive
Expand Down Expand Up @@ -136,16 +172,25 @@ Panel {
if (!batteryPresent) return

if (!batteryProc.running) batteryProc.running = true
refreshLimit()
if (!profilesProc.running) profilesProc.running = true
if (!systemProc.running) systemProc.running = true
}

function refreshLimit() {
if (batteryPresent && !limitProc.running) limitProc.running = true
}

function updateKeyValue(raw, targetName) {
var next = Model.parseKeyValue(raw)
// Keep last known good data if a refresh briefly returns nothing — happens
// around AC plug/unplug events. Avoids the section collapsing mid-transition.
if (Object.keys(next).length === 0) return
if (targetName === "battery") batteryInfo = next
else if (targetName === "limit") {
limitInfo = next
if (!cursorActive) cursorSection = next.state !== "unsupported" ? "limit" : "profiles"
}
else systemInfo = next
}

Expand Down Expand Up @@ -196,6 +241,7 @@ Panel {
var idx = profiles.indexOf(activeProfile)
profileIndex = idx >= 0 ? idx : 0
cursorActive = false
cursorSection = limitVisible ? "limit" : "profiles"
}
}

Expand All @@ -211,6 +257,12 @@ Panel {
stdout: StdioCollector { waitForEnd: true; onStreamFinished: root.updateKeyValue(text, "battery") }
}

Process {
id: limitProc
command: ["omarchy-battery-limit", "status", "--shell"]
stdout: StdioCollector { waitForEnd: true; onStreamFinished: root.updateKeyValue(text, "limit") }
}

Process {
id: profilesProc
command: ["omarchy-powerprofiles-list", "--active-state"]
Expand All @@ -228,6 +280,22 @@ Panel {
onExited: root.refresh()
}

Process {
id: limitActionProc
stderr: StdioCollector {
waitForEnd: true
onStreamFinished: {
var message = text.trim()
if (message !== "") root.limitError = message
}
}
onExited: function(exitCode) {
if (exitCode === 0) root.limitError = ""
else if (root.limitError === "") root.limitError = "Could not update battery protection"
root.refresh()
}
}

Timer { interval: 5000; running: root.opened; repeat: true; onTriggered: root.refresh() }

// Rotate the status phrase while the panel is open and we're in a
Expand Down Expand Up @@ -303,11 +371,9 @@ Panel {
id: keyCatcher
anchors.fill: parent
onMoveRequested: function(dx, dy) {
if (!root.cursorActive) { root.cursorActive = true; return }
if (dx !== 0) root.selectProfileByDelta(dx)
else if (dy !== 0) root.selectProfileByDelta(dy)
root.moveCursor(dx, dy)
}
onActivateRequested: if (root.cursorActive) root.activateSelectedProfile()
onActivateRequested: root.activateCursor()
onCloseRequested: root.close()
onTabRequested: function(direction) { root.switchPanel(direction) }

Expand Down Expand Up @@ -448,6 +514,51 @@ Panel {
}
}

// ---------- Battery health ----------
Column {
visible: root.limitVisible
width: parent.width
spacing: Style.space(10)

PanelSeparator {
foreground: root.bar.foreground
}

PanelSectionHeader {
text: "BATTERY HEALTH"
foreground: root.bar.foreground
fontFamily: root.bar.fontFamily
}

Toggle {
width: parent.width
label: "Preserve battery health"
description: root.limitDescription
checked: root.limitInfo.state === "enabled"
enabled: !limitActionProc.running
hasCursor: root.cursorActive && root.cursorSection === "limit"
foreground: root.bar.foreground
fontFamily: root.bar.fontFamily
onClicked: root.setLimit(root.limitInfo.state === "enabled" ? "disable" : "enable")
onHovered: function(h) {
if (h) {
root.cursorActive = true
root.cursorSection = "limit"
}
}
}

Text {
visible: root.limitError !== ""
width: parent.width
text: root.limitError
color: Color.urgent
font.family: root.bar.fontFamily
font.pixelSize: Style.font.caption
wrapMode: Text.WordWrap
}
}

// ---------- Power profile picker ----------
PanelSeparator {
foreground: root.bar.foreground
Expand Down Expand Up @@ -488,11 +599,12 @@ Panel {
verticalPadding: Style.spacing.controlPaddingY + Style.space(2)
bordered: true
active: root.activeProfile === modelData
hasCursor: root.cursorActive && root.profileIndex === index
hasCursor: root.cursorActive && root.cursorSection === "profiles" && root.profileIndex === index
onClicked: root.setProfile(modelData)
onHovered: function(h) {
if (h) {
root.cursorActive = true
root.cursorSection = "profiles"
root.profileIndex = index
}
}
Expand Down
4 changes: 2 additions & 2 deletions shell/plugins/panels/power/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "Power",
"version": "1.0.0",
"author": "Omarchy",
"description": "Battery, power profile, and system stats",
"description": "Battery health, power profiles, and system stats",
"kinds": [
"bar-widget"
],
Expand All @@ -13,7 +13,7 @@
},
"barWidget": {
"displayName": "Power",
"description": "Battery, power profile, and system stats",
"description": "Battery health, power profiles, and system stats",
"category": "System",
"allowMultiple": false
}
Expand Down
Loading