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

243 lines
8.4 KiB
QML

import QtQuick
// The windows of one workspace 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.
Item {
id: root
required property Compositor compositor
required property Theme theme
required property int workspaceId
// Shown and taking input. Fades and grows in when this turns on.
required property bool expanded
// A window was focused.
signal windowChosen
// Windows of the shown workspace, from parsed JSON, so this stays untyped.
readonly property var windows: {
const ws = root.compositor.workspaceById(root.workspaceId);
return ws ? (ws.windows ?? []) : [];
}
readonly property int count: root.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.loopCardWidth + root.theme.loopCardGap) / (2 * Math.PI);
const widest = root.width * root.theme.loopMaxRadiusShare;
return Math.max(root.theme.loopMinRadius, Math.min(natural, widest));
}
// About how far the loop reaches sideways from its center, card edges
// included. Sampled over the front half so it stays still while turning.
readonly property real reachX: {
const half = root.theme.loopCardWidth / 2;
if (root.count <= 1)
return half;
const focal = root.theme.loopFocalLength;
let widest = half;
for (let i = 1; i <= 16; i++) {
const angle = i / 16 * Math.PI / 2;
const depthScale = focal / (focal + root.radius * (1 - Math.cos(angle)));
const edge = (root.radius * Math.sin(angle) + half * Math.cos(angle)) * depthScale;
widest = Math.max(widest, edge);
}
return 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
// Offset and fade of the loop while it slides in after a workspace switch.
property real slideY: 0
property real switchFade: 1
readonly property int selectedIndex: root.wrapIndex(root.targetStep)
opacity: root.expanded ? 1 : 0
scale: root.expanded ? 1 : root.theme.loopStartScale
visible: root.opacity > 0
Behavior on opacity {
NumberAnimation {
duration: root.theme.loopExpandDuration
easing.type: Easing.OutCubic
}
}
Behavior on scale {
NumberAnimation {
duration: root.theme.loopExpandDuration
easing.type: Easing.OutCubic
}
}
Behavior on turn {
enabled: root.animate
NumberAnimation {
duration: root.theme.loopTurnDuration
easing.type: Easing.OutCubic
}
}
ParallelAnimation {
id: switchAnim
NumberAnimation {
target: root
property: "slideY"
to: 0
duration: root.theme.loopSwitchDuration
easing.type: Easing.OutCubic
}
NumberAnimation {
target: root
property: "switchFade"
to: 1
duration: root.theme.loopSwitchDuration
}
}
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;
}
// Select the focused window, or the first one, and turn into place.
function seed(): void {
const list = root.windows;
const focused = list.findIndex(w => w.focused);
root.animate = false;
root.targetStep = focused >= 0 ? focused : 0;
// Start one step back so the loop turns in as it appears.
root.turn = root.targetStep - (root.count > 1 ? 1 : 0);
root.animate = true;
root.turn = root.targetStep;
root.compositor.takeScreenshots(list.map(w => w.key));
}
// Show another workspace. The new windows slide in from below when
// direction is positive, from above when negative.
function switchIn(direction: int): void {
switchAnim.stop();
root.slideY = direction * root.theme.loopSwitchOffset;
root.switchFade = 0;
root.seed();
switchAnim.start();
}
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.windows;
if (index < 0 || index >= list.length)
return;
root.compositor.focusWindow(list[index].key);
root.windowChosen();
}
function activateSelected(): void {
root.activateAt(root.selectedIndex);
}
Text {
anchors.centerIn: parent
anchors.verticalCenterOffset: root.slideY
opacity: root.switchFade
visible: root.count === 0
color: root.theme.labelColor
font.pixelSize: root.theme.loopEmptySize
text: "No windows on this workspace"
}
// Zero sized point at the loop center. Pushed down by half the lift
// so the loop as a whole sits in the middle.
Item {
id: stage
anchors.centerIn: parent
anchors.verticalCenterOffset: root.theme.loopBackLift / 2 + root.slideY
opacity: root.switchFade
Repeater {
model: root.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.loopFocalLength / (root.theme.loopFocalLength + 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
live: root.expanded
toplevel: root.compositor.toplevelFor(card.modelData.key)
previewFile: card.modelData.preview
previewVersion: root.compositor.previewVersion
enabled: root.expanded
x: root.radius * Math.sin(card.angle) * card.depthScale - card.width / 2
y: -root.theme.loopBackLift * card.back - card.height / 2
z: card.cosA
scale: card.depthScale
opacity: 1 - (1 - root.theme.loopBackOpacity) * 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)
}
}
}
}