Source: shellfish-ui/ui/SplitBox.shui

shellfish-ui/ui/SplitBox.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 horizontal or vertical UI splitter with two child
 * containers, one for each side.
 *
 * Containers:
 * * `first` - The first container.
 * * `second` - The second container.
 *
 * @example
 * SplitBox {
 *     splitRatio: 0.3
 *     orientation: "vertical"
 *
 *     into first Placeholder { text: "Left Side" }
 *     into second Placeholder { text: "Right Side" }
 * }
 *
 * @memberof ui
 * @name SplitBox
 * @class
 * @extends html.Box
 *
 * @property {bool} flip - (default: `false`) If `true`, the first and second containers are swapped.
 * @property {string} orientation - (default: `horizontal`) The orientation of the splitter. Either `horizontal` or `vertical`.
 * @property {number} splitRatio - (default: `0.5`) The split ratio as a value between `0.0` and `1.0`.
 * @property {template} styleOfGutter - The gutter style template.
 */
Box {
    id: splitBox

    container first: box1
    container second: box2

    property orientation: "horizontal"
    property flip: false
    property splitRatio: 0.5

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

        layout: priv.isHorizontal ? "center-column"
                                  : "center-row"

        Box {
            width: priv.isHorizontal ? 1 : -1
            height: priv.isVertical ? 1 : -1
            fillWidth: priv.isVertical
            fillHeight: priv.isHorizontal
            color: theme.borderColor
        }

        Box {
            fillWidth: priv.isHorizontal
            fillHeight: priv.isVertical
            
            width: priv.isVertical ? theme.itemHeightLarge : -1
            height: priv.isHorizontal ? theme.itemHeightLarge : -1

            marginTop: priv.isVertical ? 2 : 0
            marginLeft: priv.isHorizontal ? 2 : 0
            marginRight: priv.isHorizontal ? 2 : 0
            marginBottom: priv.isVertical ? 2 : 0

            color: parent.pressed ? theme.highlightBackgroundColor
                                  : parent.containsMouse ? theme.hoverBackgroundColor
                                                         : theme.secondaryBackgroundColor
            borderColor: theme.borderColor
            borderWidth: 1
            borderRadius: theme.borderRadius

            layout: "center"

            Label {
                rotationAngle: priv.isVertical ? 90 : 0
                fontSize: Math.min(parent.bboxWidth, parent.bboxHeight)
                text: "[icon:core-drag_indicator]"
            }
        }

        Box {
            width: priv.isHorizontal ? 1 : -1
            height: priv.isVertical ? 1 : -1
            fillWidth: priv.isVertical
            fillHeight: priv.isHorizontal
            color: theme.borderColor
        }
    }

    event click
    event doubleClick

    Object {
        id: priv

        property isHorizontal: splitBox.orientation === "horizontal"
        property isVertical: ! isHorizontal
        property secondBox: splitBox.flip ? box2 : box1
    }

    Object {
        id: positionProfile1

        property marginTop: 0
        property marginLeft: 0

        property width: priv.isHorizontal ? gutter.x
                                          : -1
        property height: priv.isVertical ? gutter.y
                                         : -1

        property fillWidth: priv.isVertical
        property fillHeight: priv.isHorizontal
    }

    Object {
        id: positionProfile2

        property marginTop: priv.isVertical ? priv.secondBox.height + gutter.height
                                            : 0
        property marginLeft: priv.isHorizontal ? priv.secondBox.width + gutter.width
                                               : 0

        property width: -1
        property height: -1

        property fillWidth: true
        property fillHeight: true
    }

    Box {
        id: box1

        position: "free"
        profiles: [splitBox.flip ? positionProfile2 : positionProfile1]
    }

    Box {
        id: box2

        position: "free"
        profiles: [splitBox.flip ? positionProfile1 : positionProfile2]
    }

    MouseBox {
        id: gutter

        canFocus: true

        position: "free"

        x: priv.isHorizontal ? splitBox.bboxWidth * splitBox.splitRatio
                             : 0
        y: priv.isVertical ? splitBox.bboxHeight * splitBox.splitRatio
                           : 0

        width: priv.isHorizontal ? theme.paddingLarge
                                 : -1
        height: priv.isVertical ? theme.paddingLarge
                                : -1

        fillWidth: priv.isVertical
        fillHeight: priv.isHorizontal

        cursor: priv.isHorizontal ? "col-resize"
                                  : "row-resize"
        
        onDrag: (ev) =>
        {
            const w = splitBox.bboxWidth;
            const h = splitBox.bboxHeight;

            if (priv.isHorizontal && w > 0)
            {
                splitBox.splitRatio = Math.max(16, Math.min(w - width - 16, x + ev.deltaX)) / w;
            }
            else if (h > 0)
            {
                splitBox.splitRatio = Math.max(16, Math.min(h - height - 16, y + ev.deltaY)) / h;
            }

            if (ev.buttons === 1)
            {
                ev.accepted = true;
            }
        }

        onClick: (ev) => { splitBox.click(ev); ev.accepted = true; }
        onDoubleClick: (ev) => { splitBox.doubleClick(ev); ev.accepted = true; }

        onPointerDown: (ev) =>
        {
            gutter.focus = true;
            ev.accepted = true;
        }

        onKeyDown: (ev) =>
        {
            const w = splitBox.bboxWidth;
            const h = splitBox.bboxHeight;
            
            if (priv.isHorizontal && w > 0)
            {
                if (ev.key === "ArrowLeft")
                {
                    splitBox.splitRatio = Math.max(16, Math.min(w - width - 16, x - 32)) / w;
                    ev.accepted = true;
                }
                if (ev.key === "ArrowRight")
                {
                    splitBox.splitRatio = Math.max(16, Math.min(w - width - 16, x + 32)) / w;
                    ev.accepted = true;
                }
            }
            else if (h > 0)
            {
                if (ev.key === "ArrowUp")
                {
                    splitBox.splitRatio = Math.max(16, Math.min(h - height - 16, y - 32)) / h;
                    ev.accepted = true;
                }
                if (ev.key === "ArrowDown")
                {
                    splitBox.splitRatio = Math.max(16, Math.min(h - height - 16, y + 32)) / h;
                    ev.accepted = true;
                }
            }
        }

        Loader {
            fillWidth: true
            fillHeight: true

            sourceTemplate: styleOfGutter
        }

        FocusIndicator { visible: gutter.focus }
    }

}