Files
skygqts/workspace-manager/GotoView.qml
T
2026-09-23 13:53:17 -04:00

744 lines
29 KiB
QML

import QtQuick
import Quickshell
import Quickshell.Wayland
import QtQuick.Effects
PanelWindow {
id: root
required property bool shellOpen
required property Compositor compositor
required property Theme theme
required property Config config
// Focus the name field as soon as the carousel has its data.
required property bool renameOnOpen
// Open the window loop on the focused workspace as soon as the carousel
// has its data. Escape then closes everything.
required property bool openWindowsOnOpen
// 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
// The window loop is open on top of the selected workspace.
property bool windowsOpen: false
// Workspace the window loop shows.
property int loopWorkspaceId: -1
// Escape folds the loop back into the carousel, instead of closing.
property bool canCollapse: true
// Half the shown height of a workspace right above or below the loop,
// after its shrink and its tilt.
readonly property real neighbourHalfHeight: {
const shrink = 1 - Math.min(root.theme.carouselScaleStep, root.theme.carouselMaxScaleReduction);
return root.theme.cardHeight / 2 * shrink * Math.cos(root.theme.loopTiltX * Math.PI / 180);
}
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());
}
// ── window loop helpers ─────────────────────────────────────────────
function isWindowsKey(event): bool {
return root.config.matches(event, root.config.windowsKey);
}
// Open the loop on the selected workspace. The other cards move away.
function expandWindows(collapsible: bool): void {
const ws = root.selectedWorkspace();
if (!ws)
return;
root.animate = true;
root.canCollapse = collapsible;
root.loopWorkspaceId = ws.id;
root.windowsOpen = true;
windowLoop.seed();
}
// Up/Down while the loop is open: move to the next workspace on this
// monitor and show its windows.
function switchWindows(delta: int): void {
const before = root.selectedWorkspace();
root.moveWorkspace(delta);
const ws = root.selectedWorkspace();
if (!ws || ws === before)
return;
root.loopWorkspaceId = ws.id;
windowLoop.switchIn(delta);
}
function collapseWindows(): void {
root.animate = true;
root.windowsOpen = false;
}
// Escape or a backdrop click while the loop is open.
function leaveWindows(): void {
if (root.canCollapse)
root.collapseWindows();
else
root.requestBack();
}
function isToggleRenameKey(event): bool {
return root.config.matches(event, root.config.renameKey);
}
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;
root.windowsOpen = 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();
if (root.openWindowsOnOpen)
root.expandWindows(false);
if (root.renameOnOpen)
root.enterRename();
}
}
// ── backdrop ────────────────────────────────────────────────────────
Rectangle {
id: backdrop
anchors.fill: parent
color: root.theme.backdrop
layer.enabled: true
layer.effect: MultiEffect {
blurEnabled: true
blur: 1.0
blurMax: 64
}
MouseArea {
anchors.fill: parent
onClicked: {
if (root.nameFieldActive)
root.leaveRename();
else if (root.windowsOpen)
root.leaveWindows();
else
root.requestBack();
}
}
}
// ── rename bar (always visible at the top) ──────────────────────────
Item {
id: renameBar
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.top
anchors.topMargin: root.theme.renameBarTop
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;
}
// The rename key 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()
}
}
}
}
KeyHint {
anchors.right: parent.right
anchors.top: parent.bottom
anchors.topMargin: root.theme.hintGap
theme: root.theme
hint: root.config.renameHintText
shown: root.config.showHints && !root.nameFieldActive
}
}
// ── carousel ────────────────────────────────────────────────────────
FocusScope {
id: focusScope
anchors.fill: parent
focus: root.visible
Keys.onPressed: event => {
// The rename key toggles to rename mode.
if (root.isToggleRenameKey(event)) {
root.enterRename();
event.accepted = true;
return;
}
// The windows key opens or folds the window loop on the selected workspace.
if (root.isWindowsKey(event)) {
if (root.windowsOpen)
root.collapseWindows();
else
root.expandWindows(true);
event.accepted = true;
return;
}
if (root.windowsOpen) {
switch (event.key) {
case Qt.Key_Left:
case Qt.Key_Backtab:
windowLoop.step(-1);
break;
case Qt.Key_Right:
case Qt.Key_Tab:
windowLoop.step(1);
break;
case Qt.Key_Return:
case Qt.Key_Enter:
windowLoop.activateSelected();
break;
case Qt.Key_Escape:
root.leaveWindows();
break;
case Qt.Key_Up:
root.switchWindows(-1);
break;
case Qt.Key_Down:
root.switchWindows(1);
break;
default:
return;
}
event.accepted = true;
return;
}
switch (event.key) {
case Qt.Key_Up:
root.moveWorkspace(-1);
break;
case Qt.Key_Down:
root.moveWorkspace(1);
break;
case Qt.Key_Left:
root.moveMonitor(-1);
break;
case Qt.Key_Right:
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
// Vertical swipes move between workspaces, both in the carousel
// and in the loop. Horizontal ones move between monitors, or
// turn the loop while it is open.
function stepVertical(delta: int): void {
if (root.windowsOpen)
root.switchWindows(delta);
else
root.moveWorkspace(delta);
swipeHandler.accY = 0;
}
function stepHorizontal(delta: int): void {
if (root.windowsOpen)
windowLoop.step(delta);
else
root.moveMonitor(delta);
swipeHandler.accX = 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.stepVertical(1);
else if (swipeHandler.accY >= root.theme.swipeThreshold)
swipeHandler.stepVertical(-1);
} else {
swipeHandler.accX += dx;
if (swipeHandler.accX <= -root.theme.swipeThreshold)
swipeHandler.stepHorizontal(1);
else if (swipeHandler.accX >= root.theme.swipeThreshold)
swipeHandler.stepHorizontal(-1);
}
}
}
Repeater {
model: root.compositor.monitors
// One column per monitor, with its workspaces top to bottom.
Item {
id: column
required property var modelData
required property int index
readonly property int columnIndex: index
readonly property int selectedWs: root.workspaceIndexAt(columnIndex)
readonly property var workspaces: modelData.workspaces ?? []
readonly property bool isCurrentColumn: column.columnIndex === root.monitorIndex
// Other columns turn to face the middle while the loop is open.
readonly property real tiltY: root.windowsOpen && !column.isCurrentColumn ? -Math.sign(column.columnIndex - root.monitorIndex) * root.theme.loopTiltY : 0
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.horizontalCenter: parent.horizontalCenter
width: root.theme.cardWidth
// While the loop is open, the current column draws over it,
// so the workspace above stays visible over the back windows.
z: column.isCurrentColumn ? (root.windowsOpen ? 3 : 1) : 0
opacity: {
const fade = 1 - Math.min(Math.abs(column.columnIndex - root.monitorIndex) * root.theme.carouselMonitorFadeStep, root.theme.carouselMonitorMaxFade);
return root.windowsOpen && !column.isCurrentColumn ? fade * root.theme.loopHiddenFade : fade;
}
// While the loop is open, the nearest column on each side sits
// just past the loop's edge. Further columns keep the normal step.
anchors.horizontalCenterOffset: {
const steps = column.columnIndex - root.monitorIndex;
if (!root.windowsOpen || steps === 0)
return steps * root.theme.carouselStepX;
const nearest = windowLoop.reachX + root.theme.cardWidth / 2 + root.theme.loopEdgeGap;
return Math.sign(steps) * (nearest + (Math.abs(steps) - 1) * root.theme.carouselStepX);
}
Behavior on anchors.horizontalCenterOffset {
enabled: root.animate
NumberAnimation {
duration: root.theme.carouselDuration
easing.type: Easing.OutCubic
}
}
Behavior on opacity {
enabled: root.animate
NumberAnimation {
duration: root.theme.carouselDuration
}
}
transform: Rotation {
origin.x: column.width / 2
origin.y: column.height / 2
axis {
x: 0
y: 1
z: 0
}
angle: column.tiltY
Behavior on angle {
enabled: root.animate
NumberAnimation {
duration: root.theme.carouselDuration
easing.type: Easing.OutCubic
}
}
}
// Monitor name, above the rename bar.
Rectangle {
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.top
anchors.topMargin: root.theme.monitorLabelTop
z: 2
width: Math.min(nameText.implicitWidth + root.theme.monitorNamePadding * 10, column.width)
height: nameText.implicitHeight + root.theme.monitorNamePadding
radius: height / 4
color: root.theme.barBg
border.width: column.isCurrentColumn ? 2 : 1
border.color: column.isCurrentColumn ? root.theme.fieldBorder : root.theme.barBorder
Text {
id: nameText
anchors.centerIn: parent
width: parent.width - root.theme.monitorNamePadding * 2
horizontalAlignment: Text.AlignHCenter
elide: Text.ElideMiddle
color: column.isCurrentColumn ? root.theme.textColor : root.theme.labelColor
font.pixelSize: root.theme.monitorNameSize
font.bold: true
text: column.modelData.name.length > 0 ? column.modelData.name : "screen"
}
}
Repeater {
model: column.workspaces
WorkspaceCard {
id: card
required property var modelData
required property int index
readonly property int distance: Math.abs(index - column.selectedWs)
// This card turns into the window loop.
readonly property bool opensLoop: root.windowsOpen && card.selected
// Cards above and below the loop move so the nearest
// ones sit a small gap from the front window. Cards
// further out keep the normal step from those.
readonly property real spreadY: {
const steps = index - column.selectedWs;
if (!root.windowsOpen || !column.isCurrentColumn || steps === 0)
return 0;
const nearest = steps < 0 ? windowLoop.frontTop - root.theme.loopWorkspaceGap - root.neighbourHalfHeight : windowLoop.frontBottom + root.theme.loopWorkspaceGap + root.neighbourHalfHeight;
return nearest - Math.sign(steps) * root.theme.carouselStepY;
}
// They also lean back: the top edge away above the
// loop, the bottom edge away below it.
readonly property real tiltX: root.windowsOpen && column.isCurrentColumn ? -Math.sign(index - column.selectedWs) * root.theme.loopTiltX : 0
readonly property real baseOpacity: 1 - Math.min(card.distance * root.theme.carouselFadeStep, root.theme.carouselMaxFade)
readonly property real baseScale: 1 - Math.min(card.distance * root.theme.carouselScaleStep, root.theme.carouselMaxScaleReduction)
theme: root.theme
workspaceName: modelData.name
workspaceIndex: modelData.idx
selected: column.isCurrentColumn && index === column.selectedWs
current: modelData.focused === true
animateMoves: root.animate
windows: modelData.windows ?? []
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenterOffset: (index - column.selectedWs) * root.theme.carouselStepY + card.spreadY
opacity: {
if (card.opensLoop)
return 0;
if (!root.windowsOpen)
return card.baseOpacity;
if (!column.isCurrentColumn)
return card.baseOpacity * root.theme.loopHiddenFade;
// The two right next to the loop skip the fade by distance.
if (card.distance === 1)
return root.theme.loopNeighbourFade;
return card.baseOpacity * root.theme.loopNeighbourFade;
}
scale: card.opensLoop ? 1 / root.theme.loopStartScale : card.baseScale
z: card.selected ? 1 : 0
enabled: !root.windowsOpen
transform: Rotation {
origin.x: card.width / 2
origin.y: card.height / 2
axis {
x: 1
y: 0
z: 0
}
angle: card.tiltX
Behavior on angle {
enabled: root.animate
NumberAnimation {
duration: root.theme.carouselDuration
easing.type: Easing.OutCubic
}
}
}
onActivated: root.activateAt(column.columnIndex, index)
}
}
// Under the selected workspace, which always sits in the
// middle of the column and is as wide as it.
KeyHint {
anchors.right: parent.right
anchors.top: parent.verticalCenter
anchors.topMargin: root.theme.cardHeight / 2 + root.theme.hintGap
z: 3
theme: root.theme
hint: root.config.windowsHintText
shown: root.config.showHints && column.isCurrentColumn && !root.windowsOpen && column.workspaces.length > 0
}
}
}
// Sits where the selected card was and grows out of it.
WindowLoop {
id: windowLoop
anchors.fill: parent
z: 2
compositor: root.compositor
theme: root.theme
workspaceId: root.loopWorkspaceId
expanded: root.windowsOpen
exitHint: root.config.showHints ? root.config.exitWindowsHintText : ""
onWindowChosen: root.requestClose()
}
}
}