Source: shellfish-ui/ui/CheckBox.shui

shellfish-ui/ui/CheckBox.shui

/*******************************************************************************
This file is part of the Shellfish UI toolkit.
Copyright (c) 2020 - 2021 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 checkbox.
 *
 * @memberof ui
 * @name CheckBox
 * @class
 * @extends html.MouseBox
 *
 * @property {bool} checked - (default: `false`) Whether the checkbox is in checked state.
 */
MouseBox {
    id: switch

    property checked: false

    canFocus: true

    width: height
    height: theme.itemHeightSmall
    opacity: enabled && ancestorsEnabled ? 1 : 0.6

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

    color: pressed ? theme.highlightBackgroundColor
                   : containsMouse ? theme.hoverBackgroundColor
                                   : theme.secondaryBackgroundColor

    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;
        }
    }

    Box {
        visible: switch.checked
        position: "free"
        fillWidth: true
        fillHeight: true
        marginTop: 1
        marginLeft: 1
        marginRight: 1
        marginBottom: 1
        enabled: false

        color: theme.primaryBackgroundColor

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

        layout: "center"

        Label {
            color: theme.primaryColor
            text: "[icon:core-done]"
        }
    }

    FocusIndicator { visible: switch.focus }
}