repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

quickshell/nuguland/Screenshots.qml

Raw
import QtQuick
import Quickshell

Item {
    id: root

    property QtObject niriService
    property QtObject osdService
    property string lastColor: ""
    property var recent: []
    property var lastAction: ({
    })
    property var selection: ({
        "model": "normalized-cross-screen-rect",
        "phase": "idle",
        "press": {
            "x": 0,
            "y": 0
        },
        "current": {
            "x": 0,
            "y": 0
        },
        "rect": {
            "x": 0,
            "y": 0,
            "width": 0,
            "height": 0
        },
        "cursor": "default",
        "completionFlash": false
    })
    property var lastOsd: ({
    })
    readonly property bool nativeActionRoute: !!niriService && !!niriService.available
    readonly property var nativeActions: ["screenshot-screen", "screenshot-window", "screenshot", "pick-color"]
    readonly property var boundedActions: ["focused-screen", "focused-window", "interactive", "select-region", "pick-color", "copy", "save", "open-record", "clear"]
    readonly property var toolbarActions: ["copy", "save", "cancel", "color"]

    function rectFromPoints(start, end) {
        const x = Math.min(start.x, end.x);
        const y = Math.min(start.y, end.y);
        return {
            "x": x,
            "y": y,
            "width": Math.abs(end.x - start.x),
            "height": Math.abs(end.y - start.y)
        };
    }

    function record(id, kind, path) {
        const next = [{
            "id": id,
            "kind": kind,
            "path": path,
            "source": "niri-native",
            "saved": true
        }];
        for (let i = 0; i < recent.length; i++) {
            if (recent[i].id !== id)
                next.push(recent[i]);

        }
        recent = next.slice(0, 12);
    }

    function cornerHandles(rect) {
        const size = rect.width > 0 && rect.height > 0 ? 14 : 0;
        return [{
            "id": "top-left",
            "x": rect.x,
            "y": rect.y,
            "size": size,
            "role": "ui.accent"
        }, {
            "id": "top-right",
            "x": rect.x + rect.width,
            "y": rect.y,
            "size": size,
            "role": "ui.accent"
        }, {
            "id": "bottom-left",
            "x": rect.x,
            "y": rect.y + rect.height,
            "size": size,
            "role": "ui.accent"
        }, {
            "id": "bottom-right",
            "x": rect.x + rect.width,
            "y": rect.y + rect.height,
            "size": size,
            "role": "ui.accent"
        }];
    }

    function selectionVisuals(selectionState) {
        const rect = selectionState.rect || {
            "x": 0,
            "y": 0,
            "width": 0,
            "height": 0
        };
        const active = selectionState.phase === "dragging" || selectionState.phase === "completed";
        return {
            "source": "Nuguland screenSnip plus Nuguland Geom.qml",
            "parts": ["dimmed-mask", "clear-cutout", "corner-handles", "dimensions", "toolbar-actions"],
            "dimmedMask": {
                "kind": "dimmed-mask",
                "visible": active,
                "opacity": active ? 0.42 : 0,
                "role": "ui.backdrop"
            },
            "clearCutout": {
                "kind": "clear-cutout",
                "rect": rect
            },
            "cornerHandles": {
                "kind": "corner-handles",
                "handles": cornerHandles(rect)
            },
            "dimensions": {
                "kind": "dimensions",
                "text": `${Math.round(rect.width)}×${Math.round(rect.height)}`,
                "width": Math.round(rect.width),
                "height": Math.round(rect.height)
            },
            "toolbarActions": {
                "kind": "toolbar-actions",
                "actions": toolbarActions,
                "anchor": rect.height >= 48 ? "selection-top" : "screen-top"
            },
            "nugulandCornerAccents": {
                "bounded": true,
                "freePhysicsLoop": false,
                "anchors": cornerHandles(rect).map((handle) => {
                    return ({
                        "id": handle.id,
                        "x": handle.x,
                        "y": handle.y
                    });
                })
            }
        };
    }

    function screenshotsPanelContract() {
        return {
            "source": "Nuguland AreaPicker.qml plus Nuguland RegionSelector.qml WRegionSelectionPanel.qml",
            "ownedBy": "PopoutHost.qml inline ScreenshotsPanel",
            "niriCapturePanel": true,
            "captureActionGrid": true,
            "regionSelectionPreview": true,
            "recentCaptureRows": true,
            "colorPickSwatch": true,
            "toolbarActionRail": true,
            "boundedCornerAccents": true,
            "serviceActionsOnly": true,
            "captureBackend": "niri-native",
            "panelActions": ["focused-screen", "focused-window", "select-region", "pick-color"],
            "selectionParts": ["dimmed-mask", "clear-cutout", "corner-handles", "dimensions", "toolbar-actions"]
        };
    }

    function currentState() {
        return {
            "backend": "Screenshots.qml",
            "usesNiriNative": true,
            "nativeActions": nativeActions,
            "nativeActionRoute": nativeActionRoute,
            "helperProcesses": [],
            "serviceOwnedActions": true,
            "boundedActions": boundedActions,
            "screenshotsPanelContract": screenshotsPanelContract(),
            "selection": selection,
            "selectionVisuals": selectionVisuals(selection),
            "recent": recent,
            "recentCount": recent.length,
            "lastColor": lastColor,
            "lastAction": lastAction,
            "osd": lastOsd
        };
    }

    function stateJson() {
        return JSON.stringify(currentState());
    }

    function beginSelection(x, y) {
        const point = {
            "x": x,
            "y": y
        };
        selection = {
            "model": "normalized-cross-screen-rect",
            "phase": "dragging",
            "press": point,
            "current": point,
            "rect": {
                "x": x,
                "y": y,
                "width": 0,
                "height": 0
            },
            "cursor": "crosshair",
            "completionFlash": false
        };
        lastAction = {
            "kind": "select-region-begin",
            "point": point
        };
        return stateJson();
    }

    function updateSelection(x, y) {
        const start = selection.press || {
            "x": 0,
            "y": 0
        };
        const current = {
            "x": x,
            "y": y
        };
        selection = Object.assign({
        }, selection, {
            "phase": "dragging",
            "current": current,
            "rect": rectFromPoints(start, current),
            "cursor": "crosshair",
            "completionFlash": false
        });
        lastAction = {
            "kind": "select-region-update",
            "point": current
        };
        return stateJson();
    }

    function completeSelection(path) {
        selection = Object.assign({
        }, selection, {
            "phase": "completed",
            "cursor": "default",
            "completionFlash": true
        });
        const target = String(path);
        const nativeAction = "screenshot";
        if (nativeActionRoute)
            niriService.screenshot(target);

        lastAction = {
            "kind": "select-region",
            "nativeAction": nativeAction,
            "path": target,
            "nativeActionRoute": nativeActionRoute
        };
        record("capture-select-region", "select-region", target);
        return stateJson();
    }

    function cancelSelection() {
        selection = Object.assign({
        }, selection, {
            "phase": "idle",
            "cursor": "default",
            "completionFlash": false
        });
        lastAction = {
            "kind": "cancel"
        };
        return stateJson();
    }

    function capture(kind, path) {
        const actionKind = String(kind);
        const target = String(path);
        let nativeAction = "screenshot";
        let id = `capture-${actionKind}`;
        if (actionKind === "focused-screen") {
            nativeAction = "screenshot-screen";
            id = "capture-focused-screen";
            if (nativeActionRoute)
                niriService.screenshotScreen(target);

        } else if (actionKind === "focused-window") {
            nativeAction = "screenshot-window";
            id = "capture-focused-window";
            if (nativeActionRoute)
                niriService.screenshotWindow(target);

        } else if (actionKind === "interactive") {
            nativeAction = "screenshot";
            if (nativeActionRoute)
                niriService.screenshot(target);

        }
        lastAction = {
            "kind": actionKind,
            "nativeAction": nativeAction,
            "path": target,
            "nativeActionRoute": nativeActionRoute,
            "interactive": actionKind === "interactive"
        };
        record(id, actionKind, target);
        return stateJson();
    }

    function acceptPickedColor(color) {
        const value = String(color || "").toLowerCase();
        if (!/^#[0-9a-f]{6}$/.test(value))
            return ;

        lastColor = value;
        lastAction = Object.assign({
        }, lastAction, {
            "color": lastColor,
            "completed": true
        });
        if (osdService) {
            try {
                lastOsd = JSON.parse(osdService.trigger("color", lastColor));
            } catch (error) {
                lastOsd = {
                    "event": "color",
                    "value": lastColor
                };
            }
        } else {
            lastOsd = {
                "event": "color",
                "value": lastColor
            };
        }
    }

    function pickColor() {
        lastColor = "";
        if (nativeActionRoute)
            niriService.pickColor();

        lastAction = {
            "kind": "pick-color",
            "nativeAction": "pick-color",
            "color": lastColor,
            "nativeActionRoute": nativeActionRoute,
            "interactive": true,
            "completed": false
        };
        lastOsd = {
            "event": "color",
            "value": "picking"
        };
        return stateJson();
    }

    function recordAction(kind, id) {
        lastAction = {
            "kind": String(kind),
            "id": String(id),
            "nativeActionRoute": false
        };
        return stateJson();
    }

    function clear() {
        recent = [];
        lastAction = {
            "kind": "clear"
        };
        return stateJson();
    }

    Connections {
        function onLastPickedColorChanged() {
            root.acceptPickedColor(root.niriService.lastPickedColor);
        }

        target: root.niriService
    }

}