Files
lucid/luciddocks/ContextMenu.qml
T

264 lines
8.4 KiB
QML

import QtQuick
import Quickshell
import Quickshell.Hyprland
import qs
// the dock's right-click menu. it draws inside a layer window of its own, not a
// popup, because hyprland only frosts layer surfaces
Item {
id: menu
// the layer window it lives in, for the focus grab
property var grabWindow: null
// where the dock's own surface starts inside this one, so the dock's local
// coordinates can be used unchanged
property real originX: 0
property real originY: 0
readonly property real cardW: Theme.dp(210)
readonly property real cardH: list.implicitHeight + Theme.dp(16)
readonly property real cardX: Math.max(Theme.dp(8), Math.min(menu.width - menu.cardW - Theme.dp(8), menu.originX + menu.anchorLocalX - menu.cardW / 2))
readonly property real cardY: Math.max(Theme.dp(8), menu.originY + menu.anchorLocalY - menu.cardH - Theme.dp(12))
readonly property bool showing: menu.menuVisible || fadeAnim.running
property bool menuVisible: false
property string appId: ""
property string command: ""
property bool isPinned: false
property int windowCount: 0
property var hostWindow: null
property real anchorLocalX: 0
property real anchorLocalY: 0
readonly property var actions: {
var arr = [];
arr.push(menu.isPinned ? {
"id": "unpin",
"label": "Unpin from Dock",
"glyph": DockIcons.unpin,
"danger": false,
"divider": false
} : {
"id": "pin",
"label": "Pin to Dock",
"glyph": DockIcons.pin,
"danger": false,
"divider": false
});
if (menu.command !== "")
arr.push({
"id": "newWindow",
"label": "New Window",
"glyph": DockIcons.newWindow,
"danger": false,
"divider": false
});
if (menu.windowCount > 0)
arr.push({
"id": "close",
"label": "Close Window",
"glyph": DockIcons.closeWindow,
"danger": true,
"divider": true
});
if (menu.windowCount > 1)
arr.push({
"id": "closeAll",
"label": "Close All (" + menu.windowCount + ")",
"glyph": DockIcons.closeWindow,
"danger": true,
"divider": false
});
return arr;
}
property int fadeDuration: Theme.durEnter
property var fadeEasing: Theme.easeEmphasizedDecel
signal actionChosen(string id)
function openFor(item, appId, isPinned, command, windowCount) {
if (menu.menuVisible && menu.appId.toLowerCase() === appId.toLowerCase()) {
menu.menuVisible = false;
return ;
}
var localPos = item.mapToItem(null, 0, 0);
menu.appId = appId;
menu.command = command;
menu.isPinned = isPinned;
menu.windowCount = windowCount;
menu.anchorLocalX = localPos.x + item.width / 2;
menu.anchorLocalY = localPos.y;
menu.menuVisible = true;
}
onMenuVisibleChanged: {
menu.fadeDuration = menu.menuVisible ? Theme.durEnter : Theme.durExit;
menu.fadeEasing = menu.menuVisible ? Theme.easeEmphasizedDecel : Theme.easeEmphasizedAccel;
if (menu.menuVisible)
cardJelly.kickAfter(0.1, Math.round(Theme.durEnter * 0.3));
else
cardJelly.rest();
}
// the card as painted, jelly and all, for the overlay's blur
readonly property real paintX: menu.cardX + cardJelly.shiftX
readonly property real paintY: menu.cardY + cardJelly.shiftY
readonly property real paintW: menu.cardW * cardJelly.xScale
readonly property real paintH: menu.cardH * cardJelly.yScale
HyprlandFocusGrab {
windows: menu.grabWindow ? [menu.grabWindow] : []
active: menu.menuVisible
onActiveChanged: {
if (!active)
menu.menuVisible = false;
}
}
Rectangle {
id: contentRoot
x: menu.cardX
y: menu.cardY
width: menu.cardW
height: menu.cardH
radius: Theme.radiusMd
color: Theme.bg
opacity: menu.menuVisible ? 1 : 0
scale: menu.menuVisible ? 1 : 0.9
transformOrigin: Item.Bottom
readonly property bool jellyWobbling: cardJelly.wobbling
transform: Jelly {
id: cardJelly
target: contentRoot
anchorY: 1
frequency: 4.4
}
Column {
id: list
anchors.fill: parent
anchors.margins: Theme.dp(8)
spacing: Theme.dp(2)
Repeater {
model: menu.actions
delegate: Item {
id: row
required property var modelData
readonly property bool hovered: hoverHandler.hovered
readonly property bool pressed: tapHandler.pressed
readonly property color tone: row.modelData.danger ? Theme.error : Theme.text
width: list.width
height: Theme.dp(40) + (row.modelData.divider ? Theme.dp(9) : 0)
Rectangle {
id: divider
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
anchors.leftMargin: Theme.dp(8)
anchors.rightMargin: Theme.dp(8)
anchors.topMargin: Theme.dp(4)
height: 1
color: Theme.outline
visible: row.modelData.divider
}
Item {
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
height: Theme.dp(40)
Rectangle {
anchors.fill: parent
radius: Theme.radiusXs
color: row.tone
opacity: row.pressed ? Theme.statePressed : (row.hovered ? Theme.stateHover : 0)
Behavior on opacity {
NumberAnimation {
duration: Theme.durQuick
}
}
}
DockGlyph {
id: rowGlyph
anchors.left: parent.left
anchors.leftMargin: Theme.dp(10)
anchors.verticalCenter: parent.verticalCenter
width: Theme.dp(18)
height: Theme.dp(18)
pathData: row.modelData.glyph
glyphColor: row.modelData.danger ? Theme.error : Theme.subtext
}
Text {
anchors.verticalCenter: parent.verticalCenter
anchors.left: rowGlyph.right
anchors.leftMargin: Theme.dp(12)
text: row.modelData.label
color: row.tone
font.family: Theme.fontFamily
font.pixelSize: Theme.fontBody
font.variableAxes: Theme.axes(Theme.fontBody, 520, 0)
font.weight: Font.Medium
}
}
HoverHandler {
id: hoverHandler
}
TapHandler {
id: tapHandler
onTapped: {
menu.menuVisible = false;
menu.actionChosen(row.modelData.id);
}
}
}
}
}
Behavior on opacity {
NumberAnimation {
id: fadeAnim
duration: menu.fadeDuration
easing.type: Easing.Bezier
easing.bezierCurve: menu.fadeEasing
}
}
Behavior on scale {
NumberAnimation {
duration: menu.fadeDuration
easing.type: Easing.Bezier
easing.bezierCurve: menu.fadeEasing
}
}
}
}