pragma ComponentBehavior: Bound import QtQuick import QtQuick.Layouts import ".." import "../../components" import "../../services" import "../../theme" // Session actions as round buttons, wrapped to the tile. When they do not fit // it becomes one power button that extends into a list. Confirmation for // destructive actions extends below the tile. CCModule { id: root readonly property var actions: [ { id: "lock", glyph: "󰌾", label: "Lock" }, { id: "logout", glyph: "󰍃", label: "Log out" }, { id: "suspend", glyph: "󰤄", label: "Suspend" }, { id: "reboot", glyph: "󰜉", label: "Reboot" }, { id: "shutdown", glyph: "󰐥", label: "Shut down" } ] readonly property real innerWidth: width - 20 readonly property real innerHeight: baseHeight - 12 readonly property int count: actions.length function perRowFor(slot) { return Math.max(1, Math.min(count, Math.floor((innerWidth + 4) / (slot + 4)))); } function heightFor(perRow, rowHeight) { return Math.ceil(count / perRow) * (rowHeight + 4) - 4; } // Labelled buttons where they fit on four-wide or taller tiles, else plain // 34 px buttons, else one row of smaller ones, else the menu button. readonly property bool labels: (cols >= 4 || rows >= 2) && heightFor(perRowFor(52), 49) <= innerHeight readonly property bool plain: !labels && heightFor(perRowFor(34), 34) <= innerHeight readonly property real squeezed: Math.min(innerHeight, Math.floor((innerWidth - 4 * (count - 1)) / count)) readonly property int perRow: labels ? perRowFor(52) : plain ? perRowFor(34) : count readonly property real buttonSize: labels || plain ? 34 : squeezed readonly property real buttonWidth: labels ? 52 : buttonSize readonly property bool fits: labels || plain || squeezed >= 24 readonly property bool pending: PowerService.pending !== "" round: !fits hovered: menuArea.containsMouse extraHeight: (pending || (expanded && !fits)) && extension.item ? extension.implicitHeight + 20 : 0 Component.onDestruction: PowerService.cancel() function allowed(id) { return (id === "lock" && ConfigService.ready && ConfigService.data.lock.enabled) || (!!SystemService.state.power && !!SystemService.state.power[id] && !SystemService.acting); } // Everything fits: a wrapped grid of buttons. GridLayout { visible: root.fits anchors.horizontalCenter: parent.horizontalCenter y: (root.baseHeight - height) / 2 columns: root.perRow rowSpacing: 4 columnSpacing: Math.max(4, (root.innerWidth - root.perRow * root.buttonWidth) / Math.max(1, root.perRow - 1) - 2) Repeater { model: root.actions ColumnLayout { id: action required property var modelData Layout.preferredWidth: root.buttonWidth spacing: 3 CCToggle { objectName: "power-" + action.modelData.id Layout.alignment: Qt.AlignHCenter size: root.buttonSize glyph: action.modelData.glyph fallback: action.modelData.label.charAt(0) checked: PowerService.pending === action.modelData.id enabled: root.allowed(action.modelData.id) onClicked: PowerService.request(action.modelData.id) Accessible.name: action.modelData.label } Text { visible: root.labels text: action.modelData.label color: Theme.textSecondary font.pixelSize: 10 elide: Text.ElideRight horizontalAlignment: Text.AlignHCenter Layout.fillWidth: true } } } } // Too small: one button that extends into the list. MouseArea { id: menuArea visible: !root.fits width: parent.width height: root.baseHeight hoverEnabled: true cursorShape: Qt.PointingHandCursor onClicked: root.expanded = !root.expanded Accessible.role: Accessible.Button Accessible.name: "Power" Column { anchors.centerIn: parent spacing: 2 Text { anchors.horizontalCenter: parent.horizontalCenter text: Icons.available ? "󰐥" : "⏻" font.family: Icons.available ? Icons.family : "" font.pixelSize: Math.min(26, root.innerHeight * 0.5) color: Theme.text } Text { visible: root.innerHeight >= 44 anchors.horizontalCenter: parent.horizontalCenter text: "Power" color: Theme.textSecondary font.pixelSize: 10 } } } Loader { id: extension x: 10 y: root.baseHeight + 6 width: root.width - 20 active: root.pending || (root.expanded && !root.fits) sourceComponent: root.pending ? confirm : menu } Component { id: menu ColumnLayout { spacing: 2 Repeater { model: root.actions Rectangle { id: item required property var modelData Layout.fillWidth: true implicitHeight: 30 radius: 8 enabled: root.allowed(modelData.id) opacity: enabled ? 1 : 0.45 color: itemArea.containsMouse ? Theme.hover : Theme.transparent RowLayout { anchors.fill: parent anchors.leftMargin: 8 anchors.rightMargin: 8 spacing: 8 Text { text: Icons.available ? item.modelData.glyph : "" font.family: Icons.family font.pixelSize: 14 color: Theme.text } Text { text: item.modelData.label color: Theme.text font.pixelSize: 12 elide: Text.ElideRight Layout.fillWidth: true } } MouseArea { id: itemArea objectName: "power-" + item.modelData.id anchors.fill: parent hoverEnabled: true cursorShape: Qt.PointingHandCursor onClicked: PowerService.request(item.modelData.id) } } } } } Component { id: confirm ColumnLayout { spacing: 8 Text { text: "Confirm " + PowerService.pending + "? Unsaved work may be lost." color: Theme.text font.pixelSize: 12 wrapMode: Text.Wrap Layout.fillWidth: true } RowLayout { Layout.alignment: Qt.AlignRight spacing: 8 GlassButton { objectName: "powerCancel" text: "Cancel" onClicked: PowerService.cancel() } GlassButton { objectName: "powerConfirm" text: "Confirm" accented: true enabled: !SystemService.acting onClicked: PowerService.confirm() } } } } }