Files
skygqts/workspace-manager/Glass.qml
T
2026-09-23 20:04:49 -04:00

101 lines
3.5 KiB
QML

import QtQuick
// A see-through glass panel: a light tint, a soft shine over the top half,
// a rim that catches the light along the top and a shadow underneath.
// It only draws the glass; put the content on top of it.
Item {
id: root
required property Theme theme
required property real radius
required property color tint
// Blue tint and a glowing blue rim.
required property bool selected
// Soft shadow underneath. Off where the window is too small to hold it.
required property bool shadow
Rectangle {
anchors.fill: parent
radius: root.radius
color: root.selected ? root.theme.glassSelectedTint : root.tint
antialiasing: true
}
// Shine over the top half, like light on curved glass.
Rectangle {
anchors.fill: parent
radius: root.radius
antialiasing: true
gradient: Gradient {
GradientStop {
position: 0
color: root.theme.glassShine
}
GradientStop {
position: 0.55
color: Qt.rgba(1, 1, 1, 0)
}
}
}
// The rim, bright at the top and fading down, and the shadow around
// the outside. Bigger than the panel so the shadow has room.
Canvas {
id: edge
readonly property real margin: root.shadow ? root.theme.glassShadowBlur + root.theme.glassShadowOffset : 0
readonly property color rimTop: root.selected ? root.theme.fieldBorder : root.theme.glassRimTop
readonly property color rimBottom: root.selected ? root.theme.glassSelectedRimBottom : root.theme.glassRimBottom
readonly property real rimWidth: root.selected ? 2 : 1
anchors.fill: parent
anchors.margins: -edge.margin
onWidthChanged: edge.requestPaint()
onHeightChanged: edge.requestPaint()
onRimTopChanged: edge.requestPaint()
onRimBottomChanged: edge.requestPaint()
onRimWidthChanged: edge.requestPaint()
onPaint: {
const ctx = edge.getContext("2d");
ctx.reset();
const m = edge.margin;
const w = root.width;
const h = root.height;
const r = Math.min(root.radius, w / 2, h / 2);
if (w <= 0 || h <= 0)
return;
if (root.shadow) {
ctx.save();
ctx.shadowColor = root.theme.glassShadow;
ctx.shadowBlur = root.theme.glassShadowBlur;
ctx.shadowOffsetY = root.theme.glassShadowOffset;
ctx.fillStyle = "black";
ctx.beginPath();
ctx.roundedRect(m, m, w, h, r, r);
ctx.fill();
ctx.restore();
// Clear the panel itself, so the shadow only shows outside
// and the glass stays see-through.
ctx.globalCompositeOperation = "destination-out";
ctx.beginPath();
ctx.roundedRect(m, m, w, h, r, r);
ctx.fill();
ctx.globalCompositeOperation = "source-over";
}
const half = edge.rimWidth / 2;
const rim = ctx.createLinearGradient(0, m, 0, m + h);
rim.addColorStop(0, edge.rimTop);
rim.addColorStop(1, edge.rimBottom);
ctx.strokeStyle = rim;
ctx.lineWidth = edge.rimWidth;
ctx.beginPath();
ctx.roundedRect(m + half, m + half, w - edge.rimWidth, h - edge.rimWidth, r - half, r - half);
ctx.stroke();
}
}
}