Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/PopupTextArea.qml

Raw
import QtQuick
import QtQuick.Templates as T

// A multi-line PopupTextField: the same frame, focus ring and edit signal, for
// a list typed one entry per line. Tab and Shift+Tab leave it; Return adds a line.
Rectangle {
    id: root

    required property Theme theme
    property alias text: areaInput.text
    property string accessibleName: ""
    // Visible lines before it scrolls.
    property int lines: 4
    property alias input: areaInput
    signal edited(string text)

    implicitWidth: 220
    implicitHeight: lines * Math.ceil(areaInput.font.pixelSize * 1.4) + 2 * root.theme.spaceSm
    radius: theme.radiusControl
    opacity: enabled ? 1 : 0.48
    color: theme.withAlpha(theme.content.backdrop, areaInput.activeFocus ? 0.9 : 0.68)
    border.width: areaInput.activeFocus ? 2 : 1
    border.color: areaInput.activeFocus ? theme.content.focus : theme.withAlpha(theme.ui.minor, 0.3)

    // A click focuses the editor; the text edit's own press handling defers to
    // the flickable it sits in.
    TapHandler {
        onTapped: areaInput.forceActiveFocus(Qt.MouseFocusReason)
    }

    Flickable {
        id: scroller

        anchors.fill: parent
        anchors.margins: 1
        clip: true
        contentWidth: width
        // The editor's own implicit height does not follow its document; its
        // content height does, so the viewport scrolls what it renders.
        contentHeight: areaInput.height
        boundsBehavior: Flickable.StopAtBounds
        interactive: contentHeight > height

        T.TextArea {
            id: areaInput

            width: scroller.width
            height: contentHeight + topPadding + bottomPadding
            leftPadding: root.theme.spaceMd
            rightPadding: root.theme.spaceMd
            topPadding: root.theme.spaceSm
            bottomPadding: root.theme.spaceSm
            wrapMode: TextEdit.NoWrap
            color: root.theme.content.normal
            selectionColor: root.theme.ui.accent
            selectedTextColor: root.theme.content.backdrop
            font.family: root.theme.fontSans
            font.pixelSize: root.theme.textBody
            activeFocusOnTab: true
            selectByMouse: true
            Accessible.name: root.accessibleName
            onTextChanged: if (activeFocus)
                root.edited(text)
            // The caret stays in view while typing below the visible lines.
            onCursorRectangleChanged: {
                if (cursorRectangle.y < scroller.contentY)
                    scroller.contentY = cursorRectangle.y;
                else if (cursorRectangle.y + cursorRectangle.height + bottomPadding > scroller.contentY + scroller.height)
                    scroller.contentY = cursorRectangle.y + cursorRectangle.height + bottomPadding - scroller.height;
            }
            Keys.onTabPressed: event => {
                event.accepted = true;
                const next = areaInput.nextItemInFocusChain(true);
                if (next)
                    next.forceActiveFocus(Qt.TabFocusReason);
            }
            Keys.onBacktabPressed: event => {
                event.accepted = true;
                const previous = areaInput.nextItemInFocusChain(false);
                if (previous)
                    previous.forceActiveFocus(Qt.BacktabFocusReason);
            }
        }
    }
}