Source: shellfish-ui/ui/Switch.shui

shellfish-ui/ui/Switch.shui

/*******************************************************************************
This file is part of the Shellfish UI toolkit.
Copyright (c) 2020 - 2022 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 UI switch for switching on and off.
 *
 * @memberof ui
 * @name Switch
 * @class
 * @extends html.MouseBox
 *
 * @property {bool} checked - (default: `false`) Whether the switch is currently in checked state (on).
 * @property {template} styleOfBackground - The background style template.
 * @property {template} styleOfHandle - The handle style template.
 */
MouseBox {
    id: switch

    property checked: false

    property styleOfBackground: template Box {
        fillWidth: true
        fillHeight: true

        opacity: enabled && ancestorsEnabled ? 1 : 0.6

        borderColor: theme.borderColor
        borderWidth: 1
        borderRadius: theme.borderRadius

        color: switch.checked ? theme.highlightBackgroundColor
                              : theme.secondaryBackgroundColor
    }

    property styleOfHandle: template Box {
        width: switch.bboxWidth / 2
        fillHeight: true
        enabled: false

        marginTop: 1
        marginRight: 1
        marginBottom: 1
        marginLeft: switch.checked ? parent.bboxWidth - bboxWidth : 1

        color: theme.primaryBackgroundColor

        borderColor: theme.borderColor
        borderWidth: 1
        borderRadius: theme.borderRadius

        marginLeftTransition: NumberAnimation {
            duration: 200
            easing: "InExpo"
        }
    }

    canFocus: true

    width: theme.itemWidthMedium
    height: theme.itemHeightSmall

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

    onKeyDown: (ev) =>
    {
        if (ev.key === " " || ev.key === "Enter" || ev.key === "ArrowLeft" || ev.key === "ArrowRight")
        {
            checked = ! checked;
            ev.accepted = true;
        }
    }

    Loader {
        fillWidth: true
        fillHeight: true
        sourceTemplate: styleOfBackground

        onItemChanged: () =>
        {
            if (item)
            {
                item.add(contentT());
            }
        }

        property contentT: template Loader {
            position: "free"
            fillWidth: true
            fillHeight: true

            sourceTemplate: styleOfHandle
        }
    }

    FocusIndicator { visible: switch.focus }
}