Files
skygqts/window-carousel/CarouselView.qml
T
2026-09-23 11:56:03 -04:00

256 lines
8.3 KiB
QML

import QtQuick
import Quickshell
import Quickshell.Wayland
// The windows sit on a strip bent into a closed loop, seen from a little
// above. The front half shows the outside of the strip, the back half shows
// the inside, so the far windows wrap around behind and face the viewer too.
PanelWindow {
id: root
required property bool shellOpen
required property Compositor compositor
required property Theme theme
signal requestClose
visible: root.shellOpen
color: "transparent"
exclusionMode: ExclusionMode.Ignore
WlrLayershell.namespace: "quickshell:window-carousel"
WlrLayershell.layer: WlrLayer.Overlay
WlrLayershell.keyboardFocus: root.visible ? WlrKeyboardFocus.Exclusive : WlrKeyboardFocus.None
anchors {
top: true
left: true
right: true
bottom: true
}
readonly property int count: root.compositor.windows.length
// Angle between two neighbours on the loop.
readonly property real angleStep: root.count > 0 ? 2 * Math.PI / root.count : 0
// The loop is as long as the cards side by side, within limits.
readonly property real radius: {
const natural = root.count * (root.theme.cardWidth + root.theme.cardGap) / (2 * Math.PI);
const widest = root.width * root.theme.maxRadiusShare;
return Math.max(root.theme.minRadius, Math.min(natural, widest));
}
// Selected step. It is not wrapped, so the loop can keep turning the
// same way. Use selectedIndex for the position in the window list.
property int targetStep: 0
// Follows targetStep smoothly and drives every card position.
property real turn: 0
property bool animate: false
property bool seedPending: false
readonly property int selectedIndex: root.wrapIndex(root.targetStep)
Behavior on turn {
enabled: root.animate
NumberAnimation {
duration: root.theme.turnDuration
easing.type: Easing.OutCubic
}
}
function wrapIndex(step: int): int {
if (root.count === 0)
return 0;
return ((step % root.count) + root.count) % root.count;
}
// Angle of a card from the front, in radians, between -pi and pi.
function angleOf(index: int): real {
if (root.count === 0)
return 0;
let steps = (index - root.turn) % root.count;
if (steps < 0)
steps += root.count;
if (steps > root.count / 2)
steps -= root.count;
return steps * root.angleStep;
}
function seedSelection(): void {
const list = root.compositor.windows;
const focused = list.findIndex(w => w.focused);
root.animate = false;
root.targetStep = focused >= 0 ? focused : 0;
root.turn = root.targetStep;
}
function step(delta: int): void {
if (root.count === 0)
return;
root.animate = true;
root.targetStep += delta;
root.turn = root.targetStep;
}
function activateAt(index: int): void {
const list = root.compositor.windows;
if (index >= 0 && index < list.length)
root.compositor.focusWindow(list[index].key);
root.requestClose();
}
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;
if (root.compositor.refreshQueued)
return;
root.seedPending = false;
root.seedSelection();
}
}
Rectangle {
anchors.fill: parent
color: root.theme.backdrop
MouseArea {
anchors.fill: parent
onClicked: root.requestClose()
}
}
Text {
anchors.centerIn: parent
visible: root.count === 0 && !root.seedPending
color: root.theme.labelColor
font.pixelSize: root.theme.emptySize
text: "No windows on this workspace"
}
FocusScope {
id: focusScope
anchors.fill: parent
focus: root.visible
Keys.onPressed: event => {
switch (event.key) {
case Qt.Key_Left:
case Qt.Key_Backtab:
root.step(-1);
break;
case Qt.Key_Right:
case Qt.Key_Tab:
root.step(1);
break;
case Qt.Key_Return:
case Qt.Key_Enter:
root.activateAt(root.selectedIndex);
break;
case Qt.Key_Escape:
root.requestClose();
break;
default:
return;
}
event.accepted = true;
}
WheelHandler {
id: swipeHandler
target: focusScope
property real acc: 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;
swipeHandler.acc += Math.abs(dx) >= Math.abs(dy) ? dx : dy;
if (swipeHandler.acc <= -root.theme.swipeThreshold) {
root.step(1);
swipeHandler.acc = 0;
} else if (swipeHandler.acc >= root.theme.swipeThreshold) {
root.step(-1);
swipeHandler.acc = 0;
}
}
}
// Zero sized point at the loop center. Pushed down by half the lift
// so the loop as a whole sits in the middle of the screen.
Item {
id: stage
anchors.centerIn: parent
anchors.verticalCenterOffset: root.theme.backLift / 2
Repeater {
model: root.compositor.windows
WindowCard {
id: card
required property var modelData
required property int index
readonly property real angle: root.angleOf(card.index)
readonly property real cosA: Math.cos(card.angle)
// 0 at the front, 1 at the very back.
readonly property real back: (1 - card.cosA) / 2
// Simple perspective: cards farther away get smaller.
readonly property real depthScale: root.theme.focalLength / (root.theme.focalLength + 2 * root.radius * card.back)
// Turn so each card lies along the loop. The front half
// shows the outer face, the back half the inner face.
readonly property real faceAngle: {
const degrees = card.angle * 180 / Math.PI;
if (card.cosA >= 0)
return -degrees;
return degrees > 0 ? 180 - degrees : -180 - degrees;
}
theme: root.theme
title: card.modelData.title
app: card.modelData.app
selected: card.index === root.selectedIndex
current: card.modelData.focused === true
shade: card.back
toplevel: root.compositor.toplevelFor(card.modelData.key)
previewFile: card.modelData.preview
previewVersion: root.compositor.previewVersion
x: root.radius * Math.sin(card.angle) * card.depthScale - card.width / 2
y: -root.theme.backLift * card.back - card.height / 2
z: card.cosA
scale: card.depthScale
opacity: 1 - (1 - root.theme.backOpacity) * card.back
transform: Rotation {
origin.x: card.width / 2
origin.y: card.height / 2
axis {
x: 0
y: 1
z: 0
}
angle: card.faceAngle
}
onActivated: root.activateAt(card.index)
}
}
}
}
}