// A row whose trailing control is a free-text field, NOT bound to a schema key.
//
//   TextFieldRow {
//       label: "Full name"
//       text: Accounts.me.realName
//       onAccepted: value => Accounts.setRealName("gib", value)
//   }
//
// TextEntryRow is the schema-bound one, and is the right choice for anything
// that is a Panama setting. This is for values that live in the system rather
// than in settings.json -- an account's real name, the machine's hostname --
// where the label, the validation, and the write all belong to whoever owns
// that value.
//
// Committed on Enter or when focus leaves, never per keystroke: these drive
// privileged calls that prompt, and prompting once per typed character would be
// unusable.

import QtQuick
import qs.config

SettingRow {
    id: root

    property string text: ""
    property string placeholder: ""
    property bool enabled: true

    signal accepted(value: string)

    controlWidth: 220

    // An external change replaces what is shown, unless it would yank the field
    // out from under someone mid-edit.
    onTextChanged: if (!input.activeFocus) input.text = root.text

    Rectangle {
        anchors.right: parent.right
        anchors.verticalCenter: parent.verticalCenter
        width: root.controlWidth
        height: 32
        radius: 9
        color: Theme.alpha(Theme.fg, root.enabled ? 0.06 : 0.03)
        border.width: input.activeFocus ? 2 : 1
        border.color: input.activeFocus
            ? Theme.alpha(Theme.accent, 0.55)
            : Theme.alpha(Theme.fg, 0.1)
        opacity: root.enabled ? 1 : 0.5

        TextInput {
            id: input
            anchors.fill: parent
            anchors.leftMargin: 11
            anchors.rightMargin: 11
            enabled: root.enabled
            activeFocusOnTab: true
            text: root.text
            color: Theme.fg
            selectionColor: Theme.alpha(Theme.accent, 0.5)
            selectedTextColor: Theme.fg
            font.family: Theme.fontFamily
            font.pixelSize: Theme.fontSize
            verticalAlignment: TextInput.AlignVCenter
            clip: true

            onAccepted: if (input.text !== root.text) root.accepted(input.text)

            onActiveFocusChanged: {
                if (input.activeFocus)
                    return;
                if (input.text !== root.text)
                    root.accepted(input.text);
                else
                    input.text = root.text;
            }

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