Source: shellfish-ui/ui/FSTreeNode.shui

shellfish-ui/ui/FSTreeNode.shui

/*******************************************************************************
This file is part of the Shellfish UI toolkit.
Copyright (c) 2020 - 2023 Martin Grimme <martin.grimme@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************/

require "shellfish/ui";

/**
 * Element representing a recursive filesystem tree node.
 *
 * @memberof ui
 * @name FSTreeNode
 * @class
 */
DropArea {
    id: node

    property target: null

    property filesystem: null
    property name: ""
    property path: ""

    property collapsed: true
    property indentation: 0

    property targetPath: target ? target.path : ""

    fillWidth: true

    onTargetPathChanged: () =>
    {
        if (target &&
            path !== "" &&
            targetPath.length > path.length &&
            targetPath.startsWith(path))
        {
            collapsed = false;
        }
    }

    onDropAccept: (ev) =>
    {
        if (ev.types.includes("text/plain"))
        {
            ev.accepted = true;
        }
    }

    onDrop: (ev) =>
    {
        const uris = ev.data["text/plain"].split("\r\n");
        uris.forEach(uri =>
        {
            const fs = node.filesystem;
            const name = fs.filename(uri);
            if (fs.dirname(uri) !== node.path)
            {
                fs.move(uri, fs.pathJoin(node.path, name));
            }
        });
    }

    MouseBox {
        property itemHovered: node.path !== target.path &&
                              ((containsMouse || parent.accepted) && thisDocument.inputDevice !== "touch") &&
                              ! pressed
        property highlighted: node.path === target.path ||
                              pressed

        fillWidth: true
        height: theme.itemHeightSmall
        canFocus: true

        color: itemHovered ? theme.hoverBackgroundColor
                           : highlighted ? theme.highlightBackgroundColor
                                         : "transparent"

        layout: "center-row"

        onClick: () =>
        {
            if (node.target)
            {
                node.target.path = node.path;
            }
        }

        onDoubleClick: () =>
        {
            node.collapsed = ! node.collapsed;
        }

        Button {
            marginLeft: node.indentation * theme.paddingLarge
            flat: true
            icon: node.collapsed ? "core-arrow_right"
                                 : "core-arrow_down"

            onClick: (ev) =>
            {
                ev.accepted = true;
                node.collapsed = ! node.collapsed;
            }
        }

        Label {
            color: parent.highlighted || parent.itemHovered ? theme.highlightColor : theme.primaryColor
            text: node.path === "/" ? "[icon:core-drive]"
                                    : "[icon:core-folder]"
        }

        Label {
            fillWidth: true
            marginLeft: theme.paddingSmall
            bold: node.target && node.target.path === node.path
            color: parent.highlighted || parent.itemHovered ? theme.highlightColor : theme.primaryColor
            text: escapeMarkup(node.name)
        }

        FocusIndicator { visible: parent.focus }
    }

    Repeater {
        id: repeater

        model: FSModel {
            filesystem: node.collapsed ? null : node.filesystem
            path: node.path
            filter: makeFilter(true, false, true)
        }

        delegate: template FSTreeNode {
            target: node.target
            filesystem: node.filesystem
            name: modelData.value.name
            path: modelData.value.path
            indentation: node.indentation + 1
        }
    }

}