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 var anchorRegistry: ({
    })
    property string gestureState: "idle"
    property var gestureStart: ({
        "x": 0,
        "y": 0
    })
    property var gestureCurrent: ({
        "x": 0,
        "y": 0
    })
    property var gesturePath: []
    property var gestureCommitRect: ({
    })
    readonly property int closeRetentionMs: 100
    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() {
        if (screenWidth <= 0)
            return 844;

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

    function heightBudget(wanted) {
        const maxHeight = screenHeight > 0 ? Math.max(260, screenHeight - barHeight - 48) : 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(260)
            };
        case "cpu":
            return {
                "width": width,
                "height": heightBudget(300)
            };
        case "notifications":
            return {
                "width": width,
                "height": heightBudget(460)
            };
        case "tray":
            return {
                "width": width,
                "height": heightBudget(440)
            };
        case "window":
            return {
                "width": width,
                "height": heightBudget(340)
            };
        case "media":
            return {
                "width": width,
                "height": heightBudget(420)
            };
        case "clipboard":
            return {
                "width": width,
                "height": heightBudget(500)
            };
        case "launcher":
            return {
                "width": width,
                "height": heightBudget(560)
            };
        case "audio":
            return {
                "width": width,
                "height": heightBudget(520)
            };
        case "connectivity":
            return {
                "width": width,
                "height": heightBudget(540)
            };
        case "power":
            return {
                "width": width,
                "height": heightBudget(480)
            };
        case "screenshots":
            return {
                "width": width,
                "height": heightBudget(480)
            };
        case "idle":
            return {
                "width": width,
                "height": heightBudget(340)
            };
        case "diagnostics":
            return {
                "width": width,
                "height": heightBudget(540)
            };
        default:
            return {
                "width": width,
                "height": heightBudget(300)
            };
        }
    }

    function popupWindowAnchorRect() {
        const contentSize = popoutContentSize(popoutName);
        return {
            "x": Math.max(0, Math.round((screenWidth - contentSize.width) / 2)),
            "y": Math.max(1, Math.round(barHeight)),
            "width": contentSize.width,
            "height": 1
        };
    }

    function popupCardFrame(progress) {
        const t = Math.max(0, Math.min(1, Number(progress)));
        return {
            "source": "simple-center-slide-fade",
            "progress": t,
            "currentRect": {
                "x": 8,
                "y": Math.round(-18 + 26 * t),
                "width": Math.max(1, Math.round(popoutContentSize(popoutName).width - 16)),
                "height": Math.max(1, Math.round(popoutContentSize(popoutName).height - 16))
            },
            "opacity": t
        };
    }

    function popupInputMaskFrame(progress) {
        return popupCardFrame(progress).currentRect;
    }

    function resetGesture() {
        gestureState = "idle";
        gestureStart = ({
            "x": 0,
            "y": 0
        });
        gestureCurrent = ({
            "x": 0,
            "y": 0
        });
        gesturePath = [];
        gestureCommitRect = ({
        });
    }

    function gesturePoint(x, y) {
        return {
            "x": Math.round(Number(x)),
            "y": Math.round(Number(y))
        };
    }

    function gesturePress(x, y) {
        gestureState = "armed";
        gestureStart = gesturePoint(x, y);
        gestureCurrent = gestureStart;
        gesturePath = ["press"];
        gestureCommitRect = ({
        });
        return popoutState();
    }

    function gestureDrag(x, y) {
        if (gestureState !== "armed" && gestureState !== "dragging")
            return popoutState();

        gestureState = "dragging";
        gestureCurrent = gesturePoint(x, y);
        if (gesturePath.indexOf("drag") < 0)
            gesturePath = gesturePath.concat(["drag"]);

        return popoutState();
    }

    function gestureCommit() {
        const x1 = Math.min(gestureStart.x, gestureCurrent.x);
        const y1 = Math.min(gestureStart.y, gestureCurrent.y);
        const x2 = Math.max(gestureStart.x, gestureCurrent.x);
        const y2 = Math.max(gestureStart.y, gestureCurrent.y);
        gestureState = "committed";
        gestureCommitRect = {
            "x": x1,
            "y": y1,
            "width": Math.max(0, x2 - x1),
            "height": Math.max(0, y2 - y1)
        };
        gesturePath = gesturePath.concat(["commit"]);
        return popoutState();
    }

    function gestureRelease(x, y) {
        if (gestureState !== "armed" && gestureState !== "dragging")
            return popoutState();

        gestureCurrent = gesturePoint(x, y);
        gesturePath = gesturePath.concat(["release"]);
        return gestureCommit();
    }

    function gestureCancel(reason) {
        gestureState = "cancelled";
        gestureCommitRect = ({
        });
        gesturePath = gesturePath.concat(["cancel"]);
        lastCloseReason = String(reason || "gesture-cancel");
        return popoutState();
    }

    function popoutGestureMachine() {
        return {
            "source": "Nuguland Interactions.qml",
            "ownedBy": "SurfaceManager.qml",
            "states": ["idle", "armed", "dragging", "committed", "cancelled"],
            "path": ["press", "drag", "release", "cancel", "commit"],
            "pointerParity": true,
            "keyboardParity": true,
            "compositorSpecific": false
        };
    }

    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"]
                },
                "cpu": {
                    "component": "resource-card",
                    "source": "Resources.qml",
                    "visual": ["cpu-percent", "signal-role", "kernel-facts"]
                },
                "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"]
                },
                "window": {
                    "component": "window-rule-card",
                    "source": "Niri.qml",
                    "visual": ["app-id", "title", "workspace", "rule-hints"]
                },
                "media": {
                    "component": "media-controls",
                    "source": "Media.qml",
                    "visual": ["active-player", "transport", "progress", "selection"]
                },
                "clipboard": {
                    "component": "clipboard-history",
                    "source": "Clipboard.qml",
                    "visual": ["search", "pinned", "private-mode", "launcher-rows"]
                },
                "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"]
                },
                "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"]
                },
                "screenshots": {
                    "component": "screenshot-tools",
                    "source": "Screenshots.qml",
                    "visual": ["selection", "recent", "color", "actions"]
                },
                "idle": {
                    "component": "idle-privacy",
                    "source": "Idle.qml",
                    "visual": ["inhibit", "duration", "activity", "privacy"]
                },
                "diagnostics": {
                    "component": "diagnostics-card",
                    "source": "Diagnostics.qml",
                    "visual": ["services", "processes", "surfaces", "smoke"]
                }
            }
        });
    }

    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, topBarHeight) {
        screenWidth = Math.max(0, Number(width));
        screenHeight = Math.max(0, Number(height));
        barHeight = Math.max(1, Number(topBarHeight || barHeight));
        return popoutState();
    }

    function clampedTarget(origin, contentSize) {
        let x = Math.round(origin.x - contentSize.width / 2);
        const edges = [];
        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 = "";
        resetGesture();
        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 = popoutName.length > 0;
        popoutProgress = 0;
        if (popoutClosing) {
            removingLoaderName = activeLoaderName || popoutName;
            previousContent = {
                "name": removingLoaderName,
                "size": popoutContentSize(removingLoaderName),
                "fade": "outgoingFade",
                "retentionMs": closeRetentionMs
            };
        }
        popoutPhase = popoutClosing ? "closing" : "closed";
        if (popoutClosing)
            unloadTimer.restart();

        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 popoutFocusPolicy() {
        return {
            "source": "Quickshell PopupWindow.grabFocus plus Nuguland Wrapper.qml Keys.onEscapePressed",
            "nativePopupGrabFocus": true,
            "compositorFocusGrab": false,
            "outsideCloseReason": "focus-grab-outside",
            "escapeCloseReason": "escape-key",
            "insideClicksRetain": true,
            "closeReasons": ["focus-grab-outside", "escape-key", "outside", "hideAll"]
        };
    }

    function hoverSwitchPolicy() {
        return {
            "source": "Nuguland bar.checkPopout hover switching",
            "event": "hover-switch",
            "switchesOnlyWhenOpen": true,
            "retainsPreviousDuringHoverSwitch": true,
            "anchorMorph": "none",
            "loaderPolicy": "single-popup-instant-content-replace"
        };
    }

    function loaderChoreography() {
        return {
            "policy": "single-popup-instant-content-replace",
            "source": "Nuguland single popup content switch",
            "activeLoaderName": activeLoaderName,
            "pendingLoaderName": pendingLoaderName,
            "removingLoaderName": removingLoaderName,
            "previousContent": previousContent,
            "incomingFade": {
                "from": 0,
                "to": 1,
                "ms": 90
            },
            "outgoingFade": {
                "from": 1,
                "to": 0,
                "ms": closeRetentionMs,
                "unloadAfter": true
            },
            "closeRetentionMs": closeRetentionMs,
            "sizeTween": {
                "width": false,
                "height": true,
                "ms": 90,
                "easing": "InOutCubic"
            },
            "anchorTween": {
                "rect": false,
                "origin": "center",
                "ms": 0,
                "easing": "None"
            },
            "separatorBridge": {
                "growsThroughBarSeparator": false,
                "clipsContent": true
            },
            "cachePolicy": "Loader destroys items on sourceComponent change; use persistent per-panel loaders if first-open lag remains"
        };
    }

    function popoutState() {
        const contentSize = popoutContentSize(popoutName);
        const origin = {
            "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),
            "popupInputMaskFrame": popupInputMaskFrame(popoutProgress),
            "activeLoaderName": activeLoaderName,
            "pendingLoaderName": pendingLoaderName,
            "removingLoaderName": removingLoaderName,
            "previousContent": previousContent,
            "loaderChoreography": loaderChoreography(),
            "popoutFocusPolicy": popoutFocusPolicy(),
            "hoverSwitchPolicy": hoverSwitchPolicy(),
            "popoutGestureMachine": popoutGestureMachine(),
            "gestureState": gestureState,
            "gestureStart": gestureStart,
            "gestureCurrent": gestureCurrent,
            "gesturePath": gesturePath,
            "gestureCommitRect": gestureCommitRect,
            "screen": {
                "width": screenWidth,
                "height": screenHeight,
                "barHeight": barHeight
            },
            "progress": popoutProgress,
            "transition": {
                "host": "bar",
                "animation": "simple-center-slide-fade",
                "direction": popoutClosing ? "up-out" : "down-in",
                "loaderActive": popoutOpen || popoutClosing,
                "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 = ({
            });
        }
    }

}