Source: shellfish-ui/ui/Page.shui

shellfish-ui/ui/Page.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/html";

/**
 * Element representing a UI page.
 *
 * Stacked pages are a good way of structuring mobile applications. As you
 * go deeper into the application (e.g. by entering the settings), new pages
 * are stacked on top, and when returning, pages are removed from the stack.
 *
 * Page stacking is controlled by the {@link ui.PageStack PageStack} element.
 *
 * @class
 * @name Page
 * @extends html.MouseBox
 * @memberof ui
 *
 * @property {fengshui.Container} header - A container for displaying a page header.
 * @property {ui.Page} shortcutReceiver - [readonly] A reference to this page for receiving shortcuts.
 * @property {bool} scrollable - If `true`, the page is scrollable with dynamic height. Otherweise, the page's height is fixed to the screen.
 * @property {ui.PageStack} pageStack - The page stack to use.
 * @property {bool} currentPage - [readonly] Whether this page is the currently visible page.
 * @property {fengshui.Template} contentTemplate - An optional template for creating the page's content dynamically after the page was brought to front.
 * @property {bool} loading - [readonly] Whether the page is currently loading.
 * @property {bool} frozen - [readonly] Whether the page is currently frozen. All pages on the page stack except for the current page are frozen.
 * @property {number} openingPercentage - [readonly] The transitioning opening percentage as a value between `0.0` and `1.0`. You may use this value for implementing custom opening animations.
 */
MouseBox {
    id: page

    container default: contentArea
    container header: headerArea

    property shortcutReceiver: self

    property scrollable: true
    property pageStack: null
    property currentPage: false
    property contentTemplate: null
    property loading: false
    property frozen: openingPercentage < 1 || ! currentPage
    property openingPercentage: 0

    /**
     * Shows the page.
     *
     * @private
     * @method
     * @name show
     * @memberof ui.Page.prototype
     * @param {bool} immediate - Deprecated. Has no effect.
     */
    function show(immediate)
    {
        if (contentTemplate)
        {
            loading = true;
        }

        openingPercentage = 1;
    }

    /**
     * Closes the page.
     *
     * @private
     * @method
     * @name close
     * @memberof ui.Page.prototype
     * @param {function} callback - An optional callback to invoke afterwards.
     */
    function close(callback)
    {
        priv.closeCallback = callback;
        openingPercentage = 0;
    }

    function loadContent()
    {
        const item = contentTemplate();
        loading = false;
        contentArea.add(item);
    }

    Object {
        id: priv

        property frozenInternal: false
        property closeCallback: null
    }

    position: priv.frozenInternal ? "global" : "inline"
    
    width: thisDocument.bboxWidth
    height: priv.frozenInternal ? thisDocument.bboxHeight : -1
    minHeight: thisDocument.bboxHeight

    marginLeft: (1 - openingPercentage) * width

    openingPercentageTransition: NumberAnimation { duration: 700; easing: "InOutQuad" }

    color: theme.primaryBackgroundColor

    enabled: ! frozen

    canFocus: true
    trapFocus: true

    style: openingPercentage < 1 ? ["sh-no-scrollbars", "sh-dropshadow"]
                                 : ["sh-no-scrollbars"]


    onOpeningPercentageChanged: () =>
    {
        if (openingPercentage === 0)
        {
            // closed
            if (priv.closeCallback)
            {
                priv.closeCallback();
                priv.closeCallback = null;
            }
        }
        else if (openingPercentage === 1)
        {
            // opened
            if (contentTemplate && loading)
            {
                defer(() => { page.loadContent(); }, "loadContent");
            }
        }
    }

    onFrozenChanged: () =>
    {
        if (frozen)
        {
            const scrollTop = thisDocument.contentY;
            thisDocument.contentY = 0;
            contentArea.minHeight = contentArea.contentHeight;
            priv.frozenInternal = true;
            page.contentY = scrollTop;
        }
        else
        {
            const scrollTop = page.contentY;
            page.contentY = 0;
            contentArea.minHeight = 0;
            contentArea.contentY = 0;
            priv.frozenInternal = false;
            thisDocument.contentY = scrollTop;
        }
    }
    
    Box {
        id: contentArea

        width: parent.width
        height: page.scrollable ? -1 : thisDocument.bboxHeight
    }

    Box {
        id: headerArea
    }
}