// A bar module container: rounded, hover-highlighted, optionally clickable.
// Every widget that sits in the top bar wraps itself in one of these so
// spacing, hover feedback and press feedback stay identical across the bar.

import QtQuick
import qs.config

Rectangle {
    id: root

    // Fired on left click; if nothing is connected the pill is inert and
    // shows no hover affordance.
    signal activated
    signal secondaryActivated
    signal scrolled(int delta)

    property alias hovered: mouse.containsMouse
    property bool interactive: true
    property int horizontalPadding: 10

    default property alias content: layout.data

    implicitWidth: layout.implicitWidth + horizontalPadding * 2
    implicitHeight: Theme.barHeight - 8

    radius: Theme.pillRadius
    // border.width: 0 works around QTBUG-137166, where transparent rounded
    // rectangles can render with holes at the corners.
    border.width: 0

    color: {
        if (!interactive)
            return "transparent";
        if (mouse.pressed)
            return Theme.alpha(Theme.accent, Theme.activeAlpha);
        if (mouse.containsMouse)
            return Theme.alpha(Theme.fg, Theme.hoverAlpha);
        return "transparent";
    }

    Behavior on color {
        ColorAnimation {
            duration: Theme.durFast
            easing.type: Easing.OutQuad
        }
    }

    Row {
        id: layout
        anchors.centerIn: parent
        spacing: 6
    }

    MouseArea {
        id: mouse
        anchors.fill: parent
        enabled: root.interactive
        hoverEnabled: true
        acceptedButtons: Qt.LeftButton | Qt.RightButton
        cursorShape: root.interactive ? Qt.PointingHandCursor : Qt.ArrowCursor

        onClicked: event => {
            if (event.button === Qt.RightButton)
                root.secondaryActivated();
            else
                root.activated();
        }

        onWheel: event => {
            root.scrolled(event.angleDelta.y > 0 ? 1 : -1);
        }
    }
}
