Source: shellfish-ui/ui/FSItem.shui

shellfish-ui/ui/FSItem.shui

/*******************************************************************************
This file is part of the Shellfish UI toolkit.
Copyright (c) 2022 - 2024 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 file item for displaying in a list view.
 *
 * @memberof ui
 * @name FSItem
 * @class
 * @extends html.MouseBox
 *
 * @property {number[]} columnWidths - (default: `[]`) An optional array for setting the widths of the columns in `list` mode.
 * @property {string} displayMode - (default: `"list"`) The display mode. One of `list|grid|compact`
 * @property {object} file - (default: `null`) The file object to represent.
 * @property {bool} highlighted - (default: `false`) If `true`, the item is highlighted.
 * @property {string} thumbnail - (default: `""`) The URL of the thumbnail. This is usually a local blob URL.
 */
MouseBox {
    id: item

    property displayMode: "list"
    property highlighted: false
    property file: null
    property columnWidths: []
    property thumbnail: ""

    color: containsMouse && ! highlighted ? theme.hoverBackgroundColor
         : highlighted ? theme.highlightBackgroundColor
                       : "transparent"

    layout: item.displayMode === "list" || item.displayMode === "compact" ? "row" : "column"

    function makeRoleLabel(file, role)
    {
        if (! file)
        {
            return "-";
        }

        if (role === "name")
        {
            if (file.type === "d")
            {
                return escapeMarkup(file.name);
            }
            else
            {
                const parts = file.name.split(".");
                let ext = "";
                if (parts.length > 1)
                {
                    ext = parts.pop();
                }
                return escapeMarkup(parts.join("."));
            }
        }
        else if (role === "mtime")
        {
            return new Date(file.mtime * 1000).toISOString().split("T")[0];
        }
        else if (role === "size")
        {
            if (file.type === "d")
            {
                return "";
            }
            else
            {
                return core.formatBytes(file.size);
            }
        }
        else if (role === "type")
        {
            if (file.type === "d")
            {
                return "Folder";
            }
            else
            {
                const parts = file.name.split(".");
                let ext = "";
                if (parts.length > 1)
                {
                    ext = parts.pop();
                }
                return ext !== "" ? ext : file.mimetype;
            }
        }

        return "-";
    }

    // icon
    Box {
        id: iconBox

        fillWidth: displayMode === "list" || item.displayMode === "compact" ? false : true
        fillHeight: displayMode === "list" || item.displayMode === "compact" ? true : false
        width: displayMode === "list" || item.displayMode === "compact" ? bboxHeight : -1
        height: displayMode === "list" || item.displayMode === "compact" ? -1 : bboxWidth

        Image {
            id: thumbnailIcon

            visible: status === "success"
            fill: true
            margins: 2
            fitMode: "contain"

            source: item.thumbnail
        }

        FSIcon {
            id: icon

            visible: ! thumbnailIcon.visible

            fill: true
            margins: 2

            fontSize: parent.bboxHeight * 0.8
            horizontalAlignment: "center"
            color: item.highlighted ? theme.highlightColor : theme.primaryColor
            mimeType: item.file.mimetype
        }
    }

    Box {
        fill: true
        layout: item.displayMode === "list" ? "row"
              : item.displayMode === "grid" ? "column"
                                            : "center-column"

        Repeater {
            model: ListModel { data: ["name", "type", "mtime", "size"] }

            delegate: template Box {
                width: item.displayMode === "list" ? (item.columnWidths[modelData.index] - (modelData.index === 0 ? iconBox.bboxWidth : 0)) || -1
                                                    : -1
                fillWidth: width === -1
                fillHeight: item.displayMode === "list"
                layout: "center"

                Label {
                    id: label

                    property listAlignment: modelData.value === "size" && item.displayMode === "list" ? "right" : "left"

                    property hasOverflow: contentWidth > Math.round(bboxWidth) ||
                                          contentHeight > Math.round(bboxHeight)

                    fillWidth: true
                    maxHeight: fontSize * 2.4
                    marginLeft: theme.paddingSmall
                    marginRight: theme.paddingSmall
                    horizontalAlignment: item.displayMode === "list" || item.displayMode === "compact" ? listAlignment : "center"
                    overflowBehavior: item.displayMode === "grid" && modelData.value === "name" ? "break"
                                                                                                : "ellipsis"
                    fontSize: item.displayMode === "grid" && modelData.value !== "name" ? theme.fontSizeSmall
                                                                                        : theme.fontSizeMedium
                    color: item.highlighted || item.containsMouse ? theme.highlightColor 
                           : modelData.value === "name" ? theme.primaryColor
                                                        :theme.secondaryColor

                    text: item.makeRoleLabel(item.file, modelData.value)
                }

                Box {
                    visible: modelData.value === "name" &&
                             item.displayMode === "grid" &&
                             label.hasOverflow
                    fillWidth: true
                    marginLeft: theme.paddingSmall
                    marginRight: theme.paddingSmall
                    height: 1
                    color: label.color
                }

                Tooltip {
                    visible: item.hovered &&
                             modelData.value === "name" &&
                             label.hasOverflow
                    text: label.text
                }
            }
        }// Repeater
    }
}