Skip to content
Merged
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
2 changes: 1 addition & 1 deletion packages/omamobile-shell/plugins/omamobile.bar/Bar.qml
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ Item {
readonly property var battery: UPower.displayDevice
readonly property bool hasBattery: !!battery && battery.isPresent === true
readonly property int batteryPercent: hasBattery ? Math.round(battery.percentage * 100) : -1
readonly property bool charging: hasBattery && !UPower.onBattery
readonly property bool charging: hasBattery && battery.state === UPowerDeviceState.Charging
readonly property string batteryIcon: StatusModel.batteryIcon(batteryPercent, charging,
hasBattery && battery.state === UPowerDeviceState.FullyCharged)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function unreadCount(names, seen) {

function batteryIcon(percent, charging, full) {
if (percent < 0) return ""
if (full) return "󰂅"
if (full) return "󰁹"
var chargingIcons = ["󰢜", "󰂆", "󰂇", "󰂈", "󰢝", "󰂉", "󰢞", "󰂊", "󰂋", "󰂅"]
var defaultIcons = ["󰁺", "󰁻", "󰁼", "󰁽", "󰁾", "󰁿", "󰂀", "󰂁", "󰂂", "󰁹"]
var index = Math.max(0, Math.min(9, Math.floor(percent / 10)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ Item {
readonly property var battery: UPower.displayDevice
readonly property bool present: !!battery && battery.isPresent === true
readonly property int percent: present ? Math.round(battery.percentage * 100) : -1
readonly property bool charging: present && !UPower.onBattery
readonly property bool charging: present && battery.state === UPowerDeviceState.Charging
readonly property bool discharging: present && battery.state === UPowerDeviceState.Discharging
readonly property bool full: present && battery.state === UPowerDeviceState.FullyCharged
readonly property real seconds: !present ? 0 : (charging ? battery.timeToFull : battery.timeToEmpty)
readonly property real seconds: charging ? battery.timeToFull : discharging ? battery.timeToEmpty : 0

function duration(s) {
if (!s || s <= 0) return ""
Expand All @@ -29,7 +30,11 @@ Item {
readonly property string detail: !present ? "No battery"
: full ? "Full"
: charging ? (duration(seconds) ? "Full in " + duration(seconds) : "Charging")
: (duration(seconds) ? duration(seconds) + " left" : "On battery")
: discharging ? (duration(seconds) ? duration(seconds) + " left" : "On battery")
: battery.state === UPowerDeviceState.PendingCharge ? "Waiting to charge"
: battery.state === UPowerDeviceState.PendingDischarge ? "Waiting to discharge"
: battery.state === UPowerDeviceState.Empty ? "Empty"
: "Charging status unknown"

readonly property color levelColor: percent >= 0 && percent <= 15 && !charging ? Commons.Color.urgent : Commons.Color.accent
readonly property bool compact: width < 100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1557,7 +1557,7 @@ Item {
anchors.verticalCenter: parent.verticalCenter
textFormat: Text.PlainText
text: root.hasBattery
? (UPower.onBattery ? "󰁹 " : "󰂄 ") + Math.round(root.battery.percentage * 100) + "%"
? (root.battery.state === UPowerDeviceState.Charging ? "󰂄 " : "󰁹 ") + Math.round(root.battery.percentage * 100) + "%"
: ""
color: root.foreground
font.family: Style.font.family
Expand Down
1 change: 1 addition & 0 deletions packages/omamobile-shell/test/all
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,7 @@ if [[ -n ${OMAMOBILE_CALL_RENDER:-} ]]; then
fi
if command -v node >/dev/null; then
node test/status-model.js >/dev/null || { node test/status-model.js; fail "status bar model"; }
node test/battery-state.js || fail "battery state bindings"
fi
python3 -B -c 'import ast,sys; ast.parse(open(sys.argv[1]).read())' bin/omamobile-cell-watch || fail "omamobile-cell-watch parses"
if command -v dbus-run-session >/dev/null; then
Expand Down
68 changes: 68 additions & 0 deletions packages/omamobile-shell/test/battery-state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// SPDX-License-Identifier: MIT
// Exercise the actual QML JavaScript bindings with representative UPower states.
const assert = require('assert')
const fs = require('fs')
const vm = require('vm')
const path = require('path')

const root = process.argv[2] || path.join(__dirname, '..')
const read = (name) => fs.readFileSync(path.join(root, 'plugins', name), 'utf8')
const home = read('omamobile.home/BatteryWidget.qml')
const bar = read('omamobile.bar/Bar.qml')
const quick = read('omamobile.quicksettings/QuickSettings.qml')
const statusModel = require(path.resolve(root, 'plugins/omamobile.bar/StatusModel.js'))
assert.strictEqual(statusModel.batteryIcon(91, false, true), '󰁹',
'Full below 100% must use the full battery glyph without a charging bolt')
assert.notStrictEqual(statusModel.batteryIcon(91, true, false), '󰁹',
'Charging must retain its charging glyph')
function capture(text, pattern, name) {
const match = text.match(pattern)
assert(match, `could not find ${name} binding`)
return match[1]
}
const binding = (text, name) => capture(text, new RegExp(`readonly property (?:bool|real) ${name}: ([^\\n]+)`), name)
const S = { Unknown: 0, Charging: 1, Discharging: 2, Empty: 3, FullyCharged: 4, PendingCharge: 5, PendingDischarge: 6 }
const evaluate = (expr, ctx) => vm.runInNewContext(expr, ctx)
const barCharging = binding(bar, 'charging')
assert.strictEqual(evaluate(barCharging, { hasBattery: true, battery: { state: S.Unknown },
UPower: { onBattery: false }, UPowerDeviceState: S }), false, 'unknown state must not show charging')
const homeCharging = binding(home, 'charging')
const homeDischarging = binding(home, 'discharging')
const homeSeconds = binding(home, 'seconds')
const homeDetail = capture(home, /readonly property string detail: ([\s\S]*?)\n\n/, 'battery detail')
const duration = capture(home, /(function duration\(s\) \{[\s\S]*?\n \})/, 'duration')
const quickText = capture(quick, /text: (root\.hasBattery\s*\n\s*\?[^\n]+\n\s*: "")/, 'quick settings battery text')

function state(state, present = true) {
const battery = present ? { state, percentage: .5, timeToFull: 3600, timeToEmpty: 7200 } : null
// External power can be present even when the device does not report charging.
const ctx = { battery, present, hasBattery: present, UPowerDeviceState: S, UPower: { onBattery: false } }
ctx.charging = evaluate(homeCharging, ctx)
ctx.discharging = evaluate(homeDischarging, ctx)
ctx.full = present && state === S.FullyCharged
ctx.seconds = evaluate(homeSeconds, ctx)
ctx.duration = evaluate('(' + duration + ')', ctx)
return {
homeCharging: ctx.charging,
barCharging: evaluate(barCharging, ctx),
text: evaluate(quickText, { ...ctx, root: { hasBattery: present, battery } }),
detail: evaluate(homeDetail, ctx),
}
}

for (const [value, text] of [[S.Unknown, 'Charging status unknown'], [S.PendingCharge, 'Waiting to charge'],
[S.PendingDischarge, 'Waiting to discharge'], [S.Empty, 'Empty'], [S.FullyCharged, 'Full']]) {
const got = state(value)
assert.strictEqual(got.homeCharging, false)
assert.strictEqual(got.barCharging, false)
assert.strictEqual(got.text, '󰁹 50%')
assert.strictEqual(got.detail, text)
}
assert.strictEqual(state(S.Charging).detail, 'Full in 1 h 0 min')
assert.strictEqual(state(S.Charging).homeCharging, true)
assert.strictEqual(state(S.Charging).text, '󰂄 50%')
assert.strictEqual(state(S.Charging).barCharging, true)
assert.strictEqual(state(S.Discharging).detail, '2 h 0 min left')
assert.strictEqual(state(S.Unknown, false).detail, 'No battery')
assert.strictEqual(state(S.Unknown, false).text, '')
console.log('ok - explicit battery states in home, bar and quick settings')
2 changes: 1 addition & 1 deletion packages/omamobile-shell/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.101
0.2.104