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

# omarchy:summary=Stop charging below full to protect battery health, remembered across reboots
# omarchy:args=<on|off|toggle|status>

# UPower owns the limit. EnableChargeThreshold writes the kernel's
# charge_control thresholds, or switches charge_types to Long_Life on laptops
# that only offer a charging mode, saves the choice under /var/lib/upower, and
# reapplies it on every boot. Its polkit action lets the active session change
# it without a password, so nothing here needs root.
#
# The percentages stay UPower's too: the model's hwdb CHARGE_LIMIT, or the
# 75-80% default. A laptop with two batteries gets the same answer on both,
# since a limit on one pack alone protects nothing.

UPOWER=org.freedesktop.UPower
DEVICE=$UPOWER.Device

# ChargeThresholdSettingsSupported bit for a real end-of-charge percentage. Without
# it the laptop stops by charging mode, and the firmware picks the stopping point.
END_THRESHOLD_SETTING=2

property() {
busctl get-property "$UPOWER" "$1" "$DEVICE" "$2" 2>/dev/null | awk '{print $2}'
}

batteries() {
local device

for device in $(upower -e 2>/dev/null | grep '/battery_'); do
[[ $(property "$device" ChargeThresholdSupported) == "true" ]] && echo "$device"
done
}

enabled() {
local device

for device in "${devices[@]}"; do
[[ $(property "$device" ChargeThresholdEnabled) == "true" ]] || return 1
done
}

set_limit() {
local device

for device in "${devices[@]}"; do
busctl call "$UPOWER" "$device" "$DEVICE" EnableChargeThreshold b "$1" >/dev/null || return 1
done
}

# Prints "on" or "off", then the stopping percentage when the laptop has one the
# user can rely on.
status() {
local state=off end=""

if enabled; then
state=on
fi

if (( $(property "${devices[0]}" ChargeThresholdSettingsSupported) & END_THRESHOLD_SETTING )); then
end=$(property "${devices[0]}" ChargeEndThreshold)
fi

echo "$state${end:+ $end}"
}

case "${1:-}" in
on | off | toggle | status) ;;
*)
echo "Usage: omarchy-battery-charge-limit <on|off|toggle|status>" >&2
exit 1
;;
esac

mapfile -t devices < <(batteries)

if (( ${#devices[@]} == 0 )); then
echo "omarchy-battery-charge-limit: no battery that supports a charge limit" >&2
exit 1
fi

case "$1" in
on)
set_limit true
;;
off)
set_limit false
;;
toggle)
if enabled; then
set_limit false
else
set_limit true
fi
;;
status)
status
;;
esac
2 changes: 1 addition & 1 deletion manual/05-the-top-bar.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ 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, switches power profiles (it remembers a separate choice for battery and AC), sets a charge limit on batteries that support one, and prints some system info.
- **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
6 changes: 6 additions & 0 deletions manual/36-system-sleep.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ On a laptop, Omarchy remembers your power profile separately for plugged in and

You can see what your machine offers with `omarchy powerprofiles list`, and set the one you want for the state you're currently in with `omarchy powerprofiles set autodetect power-saver`. To set the other state without unplugging anything, name it directly: `omarchy powerprofiles set battery power-saver`. Whatever you pick is what you'll get back the next time you're in that state.

### Charge limit

A laptop that spends most of its life plugged in wears its battery fastest sitting at 100%. If your battery supports it, the _Charge limit_ row in the top bar's power panel stops charging short of full, usually at 80%, while the laptop keeps running off the charger. Pick _Full_ the night before you need the whole battery, and switch back afterwards. Some laptops, like many Dells, stop by a long-life charging mode rather than at a set percentage, so the buttons just say _Limit_ and _Full_.

From the terminal, use `omarchy battery charge-limit on`, `off`, `toggle`, or `status`. The choice is kept across reboots.

### Toggle suspend

You toggle suspend by running `omarchy toggle suspend` from the terminal. That just reveals/hides the option under _System_ (or `Super + Esc`), and then you can see if it works consistently on your system. If not, you can hide it again with the same command.
Expand Down
22 changes: 22 additions & 0 deletions shell/plugins/panels/power/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ function parseProfiles(raw, previousIndex) {
}
}

// omarchy-battery-charge-limit status prints "on" or "off", then the stopping
// percentage when the laptop has one. No output means no supported battery.
function parseChargeLimit(raw) {
var parts = String(raw || "").trim().split(/\s+/)
var state = parts[0]
if (state !== "on" && state !== "off") return { supported: false, enabled: false, end: 0 }

var end = parseInt(parts[1], 10)
return { supported: true, enabled: state === "on", end: end > 0 && end < 100 ? end : 0 }
}

// Laptops that stop by charging mode rather than at a number get plain labels,
// so the panel never promises a percentage the firmware does not keep.
function chargeLimitOptions(end) {
return [
{ enabled: true, icon: "\u{F0081}", label: end > 0 ? "Limit " + end + "%" : "Limit" },
{ enabled: false, icon: "\u{F0079}", label: end > 0 ? "Full 100%" : "Full" }
]
}

