Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/SurfaceManager.qml

Raw
import QtQuick
import "diagnostics.js" as DiagnosticData

Item {
    id: root

    property int operationId: 0
    property bool popupOpen: false
    property string popupPhase: "closed"
    property string popupName: ""
    property string popupOutput: ""
    property string lastCloseReason: ""
    property rect popupAnchor: Qt.rect(0, 0, 0, 0)
    property real popupProgress: 0
    property int screenWidth: 0
    property int screenHeight: 0
    property int barHeight: 34
    property string orientation: "horizontal"
    // Size of the open popup's output when it is not the bar's (PopupHost
    // resolves it); null on the bar's own. Such an output has no bar to clear.
    property var popupViewport: null
    property Niri niriService
    required property Registry registry
    property var returnWindowId: null
    readonly property bool vertical: orientation === "vertical"
    property var anchorRegistry: ({})
    // Ordinary view state lives for the shell session, never on disk.
    property var panelStates: ({})
    // The open panel's natural height (FeatureEntry.qml), set by PopupHost;
    // 0 while the panel declares none, which keeps the entry height.
    property real panelHeight: 0
    // The card animates height changes only once the open panel has drawn its
    // first frame (PopupHost), never on open or switch.
    property bool panelSettled: false
    signal dismissing(string name)

    onRegistryChanged: seedFeatureStates()
    Component.onCompleted: seedFeatureStates()

    function dismissTask() {
        dismissing(popupName);
        const feature = featureEntry(popupName);
        if (feature)
            feature.dismissed();
    }

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

    // Registry features carry their own sizes and retained state.
    function featureEntry(name) {
        return root.registry ? root.registry.popup(name) : null;
    }

    // The closed state's size: no popup entry is named while nothing is open.
    function fallbackSize() {
        return {
            "width": 520,
            "height": 300
        };
    }

    function sizeEntry(name) {
        const feature = featureEntry(String(name));
        return feature ? {
            "width": feature.width,
            "height": feature.height
        } : fallbackSize();
    }

    function seedFeatureStates() {
        for (const feature of (root.registry ? root.registry.popups : []))
            if (!root.panelStates[feature.name])
                root.panelStates[feature.name] = Object.assign({}, feature.retainedState);
    }

    function viewWidth() {
        return root.popupViewport ? root.popupViewport.width : screenWidth;
    }

    function viewHeight() {
        return root.popupViewport ? root.popupViewport.height : screenHeight;
    }

    function viewBar() {
        return root.popupViewport ? 0 : barHeight;
    }

    function emptyPanel(name) {
        const feature = featureEntry(name);
        return feature ? feature.empty : false;
    }

    function popupWidth(name) {
        const wanted = emptyPanel(name) ? 368 : sizeEntry(name).width;
        const along = vertical ? viewWidth() - viewBar() : viewWidth();
        return along > 0 ? Math.min(wanted, Math.max(1, along - 16)) : wanted;
    }

    // The open popup's card fits its panel's natural height plus the 48 px of
    // card inset and margins; the entry height stays the maximum.
    function popupHeight(name) {
        if (emptyPanel(name))
            return 104;
        const maximum = sizeEntry(name).height;
        return name === popupName && panelHeight > 0 ? Math.min(maximum, Math.ceil(panelHeight) + 48) : maximum;
    }

    // A wanted height is never inflated, only capped by the screen.
    function heightBudget(wanted) {
        const reserve = vertical ? 48 : viewBar() + 48;
        const maxHeight = viewHeight() > 0 ? Math.max(260, viewHeight() - reserve) : 680;
        return Math.min(maxHeight, Number(wanted));
    }

    function popupContentSize(name) {
        const key = String(name);
        return {
            "width": popupWidth(key),
            "height": heightBudget(popupHeight(key))
        };
    }

    function popupCardFrame(progress) {
        const t = Math.max(0, Math.min(1, Number(progress)));
        const contentSize = popupContentSize(popupName);
        if (vertical) {
            const surfaceHeight = viewHeight() > 0 ? viewHeight() : contentSize.height;
            return {
                "source": "left-bar-slide-fade",
                "progress": t,
                "currentRect": {
                    "x": Math.round(viewBar() - 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 = viewWidth() > 0 ? viewWidth() : 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(viewBar() - 18 + 26 * t),
                "width": Math.max(1, Math.round(contentSize.width - 16)),
                "height": Math.max(1, Math.round(contentSize.height - 16))
            },
            "opacity": t
        };
    }

    function registerAnchor(name, anchor: rect) {
        const next = Object.assign({}, anchorRegistry);
        next[String(name)] = anchor;
        anchorRegistry = next;
    }

    function setPopupScreen(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;
    }

    // Only registry popups open; any other name, e.g. from IPC, leaves the state as is.
    function openPopup(name, anchor: rect, outputName) {
        const nextName = String(name);
        const feature = featureEntry(nextName);
        if (!feature)
            return popupState();
        const wasOpen = popupOpen;
        if (wasOpen && popupName !== nextName)
            dismissTask();
        feature.opening();
        if (!wasOpen)
            returnWindowId = niriService ? niriService.focusedWindow : null;
        if (popupName !== nextName)
            panelSettled = false;
        operationId = DiagnosticData.nextOperationId(operationId);
        openTimer.stop();
        lastCloseReason = "";
        popupName = nextName;
        popupOutput = String(outputName || "");
        popupAnchor = anchor;
        popupOpen = true;
        if (wasOpen) {
            popupProgress = 1;
            popupPhase = "open";
        } else {
            popupPhase = "opening";
            popupProgress = 0;
            openTimer.restart();
        }
        console.info(JSON.stringify(DiagnosticData.transition(wasOpen ? "switched" : "opening", root)));
        return popupState();
    }

    function openRegisteredPopup(name, outputName) {
        const anchor = anchorRegistry[String(name)] || Qt.rect(0, 0, 0, 0);
        return openPopup(name, anchor, outputName);
    }

    function closePopup(reason) {
        if (!popupOpen)
            return popupState();

        dismissTask();
        panelSettled = false;
        operationId = DiagnosticData.nextOperationId(operationId);
        openTimer.stop();
        if (reason && String(reason).length > 0)
            lastCloseReason = String(reason);

        popupOpen = false;
        popupProgress = 0;
        popupPhase = "closed";
        popupName = "";
        popupOutput = "";
        console.info(JSON.stringify(DiagnosticData.transition("closed", root)));
        return popupState();
    }

    function popupState() {
        return JSON.stringify({
            "open": popupOpen,
            "phase": popupPhase,
            "name": popupName,
            "output": popupOutput,
            "lastCloseReason": lastCloseReason,
            "anchor": rectObject(popupAnchor),
            "contentSize": popupContentSize(popupName),
            "panelHeight": Math.ceil(panelHeight),
            "popupCardFrame": popupCardFrame(popupProgress),
            "screen": {
                "width": screenWidth,
                "height": screenHeight,
                "barHeight": barHeight
            },
            "progress": popupProgress
        });
    }

    function completeOpen() {
        if (!popupOpen || popupPhase !== "opening")
            return;

        popupProgress = 1;
        popupPhase = "open";
        console.info(JSON.stringify(DiagnosticData.transition("opened", root)));
    }

    // A fresh open completes on the next event loop turn, after the panel loads;
    // a switch or close stops it.
    Timer {
        id: openTimer

        interval: 0
        repeat: false
        onTriggered: root.completeOpen()
    }
}