Files
2026-10-27 13:35:31 -04:00

178 lines
7.3 KiB
QML

import QtQuick
// One set of window cards orbiting a small planet. With only a few windows
// there is no planet, just flat cards side by side. While Up/Down switch
// workspaces the loop shows two of these: the old one folding away and the
// new one opening. Zero sized, placed at the front window's center.
Item {
id: root
required property Compositor compositor
required property Theme theme
// Windows to show, from parsed JSON, so this stays untyped.
required property var windows
// Widest the loop may be on each side of its center.
required property real maxRadius
// Drives every card position. Not wrapped.
required property real turn
required property int selectedIndex
// Stream the Hyprland previews.
required property bool live
// Previews grow out from the icon once they are ready.
required property bool growPreview
// 0 for the open loop, 1 with every card folded into one small box.
required property real gather
// Size of the planet, 1 for full size. Goes a little past 1 as it pops out.
required property real planetGrow
// A card was clicked.
signal activated(int index)
readonly property int count: root.windows.length
// Few windows: flat cards side by side, no planet.
readonly property bool flat: root.count <= root.theme.loopFlatMaxWindows
// 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);
return Math.max(root.theme.loopMinRadius, Math.min(natural, root.maxRadius));
}
// 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;
if (root.flat)
return root.count * half + (root.count - 1) * root.theme.loopFlatGap / 2;
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 faceWidth = Math.abs(Math.cos(root.faceTurn(angle) * Math.PI / 180));
const edge = (root.radius * Math.sin(angle) + half * faceWidth) * depthScale;
widest = Math.max(widest, edge);
}
return widest;
}
// Where round the loop, in degrees, cards turn edge-on. Halfway between
// two cards, so none rests edge-on.
readonly property real flipAngle: {
if (root.count <= 1)
return 90;
const stepDegrees = 360 / root.count;
const flip = (Math.floor(root.theme.loopFlipAngle / stepDegrees) + 0.5) * stepDegrees;
return Math.min(flip, 180);
}
// 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;
}
// How far a card at this angle (radians) turns, in degrees. It turns
// from facing the viewer at the front to edge-on at flipAngle, then on
// round to face the viewer again at the back, showing its inner face.
function faceTurn(angle: real): real {
const degrees = Math.abs(angle) * 180 / Math.PI;
const side = angle < 0 ? -1 : 1;
const flip = root.flipAngle;
if (degrees <= flip || flip >= 180)
return -side * 90 * degrees / flip;
const turn = 90 + 90 * (degrees - flip) / (180 - flip);
return -side * (turn - 180);
}
// The planet, in the middle of the loop. The back windows pass behind
// it and the front ones in front. It spins as the loop turns.
Globe {
id: planet
readonly property real size: Math.min(2 * root.radius * root.theme.planetRadiusShare, root.theme.planetMaxSize)
x: -planet.size / 2
y: -root.theme.loopBackLift / 2 - planet.size / 2
z: 0
width: planet.size
height: planet.size
visible: !root.flat && planet.opacity > 0
// Folds away with the cards.
opacity: 1 - root.gather
scale: root.planetGrow * (1 - root.gather)
theme: root.theme
yaw: root.turn * root.angleStep
pitch: root.theme.planetPitch
lonStep: root.angleStep
latStep: root.theme.planetLatStep
focalLength: root.theme.loopFocalLength
}
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. Flat cards are all at the front.
readonly property real back: root.flat ? 0 : (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 roughly lies along the loop, less so
// at the sides, so they can still be seen.
readonly property real faceAngle: root.flat ? 0 : root.faceTurn(card.angle)
// Place in a flat row, from the middle of the row.
readonly property real flatX: (card.index - (root.count - 1) / 2) * (root.theme.loopCardWidth + root.theme.loopFlatGap)
// 1 when open, 0 when folded into the box.
readonly property real open: 1 - root.gather
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.live
growPreview: root.growPreview
toplevel: root.compositor.toplevelFor(card.modelData.key)
previewFile: card.modelData.preview
previewVersion: root.compositor.previewVersion
// Folding pulls every card into the middle, facing front.
x: (root.flat ? card.flatX : root.radius * Math.sin(card.angle) * card.depthScale) * card.open - card.width / 2
y: -root.theme.loopBackLift * card.back * card.open - card.height / 2
z: root.flat ? 1 : card.cosA
scale: card.depthScale + (root.theme.loopBoxScale - card.depthScale) * root.gather
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 * card.open
}
onActivated: root.activated(card.index)
}
}
}