summary refs log tree commit diff
path: root/resources/qml/QuickSwitcher.qml
blob: 2d2ae5cf06b1da1037f74920f6d36b1224b2153a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import QtQuick 2.9
import QtQuick.Controls 2.3
import im.nheko 1.0

Popup {
    id: quickSwitcher

    property int textWidth: 48

    x: parent.width / 2 - width / 2
    y: parent.height / 4 - height / 2
    width: parent.width / 2
    height: textWidth
    modal: true
    closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
    parent: Overlay.overlay

    Overlay.modal: Rectangle {
        color: "#aa1E1E1E"
    }

    MatrixTextField {
        id: roomTextInput

        anchors.fill: parent
        font.pixelSize: quickSwitcher.textWidth - 12

        onTextEdited: {
            completerPopup.completer.setSearchString(text)
        }

        Keys.onPressed: {
            if (event.key == Qt.Key_Up && completerPopup.opened) {
                event.accepted = true;
                completerPopup.up();
            } else if (event.key == Qt.Key_Down && completerPopup.opened) {
                event.accepted = true;
                completerPopup.down();
            } else if (event.matches(StandardKey.InsertParagraphSeparator)) {
                completerPopup.finishCompletion()
                event.accepted = true;
            }
        }
    }

    Completer {
        id: completerPopup

        x: roomTextInput.x - 5
        y: roomTextInput.y + roomTextInput.height + 5
        width: parent.width + 10
        completerName: "room"
        bottomToTop: true
        fullWidth: true
        avatarHeight: textWidth
        avatarWidth: textWidth

        closePolicy: Popup.NoAutoClose
    }

    onOpened: {
        completerPopup.open()
        delay(200, function() {
            roomTextInput.forceActiveFocus()
        })
    }

    onClosed: {
        completerPopup.close()
    }

    Connections {
        onCompletionSelected: {
            console.log(id)
            TimelineManager.setHistoryView(id)
            TimelineManager.highlightRoom(id)
            quickSwitcher.close()
        }
        target: completerPopup
    }

    Timer {
        id: timer
    }

    function delay(delayTime, cb) {
        timer.interval = delayTime;
        timer.repeat = false;
        timer.triggered.connect(cb);
        timer.start();
    }
}