Source: shellfish-ui/ui/Tooltip.shui

shellfish-ui/ui/Tooltip.shui

/*******************************************************************************
This file is part of the Shellfish UI toolkit.
Copyright (c) 2021 - 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 tooltip popup.
 *
 * By default, the visibility of the tooltip is controlled by the `hovered`
 * status of the target element, but may also be controlled manually by changing
 * the `visible` property.
 * 
 * @example
 * Button {
 *     text: "Click Me"
 *
 *     Tooltip { text: "I am a tooltip." }
 * }
 *
 * @memberof ui
 * @name Tooltip
 * @class
 * @extends html.Object
 *
 * @property {html.Item} target - (default: `thisMouseBox`) The target element of the tooltip.
 * @property {string} text - The text to display.
 * @property {bool} visible - Whether the tooltip is visible. This property may be controlled externally, if needed.
 */
Object {
    id: tooltip

    property target: thisMouseBox
    property text: ""
    property visible: thisDocument.inputDevice === "mouse" && parent.hovered ||
                      thisDocument.inputDevice === "keyboard" && parent.focus

    onVisibleChanged: () =>
    {
        if (visible)
        {
            box = boxT();
            thisDocument.add(box);
        }
        else if (box)
        {
            box.parent = null;
            box = null;
        }
    }

    property box: null
    property boxT: template Box {

        property initialized: false

        position: "global"
        x: 0
        y: thisDocument.bboxHeight
        width: innerBox.bboxWidth
        height: innerBox.bboxHeight
        maxWidth: thisDocument.windowWidth

        onHeightChanged: () =>
        {
            if (height > 0 && ! initialized && tooltip.target)
            {
                const tbbox = {
                    x: tooltip.target.bboxX,
                    y: tooltip.target.bboxY,
                    width: tooltip.target.bboxWidth,
                    height: tooltip.target.bboxHeight
                };

                if (tbbox.x + bboxWidth < thisDocument.bboxWidth)
                {
                    x = Math.max(0, tbbox.x);
                }
                else
                {
                    x = Math.max(0, tbbox.x + tbbox.width - bboxWidth);
                }
                if (tbbox.y + tbbox.height + bboxHeight + 2 < thisDocument.bboxHeight)
                {
                    y = tbbox.y + tbbox.height + 2;
                    riseAnim.from = -height;
                }
                else
                {
                    y = tbbox.y - bboxHeight - 2;
                    riseAnim.from = height;
                }
                riseAnim.start();

                initialized = true;
            }
        }

        Box {
            id: innerBox

            position: "free"
            y: thisDocument.bboxHeight
            color: theme.contentBackgroundColor
            borderColor: theme.borderColor
            borderWidth: 1

            Label {
                marginTop: 2
                marginLeft: 2
                marginRight: 2
                marginBottom: 2
                fontSize: theme.fontSizeSmall
                text: tooltip.text
            }
        }

        NumberAnimation {
            id: riseAnim

            from: 0
            to: 0
            duration: 300
            easing: "InOutQuad"

            onNext: (v) => { innerBox.y = v; }
            onFinish: () => { innerBox.y = 0; }
        }
    }

}