Files
2026-10-27 13:30:14 -04:00

124 lines
4.6 KiB
QML

import QtQuick
// A workspace card bent onto the globe. The flat card is drawn into a
// picture, which a small shader wraps over the sphere. The flat card stays
// in place under it, hidden, so clicks still reach it. If the shader cannot
// be loaded, the flat card shows instead, tilted to face out of the globe.
// Fill the globe with this item; its middle is the middle of the globe.
Item {
id: root
required property Theme theme
// Where the card's middle sits on the globe, in radians. 0 is at the
// front, down and right are positive.
required property real lat
required property real lon
// Fade from the carousel steps. The fade toward the globe edge is added here.
required property real fade
required property string workspaceName
required property int workspaceIndex
required property bool selected
required property bool current
// Windows from parsed JSON, so this stays untyped.
required property var windows
signal activated
// 1 facing the viewer, 0 edge-on, below 0 on the back.
readonly property real facing: Math.cos(root.lat) * Math.cos(root.lon)
// Simple perspective: cards farther away get smaller.
readonly property real depthScale: root.theme.globeFocalLength / (root.theme.globeFocalLength + root.theme.globeRadius * (1 - root.facing))
// Fades out as it turns toward the edge of the globe.
readonly property real edgeFade: Math.max(0, Math.min(1, (root.facing - root.theme.globeHideFacing) / (root.theme.globeFadeFacing - root.theme.globeHideFacing)))
// The shader loaded, so the card is drawn bent.
readonly property bool warped: effect.status === ShaderEffect.Compiled
// The bent card picks this up from its parent, the flat one too.
opacity: root.fade * root.edgeFade
visible: root.facing > root.theme.globeHideFacing
WorkspaceCard {
id: card
theme: root.theme
workspaceName: root.workspaceName
workspaceIndex: root.workspaceIndex
selected: root.selected
current: root.current
windows: root.windows
x: root.width / 2 + root.theme.globeRadius * Math.cos(root.lat) * Math.sin(root.lon) * root.depthScale - card.width / 2
y: root.height / 2 + root.theme.globeRadius * Math.sin(root.lat) * root.depthScale - card.height / 2
scale: root.depthScale
// Only used when the shader is missing: lean back by the latitude,
// then turn by the longitude.
transform: [
Rotation {
origin.x: card.width / 2
origin.y: card.height / 2
axis {
x: 1
y: 0
z: 0
}
angle: root.warped ? 0 : -root.lat * 180 / Math.PI
},
Rotation {
origin.x: card.width / 2
origin.y: card.height / 2
axis {
x: 0
y: 1
z: 0
}
angle: root.warped ? 0 : -root.lon * 180 / Math.PI
}
]
onActivated: root.activated()
}
// The flat card as a picture, with room round it for the glow.
ShaderEffectSource {
id: picture
readonly property real margin: root.theme.globeCardMargin
sourceItem: card
sourceRect: Qt.rect(-picture.margin, -picture.margin, card.width + picture.margin * 2, card.height + picture.margin * 2)
hideSource: root.warped
smooth: true
mipmap: true
// Only the shader shows it.
width: 0
height: 0
}
ShaderEffect {
id: effect
// Read by the shaders, see shaders/globecard.vert.
property Item source: picture
property vector2d meshSize: Qt.vector2d(effect.width, effect.height)
property real sphereRadius: root.theme.globeRadius
property real focalLength: root.theme.globeFocalLength
property real lat: root.lat
property real lon: root.lon
// Centered on the middle of the globe. The shader moves each point
// to its place on the sphere from here.
x: (root.width - effect.width) / 2
y: (root.height - effect.height) / 2
width: picture.sourceRect.width
height: picture.sourceRect.height
visible: root.warped
mesh: GridMesh {
resolution: Qt.size(root.theme.globeCardMeshColumns, root.theme.globeCardMeshRows)
}
vertexShader: Qt.resolvedUrl("shaders/globecard.vert.qsb")
fragmentShader: Qt.resolvedUrl("shaders/globecard.frag.qsb")
}
}