// One row of a TrayMenu: optional icon, label, and either a check/radio mark
// or a submenu chevron on the right.

import Quickshell.Widgets
import QtQuick
import qs.config

Rectangle {
    id: root

    signal activated

    property string label: ""
    property string icon: ""
    // Named `rowEnabled` rather than `enabled`: overriding Item.enabled would
    // also disable the MouseArea, and we still want the row to swallow clicks
    // rather than let them fall through to whatever is behind the popover.
    property bool rowEnabled: true
    property int buttonType: 0   // QsMenuButtonType: 0 None, 1 CheckBox, 2 RadioButton
    property int checkState: Qt.Unchecked
    property bool submenu: false

    implicitWidth: rowContent.implicitWidth + 20
    implicitHeight: 30

    radius: Theme.cardRadius
    border.width: 0
    color: mouse.containsMouse && root.rowEnabled ? Theme.alpha(Theme.fg, Theme.hoverAlpha) : "transparent"

    Behavior on color {
        ColorAnimation {
            duration: Theme.durFast
        }
    }

    Row {
        id: rowContent
        anchors.left: parent.left
        anchors.leftMargin: 10
        anchors.verticalCenter: parent.verticalCenter
        spacing: 8

        IconImage {
            anchors.verticalCenter: parent.verticalCenter
            implicitSize: 16
            asynchronous: true
            visible: root.icon !== ""
            source: root.icon
        }

        Text {
            anchors.verticalCenter: parent.verticalCenter
            text: root.label
            font.family: Theme.fontFamily
            font.pixelSize: Theme.fontSize
            color: root.rowEnabled ? Theme.fg : Theme.fgMuted
        }
    }

    Text {
        anchors.right: parent.right
        anchors.rightMargin: 10
        anchors.verticalCenter: parent.verticalCenter
        font.family: Theme.fontMono
        font.pixelSize: Theme.fontSize
        color: Theme.accent

        text: {
            if (root.submenu)
                return "\u{F0142}";  // md-chevron_right
            if (root.buttonType !== 0 && root.checkState === Qt.Checked)
                return "\u{F012C}";  // md-check
            return "";
        }
    }

    MouseArea {
        id: mouse
        anchors.fill: parent
        hoverEnabled: true
        cursorShape: root.rowEnabled ? Qt.PointingHandCursor : Qt.ArrowCursor
        onClicked: if (root.rowEnabled)
            root.activated()
    }
}
