repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

quickshell/nuguland/SurfaceManager.qml

Raw
import QtQuick

Item {
    id: root

    property bool popoutOpen: false
    property bool popoutClosing: false
    property string popoutPhase: "closed"
    property string popoutName: ""
    property string activeLoaderName: ""
    property string pendingLoaderName: ""
    property string removingLoaderName: ""
    property var previousContent: ({
    })
    property string lastInteraction: ""
    property string lastCloseReason: ""
    property rect popoutAnchor: Qt.rect(0, 0, 0, 0)
    property real popoutProgress: 0
    property int screenWidth: 0
    property int screenHeight: 0
    property int barHeight: 34
    property string orientation: "horizontal"
    readonly property bool vertical: orientation === "vertical"
    property var anchorRegistry: ({
    })
    readonly property int closeRetentionMs: 0
    readonly property real goldenRatio: 1.61803

    function parseRect(text) {
        const parts = String(text).split(/[, x]+/).filter((p) => {
            return p.length > 0;
        }).map(Number);
        if (parts.length !== 4 || parts.some((n) => {
            return !Number.isFinite(n);
        }))
            return Qt.rect(0, 0, 0, 0);

        return Qt.rect(parts[0], parts[1], parts[2], parts[3]);
    }

    function rectObject(rect) {
        return {
            "x": Math.round(rect.x),
            "y": Math.round(rect.y),
            "width": Math.round(rect.width),
            "height": Math.round(rect.height)
        };
    }

    function popoutWidth() {
        const along = vertical ? Math.max(0, screenWidth - barHeight) : screenWidth;
        if (along <= 0)
            return 844;

        return Math.max(520, Math.min(920, Math.round(along / goldenRatio)));
    }

    function heightBudget(wanted) {
        const reserve = vertical ? 48 : barHeight + 48;
        const maxHeight = screenHeight > 0 ? Math.max(260, screenHeight - reserve) : 680;
        return Math.min(maxHeight, Math.max(220, Number(wanted)));
    }

    function popoutContentSize(name) {
        const width = popoutWidth();
        switch (String(name)) {
        case "clock":
            return {
                "width": width,
                "height": heightBudget(580)
            };
        case "system":
            // two equal cards side-by-side + issues band + top-procs row; the
            // golden-ratio width already gives ~840px here (room for both cards),
            // so only the height budget grows for the stacked sections.
            return {
                "width": width,
                "height": heightBudget(600)
            };
        case "notifications":
            return {
                "width": width,
                "height": heightBudget(460)
            };
        case "tray":
            return {
                "width": width,
                "height": heightBudget(440)
            };
        case "media":
            return {
                "width": width,
                "height": heightBudget(420)
            };
        case "clipboard":
            return {
                "width": width,
                "height": heightBudget(560)
            };
        case "launcher":
            return {
                "width": width,
                "height": heightBudget(560)
            };
        case "audio":
            return {
                "width": width,
                "height": heightBudget(520)
            };
        case "brightness":
            return {
                "width": width,
                "height": heightBudget(260)
            };
        case "connectivity":
            return {
                "width": width,
                "height": heightBudget(540)
            };
        case "power":
            return {
                "width": width,
                "height": heightBudget(480)
            };
        default:
            return {
                "width": width,
                "height": heightBudget(300)
            };
        }
    }

    function popupWindowAnchorRect() {
        if (vertical)
            return {
                "x": Math.max(1, Math.round(barHeight)),
                "y": 0,
                "width": 1,
                "height": screenHeight > 0 ? screenHeight : popoutContentSize(popoutName).height
            };

        return {
            "x": 0,
            "y": Math.max(1, Math.round(barHeight)),
            "width": screenWidth > 0 ? screenWidth : popoutContentSize(popoutName).width,
            "height": 1
        };
    }

    function popupCardFrame(progress) {
        const t = Math.max(0, Math.min(1, Number(progress)));
        const contentSize = popoutContentSize(popoutName);
        if (vertical) {
            const surfaceHeight = screenHeight > 0 ? screenHeight : contentSize.height;
            return {
                "source": "left-bar-slide-fade",
                "progress": t,
                "currentRect": {
                    "x": Math.round(barHeight - 18 + 26 * t),
                    "y": Math.max(8, Math.round((surfaceHeight - contentSize.height) / 2) + 8),
                    "width": Math.max(1, Math.round(contentSize.width - 16)),
                    "height": Math.max(1, Math.round(contentSize.height - 16))
                },
                "opacity": t
            };
        }
        const surfaceWidth = screenWidth > 0 ? screenWidth : contentSize.width;
        return {
            "source": "simple-center-slide-fade",
            "progress": t,
            "currentRect": {
                "x": Math.max(8, Math.round((surfaceWidth - contentSize.width) / 2) + 8),
                "y": Math.round(barHeight - 18 + 26 * t),
                "width": Math.max(1, Math.round(contentSize.width - 16)),
                "height": Math.max(1, Math.round(contentSize.height - 16))
            },
            "opacity": t
        };
    }

    function popoutContentCatalog() {
        return JSON.stringify({
            "host": "PopoutHost.qml",
            "loaderPolicy": "single-active-loader-with-close-retention",
            "completeContent": true,
            "entries": {
                "clock": {
                    "component": "calendar-agenda",
                    "source": "Time.qml",
                    "visual": ["clock", "date", "calendar-grid", "agenda"]
                },
                "system": {
                    "component": "system-resources",
                    "source": "Resources.qml",
                    "visual": ["identity-card", "live-metrics", "sparklines", "issues-band", "top-processes"]
                },
                "notifications": {
                    "component": "notification-history",
                    "source": "Notifications.qml",
                    "visual": ["summary", "attention", "actions"]
                },
                "tray": {
                    "component": "tray-menu-host",
                    "source": "Tray.qml",
                    "visual": ["items", "attention", "native-menu-host"]
                },
                "media": {
                    "component": "media-controls",
                    "source": "Media.qml",
                    "visual": ["active-player", "transport", "progress", "selection"]
                },
                "clipboard": {
                    "component": "clipboard-history",
                    "source": "Clipboard.qml",
                    "visual": ["search", "cards", "thumbnails", "private-mode"]
                },
                "launcher": {
                    "component": "launcher-search",
                    "source": "Launcher.qml",
                    "visual": ["provider-chips", "single-list", "preview", "bottom-search"]
                },
                "audio": {
                    "component": "audio-mixer",
                    "source": "Audio.qml",
                    "visual": ["sinks", "sources", "streams", "mute"]
                },
                "brightness": {
                    "component": "brightness-control",
                    "source": "Brightness.qml",
                    "visual": ["level", "luminance-shader", "osd"]
                },
                "connectivity": {
                    "component": "connectivity-panel",
                    "source": "Network.qml+Bluetooth.qml",
                    "visual": ["network", "wifi", "bluetooth", "devices"]
                },
                "power": {
                    "component": "power-battery",
                    "source": "Power.qml",
                    "visual": ["battery", "profile", "session", "confirmation"]
                }
            }
        });
    }

    function registerAnchor(name, anchorText) {
        const next = Object.assign({
        }, anchorRegistry);
        next[String(name)] = rectObject(parseRect(anchorText));
        anchorRegistry = next;
        return registeredAnchors();
    }

    function registeredAnchors() {
        return JSON.stringify(anchorRegistry);
    }

    function setPopoutScreen(width, height, topBarThickness, orient) {
        screenWidth = Math.max(0, Number(width));
        screenHeight = Math.max(0, Number(height));
        barHeight = Math.max(1, Number(topBarThickness || barHeight));
        if (orient === "horizontal" || orient === "vertical")
            orientation = orient;

        return popoutState();
    }

    function clampedTarget(origin, contentSize) {
        const edges = [];
        if (vertical) {
            let y = Math.round(origin.y - contentSize.height / 2);
            if (y < 0) {
                y = 0;
                edges.push("top");
            }
            if (screenHeight > 0 && y + contentSize.height > screenHeight) {
                y = Math.max(0, screenHeight - contentSize.height);
                edges.length = 0;
                edges.push("bottom");
            }
            return {
                "rect": {
                    "x": Math.max(0, Math.round(barHeight)),
                    "y": y,
                    "width": contentSize.width,
                    "height": contentSize.height
                },
                "edges": edges
            };
        }
        let x = Math.round(origin.x - contentSize.width / 2);
        if (x < 0) {
            x = 0;
            edges.push("left");
        }
        if (screenWidth > 0 && x + contentSize.width > screenWidth) {
            x = Math.max(0, screenWidth - contentSize.width);
            edges.length = 0;
            edges.push("right");
        }
        return {
            "rect": {
                "x": x,
                "y": origin.y,
                "width": contentSize.width,
                "height": contentSize.height
            },
            "edges": edges
        };
    }

    function openPopout(name, anchorText) {
        const nextName = String(name);
        const wasOpen = popoutOpen && !popoutClosing;
        unloadTimer.stop();
        openTimer.stop();
        popoutClosing = false;
        lastInteraction = "";
        lastCloseReason = "";
        popoutName = nextName;
        activeLoaderName = nextName;
        pendingLoaderName = "";
        removingLoaderName = "";
        previousContent = ({
        });
        popoutAnchor = parseRect(anchorText);
        popoutOpen = true;
        if (wasOpen) {
            popoutProgress = 1;
            popoutPhase = "open";
        } else {
            popoutPhase = "opening";
            popoutProgress = 0;
            openTimer.restart();
        }
        return popoutState();
    }

    function openRegisteredPopout(name) {
        const anchor = anchorRegistry[String(name)] || {
            "x": 0,
            "y": 0,
            "width": 0,
            "height": 0
        };
        return openPopout(name, `${anchor.x},${anchor.y},${anchor.width},${anchor.height}`);
    }

    function hoverRegisteredPopout(name) {
        if (!popoutOpen)
            return popoutState();

        openRegisteredPopout(name);
        lastInteraction = "hover-switch";
        return popoutState();
    }

    function closePopout(reason) {
        if (!popoutOpen && !popoutClosing)
            return popoutState();

        openTimer.stop();
        if (reason && String(reason).length > 0)
            lastCloseReason = String(reason);

        popoutOpen = false;
        popoutClosing = false;
        popoutProgress = 0;
        popoutPhase = "closed";
        popoutName = "";
        activeLoaderName = "";
        pendingLoaderName = "";
        removingLoaderName = "";
        previousContent = ({
        });
        return popoutState();
    }

    function popoutInteraction(kind) {
        lastInteraction = String(kind);
        if (lastInteraction === "inside")
            return popoutState();

        if (lastInteraction === "outside" || lastInteraction === "escape" || lastInteraction === "focus-grab-outside" || lastInteraction === "escape-key")
            return closePopout(lastInteraction);

        return popoutState();
    }

    function popoutState() {
        const contentSize = popoutContentSize(popoutName);
        const origin = vertical ? {
            "x": Math.round(popoutAnchor.x + popoutAnchor.width),
            "y": Math.round(popoutAnchor.y + popoutAnchor.height / 2)
        } : {
            "x": Math.round(popoutAnchor.x + popoutAnchor.width / 2),
            "y": Math.round(popoutAnchor.y + popoutAnchor.height)
        };
        const target = clampedTarget(origin, contentSize);
        return JSON.stringify({
            "open": popoutOpen,
            "closing": popoutClosing,
            "phase": popoutPhase,
            "name": popoutName,
            "lastInteraction": lastInteraction,
            "lastCloseReason": lastCloseReason,
            "anchor": rectObject(popoutAnchor),
            "origin": origin,
            "contentSize": contentSize,
            "popupAnchorRect": popupWindowAnchorRect(),
            "popupCardFrame": popupCardFrame(popoutProgress),
            "activeLoaderName": activeLoaderName,
            "pendingLoaderName": pendingLoaderName,
            "removingLoaderName": removingLoaderName,
            "previousContent": previousContent,
            "screen": {
                "width": screenWidth,
                "height": screenHeight,
                "barHeight": barHeight
            },
            "progress": popoutProgress,
            "transition": {
                "host": "bar",
                "animation": vertical ? "left-bar-slide-fade" : "simple-center-slide-fade",
                "direction": vertical ? (popoutClosing ? "left-out" : "right-in") : (popoutClosing ? "up-out" : "down-in"),
                "loaderActive": popoutOpen,
                "targetRect": popupWindowAnchorRect()
            }
        });
    }

    Timer {
        id: openTimer

        interval: root.closeRetentionMs
        repeat: false
        onTriggered: {
            if (!root.popoutOpen || root.popoutClosing)
                return ;

            root.popoutProgress = 1;
            root.popoutPhase = "open";
        }
    }

    Timer {
        id: unloadTimer

        interval: root.closeRetentionMs
        repeat: false
        onTriggered: {
            root.popoutClosing = false;
            root.popoutPhase = "closed";
            root.popoutName = "";
            root.activeLoaderName = "";
            root.pendingLoaderName = "";
            root.removingLoaderName = "";
            root.previousContent = ({
            });
        }
    }

}