Source: shellfish-ui/ui/Dialog.shui

shellfish-ui/ui/Dialog.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 dialog window.
 *
 * The size of the dialog is determined by its content area, which adapts to
 * its contents by default.
 *
 * If you want to set an explicit size initially, you can do so in the
 * `initialization` handler.
 *
 *     onInitialization: () =>
 *     {
 *         thisContentArea.width = 640;
 *         thisContentArea.height = 400;
 *     }
 *
 * Containers:
 * * `buttons` - The buttons box.
 *
 * @memberof ui
 * @class
 * @name Dialog
 * @extends ui.Overlay
 *
 * @property {string} title - (default: `""`) The title text.
 * @property {html.Box} thisContentArea - [readonly] A reference to this window's content area visible to sub-elements.
 */
Overlay {
    id: dialog

    container default: contentArea
    container buttons: buttonBox

    property shortcutReceiver: self

    property title: ""

    property thisContentArea: contentArea

    dim: true

    Window {
        id: win

        property offset: 1.0

        position: "free"
        x: (parent.bboxWidth - bboxWidth) / 2
        y: (parent.bboxHeight - bboxHeight) / 2 - (offset * theme.itemHeightLarge)
        width: -1
        height: -1
        maxWidth: thisDocument.bboxWidth - 2 * theme.paddingLarge
        maxHeight: thisDocument.bboxHeight - 2 * theme.paddingLarge

        offsetTransition: NumberAnimation { duration: 500; easing: "InOutQuad" }

        color: theme.primaryBackgroundColor
        opacity: win.pressed ? 0.4 : 1 * (1.0 - offset)

        onInitialization: () =>
        {
            offset = 0.0;
        }

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

        // titlebar
        into title WindowTitle {
            showClose: false
            showMaximize: false
            showMinimize: false

            text: dialog.title
        }

        // content
        Box {
            id: contentArea

            property outerWidth: thisWindow.thisContentArea.width
            property outerHeight: thisWindow.thisContentArea.height

            marginTop: theme.paddingLarge
            marginLeft: theme.paddingLarge
            marginRight: theme.paddingLarge
            marginBottom: theme.paddingLarge
            color: theme.primaryBackgroundColor
            overflowBehavior: "scroll"

            onOuterWidthChanged: () =>
            {
                const newWidth = outerWidth - (marginLeft + marginRight);
                if (outerWidth !== newWidth)
                {
                    width = newWidth;
                }
            }

            onOuterHeightChanged: () =>
            {
                const newHeight = outerHeight - (marginTop + marginBottom + buttonsArea.height);
                if (outerHeight !== newHeight)
                {
                    height = newHeight;
                }
            }

            onWidthChanged: () =>
            {
                const newOuterWidth = width + (marginLeft + marginRight);
                if (outerWidth !== newOuterWidth)
                {
                    thisWindow.thisContentArea.width = newOuterWidth;
                }
            }

            onHeightChanged: () =>
            {
                const newOuterHeight = height + (marginTop + marginBottom + buttonsArea.height);
                if (outerHeight !== newOuterHeight)
                {
                    thisWindow.thisContentArea.height = newOuterHeight;
                }
            }
        }

        ScrollIndicator {
            height: contentArea.bboxHeight + 2 * theme.paddingLarge
            target: contentArea
        }

        // buttons
        Box {
            id: buttonsArea

            origin: "bottom-left"
            fillWidth: true
            height: theme.itemHeightMedium + theme.paddingSmall
            color: theme.secondaryBackgroundColor

    	    Box {
                id: buttonBox

                fillWidth: true
                fillHeight: true
                marginTop: theme.paddingSmall / 2
                marginLeft: theme.paddingSmall
                marginRight: theme.paddingSmall
                marginBottom: theme.paddingSmall / 2
                layout: "center-row"
    
                Box { fillWidth: true }
            }
        }

    }

}