<commit_before>/* Popup Component

  Author: @benlau
  Project: https://github.com/benlau/quickandroid
 */

import QtQuick 2.0
import QuickAndroid 0.1

Item {
    id: popup

    /// The anchor point of the popup.
    /// Possible values: [ Constants.leftTop , Constants.rightTop , Constants.leftBottom, Constants.leftBottom]
    property string anchorPoint : Constants.leftTop

    /// The component (may be a button) to trigger to launch this popup. If it not set, it will be the parent component.
    property var anchorView: parent;

    /// The parent component of the launched popup item. If it is not specificed, it will use the top most component.
    property var viewport: null;

    /// The Paper component behind the popup.
    readonly property alias paper: container

    default property alias content: container.content

    property bool isOpened: false

    property int margin: 8

    property int _paperWidth: 0
    property int _paperHeight: 0

    signal aboutToOpen
    signal opened

    signal aboutToClose
    signal closed

    function open() {
        if (isOpened)
            return;

        aboutToOpen();

        var root = viewport;
        if (!root)
            root = _topmost();

        layer.parent = root;

        if (content.length > 0) {
            var child = content[0];
            _paperWidth = child.implicitWidth > 0 ? child.implicitWidth : Math.max(child.childrenRect.width,child.width);
            _paperHeight = child.implicitHeight > 0 ? child.implicitHeight : Math.max(child.childrenRect.height,child.height);
        } else {
            _paperWidth = 240 * A.dp;
            _paperHeight = 80 * A.dp;
        }

        _move(root);
        _adjustX(root);
        _adjustY(root);
        _shrinkHeight(root);
        isOpened = true;
    }

    function close() {
        if (!isOpened)
            return;

        aboutToClose();

        isOpened = false;
    }

    // Find the top most component
    function _topmost() {
        var p = parent;

        while (p.parent) {
            p = p.parent;
        }
        return p;
    }

    // Move the paper according to the anchorPoint and anchorView
    function _move(viewport) {
        var view = anchorView;
        var pos;

        switch (anchorPoint) {

        case Constants.leftTop:
            pos = viewport.mapFromItem(view.parent,view.x,view.y)
            paper.x = pos.x;
            paper.y = pos.y;
            break;

        case Constants.rightTop:
            pos = viewport.mapFromItem(view.parent,view.x + view.width,view.y)
            paper.x = pos.x - _paperWidth;
            paper.y = pos.y;
            break;

        case Constants.leftBottom:
            pos = viewport.mapFromItem(view.parent,view.x,view.y + view.height)
            paper.x = pos.x;
            paper.y = pos.y - _paperHeight;
            break;

        case Constants.rightBottom:
            pos = viewport.mapFromItem(view.parent,view.x + view.width,view.y + view.height)
            paper.x = pos.x - _paperWidth;
            paper.y = pos.y - _paperHeight;
            break;
        }
    }

    function _adjustX(viewport) {
        if (paper.x + _paperWidth + margin * A.dp > viewport.width) {
            paper.x = viewport.width - _paperWidth - margin * A.dp
        }

        if (paper.x < margin * A.dp) {
            paper.x = margin * A.dp
        }
    }

    function _adjustY(viewport) {
        if (paper.y + _paperHeight + margin * A.dp > viewport.height) {
            paper.y = viewport.height - _paperHeight - margin * A.dp
        }

        if (paper.y < margin * A.dp) {
            paper.y = margin * A.dp
        }
    }

    function _shrinkHeight(viewport) {
        if (paper.y + _paperHeight + margin * A.dp > viewport.height) {
            _paperHeight = viewport.height - paper.y - margin * A.dp;
        }
    }

    Item {
        id: layer
        anchors.fill: parent
        enabled: false
        opacity: 0

        MouseArea {
            anchors.fill: parent
            onClicked: {
                popup.close();
            }
        }

        MouseArea {
            // block mouse event
            anchors.fill: container
        }

        Paper {
            id: container
            width: 0
            height: 0
            z: Constants.zPopupLayer

            Keys.onReleased: {
                if (event.key === Qt.Key_Back) {
                    close();
                }
            }
        }

    }

    states: [    
        State {
            name: "Opened"
            when: isOpened

            PropertyChanges {
                target: layer
                enabled: true
                opacity: 1
            }

            PropertyChanges {
                target: paper
                focus: true
                height: _paperHeight
                width: _paperWidth
            }<commit_msg>Popup.qml : Press "back" key to close.<commit_after>/* Popup Component

  Author: @benlau
  Project: https://github.com/benlau/quickandroid
 */

import QtQuick 2.0
import QuickAndroid 0.1

FocusScope {
    id: popup

    /// The anchor point of the popup.
    /// Possible values: [ Constants.leftTop , Constants.rightTop , Constants.leftBottom, Constants.leftBottom]
    property string anchorPoint : Constants.leftTop

    /// The component (may be a button) to trigger to launch this popup. If it not set, it will be the parent component.
    property var anchorView: parent;

    /// The parent component of the launched popup item. If it is not specificed, it will use the top most component.
    property var viewport: null;

    /// The Paper component behind the popup.
    readonly property alias paper: container

    default property alias content: container.content

    property bool isOpened: false

    property int margin: 8

    property int _paperWidth: 0
    property int _paperHeight: 0

    signal aboutToOpen
    signal opened

    signal aboutToClose
    signal closed

    function open() {
        if (isOpened)
            return;

        aboutToOpen();

        var root = viewport;
        if (!root)
            root = _topmost();

        layer.parent = root;

        if (content.length > 0) {
            var child = content[0];
            _paperWidth = child.implicitWidth > 0 ? child.implicitWidth : Math.max(child.childrenRect.width,child.width);
            _paperHeight = child.implicitHeight > 0 ? child.implicitHeight : Math.max(child.childrenRect.height,child.height);
        } else {
            _paperWidth = 240 * A.dp;
            _paperHeight = 80 * A.dp;
        }

        _move(root);
        _adjustX(root);
        _adjustY(root);
        _shrinkHeight(root);
        isOpened = true;
    }

    function close() {
        if (!isOpened)
            return;

        aboutToClose();

        isOpened = false;
    }

    // Find the top most component
    function _topmost() {
        var p = parent;

        while (p.parent) {
            p = p.parent;
        }
        return p;
    }

    // Move the paper according to the anchorPoint and anchorView
    function _move(viewport) {
        var view = anchorView;
        var pos;

        switch (anchorPoint) {

        case Constants.leftTop:
            pos = viewport.mapFromItem(view.parent,view.x,view.y)
            paper.x = pos.x;
            paper.y = pos.y;
            break;

        case Constants.rightTop:
            pos = viewport.mapFromItem(view.parent,view.x + view.width,view.y)
            paper.x = pos.x - _paperWidth;
            paper.y = pos.y;
            break;

        case Constants.leftBottom:
            pos = viewport.mapFromItem(view.parent,view.x,view.y + view.height)
            paper.x = pos.x;
            paper.y = pos.y - _paperHeight;
            break;

        case Constants.rightBottom:
            pos = viewport.mapFromItem(view.parent,view.x + view.width,view.y + view.height)
            paper.x = pos.x - _paperWidth;
            paper.y = pos.y - _paperHeight;
            break;
        }
    }

    function _adjustX(viewport) {
        if (paper.x + _paperWidth + margin * A.dp > viewport.width) {
            paper.x = viewport.width - _paperWidth - margin * A.dp
        }

        if (paper.x < margin * A.dp) {
            paper.x = margin * A.dp
        }
    }

    function _adjustY(viewport) {
        if (paper.y + _paperHeight + margin * A.dp > viewport.height) {
            paper.y = viewport.height - _paperHeight - margin * A.dp
        }

        if (paper.y < margin * A.dp) {
            paper.y = margin * A.dp
        }
    }

    function _shrinkHeight(viewport) {
        if (paper.y + _paperHeight + margin * A.dp > viewport.height) {
            _paperHeight = viewport.height - paper.y - margin * A.dp;
        }
    }

    Item {
        id: layer
        anchors.fill: parent
        enabled: false
        opacity: 0

        MouseArea {
            anchors.fill: parent
            onClicked: {
                popup.close();
            }
        }

        MouseArea {
            // block mouse event
            anchors.fill: container
        }

        Paper {
            id: container
            width: 0
            height: 0
            z: Constants.zPopupLayer
        }

    }

    Keys.onReleased: {
        if (event.key === Qt.Key_Back) {
            close();
            event.accepted = true;
        }
    }

    states: [    
        State {
            name: "Opened"
            when: isOpened

            PropertyChanges {
                target: popup
                focus: true;
            }

            PropertyChanges {
                target: layer
                enabled: true
                opacity: 1
            }

            PropertyChanges {
                target: paper
                height: _paperHeight
                width: _paperWidth
            }
{'fraction_non_alphanumeric': 0.09083552498482703, 'fraction_numerical': 0.004046125834513453, 'mean_word_length': 1.469897576817387, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '78036', 'n_tokens_mistral': 2908, 'n_tokens_neox': 2751, 'n_words': 732}