function profileIcon(name) {
if (name === "power-saver") return "󰌪"
if (name === "balanced") return "󰊚"
Expand Down Expand Up @@ -95,6 +115,8 @@ if (typeof module !== "undefined") {
selectProfileIndex: selectProfileIndex,
parseKeyValue: parseKeyValue,
parseProfiles: parseProfiles,
parseChargeLimit: parseChargeLimit,
chargeLimitOptions: chargeLimitOptions,
profileIcon: profileIcon,
batteryFraction: batteryFraction,
chargeThresholdActive: chargeThresholdActive,
Expand Down
112 changes: 108 additions & 4 deletions shell/plugins/panels/power/Panel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ Panel {
property string activeProfile: ""
property int profileIndex: 0
property bool cursorActive: false
property var chargeLimit: Model.parseChargeLimit("")
readonly property var chargeLimitOptions: Model.chargeLimitOptions(chargeLimit.end)
property int chargeLimitIndex: 0
// Which button row the keyboard cursor is on: 0 = power profile, 1 = charge limit.
property int cursorRow: 0
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 All @@ -43,6 +48,31 @@ Panel {
profileIndex = Model.selectProfileIndex(profileIndex, delta, profiles)
}

function moveCursor(dx, dy) {
if (dy !== 0 && chargeLimit.supported) {
cursorRow = dy > 0 ? 1 : 0
} else if (cursorRow === 1) {
chargeLimitIndex = Model.clampIndex(chargeLimitIndex + (dx !== 0 ? dx : dy), chargeLimitOptions.length)
} else {
selectProfileByDelta(dx !== 0 ? dx : dy)
}
}

function activateCursor() {
if (cursorRow === 1) setChargeLimit(chargeLimitOptions[chargeLimitIndex].enabled)
else activateSelectedProfile()
}

function refreshChargeLimit() {
if (!chargeLimitProc.running) chargeLimitProc.running = true
}

function setChargeLimit(enabled) {
if (chargeLimitSetProc.running || enabled === chargeLimit.enabled) return
chargeLimitSetProc.command = ["omarchy-battery-charge-limit", enabled ? "on" : "off"]
chargeLimitSetProc.running = true
}

function activateSelectedProfile() {
if (profileIndex < 0 || profileIndex >= profiles.length) return
setProfile(profiles[profileIndex])
Expand Down Expand Up @@ -197,9 +227,13 @@ Panel {
}

refresh()
// Only Omarchy's own commands change the limit in practice, so read it once
// per open and after each change rather than on the 5s refresh tick.
refreshChargeLimit()
var idx = profiles.indexOf(activeProfile)
profileIndex = idx >= 0 ? idx : 0
cursorActive = false
cursorRow = 0
}
}

Expand Down Expand Up @@ -232,6 +266,24 @@ Panel {
onExited: root.refresh()
}

Process {
id: chargeLimitProc
command: ["omarchy-battery-charge-limit", "status"]
stdout: StdioCollector {
waitForEnd: true
onStreamFinished: {
root.chargeLimit = Model.parseChargeLimit(text)
if (!root.cursorActive) root.chargeLimitIndex = root.chargeLimit.enabled ? 0 : 1
}
}
}

Process {
id: chargeLimitSetProc
// Re-read rather than assume: the firmware or polkit can refuse the change.
onExited: root.refreshChargeLimit()
}

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 @@ -309,10 +361,9 @@ Panel {
implicitHeight: column.implicitHeight
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: if (root.cursorActive) root.activateCursor()
onCloseRequested: root.close()
onTabRequested: function(direction) { root.switchPanel(direction) }

Expand Down Expand Up @@ -496,18 +547,71 @@ Panel {
verticalPadding: Style.spacing.controlPaddingY + Style.space(2)
bordered: true
active: root.activeProfile === modelData
hasCursor: root.cursorActive && root.profileIndex === index
hasCursor: root.cursorActive && root.cursorRow === 0 && root.profileIndex === index
onClicked: root.setProfile(modelData)
onHovered: function(h) {
if (h) {
root.cursorActive = true
root.cursorRow = 0
root.profileIndex = index
}
}
}
}
}
}

// ---------- Charge limit ----------
PanelSeparator {
visible: root.chargeLimit.supported
foreground: root.bar.foreground
}

Column {
visible: root.chargeLimit.supported
width: parent.width
spacing: Style.space(10)

PanelSectionHeader {
text: "CHARGE LIMIT"
foreground: root.bar.foreground
fontFamily: root.bar.fontFamily
}

Row {
id: chargeLimitRow
width: parent.width
spacing: Style.space(6)

Repeater {
model: root.chargeLimitOptions
Button {
required property var modelData
required property int index
width: (chargeLimitRow.width - chargeLimitRow.spacing) / 2
iconText: modelData.icon
iconSize: Style.font.title
text: modelData.label
fontSize: Style.font.bodySmall
foreground: root.bar.foreground
fontFamily: root.bar.fontFamily
horizontalPadding: Style.spacing.controlPaddingX
verticalPadding: Style.spacing.controlPaddingY + Style.space(2)
bordered: true
active: root.chargeLimit.enabled === modelData.enabled
hasCursor: root.cursorActive && root.cursorRow === 1 && root.chargeLimitIndex === index
onClicked: root.setChargeLimit(modelData.enabled)
onHovered: function(h) {
if (h) {
root.cursorActive = true
root.cursorRow = 1
root.chargeLimitIndex = index
}
}
}
}
}
}
}
}
}
Expand Down
Loading