diff --git a/src/etc/sleex/hyprland/rules.lua b/src/etc/sleex/hyprland/rules.lua index d7120786..cccd2e5e 100644 --- a/src/etc/sleex/hyprland/rules.lua +++ b/src/etc/sleex/hyprland/rules.lua @@ -151,6 +151,13 @@ hl.layer_rule({ match = { namespace = "quickshell:bar" }, animation = "slide", }) +-- The critical battery countdown border shares the top layer with the bar. +-- A higher order renders lower, so this keeps the border underneath the bar +-- and the island, which then always cover the screen edges they share. +hl.layer_rule({ + match = { namespace = "quickshell:criticalaction" }, + order = 1, +}) hl.layer_rule({ match = { namespace = "quickshell:cheatsheet" }, animation = "slide bottom", diff --git a/src/share/sleex/CriticalBatteryOverlay.qml b/src/share/sleex/CriticalBatteryOverlay.qml new file mode 100644 index 00000000..d9b2c26c --- /dev/null +++ b/src/share/sleex/CriticalBatteryOverlay.qml @@ -0,0 +1,124 @@ +import qs +import qs.modules.common +import qs.services +import SleexUiKit.Appearance +import QtQuick +import Quickshell +import Quickshell.Wayland +import QtQuick.Shapes + +Scope { + id: root + + // Fallbacks mirror the defaults in modules/common/Config.qml. + property int borderThick: Config.options.battery.criticalOverlayBorderThick ?? 5 + property int pathInset: Config.options.battery.criticalOverlayPathInset ?? 0 + property int cornerRadius: Config.options.battery.criticalOverlayCornerRadius ?? 25 + + Variants { + model: Quickshell.screens + + PanelWindow { + id: win + required property var modelData + screen: modelData + + readonly property real prog: Battery.criticalActionProgress + + visible: Battery.criticalActionWanted + color: "transparent" + + anchors { top: true; bottom: true; left: true; right: true } + exclusiveZone: -1 + WlrLayershell.layer: WlrLayer.Top + WlrLayershell.namespace: "quickshell:criticalaction" + WlrLayershell.keyboardFocus: WlrKeyboardFocus.None + mask: Region { } + + readonly property real halfThick: root.borderThick / 2 + readonly property real radius: root.cornerRadius + readonly property real lineLeft: root.pathInset + halfThick + readonly property real lineTop: root.pathInset + halfThick + readonly property real lineRight: win.width - root.pathInset - halfThick + readonly property real lineBottom: win.height - root.pathInset - halfThick + + component EdgePath: ShapePath { + strokeWidth: root.borderThick + strokeColor: Appearance.colors.colPrimary + fillColor: "transparent" + capStyle: ShapePath.RoundCap + joinStyle: ShapePath.MiterJoin + + PathMove { x: win.width / 2; y: win.lineTop } + PathLine { x: win.lineRight - win.radius; y: win.lineTop } + PathAngleArc { + centerX: win.lineRight - win.radius + centerY: win.lineTop + win.radius + radiusX: win.radius + radiusY: win.radius + startAngle: -90 + sweepAngle: 90 + } + PathLine { x: win.lineRight; y: win.lineBottom - win.radius } + PathAngleArc { + centerX: win.lineRight - win.radius + centerY: win.lineBottom - win.radius + radiusX: win.radius + radiusY: win.radius + startAngle: 0 + sweepAngle: 90 + } + PathLine { x: win.lineLeft + win.radius; y: win.lineBottom } + PathAngleArc { + centerX: win.lineLeft + win.radius + centerY: win.lineBottom - win.radius + radiusX: win.radius + radiusY: win.radius + startAngle: 90 + sweepAngle: 90 + } + PathLine { x: win.lineLeft; y: win.lineTop + win.radius } + PathAngleArc { + centerX: win.lineLeft + win.radius + centerY: win.lineTop + win.radius + radiusX: win.radius + radiusY: win.radius + startAngle: 180 + sweepAngle: 90 + } + PathLine { x: win.width / 2; y: win.lineTop } + } + + Shape { + id: trackShape + anchors.fill: parent + visible: Battery.criticalActionWanted + antialiasing: true + + // The curve renderer antialiases the strokes on the GPU, so the + // shapes need no offscreen buffer of their own. layer.enabled + // would allocate a full screen sized (and, with layer.samples, + // multisampled) texture for each of them. + preferredRendererType: Shape.CurveRenderer + opacity: 0.15 + EdgePath { } + } + Shape { + id: snakeShape + anchors.fill: parent + visible: Battery.criticalActionWanted + antialiasing: true + + preferredRendererType: Shape.CurveRenderer + EdgePath { + // ShapePath.trim is a property group (QQuickShapeTrim) that + // holds fractions of the path length, so the snake stroke + // grows along the border as the countdown runs. + trim.start: 0 + trim.end: Math.max(win.prog, 0.002) + } + } + } + } + +} diff --git a/src/share/sleex/modules/BatteryPopup/BatteryPopup.qml b/src/share/sleex/modules/BatteryPopup/BatteryPopup.qml index ba2ecbb2..e89d9b50 100644 --- a/src/share/sleex/modules/BatteryPopup/BatteryPopup.qml +++ b/src/share/sleex/modules/BatteryPopup/BatteryPopup.qml @@ -119,8 +119,15 @@ Scope { overlayLoader.loading = true if (Config.options.battery.sound) Audio.playSound("assets/sounds/battery/05_critical.wav") + } else { + root.isCritical = false + overlayLoader.active = false } } + + function onCriticalActionExecuted() { + overlayLoader.active = false + } } LazyLoader { @@ -245,6 +252,16 @@ Scope { font.pixelSize: Appearance.font.pixelSize.small color: Appearance.colors.colOnLayer1 } + + StyledText { + Layout.fillWidth: true + horizontalAlignment: Text.AlignHCenter + visible: Battery.criticalActionWanted + text: qsTr("%1 in %2s").arg(Battery.criticalActionLabel).arg(Battery.criticalActionRemaining) + font.pixelSize: Appearance.font.pixelSize.small + font.weight: Font.Medium + color: Appearance.colors.colError + } } Divider {} diff --git a/src/share/sleex/modules/bar/Bar.qml b/src/share/sleex/modules/bar/Bar.qml index 455eda09..9672383c 100644 --- a/src/share/sleex/modules/bar/Bar.qml +++ b/src/share/sleex/modules/bar/Bar.qml @@ -48,9 +48,6 @@ Scope { property ShellScreen modelData property bool centerPopupOpen: false property bool barRevealed: !Config.options.bar.autoHide || barHoverArea.containsMouse || barRoot.centerPopupOpen || GlobalStates.dashboardOpen - - readonly property bool criticalShift: Battery.criticalActionWanted && barRoot.barRevealed - readonly property int autoHideAnimMs: 400 property var brightnessMonitor: Brightness.getMonitorForScreen(modelData) property real useShortenedForm: (Appearance.sizes.barHellaShortenScreenWidthThreshold >= screen.width) ? 2 : @@ -76,8 +73,8 @@ Scope { WlrLayershell.namespace: "quickshell:bar" WlrLayershell.layer: WlrLayer.Top WlrLayershell.keyboardFocus: barRoot.centerPopupOpen ? WlrKeyboardFocus.Exclusive : WlrKeyboardFocus.None - implicitHeight: (barRoot.criticalShift ? 20 : 0) + barHeight + Appearance.rounding.screenRounding + centerPopup.popupHeight + Appearance.sizes.elevationMargin * 2 - exclusiveZone: (Config.options.bar.autoHide && !Config.options.bar.reserveSpace ? (barContent.visible ? barHeight : 0) : barHeight) + (barRoot.criticalShift ? 20 : 0) + implicitHeight: barHeight + Appearance.rounding.screenRounding + centerPopup.popupHeight + Appearance.sizes.elevationMargin * 2 + exclusiveZone: Config.options.bar.autoHide && !Config.options.bar.reserveSpace ? (barContent.visible ? barHeight : 0) : barHeight mask: Region { Region { item: Config.options.bar.autoHide ? barHoverArea : null @@ -136,8 +133,8 @@ Scope { left: parent.left top: !Config.options.bar.bottom ? parent.top : undefined bottom: Config.options.bar.bottom ? parent.bottom : undefined - topMargin: !Config.options.bar.bottom ? ((barContent.visible ? 0 : -barHeight) + (barRoot.criticalShift ? 20 : 0)) : 0 - bottomMargin: Config.options.bar.bottom ? ((barContent.visible ? 0 : -barHeight) + (barRoot.criticalShift ? 20 : 0)) : 0 + topMargin: !Config.options.bar.bottom ? (barContent.visible ? 0 : -barHeight) : 0 + bottomMargin: Config.options.bar.bottom ? (barContent.visible ? 0 : -barHeight) : 0 } Behavior on anchors.topMargin { diff --git a/src/share/sleex/modules/common/Config.qml b/src/share/sleex/modules/common/Config.qml index 111ba880..52bdd130 100644 --- a/src/share/sleex/modules/common/Config.qml +++ b/src/share/sleex/modules/common/Config.qml @@ -127,6 +127,12 @@ Singleton { property int suspend: 2 property bool sound: true //Added for Battery Sound Toggle property bool overlayEnabled: true + property bool criticalActionEnabled: false + property int criticalActionDelay: 30 + property string criticalAction: "suspend" + property int criticalOverlayBorderThick: 5 + property int criticalOverlayPathInset: 0 + property int criticalOverlayCornerRadius: 25 } property JsonObject bar: JsonObject { diff --git a/src/share/sleex/modules/settings/BehaviorConfig.qml b/src/share/sleex/modules/settings/BehaviorConfig.qml index 15199ca3..c5cd6e4b 100644 --- a/src/share/sleex/modules/settings/BehaviorConfig.qml +++ b/src/share/sleex/modules/settings/BehaviorConfig.qml @@ -131,6 +131,84 @@ ContentPage { } } } + ContentSubsection { + title: "Critical charge behavior" + tooltip: "On critical battery, run a screen-edge countdown, then the chosen action." + visible: UPower.displayDevice.isLaptopBattery + + ConfigSwitch { + text: "Enable critical charge behavior" + checked: Config.options.battery.criticalActionEnabled + onClicked: checked = !checked; + onCheckedChanged: { + Config.options.battery.criticalActionEnabled = checked; + } + } + ConfigSpinBox { + text: "Countdown (seconds)" + value: Config.options.battery.criticalActionDelay + from: 5 + to: 120 + stepSize: 5 + onValueChanged: { + Config.options.battery.criticalActionDelay = value; + } + } + ContentSubsectionLabel { + text: "Action on countdown end" + } + ConfigSelectionArray { + currentValue: Config.options.battery.criticalAction + configOptionName: "battery.criticalAction" + onSelected: (newValue) => { + Config.options.battery.criticalAction = newValue; + } + options: [ + {"value": "suspend", "displayName": "Suspend"}, + {"value": "hibernate", "displayName": "Hibernate"}, + {"value": "shutdown", "displayName": "Shut down"} + ] + } + ContentSubsectionLabel { + text: "Countdown appearance" + } + ConfigRow { + uniform: true + ConfigSpinBox { + text: "Border thickness" + value: Config.options.battery.criticalOverlayBorderThick + from: 2 + to: 40 + stepSize: 1 + onValueChanged: { + Config.options.battery.criticalOverlayBorderThick = value; + } + } + ConfigSpinBox { + text: "Path inset" + value: Config.options.battery.criticalOverlayPathInset + from: 0 + to: 40 + stepSize: 1 + onValueChanged: { + Config.options.battery.criticalOverlayPathInset = value; + } + } + } + ConfigRow { + uniform: true + ConfigSpinBox { + text: "Corner radius" + value: Config.options.battery.criticalOverlayCornerRadius + from: 0 + to: 60 + stepSize: 1 + onValueChanged: { + Config.options.battery.criticalOverlayCornerRadius = value; + } + } + } + } ContentSubsectionLabel { text: "Power profile" } diff --git a/src/share/sleex/plugins/src/Sleex/core/dpmsFade.cpp b/src/share/sleex/plugins/src/Sleex/core/dpmsFade.cpp index 0b1d0f5e..ee2442c4 100644 --- a/src/share/sleex/plugins/src/Sleex/core/dpmsFade.cpp +++ b/src/share/sleex/plugins/src/Sleex/core/dpmsFade.cpp @@ -80,6 +80,7 @@ void SuspendFader::releaseLock() { } void SuspendFader::onPrepareForSleep(bool sleeping) { + emit prepareForSleep(sleeping); if (sleeping) { std::system("wlr-dpms off"); std::this_thread::sleep_for(FADE_DURATION); @@ -88,4 +89,4 @@ void SuspendFader::onPrepareForSleep(bool sleeping) { std::system("wlr-dpms on"); acquireLock(); } -} \ No newline at end of file +} diff --git a/src/share/sleex/plugins/src/Sleex/core/dpmsFade.hpp b/src/share/sleex/plugins/src/Sleex/core/dpmsFade.hpp index c641809b..027e64f9 100644 --- a/src/share/sleex/plugins/src/Sleex/core/dpmsFade.hpp +++ b/src/share/sleex/plugins/src/Sleex/core/dpmsFade.hpp @@ -22,6 +22,9 @@ class SuspendFader : public QObject { Q_INVOKABLE void start(); Q_INVOKABLE void stop(); + // Emitted when the system is about to sleep (true) or has just woken up (false). + Q_SIGNAL void prepareForSleep(bool sleeping); + private: void acquireLock(); void releaseLock(); @@ -32,4 +35,4 @@ class SuspendFader : public QObject { sdbus::ServiceName destination; sdbus::ObjectPath objectPath; int lock_fd_{-1}; -}; \ No newline at end of file +}; diff --git a/src/share/sleex/services/Battery.qml b/src/share/sleex/services/Battery.qml index f7149204..2fc50dab 100644 --- a/src/share/sleex/services/Battery.qml +++ b/src/share/sleex/services/Battery.qml @@ -6,6 +6,7 @@ import Quickshell import Quickshell.Services.UPower import QtQuick import Quickshell.Io +import Sleex.Core Singleton { id: batteryService @@ -30,6 +31,79 @@ Singleton { property bool isLowAndNotCharging: isLow && !isCharging property bool isCriticalAndNotCharging: isCritical && !isCharging + property bool criticalActionWanted: available && Config.options.battery.criticalActionEnabled + && isCriticalAndNotCharging && !_criticalActionFired + property real criticalActionProgress: 0 + property bool _criticalActionFired: false + + // The remaining seconds of the countdown. The progress is animated every + // frame, so this rounds it down to whole seconds: the value only changes + // once a second, which keeps anything bound to it from being rebuilt on + // every frame of the animation. + readonly property int criticalActionRemaining: Math.max(0, Math.ceil((1 - criticalActionProgress) * Config.options.battery.criticalActionDelay)) + + readonly property string criticalActionLabel: { + switch (Config.options.battery.criticalAction) { + case "suspend": return qsTr("Suspend") + case "hibernate": return qsTr("Hibernate") + case "shutdown": return qsTr("Shut down") + default: return qsTr("Unknown") + } + } + + onCriticalActionWantedChanged: { + if (criticalActionWanted) { + criticalActionProgress = 0 + criticalActionAnim.restart() + } else { + criticalActionAnim.stop() + criticalActionProgress = 0 + } + } + + NumberAnimation { + id: criticalActionAnim + target: batteryService + property: "criticalActionProgress" + from: 0 + to: 1 + duration: Config.options.battery.criticalActionDelay * 1000 + easing.type: Easing.Linear + onFinished: { + if (batteryService.criticalActionWanted) + batteryService.runCriticalAction() + } + } + + signal criticalActionExecuted() + + function runCriticalAction() { + _criticalActionFired = true + criticalActionExecuted() + const action = Config.options.battery.criticalAction + if (action === "shutdown") { + PowerActions.poweroff() + } else if (action === "hibernate") { + PowerActions.hibernate() + } else { + PowerActions.suspend() + } + } + + // The critical action only fires once per critical discharge: the flag is + // cleared as soon as the battery is no longer critical *and* discharging, + // so plugging the charger in (and unplugging it again) re-arms the action. + Connections { + target: SuspendFader + + function onPrepareForSleep(sleeping) { + // Woke up while still critical: re-arm the action so the warning + // and the countdown are shown again. + if (!sleeping && batteryService.isCriticalAndNotCharging) + batteryService._criticalActionFired = false + } + } + property real energyRate: UPower.displayDevice.changeRate property real timeToFull: UPower.displayDevice.timeToFull @@ -70,6 +144,7 @@ Singleton { } onIsCriticalAndNotChargingChanged: { + if (!isCriticalAndNotCharging) _criticalActionFired = false if (!available) return if (isCriticalAndNotCharging && !Config.options.battery.overlayEnabled) { if (Config.options.battery.sound) diff --git a/src/share/sleex/shell.qml b/src/share/sleex/shell.qml index 4a7825f1..18ecfc8c 100644 --- a/src/share/sleex/shell.qml +++ b/src/share/sleex/shell.qml @@ -45,6 +45,8 @@ ShellRoot { property bool enableOverview: true property bool enablePolkit: true property bool enableReloadPopup: true + property bool enableBatteryPopup: true + property bool enableCriticalBatteryOverlay: true property bool enableScreenCorners: false property bool enableSession: true property bool enableDashboard: true @@ -73,7 +75,10 @@ ShellRoot { LazyLoader { active: enableOverview; component: Overview {} } LazyLoader { active: enablePolkit; component: Polkit {} } LazyLoader { active: enableReloadPopup; component: ReloadPopup {} } - LazyLoader { active: true; component: BatteryPopup {} } + LazyLoader { active: enableBatteryPopup; component: BatteryPopup {} } + // The countdown border is only of any use while the countdown runs, so it + // is not loaded - and its layer surfaces are not created - before that. + LazyLoader { active: enableCriticalBatteryOverlay && Battery.criticalActionWanted; component: CriticalBatteryOverlay {} } LazyLoader { active: enableScreenCorners; component: ScreenCorners {} } LazyLoader { active: enableSession; component: Session {} } LazyLoader { active: enableDashboard; component: Dashboard {} }