305 lines
10 KiB
QML
305 lines
10 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
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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));
|
|
}
|
|
|
|
onVisibleChanged: {
|
|
root.animate = 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;
|
|
// This read is stale. Keep waiting for the one already queued.
|
|
if (root.compositor.refreshQueued)
|
|
return;
|
|
root.seedPending = false;
|
|
root.seedSelection();
|
|
}
|
|
}
|
|
|
|
Rectangle {
|
|
anchors.fill: parent
|
|
color: root.theme.backdrop
|
|
|
|
MouseArea {
|
|
anchors.fill: parent
|
|
onClicked: root.requestBack()
|
|
}
|
|
}
|
|
|
|
FocusScope {
|
|
id: focusScope
|
|
anchors.fill: parent
|
|
focus: root.visible
|
|
|
|
Keys.onPressed: event => {
|
|
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
|
|
|
|
// Each row centers its own workspace. Rows are not lined up
|
|
// in columns across monitors.
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|