Source: shellfish-ui/ui/Menu.shui

shellfish-ui/ui/Menu.shui

/*******************************************************************************
This file is part of the Shellfish UI toolkit.
Copyright (c) 2020 - 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/low" as low;
require "shellfish/ui";

/**
 * Element representing a drow-down menu.
 *
 * @memberof ui
 * @name Menu
 * @class
 * @extends ui.Overlay
 *
 * @property {bool} menuOpened - [readonly] Whether the menu is currently opened.
 * @property {number} reservedHeight - (default: `6`) The minimum height reserved for this menu. Use when you want to create menu items dynamically on scrolling.
 */
Overlay {
    id: menuOverlay

    container default: menuBoxContent

    property menuOpened: visible
    property menu: menuBox

    /**
     * Shows the menu next to a parent element.
     *
     * The menu is shown below or above the given parent element, depending
     * on the screen space. If `{ horizontal: true }` is passed for options,
     * the menu is instead shown right or left to the parent element.
     *
     * If `null` is passed for the parent element, the menu is positioned near
     * the mouse pointer.
     *
     * @memberof ui.Menu.prototype
     * @name popup
     * @method
     *
     * @param {html.Item} parent - The parent element, or `null`.
     * @param {object} [options = { }] - A dictionary of options.
     */
    function popup(parent, options)
    {
        options = options || { };

        if (parent)
        {
            const parentBbox = {
                x: parent.bboxX,
                y: parent.bboxY,
                width: parent.bboxWidth,
                height: parent.bboxHeight
            };
            if (options.horizontal)
            {
                menuBox.anchorX = parentBbox.x + parentBbox.width;
                menuBox.anchorY = parentBbox.y;
                menuBox.anchorWidth = parentBbox.width;
                menuBox.anchorHeight = 0;
            }
            else
            {
                menuBox.anchorX = parentBbox.x;
                menuBox.anchorY = parentBbox.y + parentBbox.height;
                menuBox.anchorWidth = 0;
                menuBox.anchorHeight = parentBbox.height;
            }
        }
        else
        {
            menuBox.anchorX = thisDocument.pointerX + 4;
            menuBox.anchorY = thisDocument.pointerY + 4;
            menuBox.anchorWidth = 0;
            menuBox.anchorHeight = 0;
        }

        show();
    }

    event selectionMove

    /**
     * Is triggered when the menu cascade (this menu and all its submenus) is closing.
     *
     * @memberof ui.Menu.prototype
     * @name cascadingClose
     * @event
     */
    event cascadingClose

    visible: false
    //opacity: visible ? 1 : 0
    //opacityTransition: NumberAnimation { duration: 400; easing: "InOutQuad" }

    onPointerDown: (ev) =>
    {
        ev.accepted = true;
        cascadingClose();
        close();
    }

    // cheap shadow box
    Box {
        position: "global"
        x: menuBox.bboxX + 2
        y: menuBox.bboxY + 2
        width: menuBox.bboxWidth
        height: menuBox.bboxHeight
        color: "black"
        opacity: 0.2
    }

    Box {
        id: menuBox

        property anchorX: 0
        property anchorY: 0
        property anchorWidth: 0
        property anchorHeight: 0

        property spaceAbove: anchorY - anchorHeight
        property spaceBelow: menuOverlay.bboxHeight - anchorY
        property showBelow: spaceBelow > menuBoxContent.bboxHeight ? true
                                                                   : spaceBelow > spaceAbove

        property spaceLeft: anchorX - anchorWidth
        property spaceRight: menuOverlay.bboxWidth - anchorX
        property showRight: spaceRight > bboxWidth ? true
                                                   : spaceRight > spaceLeft

        position: "global"
        overflowBehavior: "scroll"

        x: showRight ? anchorX
                     : anchorWidth > 0 ? anchorX - anchorWidth - bboxWidth
                                       : menuOverlay.bboxWidth - bboxWidth
        y: showBelow ? anchorY
                     : anchorY - anchorHeight - height

        height: Math.min((showBelow ? spaceBelow : spaceAbove) - theme.paddingLarge, menuBoxContent.bboxHeight + 3 /* borders */)
        color: theme.primaryBackgroundColor
        borderWidth: 1
        borderColor: theme.borderColor

        MouseBox {
            id: menuBoxContent

            property isMenu: true

            onPointerDown: (ev) => { ev.accepted = true; }

            onClick: (ev) =>
            {
                ev.accepted = true;

                wait(100).then(() =>
                {
                    menuOverlay.cascadingClose();
                    menuOverlay.close();
                });
            }

            onKeyDown: (ev) =>
            {
                if (ev.key === "Escape" || ev.key === " " || ev.key === "Enter")
                {
                    ev.accepted = true;

                    menuOverlay.cascadingClose();
                    menuOverlay.close();
                    return;
                }

                let current = -1;
                const cs = children().filter((c) => { return c.selectItem && c.enabled && c.visible; });
                for (let i = 0; i < cs.length; ++i)
                {
                    if (cs[i].selected)
                    {
                        current = i;
                        break;
                    }
                }

                if (ev.key === "ArrowUp")
                {
                    if (current !== -1)
                    {
                        current = (cs.length + current - 1) % cs.length;
                    }
                    else
                    {
                        current = cs.length - 1;
                    }
                    ev.accepted = true;
                }
                else if (ev.key === "ArrowDown")
                {
                    if (current !== -1)
                    {
                        current = (current + 1) % cs.length;
                    }
                    else
                    {
                        current = 0;
                    }
                    ev.accepted = true;
                }
                if (current !== -1 && cs[current])
                {
                    cs[current].selectItem(false);
                }
            }
        }

        ScrollIndicator { margins: 1 }
        OverflowScroller { margins: 1 }
    }

}