// The filter box at the top of the clipboard popover. Nothing is submitted:
// the text is read live and narrows the list that is already loaded, which is
// how GNOME's own search entries behave.

import QtQuick
import qs.config

Rectangle {
    id: root

    property alias text: input.text
    property string placeholder: "Search"

    signal accepted
    signal dismissed

    implicitHeight: 32
    radius: Theme.pillRadius
    // border.width is set below rather than left at 0, so the focus ring has
    // something to change. See QTBUG-137166 for why 0 matters elsewhere.
    color: Theme.alpha(Theme.fg, 0.07)
    border.width: 1
    border.color: input.activeFocus ? Theme.alpha(Theme.accent, 0.55) : "transparent"

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

    function clear(): void {
        input.text = "";
    }

    function grab(): void {
        input.forceActiveFocus();
    }

    Text {
        id: glyph
        anchors.left: parent.left
        anchors.leftMargin: 12
        anchors.verticalCenter: parent.verticalCenter
        text: "\u{F002}"   // nf-fa-search — fontMono is the Nerd Font, icon only
        font.family: Theme.fontMono
        font.pixelSize: 12
        color: input.activeFocus ? Theme.accent : Theme.fgMuted
    }

    TextInput {
        id: input
        anchors.left: glyph.right
        anchors.leftMargin: 9
        anchors.right: parent.right
        anchors.rightMargin: 12
        anchors.verticalCenter: parent.verticalCenter

        color: Theme.fg
        selectionColor: Theme.alpha(Theme.accent, 0.5)
        selectedTextColor: Theme.fg
        font.family: Theme.fontFamily
        font.pixelSize: Theme.fontSize
        clip: true

        onAccepted: root.accepted()

        // Escape clears a non-empty filter first and only closes the popover on
        // a second press — losing a typed query to a stray Escape is worse than
        // one extra keystroke.
        Keys.onEscapePressed: {
            if (input.text !== "")
                input.text = "";
            else
                root.dismissed();
        }

        Text {
            anchors.fill: parent
            verticalAlignment: Text.AlignVCenter
            visible: input.text === ""
            text: root.placeholder
            color: Theme.fgMuted
            font: input.font
        }
    }
}
