1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2026-01-24 13:32:50 -05:00

Add hibernate support to power menus

This commit is contained in:
bbedward
2025-09-21 22:06:12 -04:00
parent a3b3e49313
commit 66b493fb2b
6 changed files with 180 additions and 45 deletions

View File

@@ -1,45 +1,51 @@
import QtQuick import QtQuick
import qs.Common import qs.Common
import qs.Modals.Common import qs.Modals.Common
import qs.Services
import qs.Widgets import qs.Widgets
DankModal { DankModal {
id: root id: root
property int selectedIndex: 0 property int selectedIndex: 0
property int optionCount: 4 property int optionCount: SessionService.hibernateSupported ? 5 : 4
signal powerActionRequested(string action, string title, string message) signal powerActionRequested(string action, string title, string message)
function selectOption() { function selectOption(action) {
close(); close();
const actions = [{ const actions = {
"action": "logout", "logout": {
"title": "Log Out", "title": "Log Out",
"message": "Are you sure you want to log out?" "message": "Are you sure you want to log out?"
}, { },
"action": "suspend", "suspend": {
"title": "Suspend", "title": "Suspend",
"message": "Are you sure you want to suspend the system?" "message": "Are you sure you want to suspend the system?"
}, { },
"action": "reboot", "hibernate": {
"title": "Reboot", "title": "Hibernate",
"message": "Are you sure you want to reboot the system?" "message": "Are you sure you want to hibernate the system?"
}, { },
"action": "poweroff", "reboot": {
"title": "Power Off", "title": "Reboot",
"message": "Are you sure you want to power off the system?" "message": "Are you sure you want to reboot the system?"
}]; },
const selected = actions[selectedIndex]; "poweroff": {
"title": "Power Off",
"message": "Are you sure you want to power off the system?"
}
}
const selected = actions[action]
if (selected) { if (selected) {
root.powerActionRequested(selected.action, selected.title, selected.message); root.powerActionRequested(action, selected.title, selected.message);
} }
} }
shouldBeVisible: false shouldBeVisible: false
width: 320 width: 320
height: 300 height: contentLoader.item ? contentLoader.item.implicitHeight : 300
enableShadow: true enableShadow: true
onBackgroundClicked: () => { onBackgroundClicked: () => {
return close(); return close();
@@ -64,7 +70,12 @@ DankModal {
break; break;
case Qt.Key_Return: case Qt.Key_Return:
case Qt.Key_Enter: case Qt.Key_Enter:
selectOption(); const actions = ["logout", "suspend"];
if (SessionService.hibernateSupported) actions.push("hibernate");
actions.push("reboot", "poweroff");
if (selectedIndex < actions.length) {
selectOption(actions[selectedIndex]);
}
event.accepted = true; event.accepted = true;
break; break;
} }
@@ -73,8 +84,10 @@ DankModal {
content: Component { content: Component {
Item { Item {
anchors.fill: parent anchors.fill: parent
implicitHeight: mainColumn.implicitHeight + Theme.spacingL * 2
Column { Column {
id: mainColumn
anchors.fill: parent anchors.fill: parent
anchors.margins: Theme.spacingL anchors.margins: Theme.spacingL
spacing: Theme.spacingM spacing: Theme.spacingM
@@ -157,7 +170,7 @@ DankModal {
cursorShape: Qt.PointingHandCursor cursorShape: Qt.PointingHandCursor
onClicked: () => { onClicked: () => {
selectedIndex = 0; selectedIndex = 0;
selectOption(); selectOption("logout");
} }
} }
@@ -210,7 +223,7 @@ DankModal {
cursorShape: Qt.PointingHandCursor cursorShape: Qt.PointingHandCursor
onClicked: () => { onClicked: () => {
selectedIndex = 1; selectedIndex = 1;
selectOption(); selectOption("suspend");
} }
} }
@@ -222,15 +235,70 @@ DankModal {
radius: Theme.cornerRadius radius: Theme.cornerRadius
color: { color: {
if (selectedIndex === 2) { if (selectedIndex === 2) {
return Qt.rgba(Theme.warning.r, Theme.warning.g, Theme.warning.b, 0.12); return Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.12);
} else if (rebootArea.containsMouse) { } else if (hibernateArea.containsMouse) {
return Qt.rgba(Theme.warning.r, Theme.warning.g, Theme.warning.b, 0.08); return Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.08);
} else { } else {
return Qt.rgba(Theme.surfaceVariant.r, Theme.surfaceVariant.g, Theme.surfaceVariant.b, 0.08); return Qt.rgba(Theme.surfaceVariant.r, Theme.surfaceVariant.g, Theme.surfaceVariant.b, 0.08);
} }
} }
border.color: selectedIndex === 2 ? Theme.warning : "transparent" border.color: selectedIndex === 2 ? Theme.primary : "transparent"
border.width: selectedIndex === 2 ? 1 : 0 border.width: selectedIndex === 2 ? 1 : 0
visible: SessionService.hibernateSupported
Row {
anchors.left: parent.left
anchors.leftMargin: Theme.spacingM
anchors.verticalCenter: parent.verticalCenter
spacing: Theme.spacingM
DankIcon {
name: "ac_unit"
size: Theme.iconSize
color: Theme.surfaceText
anchors.verticalCenter: parent.verticalCenter
}
StyledText {
text: "Hibernate"
font.pixelSize: Theme.fontSizeMedium
color: Theme.surfaceText
font.weight: Font.Medium
anchors.verticalCenter: parent.verticalCenter
}
}
MouseArea {
id: hibernateArea
anchors.fill: parent
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
onClicked: () => {
selectedIndex = 2;
selectOption("hibernate");
}
}
}
Rectangle {
width: parent.width
height: 50
radius: Theme.cornerRadius
color: {
const rebootIndex = SessionService.hibernateSupported ? 3 : 2;
if (selectedIndex === rebootIndex) {
return Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.12);
} else if (rebootArea.containsMouse) {
return Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.08);
} else {
return Qt.rgba(Theme.surfaceVariant.r, Theme.surfaceVariant.g, Theme.surfaceVariant.b, 0.08);
}
}
border.color: selectedIndex === (SessionService.hibernateSupported ? 3 : 2) ? Theme.primary : "transparent"
border.width: selectedIndex === (SessionService.hibernateSupported ? 3 : 2) ? 1 : 0
Row { Row {
anchors.left: parent.left anchors.left: parent.left
@@ -262,8 +330,8 @@ DankModal {
hoverEnabled: true hoverEnabled: true
cursorShape: Qt.PointingHandCursor cursorShape: Qt.PointingHandCursor
onClicked: () => { onClicked: () => {
selectedIndex = 2; selectedIndex = SessionService.hibernateSupported ? 3 : 2;
selectOption(); selectOption("reboot");
} }
} }
@@ -274,16 +342,17 @@ DankModal {
height: 50 height: 50
radius: Theme.cornerRadius radius: Theme.cornerRadius
color: { color: {
if (selectedIndex === 3) { const powerOffIndex = SessionService.hibernateSupported ? 4 : 3;
return Qt.rgba(Theme.error.r, Theme.error.g, Theme.error.b, 0.12); if (selectedIndex === powerOffIndex) {
return Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.12);
} else if (powerOffArea.containsMouse) { } else if (powerOffArea.containsMouse) {
return Qt.rgba(Theme.error.r, Theme.error.g, Theme.error.b, 0.08); return Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.08);
} else { } else {
return Qt.rgba(Theme.surfaceVariant.r, Theme.surfaceVariant.g, Theme.surfaceVariant.b, 0.08); return Qt.rgba(Theme.surfaceVariant.r, Theme.surfaceVariant.g, Theme.surfaceVariant.b, 0.08);
} }
} }
border.color: selectedIndex === 3 ? Theme.error : "transparent" border.color: selectedIndex === (SessionService.hibernateSupported ? 4 : 3) ? Theme.primary : "transparent"
border.width: selectedIndex === 3 ? 1 : 0 border.width: selectedIndex === (SessionService.hibernateSupported ? 4 : 3) ? 1 : 0
Row { Row {
anchors.left: parent.left anchors.left: parent.left
@@ -315,8 +384,8 @@ DankModal {
hoverEnabled: true hoverEnabled: true
cursorShape: Qt.PointingHandCursor cursorShape: Qt.PointingHandCursor
onClicked: () => { onClicked: () => {
selectedIndex = 3; selectedIndex = SessionService.hibernateSupported ? 4 : 3;
selectOption(); selectOption("poweroff");
} }
} }

View File

@@ -192,8 +192,8 @@ Item {
width: parent.width width: parent.width
text: "Hibernate system after" text: "Hibernate system after"
description: "Requires swap space or a hibernation file"
options: timeoutOptions options: timeoutOptions
visible: SessionService.hibernateSupported
Connections { Connections {
target: powerCategory target: powerCategory

View File

@@ -377,11 +377,11 @@ DankPopout {
Row { Row {
anchors.centerIn: parent anchors.centerIn: parent
spacing: Theme.spacingL spacing: SessionService.hibernateSupported ? Theme.spacingS : Theme.spacingL
visible: root.powerOptionsExpanded visible: root.powerOptionsExpanded
Rectangle { Rectangle {
width: 100 width: SessionService.hibernateSupported ? 85 : 100
height: 34 height: 34
radius: Theme.cornerRadius radius: Theme.cornerRadius
color: logoutButton.containsMouse ? Qt.rgba( color: logoutButton.containsMouse ? Qt.rgba(
@@ -431,7 +431,7 @@ DankPopout {
} }
Rectangle { Rectangle {
width: 100 width: SessionService.hibernateSupported ? 85 : 100
height: 34 height: 34
radius: Theme.cornerRadius radius: Theme.cornerRadius
color: rebootButton.containsMouse ? Qt.rgba( color: rebootButton.containsMouse ? Qt.rgba(
@@ -481,7 +481,7 @@ DankPopout {
} }
Rectangle { Rectangle {
width: 100 width: SessionService.hibernateSupported ? 85 : 100
height: 34 height: 34
radius: Theme.cornerRadius radius: Theme.cornerRadius
color: suspendButton.containsMouse ? Qt.rgba( color: suspendButton.containsMouse ? Qt.rgba(
@@ -531,7 +531,58 @@ DankPopout {
} }
Rectangle { Rectangle {
width: 100 width: SessionService.hibernateSupported ? 85 : 100
height: 34
radius: Theme.cornerRadius
color: hibernateButton.containsMouse ? Qt.rgba(
Theme.primary.r,
Theme.primary.g,
Theme.primary.b,
0.12) : Qt.rgba(
Theme.surfaceVariant.r,
Theme.surfaceVariant.g,
Theme.surfaceVariant.b,
0.5)
visible: SessionService.hibernateSupported
Row {
anchors.centerIn: parent
spacing: Theme.spacingXS
DankIcon {
name: "ac_unit"
size: Theme.fontSizeSmall
color: hibernateButton.containsMouse ? Theme.primary : Theme.surfaceText
anchors.verticalCenter: parent.verticalCenter
}
StyledText {
text: "Hibernate"
font.pixelSize: Theme.fontSizeSmall
color: hibernateButton.containsMouse ? Theme.primary : Theme.surfaceText
font.weight: Font.Medium
anchors.verticalCenter: parent.verticalCenter
}
}
MouseArea {
id: hibernateButton
anchors.fill: parent
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
onPressed: {
root.powerOptionsExpanded = false
root.close()
root.powerActionRequested(
"hibernate", "Hibernate",
"Are you sure you want to hibernate?")
}
}
}
Rectangle {
width: SessionService.hibernateSupported ? 85 : 100
height: 34 height: 34
radius: Theme.cornerRadius radius: Theme.cornerRadius
color: shutdownButton.containsMouse ? Qt.rgba( color: shutdownButton.containsMouse ? Qt.rgba(

View File

@@ -109,7 +109,7 @@ Singleton {
}) })
} }
if (hibernateTimeout > 0) { if (hibernateTimeout > 0 && SessionService.hibernateSupported) {
hibernateMonitor = Qt.createQmlObject(qmlString, root, "IdleService.HibernateMonitor") hibernateMonitor = Qt.createQmlObject(qmlString, root, "IdleService.HibernateMonitor")
hibernateMonitor.enabled = Qt.binding(() => root._enableGate && root.enabled && root.idleMonitorAvailable && root.hibernateTimeout > 0) hibernateMonitor.enabled = Qt.binding(() => root._enableGate && root.enabled && root.idleMonitorAvailable && root.hibernateTimeout > 0)
hibernateMonitor.respectInhibitors = Qt.binding(() => root.respectInhibitors) hibernateMonitor.respectInhibitors = Qt.binding(() => root.respectInhibitors)

View File

@@ -13,12 +13,14 @@ Singleton {
property bool hasUwsm: false property bool hasUwsm: false
property bool isElogind: false property bool isElogind: false
property bool hibernateSupported: false
property bool inhibitorAvailable: true property bool inhibitorAvailable: true
property bool idleInhibited: false property bool idleInhibited: false
property string inhibitReason: "Keep system awake" property string inhibitReason: "Keep system awake"
Component.onCompleted: { Component.onCompleted: {
detectElogindProcess.running = true detectElogindProcess.running = true
detectHibernateProcess.running = true
} }
@@ -43,6 +45,16 @@ Singleton {
} }
} }
Process {
id: detectHibernateProcess
running: false
command: ["grep", "-q", "disk", "/sys/power/state"]
onExited: function (exitCode) {
hibernateSupported = (exitCode === 0)
}
}
Process { Process {
id: uwsmLogout id: uwsmLogout
command: ["uwsm", "stop"] command: ["uwsm", "stop"]

View File

@@ -203,6 +203,9 @@ ShellRoot {
case "suspend": case "suspend":
SessionService.suspend() SessionService.suspend()
break break
case "hibernate":
SessionService.hibernate()
break
case "reboot": case "reboot":
SessionService.reboot() SessionService.reboot()
break break