Debian Codegraph @

resources/qml/Completer.qml

raw
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 import "./ui"
6 import QtQuick 2.15
7 import QtQuick.Controls 2.15
8 import QtQuick.Layouts 1.15
9 import im.nheko 1.0
10
11 Control {
12 id: popup
13
14 property int avatarHeight: 24
15 property int avatarWidth: 24
16 property bool bottomToTop: true
17 property bool centerRowContent: true
18 property var completer
19 property string completerName
20 property alias count: listView.count
21 property alias currentIndex: listView.currentIndex
22 property bool fullWidth: false
23 property string roomId
24 property int rowMargin: 0
25 property int rowSpacing: Nheko.paddingSmall
26
27 signal completionClicked(string completion)
28 signal completionSelected(string id)
29
30 function changeCompleter() {
31 if (completerName) {
32 completer = TimelineManager.completerFor(completerName, completerName == "room" ? "" : (popup.roomId != "" ? popup.roomId : room.roomId));
33 completer.setSearchString("");
34 } else {
35 completer = undefined;
36 }
37 currentIndex = -1;
38 }
39 function currentCompletion() {
40 if (currentIndex > -1 && currentIndex < listView.count)
41 return completer.completionAt(currentIndex);
42 else
43 return null;
44 }
45 function currentUserid() {
46 if (popup.completerName == "user") {
47 return listView.itemAtIndex(currentIndex).modelData.userid;
48 } else {
49 return "";
50 }
51 }
52 function down() {
53 if (bottomToTop)
54 up_();
55 else
56 down_();
57 }
58 function down_() {
59 currentIndex = currentIndex + 1;
60 if (currentIndex >= listView.count)
61 currentIndex = -1;
62 }
63 function finishCompletion() {
64 if (popup.completerName == "room")
65 popup.completionSelected(listView.itemAtIndex(currentIndex).modelData.rawroomid);
66 else if (popup.completerName == "user")
67 popup.completionSelected(listView.itemAtIndex(currentIndex).modelData.userid);
68 }
69 function up() {
70 if (bottomToTop)
71 down_();
72 else
73 up_();
74 }
75 function up_() {
76 currentIndex = currentIndex - 1;
77 if (currentIndex == -2)
78 currentIndex = listView.count - 1;
79 }
80
81 bottomPadding: 1
82 leftPadding: 1
83
84 // Workaround palettes not inheriting for popups
85 palette: timelineRoot.palette
86 rightPadding: 1
87 topPadding: 1
88
89 background: Rectangle {
90 border.color: palette.mid
91 color: palette.base
92 }
93 contentItem: ListView {
94 id: listView
95
96 clip: true
97 displayMarginBeginning: height / 2
98 displayMarginEnd: height / 2
99 highlightFollowsCurrentItem: true
100
101 // If we have fewer than 7 items, just use the list view's content height.
102 // Otherwise, we want to show 7 items. Each item consists of row spacing between rows, row margins
103 // on each side of a row, 1px of padding above the first item and below the last item, and nominally
104 // some kind of content height. avatarHeight is used for just about every delegate, so we're using
105 // that until we find something better. Put is all together and you have the formula below!
106 implicitHeight: Math.min(contentHeight, 6 * rowSpacing + 7 * (popup.avatarHeight + 2 * rowMargin))
107
108 // Broken, see https://bugreports.qt.io/browse/QTBUG-102811
109 //reuseItems: true
110 implicitWidth: Math.max(listView.contentItem.childrenRect.width, 20)
111 model: completer
112 pixelAligned: true
113 spacing: rowSpacing
114 verticalLayoutDirection: popup.bottomToTop ? ListView.BottomToTop : ListView.TopToBottom
115
116 delegate: Rectangle {
117 property variant modelData: model
118
119 ListView.delayRemove: true
120 color: model.index == popup.currentIndex ? palette.highlight : palette.base
121 height: (chooser.child?.implicitHeight ?? 0) + 2 * popup.rowMargin
122 implicitWidth: popup.fullWidth ? ListView.view.width : chooser.child.implicitWidth + 4
123
124 MouseArea {
125 id: mouseArea
126
127 anchors.fill: parent
128 hoverEnabled: true
129
130 onClicked: {
131 popup.completionClicked(completer.completionAt(model.index));
132 if (popup.completerName == "room")
133 popup.completionSelected(model.roomid);
134 else if (popup.completerName == "user")
135 popup.completionSelected(model.userid);
136 }
137 onPositionChanged: if (!listView.moving && !deadTimer.running)
138 popup.currentIndex = model.index
139 }
140 Ripple {
141 color: Qt.rgba(palette.base.r, palette.base.g, palette.base.b, 0.5)
142 }
143 DelegateChooser {
144 id: chooser
145
146 anchors.fill: parent
147 anchors.margins: popup.rowMargin
148 enabled: false
149 roleValue: popup.completerName
150
151 DelegateChoice {
152 roleValue: "user"
153
154 RowLayout {
155 anchors.centerIn: centerRowContent ? parent : undefined
156 spacing: Nheko.paddingSmall
157
158 Avatar {
159 displayName: model.displayName
160 enabled: false
161 Layout.preferredHeight: popup.avatarHeight
162 Layout.preferredWidth: popup.avatarWidth
163 url: model.avatarUrl.replace("mxc://", "image://MxcImage/")
164 userid: model.userid
165 }
166 Label {
167 color: model.index == popup.currentIndex ? palette.highlightedText : palette.text
168 text: model.displayName
169 }
170 Label {
171 color: model.index == popup.currentIndex ? palette.highlightedText : palette.buttonText
172 text: "(" + model.userid + ")"
173 }
174 }
175 }
176 DelegateChoice {
177 roleValue: "emoji"
178
179 RowLayout {
180 anchors.centerIn: parent
181 spacing: Nheko.paddingSmall
182
183 Label {
184 color: model.index == popup.currentIndex ? palette.highlightedText : palette.text
185 font.family: Settings.emojiFont != "" ? Settings.emojiFont : undefined
186 text: model.unicode
187 visible: !!model.unicode
188 }
189 Avatar {
190 crop: false
191 displayName: model.shortcode
192 enabled: false
193 Layout.preferredHeight: popup.avatarHeight
194 //userid: model.shortcode
195 url: (model.url ? model.url : "").replace("mxc://", "image://MxcImage/")
196 visible: !model.unicode
197 Layout.preferredWidth: popup.avatarWidth
198 }
199 Label {
200 Layout.leftMargin: Nheko.paddingSmall
201 Layout.rightMargin: Nheko.paddingSmall
202 color: model.index == popup.currentIndex ? palette.highlightedText : palette.text
203 text: model.shortcode
204 }
205 Label {
206 color: model.index == popup.currentIndex ? palette.highlightedText : palette.buttonText
207 text: "(" + model.packname + ")"
208 }
209 }
210 }
211 DelegateChoice {
212 roleValue: "command"
213
214 RowLayout {
215 anchors.centerIn: parent
216 spacing: Nheko.paddingSmall
217
218 Label {
219 color: model.index == popup.currentIndex ? palette.highlightedText : palette.text
220 font.bold: true
221 text: model.name
222 }
223 Label {
224 color: model.index == popup.currentIndex ? palette.highlightedText : palette.buttonText
225 text: model.description
226 }
227 }
228 }
229 DelegateChoice {
230 roleValue: "room"
231
232 RowLayout {
233 anchors.centerIn: centerRowContent ? parent : undefined
234 spacing: Nheko.paddingSmall
235
236 Avatar {
237 displayName: model.roomName
238 enabled: false
239 Layout.preferredHeight: popup.avatarHeight
240 roomid: model.roomid
241 url: model.avatarUrl.replace("mxc://", "image://MxcImage/")
242 Layout.preferredWidth: popup.avatarWidth
243 }
244 Label {
245 color: model.index == popup.currentIndex ? palette.highlightedText : palette.text
246 font.italic: model.isTombstoned
247 font.bold: model.isSpace
248 font.pixelSize: popup.avatarHeight * 0.5
249 text: model.roomName
250 textFormat: Text.RichText
251 }
252 }
253 }
254 DelegateChoice {
255 roleValue: "roomAliases"
256
257 RowLayout {
258 anchors.centerIn: parent
259 spacing: Nheko.paddingSmall
260
261 Avatar {
262 displayName: model.roomName
263 enabled: false
264 Layout.preferredHeight: popup.avatarHeight
265 roomid: model.roomid
266 url: model.avatarUrl.replace("mxc://", "image://MxcImage/")
267 Layout.preferredWidth: popup.avatarWidth
268 }
269 Label {
270 color: model.index == popup.currentIndex ? palette.highlightedText : palette.text
271 font.italic: model.isTombstoned
272 font.bold: model.isSpace
273 text: model.roomName
274 textFormat: Text.RichText
275 }
276 Label {
277 color: model.index == popup.currentIndex ? palette.highlightedText : palette.buttonText
278 text: "(" + model.roomAlias + ")"
279 textFormat: Text.RichText
280 }
281 }
282 }
283 }
284 }
285
286 onContentYChanged: deadTimer.restart()
287
288 Timer {
289 id: deadTimer
290
291 interval: 50
292 }
293 }
294
295 onCompleterNameChanged: changeCompleter()
296 onRoomIdChanged: changeCompleter()
297 }