1// SPDX-FileCopyrightText: 2021 Nheko Contributors
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5import QtQuick 2.9
6import QtQuick.Controls 2.3
7import im.nheko 1.0
8
9Popup {
10    id: quickSwitcher
11
12    property int textHeight: Math.round(Qt.application.font.pixelSize * 2.4)
13    property int textMargin: Math.round(textHeight / 8)
14
15    background: null
16    width: Math.round(parent.width / 2)
17    x: Math.round(parent.width / 2 - width / 2)
18    y: Math.round(parent.height / 4 - height / 2)
19    modal: true
20    closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
21    parent: Overlay.overlay
22    palette: Nheko.colors
23    onOpened: {
24        completerPopup.open();
25        roomTextInput.forceActiveFocus();
26    }
27    onClosed: {
28        completerPopup.close();
29    }
30
31    MatrixTextField {
32        id: roomTextInput
33
34        anchors.fill: parent
35        font.pixelSize: Math.ceil(quickSwitcher.textHeight * 0.6)
36        padding: textMargin
37        color: Nheko.colors.text
38        onTextEdited: {
39            completerPopup.completer.searchString = text;
40        }
41        Keys.onPressed: {
42            if ((event.key == Qt.Key_Up || event.key == Qt.Key_Backtab) && completerPopup.opened) {
43                event.accepted = true;
44                completerPopup.up();
45            } else if ((event.key == Qt.Key_Down || event.key == Qt.Key_Tab) && completerPopup.opened) {
46                event.accepted = true;
47                if (event.key == Qt.Key_Tab && (event.modifiers & Qt.ShiftModifier))
48                    completerPopup.up();
49                else
50                    completerPopup.down();
51            } else if (event.matches(StandardKey.InsertParagraphSeparator)) {
52                completerPopup.finishCompletion();
53                event.accepted = true;
54            }
55        }
56    }
57
58    Completer {
59        id: completerPopup
60
61        x: roomTextInput.x
62        y: roomTextInput.y + quickSwitcher.textHeight + quickSwitcher.textMargin
63        visible: roomTextInput.length > 0
64        width: parent.width
65        completerName: "room"
66        bottomToTop: false
67        fullWidth: true
68        avatarHeight: quickSwitcher.textHeight
69        avatarWidth: quickSwitcher.textHeight
70        centerRowContent: false
71        rowMargin: Math.round(quickSwitcher.textMargin / 2)
72        rowSpacing: quickSwitcher.textMargin
73        closePolicy: Popup.NoAutoClose
74    }
75
76    Connections {
77        function onCompletionSelected(id) {
78            Rooms.setCurrentRoom(id);
79            quickSwitcher.close();
80        }
81
82        function onCountChanged() {
83            if (completerPopup.count > 0 && (completerPopup.currentIndex < 0 || completerPopup.currentIndex >= completerPopup.count))
84                completerPopup.currentIndex = 0;
85
86        }
87
88        target: completerPopup
89    }
90
91    Overlay.modal: Rectangle {
92        color: "#aa1E1E1E"
93    }
94
95}
96