Files
skygqts/workspace-manager/GotoView.qml
T
2026-09-23 11:24:58 -04:00

488 lines
17 KiB
QML

import QtQuick
import Quickshell
import Quickshell.Wayland
PanelWindow {
id: root
required property bool shellOpen
required property Compositor compositor
required property Theme theme
// Enter, or a click on a card: the workspace was chosen.
signal requestClose
// Escape, or a click on the backdrop: leave without switching.
signal requestBack
visible: root.shellOpen
color: "transparent"
exclusionMode: ExclusionMode.Ignore
WlrLayershell.namespace: "quickshell:workspace-manager-goto"
WlrLayershell.layer: WlrLayer.Overlay
WlrLayershell.keyboardFocus: root.visible ? WlrKeyboardFocus.Exclusive : WlrKeyboardFocus.None
anchors {
top: true
left: true
right: true
bottom: true
}
property int monitorIndex: 0
// One selected workspace index per monitor. Reassigned as a whole so
// the carousel notices the change. Parallel to compositor.monitors.
property var workspaceIndices: []
property bool seedPending: false
property bool animate: false
// Whether the name field (rename mode) currently has focus.
property bool nameFieldActive: false
function workspaceIndexAt(index: int): int {
const list = root.workspaceIndices;
if (!list || index < 0 || index >= list.length)
return 0;
const value = list[index];
if (value === undefined || value === null)
return 0;
return value;
}
function seedSelection(): void {
const mons = root.compositor.monitors ?? [];
const indices = [];
let focusedMonitor = 0;
for (let i = 0; i < mons.length; i++) {
const workspaces = mons[i].workspaces ?? [];
let activeIndex = 0;
let focusedIndex = -1;
for (let j = 0; j < workspaces.length; j++) {
if (workspaces[j].active)
activeIndex = j;
if (workspaces[j].focused) {
focusedIndex = j;
focusedMonitor = i;
}
}
indices.push(focusedIndex >= 0 ? focusedIndex : activeIndex);
}
root.monitorIndex = mons.length === 0 ? 0 : focusedMonitor;
root.workspaceIndices = indices;
}
function moveWorkspace(delta: int): void {
root.animate = true;
const mons = root.compositor.monitors ?? [];
if (root.monitorIndex < 0 || root.monitorIndex >= mons.length)
return;
const count = (mons[root.monitorIndex].workspaces ?? []).length;
if (count === 0)
return;
const current = root.workspaceIndexAt(root.monitorIndex);
const next = (current + delta + count) % count;
const copy = root.workspaceIndices.slice();
while (copy.length <= root.monitorIndex)
copy.push(0);
copy[root.monitorIndex] = next;
root.workspaceIndices = copy;
root.syncNameField();
}
function moveMonitor(delta: int): void {
root.animate = true;
const count = (root.compositor.monitors ?? []).length;
if (count === 0)
return;
const next = root.monitorIndex + delta;
if (next < 0 || next >= count)
return;
root.monitorIndex = next;
root.syncNameField();
}
function activateAt(monitorAt: int, workspaceAt: int): void {
const mons = root.compositor.monitors ?? [];
if (monitorAt < 0 || monitorAt >= mons.length) {
root.requestClose();
return;
}
const workspaces = mons[monitorAt].workspaces ?? [];
if (workspaceAt < 0 || workspaceAt >= workspaces.length) {
root.requestClose();
return;
}
const ws = workspaces[workspaceAt];
root.compositor.focusWorkspace(root.compositor.asString(mons[monitorAt].name), ws.idx, ws.id);
root.requestClose();
}
function activateSelection(): void {
root.activateAt(root.monitorIndex, root.workspaceIndexAt(root.monitorIndex));
}
// ── rename bar helpers ──────────────────────────────────────────────
function selectedWorkspace(): var {
const mons = root.compositor.monitors ?? [];
if (root.monitorIndex < 0 || root.monitorIndex >= mons.length)
return null;
const wsList = mons[root.monitorIndex].workspaces ?? [];
const idx = root.workspaceIndexAt(root.monitorIndex);
if (idx < 0 || idx >= wsList.length)
return null;
return wsList[idx];
}
function syncNameField(): void {
const ws = root.selectedWorkspace();
nameInput.text = ws ? ws.name : "";
}
function enterRename(): void {
root.nameFieldActive = true;
root.syncNameField();
nameInput.forceActiveFocus();
nameInput.selectAll();
}
function leaveRename(): void {
root.nameFieldActive = false;
nameInput.focus = false;
Qt.callLater(() => focusScope.forceActiveFocus());
}
function isToggleRenameKey(event): bool {
const mods = event.modifiers;
const ctrl = (mods & Qt.ControlModifier) && !(mods & Qt.AltModifier) && !(mods & Qt.MetaModifier);
return event.key === Qt.Key_G && ctrl;
}
function applyRename(): void {
const ws = root.selectedWorkspace();
if (ws)
root.compositor.renameWorkspace(ws.id, ws.idx, nameInput.text);
root.leaveRename();
}
// ── visibility handling ─────────────────────────────────────────────
onVisibleChanged: {
root.animate = false;
root.nameFieldActive = false;
if (!root.visible) {
root.seedPending = false;
return;
}
root.seedPending = true;
root.compositor.refresh();
Qt.callLater(() => focusScope.forceActiveFocus());
}
Connections {
target: root.compositor
function onRefreshed(): void {
if (!root.visible || !root.seedPending)
return;
if (root.compositor.refreshQueued)
return;
root.seedPending = false;
root.seedSelection();
root.syncNameField();
}
}
// ── backdrop ────────────────────────────────────────────────────────
Rectangle {
id: backdrop
anchors.fill: parent
color: root.theme.backdrop
MouseArea {
anchors.fill: parent
onClicked: {
if (root.nameFieldActive)
root.leaveRename();
else
root.requestBack();
}
}
}
// ── rename bar (always visible at the top) ──────────────────────────
Item {
id: renameBar
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.top
anchors.topMargin: 40
width: root.theme.barWidth
height: root.theme.barHeight
z: 10
Rectangle {
anchors.fill: parent
radius: root.theme.cornerRadius
color: root.theme.barBg
border.color: root.theme.barBorder
border.width: 1
Row {
anchors.fill: parent
anchors.margins: root.theme.barPadding
spacing: root.theme.barSpacing
Image {
width: root.theme.logoSize
height: root.theme.logoSize
anchors.verticalCenter: parent.verticalCenter
source: root.compositor.logoSource
visible: root.compositor.logoSource !== ""
sourceSize.width: root.theme.logoSize * 2
sourceSize.height: root.theme.logoSize * 2
fillMode: Image.PreserveAspectFit
smooth: true
mipmap: true
}
Text {
id: label
anchors.verticalCenter: parent.verticalCenter
text: "Workspace Name: "
color: root.theme.labelColor
font.pixelSize: root.theme.fontSize
}
Rectangle {
id: field
anchors.verticalCenter: parent.verticalCenter
width: parent.width - label.width - okButton.width - root.theme.logoSize - root.theme.barSpacing * 3
height: root.theme.barHeight - root.theme.barPadding * 2
radius: root.theme.cornerRadius / 2
color: root.theme.fieldBg
border.color: nameInput.activeFocus ? root.theme.fieldBorder : root.theme.barBorder
border.width: 1
TextInput {
id: nameInput
anchors.fill: parent
anchors.leftMargin: root.theme.fieldPadding
anchors.rightMargin: root.theme.fieldPadding
verticalAlignment: TextInput.AlignVCenter
color: root.theme.textColor
font.pixelSize: root.theme.fontSize
selectByMouse: true
selectionColor: root.theme.fieldBorder
selectedTextColor: root.theme.buttonText
clip: true
focus: false
onAccepted: root.applyRename()
// Escape from rename field returns to carousel.
Keys.onEscapePressed: event => {
root.leaveRename();
event.accepted = true;
}
// Ctrl+G toggles back to carousel.
Keys.onShortcutOverride: event => {
if (root.isToggleRenameKey(event))
event.accepted = true;
}
Keys.onPressed: event => {
if (root.isToggleRenameKey(event)) {
root.leaveRename();
event.accepted = true;
}
}
}
}
Rectangle {
id: okButton
anchors.verticalCenter: parent.verticalCenter
width: root.theme.buttonWidth
height: root.theme.barHeight - root.theme.barPadding * 2
radius: root.theme.cornerRadius / 2
color: okArea.pressed ? Qt.darker(root.theme.buttonBg, 1.2) : root.theme.buttonBg
Text {
anchors.centerIn: parent
text: "OK"
color: root.theme.buttonText
font.pixelSize: root.theme.fontSize
font.bold: true
}
MouseArea {
id: okArea
anchors.fill: parent
onClicked: root.applyRename()
}
}
}
}
}
// ── carousel ────────────────────────────────────────────────────────
FocusScope {
id: focusScope
anchors.fill: parent
focus: root.visible
Keys.onPressed: event => {
// Ctrl+G toggles to rename mode.
if (root.isToggleRenameKey(event)) {
root.enterRename();
event.accepted = true;
return;
}
switch (event.key) {
case Qt.Key_Left:
root.moveWorkspace(-1);
break;
case Qt.Key_Right:
root.moveWorkspace(1);
break;
case Qt.Key_Up:
root.moveMonitor(-1);
break;
case Qt.Key_Down:
root.moveMonitor(1);
break;
case Qt.Key_Return:
case Qt.Key_Enter:
root.activateSelection();
break;
case Qt.Key_Escape:
root.requestBack();
break;
default:
return;
}
event.accepted = true;
}
WheelHandler {
id: swipeHandler
target: focusScope
property real accX: 0
property real accY: 0
function stepWorkspace(delta: int): void {
root.moveWorkspace(delta);
swipeHandler.accX = 0;
}
function stepMonitor(delta: int): void {
root.moveMonitor(delta);
swipeHandler.accY = 0;
}
onWheel: event => {
const px = event.pixelDelta;
const ang = event.angleDelta;
const dx = px.x !== 0 ? px.x : ang.x;
const dy = px.y !== 0 ? px.y : ang.y;
if (Math.abs(dy) >= Math.abs(dx)) {
swipeHandler.accY += dy;
if (swipeHandler.accY <= -root.theme.swipeThreshold)
swipeHandler.stepMonitor(1);
else if (swipeHandler.accY >= root.theme.swipeThreshold)
swipeHandler.stepMonitor(-1);
} else {
swipeHandler.accX += dx;
if (swipeHandler.accX <= -root.theme.swipeThreshold)
swipeHandler.stepWorkspace(1);
else if (swipeHandler.accX >= root.theme.swipeThreshold)
swipeHandler.stepWorkspace(-1);
}
}
}
Repeater {
model: root.compositor.monitors
Item {
id: row
required property var modelData
required property int index
readonly property int rowIndex: index
readonly property int selectedWs: root.workspaceIndexAt(rowIndex)
readonly property var workspaces: modelData.workspaces ?? []
anchors.left: parent.left
anchors.right: parent.right
anchors.verticalCenter: parent.verticalCenter
height: root.theme.cardHeight
z: row.rowIndex === root.monitorIndex ? 1 : 0
opacity: 1 - Math.min(Math.abs(row.rowIndex - root.monitorIndex) * root.theme.carouselRowFadeStep, root.theme.carouselRowMaxFade)
anchors.verticalCenterOffset: (row.rowIndex - root.monitorIndex) * root.theme.carouselStepY
Behavior on anchors.verticalCenterOffset {
enabled: root.animate
NumberAnimation {
duration: root.theme.carouselDuration
easing.type: Easing.OutCubic
}
}
Behavior on opacity {
enabled: root.animate
NumberAnimation {
duration: root.theme.carouselDuration
}
}
Text {
width: root.theme.cardWidth * 2
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom: parent.top
anchors.bottomMargin: root.theme.monitorLabelGap
horizontalAlignment: Text.AlignHCenter
elide: Text.ElideMiddle
color: root.theme.labelColor
font.pixelSize: root.theme.monitorLabelSize
text: row.modelData.name.length > 0 ? row.modelData.name : "screen"
}
Repeater {
model: row.workspaces
WorkspaceCard {
id: card
required property var modelData
required property int index
readonly property int distance: Math.abs(index - row.selectedWs)
theme: root.theme
workspaceName: modelData.name
workspaceIndex: modelData.idx
selected: row.rowIndex === root.monitorIndex && index === row.selectedWs
current: modelData.focused === true
animateMoves: root.animate
windows: modelData.windows ?? []
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
anchors.horizontalCenterOffset: (index - row.selectedWs) * root.theme.carouselStepX
opacity: 1 - Math.min(card.distance * root.theme.carouselFadeStep, root.theme.carouselMaxFade)
scale: 1 - Math.min(card.distance * root.theme.carouselScaleStep, root.theme.carouselMaxScaleReduction)
z: card.selected ? 1 : 0
onActivated: root.activateAt(row.rowIndex, index)
}
}
}
}
}
}