From 1a6e5af692e50499133eac5f799046e87f514b73 Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Wed, 23 Sep 2026 11:56:03 -0400 Subject: [PATCH] carousel --- package.nix | 2 + skygqts.nu | 28 +++ window-carousel/CarouselView.qml | 255 ++++++++++++++++++++++++++ window-carousel/Compositor.qml | 300 +++++++++++++++++++++++++++++++ window-carousel/Theme.qml | 53 ++++++ window-carousel/WindowCard.qml | 147 +++++++++++++++ window-carousel/shell.qml | 46 +++++ 7 files changed, 831 insertions(+) create mode 100644 window-carousel/CarouselView.qml create mode 100644 window-carousel/Compositor.qml create mode 100644 window-carousel/Theme.qml create mode 100644 window-carousel/WindowCard.qml create mode 100644 window-carousel/shell.qml diff --git a/package.nix b/package.nix index 6e8df8d..dad257b 100644 --- a/package.nix +++ b/package.nix @@ -16,6 +16,7 @@ stdenvNoCC.mkDerivation { fileset = lib.fileset.unions [ ./app-carousel ./workspace-manager + ./window-carousel ./skygqts.nu ]; }; @@ -29,6 +30,7 @@ stdenvNoCC.mkDerivation { mkdir -p $out/share/skygqts cp -r app-carousel $out/share/skygqts/ cp -r workspace-manager $out/share/skygqts/ + cp -r window-carousel $out/share/skygqts/ cp skygqts.nu $out/share/skygqts/ mkdir -p $out/bin makeWrapper ${lib.getExe nushell} $out/bin/skygqts \ diff --git a/skygqts.nu b/skygqts.nu index d00d558..865eb92 100755 --- a/skygqts.nu +++ b/skygqts.nu @@ -42,3 +42,31 @@ def "main workspace-manager toggle" [] { def "main workspace-manager goto" [] { ws-ipc "goTo" } + +const WC_PATH = $REPO_ROOT | path join "window-carousel" + +# Send an IPC call to the running window carousel +def wc-ipc [fn: string] { + qs ipc -p $WC_PATH call windows $fn +} + +# Run the window carousel +def "main window-carousel" [ + --daemon (-d) # detach from the terminal +] { + if $daemon { + qs -d -n -p $WC_PATH + } else { + qs -n -p $WC_PATH + } +} + +# Toggle the window carousel +def "main window-carousel toggle" [] { + wc-ipc "toggle" +} + +# Open the window carousel +def "main window-carousel open" [] { + wc-ipc "open" +} diff --git a/window-carousel/CarouselView.qml b/window-carousel/CarouselView.qml new file mode 100644 index 0000000..a2bb476 --- /dev/null +++ b/window-carousel/CarouselView.qml @@ -0,0 +1,255 @@ +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) + } + } + } + } +} diff --git a/window-carousel/Compositor.qml b/window-carousel/Compositor.qml new file mode 100644 index 0000000..f6de170 --- /dev/null +++ b/window-carousel/Compositor.qml @@ -0,0 +1,300 @@ +import QtQuick +import Quickshell +import Quickshell.Io +import Quickshell.Hyprland +import Quickshell.Wayland + +// Lists the windows on the focused workspace and focuses one of them. +// Everything goes through the command line tools, so niri and Hyprland +// behave the same way. +Item { + id: root + + // "niri", "hyprland" or "" when neither was detected. + readonly property string kind: { + if (Quickshell.env("NIRI_SOCKET")) + return "niri"; + if (Quickshell.env("HYPRLAND_INSTANCE_SIGNATURE")) + return "hyprland"; + return ""; + } + + // Take niri screenshots for previews. Each one also replaces the + // clipboard, and niri has no way to turn that off. + required property bool niriScreenshots + + // Id of the focused workspace, -1 when unknown. + property int workspaceId: -1 + + // Windows on the focused workspace, left to right. Each entry is + // { key, title, app, focused, preview }. Built from parsed JSON, so this + // stays an untyped array. key is the niri window id or the Hyprland + // address. preview is a screenshot file on niri, "" otherwise. + property var windows: [] + + // Bumped after each niri screenshot so preview images load again. + property int previewVersion: 0 + // niri window ids still waiting for a screenshot. + property list shotQueue: [] + + readonly property string previewDir: { + const dir = Quickshell.env("XDG_RUNTIME_DIR"); + return dir ? dir : "/tmp"; + } + + // "" while idle, otherwise the query step currently running. + property string queryStage: "" + property bool refreshQueued: false + // False while a query is being restarted, so a leftover finish is ignored. + property bool acceptStream: false + + signal refreshed + + function refresh(): void { + if (root.kind === "") + return; + if (root.queryStage !== "") { + root.refreshQueued = true; + return; + } + root.startStage("workspace"); + } + + function startStage(stage: string): void { + root.acceptStream = false; + query.running = false; + root.queryStage = stage; + if (stage === "workspace") { + if (root.kind === "niri") + query.command = ["niri", "msg", "--json", "workspaces"]; + else + query.command = ["hyprctl", "-j", "activeworkspace"]; + } else { + if (root.kind === "niri") + query.command = ["niri", "msg", "--json", "windows"]; + else + query.command = ["hyprctl", "-j", "clients"]; + } + root.acceptStream = true; + query.running = true; + } + + function previewPath(key: string): string { + return root.previewDir + "/skygqts-window-carousel-" + key + ".png"; + } + + // Live capture handle for a Hyprland window, null when there is none. + function toplevelFor(key: string): Toplevel { + if (root.kind !== "hyprland") + return null; + const wanted = key.toLowerCase().replace(/^0x/, ""); + const list = Hyprland.toplevels.values; + for (let i = 0; i < list.length; i++) { + if (list[i].address.toLowerCase().replace(/^0x/, "") === wanted) + return list[i].wayland; + } + return null; + } + + // Screenshot the listed niri windows one at a time. + function takeScreenshots(): void { + if (root.kind !== "niri" || !root.niriScreenshots) + return; + root.shotQueue = root.windows.map(w => w.key); + if (!shotProc.running) + root.nextScreenshot(); + } + + function nextScreenshot(): void { + if (root.shotQueue.length === 0) { + // niri may still be writing the last file, so look again shortly. + shotSettle.restart(); + return; + } + const key = root.shotQueue[0]; + root.shotQueue = root.shotQueue.slice(1); + shotProc.command = ["niri", "msg", "action", "screenshot-window", "--id", key, "--write-to-disk", "true", "--path", root.previewPath(key)]; + shotProc.running = true; + } + + function finishQuery(): void { + root.queryStage = ""; + root.refreshed(); + if (!root.refreshQueued) + return; + root.refreshQueued = false; + root.refresh(); + } + + // Some niri versions wrap replies in {"Ok": {"Variant": value}}. + // This returns the bare value either way, or null on an error reply. + function unwrapJson(text: string): var { + const parsed = JSON.parse(text); + if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) { + if ("Ok" in parsed) { + const inner = parsed.Ok; + if (inner && typeof inner === "object" && !Array.isArray(inner)) { + const keys = Object.keys(inner); + if (keys.length === 1) + return inner[keys[0]]; + } + return inner; + } + if ("Err" in parsed) { + console.warn("window-carousel: compositor IPC error:", parsed.Err); + return null; + } + } + return parsed; + } + + function asString(value): string { + if (value === null || value === undefined) + return ""; + return String(value); + } + + function asNumber(value, fallback: real): real { + const number = Number(value); + return Number.isFinite(number) ? number : fallback; + } + + function parseWorkspace(parsed): void { + if (root.kind === "niri") { + const list = Array.isArray(parsed) ? parsed : []; + const focused = list.find(ws => ws.is_focused === true); + root.workspaceId = focused ? root.asNumber(focused.id, -1) : -1; + } else { + root.workspaceId = root.asNumber(parsed?.id, -1); + } + } + + function parseWindows(parsed): void { + const list = Array.isArray(parsed) ? parsed : []; + const found = []; + for (let i = 0; i < list.length; i++) { + const w = list[i]; + if (root.kind === "niri") { + if (root.asNumber(w.workspace_id, -1) !== root.workspaceId) + continue; + // Tiled windows sort by column then row. Floating ones have + // no position and go last. + const pos = w.layout?.pos_in_scrolling_layout; + const tiled = Array.isArray(pos) && pos.length >= 2; + found.push({ + key: root.asString(w.id), + title: root.asString(w.title), + app: root.asString(w.app_id), + focused: w.is_focused === true, + sortA: tiled ? root.asNumber(pos[0], 0) : Number.MAX_SAFE_INTEGER, + sortB: tiled ? root.asNumber(pos[1], 0) : i + }); + } else { + if (w.mapped === false || w.hidden === true) + continue; + if (root.asNumber(w.workspace?.id, -1) !== root.workspaceId) + continue; + const at = Array.isArray(w.at) ? w.at : [0, 0]; + found.push({ + key: root.asString(w.address), + title: root.asString(w.title), + app: root.asString(w["class"]), + focused: root.asNumber(w.focusHistoryID, -1) === 0, + sortA: root.asNumber(at[0], 0), + sortB: root.asNumber(at[1], 0) + }); + } + } + found.sort((a, b) => a.sortA !== b.sortA ? a.sortA - b.sortA : a.sortB - b.sortB); + root.windows = found.map(w => ({ + key: w.key, + title: w.title, + app: w.app, + focused: w.focused, + preview: root.kind === "niri" && root.niriScreenshots ? root.previewPath(w.key) : "" + })); + root.takeScreenshots(); + } + + function focusWindow(key: string): void { + if (key.length === 0) + return; + if (root.kind === "niri") + action.command = ["niri", "msg", "action", "focus-window", "--id", key]; + else if (root.kind === "hyprland") + action.command = ["hyprctl", "dispatch", "focuswindow", "address:" + key]; + else + return; + action.running = false; + action.running = true; + } + + Process { + id: query + running: false + + stdout: StdioCollector { + onStreamFinished: { + if (!root.acceptStream) + return; + root.acceptStream = false; + + let parsed = null; + try { + parsed = root.unwrapJson(this.text); + } catch (e) { + console.warn("window-carousel: could not read compositor data:", e); + } + + if (root.queryStage === "workspace") { + root.parseWorkspace(parsed); + if (root.workspaceId < 0) { + console.warn("window-carousel: no focused workspace reported"); + root.windows = []; + root.finishQuery(); + return; + } + root.startStage("windows"); + return; + } + root.parseWindows(parsed); + root.finishQuery(); + } + } + } + + Process { + id: shotProc + running: false + + onExited: { + root.previewVersion += 1; + root.nextScreenshot(); + } + + stderr: StdioCollector { + onStreamFinished: { + if (this.text.trim().length > 0) + console.warn("window-carousel:", this.text.trim()); + } + } + } + + Timer { + id: shotSettle + interval: 300 + onTriggered: root.previewVersion += 1 + } + + Process { + id: action + running: false + + stderr: StdioCollector { + onStreamFinished: { + if (this.text.trim().length > 0) + console.warn("window-carousel:", this.text.trim()); + } + } + } +} diff --git a/window-carousel/Theme.qml b/window-carousel/Theme.qml new file mode 100644 index 0000000..9d0d32e --- /dev/null +++ b/window-carousel/Theme.qml @@ -0,0 +1,53 @@ +import QtQuick + +QtObject { + readonly property color textColor: "#ffffff" + readonly property color labelColor: "#a9b1d6" + readonly property color accent: "#7aa2f7" + readonly property color cardBg: "#e61a1e2a" + readonly property color cardBorder: "#40ffffff" + + // Fullscreen dim behind the carousel. + readonly property color backdrop: "#990b0e14" + + // Off by default: every niri preview screenshot also replaces the + // clipboard. Hyprland previews are live and never touch it. + readonly property bool niriScreenshots: true + + readonly property real cardWidth: 440 + readonly property real cardHeight: 320 + readonly property real cardRadius: 16 + readonly property real cardPadding: 14 + readonly property color previewBg: "#660b0e14" + readonly property real previewRadius: 8 + // Big icon shown in the preview area when there is no picture. + readonly property real iconSize: 96 + // Title row under the preview. + readonly property real rowHeight: 44 + readonly property real rowIconSize: 32 + readonly property real titleSize: 18 + readonly property real appSize: 14 + readonly property real emptySize: 22 + + // The loop is as long as the cards laid side by side, plus this gap + // between them. Its radius stays inside these limits. + readonly property real cardGap: 60 + readonly property real minRadius: 260 + // Share of the screen width the loop may take on each side of center. + readonly property real maxRadiusShare: 0.42 + + // How strongly far cards shrink. Bigger means a flatter loop. + readonly property real focalLength: 1400 + // How far the back of the loop is raised, so it shows above the front. + readonly property real backLift: 200 + // Opacity of the card at the very back. + readonly property real backOpacity: 0.35 + // Extra darkening of the back half, 0 to 1. + readonly property real backShade: 0.55 + + readonly property int turnDuration: 320 + + // A swipe must cross this many pixels (or angleDelta units) before the + // loop turns one step. + readonly property int swipeThreshold: 120 +} diff --git a/window-carousel/WindowCard.qml b/window-carousel/WindowCard.qml new file mode 100644 index 0000000..b4e904f --- /dev/null +++ b/window-carousel/WindowCard.qml @@ -0,0 +1,147 @@ +import QtQuick +import Quickshell +import Quickshell.Wayland + +Rectangle { + id: root + + required property Theme theme + required property string title + required property string app + required property bool selected + required property bool current + // 0 at the front of the loop, 1 at the very back. + required property real shade + // Live capture source on Hyprland, null elsewhere. + required property Toplevel toplevel + // Screenshot file on niri, "" elsewhere. + required property string previewFile + // Changes whenever the screenshot files are rewritten. + required property int previewVersion + + signal activated + + readonly property string iconSource: Quickshell.iconPath(root.app, "application-x-executable") + + width: root.theme.cardWidth + height: root.theme.cardHeight + radius: root.theme.cardRadius + color: root.theme.cardBg + border.width: root.selected ? 3 : 1 + border.color: root.selected ? root.theme.accent : root.theme.cardBorder + antialiasing: true + + Rectangle { + id: previewArea + anchors.top: parent.top + anchors.left: parent.left + anchors.right: parent.right + anchors.bottom: infoRow.top + anchors.margins: root.theme.cardPadding + radius: root.theme.previewRadius + color: root.theme.previewBg + clip: true + + ScreencopyView { + id: liveView + + // Keeps the window's own shape inside the preview area. + readonly property real aspect: liveView.sourceSize.height > 0 ? liveView.sourceSize.width / liveView.sourceSize.height : 16 / 9 + + anchors.centerIn: parent + width: Math.min(parent.width, parent.height * liveView.aspect) + height: liveView.width / liveView.aspect + captureSource: root.toplevel + live: true + visible: root.toplevel !== null && liveView.hasContent + } + + Image { + id: shotView + anchors.fill: parent + source: root.previewFile.length > 0 ? "file://" + root.previewFile + "?v=" + root.previewVersion : "" + // The file is rewritten in place, so never reuse an old copy. + cache: false + asynchronous: true + sourceSize.width: root.theme.cardWidth * 2 + fillMode: Image.PreserveAspectFit + smooth: true + mipmap: true + visible: shotView.status === Image.Ready + } + + Image { + anchors.centerIn: parent + width: root.theme.iconSize + height: root.theme.iconSize + source: root.iconSource + sourceSize.width: root.theme.iconSize * 2 + sourceSize.height: root.theme.iconSize * 2 + fillMode: Image.PreserveAspectFit + smooth: true + mipmap: true + visible: !liveView.visible && !shotView.visible + } + } + + Row { + id: infoRow + anchors.left: parent.left + anchors.right: parent.right + anchors.bottom: parent.bottom + anchors.leftMargin: root.theme.cardPadding + anchors.rightMargin: root.theme.cardPadding + anchors.bottomMargin: root.theme.cardPadding + height: root.theme.rowHeight + spacing: 10 + + Image { + anchors.verticalCenter: parent.verticalCenter + width: root.theme.rowIconSize + height: root.theme.rowIconSize + source: root.iconSource + sourceSize.width: root.theme.rowIconSize * 2 + sourceSize.height: root.theme.rowIconSize * 2 + fillMode: Image.PreserveAspectFit + smooth: true + mipmap: true + } + + Column { + anchors.verticalCenter: parent.verticalCenter + width: parent.width - root.theme.rowIconSize - parent.spacing + + Text { + width: parent.width + elide: Text.ElideRight + color: root.theme.textColor + font.pixelSize: root.theme.titleSize + text: root.title.length > 0 ? root.title : "Untitled" + } + + Text { + width: parent.width + elide: Text.ElideRight + color: root.current ? root.theme.accent : root.theme.labelColor + font.pixelSize: root.theme.appSize + text: { + const name = root.app.length > 0 ? root.app : "unknown app"; + return root.current ? name + " ยท focused" : name; + } + } + } + } + + // Darkens cards toward the back of the loop. + Rectangle { + anchors.fill: parent + radius: parent.radius + color: "black" + opacity: root.shade * root.theme.backShade + } + + MouseArea { + anchors.fill: parent + onClicked: root.activated() + } +} diff --git a/window-carousel/shell.qml b/window-carousel/shell.qml new file mode 100644 index 0000000..bd08fc7 --- /dev/null +++ b/window-carousel/shell.qml @@ -0,0 +1,46 @@ +import QtQuick +import Quickshell +import Quickshell.Io + +ShellRoot { + id: shell + + property bool open: false + + Compositor { + id: compositor + niriScreenshots: theme.niriScreenshots + } + + Theme { + id: theme + } + + Component.onCompleted: { + if (compositor.kind === "") + console.warn("window-carousel: neither niri nor Hyprland was detected"); + } + + IpcHandler { + target: "windows" + + function toggle(): void { + shell.open = !shell.open; + } + + function open(): void { + shell.open = true; + } + + function close(): void { + shell.open = false; + } + } + + CarouselView { + shellOpen: shell.open + compositor: compositor + theme: theme + onRequestClose: shell.open = false + } +}