Debian Codegraph @

resources/qml/MessageInput.qml

raw
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 import "./emoji"
6 import "./voip"
7 import "./ui"
8 import QtQuick 2.12
9 import QtQuick.Controls 2.3
10 import QtQuick.Layouts 1.2
11 import QtQuick.Window 2.13
12 import im.nheko 1.0
13
14 Rectangle {
15 id: inputBar
16
17 property bool showAllButtons: width > 450 || (messageInput.length == 0 && !messageInput.inputMethodComposing)
18 readonly property string text: messageInput.text
19
20 Layout.fillWidth: true
21 Layout.minimumHeight: 40
22 Layout.preferredHeight: row.implicitHeight
23 color: palette.window
24
25 Component {
26 id: placeCallDialog
27
28 PlaceCall {
29 }
30 }
31 Component {
32 id: screenShareDialog
33
34 ScreenShare {
35 }
36 }
37 RowLayout {
38 id: row
39
40 anchors.fill: parent
41 spacing: 0
42 visible: room ? room.permissions.canSend(MtxEvent.TextMessage) : false
43
44 ImageButton {
45 Layout.alignment: Qt.AlignBottom
46 Layout.margins: 8
47 ToolTip.text: CallManager.isOnCall ? qsTr("Hang up") : (CallManager.isOnCallOnOtherDevice ? qsTr("Already on a call") : qsTr("Place a call"))
48 ToolTip.visible: hovered
49 Layout.preferredHeight: 22
50 hoverEnabled: true
51 image: CallManager.isOnCall ? ":/icons/icons/ui/end-call.svg" : ":/icons/icons/ui/place-call.svg"
52 opacity: (CallManager.haveCallInvite || CallManager.isOnCallOnOtherDevice) ? 0.3 : 1
53 visible: CallManager.callsSupported && showAllButtons
54 Layout.preferredWidth: 22
55
56 onClicked: {
57 if (room) {
58 if (CallManager.haveCallInvite) {
59 return;
60 } else if (CallManager.isOnCall) {
61 CallManager.hangUp();
62 } else if (CallManager.isOnCallOnOtherDevice) {
63 return;
64 } else {
65 var dialog = placeCallDialog.createObject(timelineRoot);
66 dialog.open();
67 timelineRoot.destroyOnClose(dialog);
68 }
69 }
70 }
71 }
72 ImageButton {
73 Layout.alignment: Qt.AlignBottom
74 Layout.margins: 8
75 ToolTip.text: qsTr("Send a file")
76 ToolTip.visible: hovered
77 Layout.preferredHeight: 22
78 hoverEnabled: true
79 image: ":/icons/icons/ui/attach.svg"
80 visible: showAllButtons
81 Layout.preferredWidth: 22
82
83 onClicked: room.input.openFileSelection()
84
85 Rectangle {
86 anchors.fill: parent
87 color: palette.window
88 visible: room && room.input.uploading
89
90 Spinner {
91 anchors.centerIn: parent
92 height: parent.height / 2
93 running: parent.visible
94 }
95 }
96 }
97 ScrollView {
98 id: textInput
99
100 Layout.alignment: Qt.AlignVCenter
101 Layout.fillWidth: true
102 Layout.maximumHeight: Window.height / 4
103 Layout.minimumHeight: fontMetrics.lineSpacing
104 Layout.preferredHeight: contentHeight
105 ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
106 contentWidth: availableWidth
107
108 TextArea {
109 id: messageInput
110
111 property int completerTriggeredAt: 0
112 property string lastChar
113
114 function insertCompletion(completion) {
115 messageInput.remove(completerTriggeredAt, cursorPosition);
116 messageInput.insert(cursorPosition, completion);
117 let userid = completer.currentUserid();
118 if (userid) {
119 room.input.addMention(userid, completion);
120 }
121 }
122 function openCompleter(pos, type) {
123 if (popup.opened)
124 return;
125 completerTriggeredAt = pos;
126 completer.completerName = type;
127 popup.open();
128 completer.completer.setSearchString(messageInput.getText(completerTriggeredAt, cursorPosition) + messageInput.preeditText);
129 }
130 function positionCursorAtEnd() {
131 cursorPosition = messageInput.length;
132 }
133 function positionCursorAtStart() {
134 cursorPosition = 0;
135 }
136
137 background: null
138 bottomPadding: 8
139 color: palette.text
140 focus: true
141 leftPadding: inputBar.showAllButtons ? 0 : 8
142 padding: 0
143 placeholderText: qsTr("Write a message...")
144 placeholderTextColor: palette.buttonText
145 selectByMouse: true
146 topPadding: 8
147 verticalAlignment: TextEdit.AlignVCenter
148 width: textInput.width
149 wrapMode: TextEdit.Wrap
150
151 Keys.onPressed: event => {
152 if (event.matches(StandardKey.Paste)) {
153 event.accepted = room.input.tryPasteAttachment(false);
154 } else if (event.key == Qt.Key_Space) {
155 // close popup if user enters space after colon
156 if (cursorPosition == completerTriggeredAt + 1)
157 popup.close();
158 if (popup.opened && completer.count <= 0)
159 popup.close();
160 } else if (event.modifiers == Qt.ControlModifier && event.key == Qt.Key_U) {
161 messageInput.clear();
162 } else if (event.modifiers == Qt.ControlModifier && event.key == Qt.Key_P) {
163 messageInput.text = room.input.previousText();
164 } else if (event.modifiers == Qt.ControlModifier && event.key == Qt.Key_N) {
165 messageInput.text = room.input.nextText();
166 } else if (event.key == Qt.Key_Escape && popup.opened) {
167 completer.completerName = "";
168 popup.close();
169 event.accepted = true;
170 } else if (event.matches(StandardKey.SelectAll) && popup.opened) {
171 completer.completerName = "";
172 popup.close();
173 } else if (event.matches(StandardKey.InsertLineSeparator)) {
174 if (popup.opened)
175 popup.close();
176 if (Settings.invertEnterKey) {
177 room.input.send();
178 event.accepted = true;
179 }
180 } else if (event.matches(StandardKey.InsertParagraphSeparator)) {
181 if (popup.opened) {
182 var currentCompletion = completer.currentCompletion();
183 let userid = completer.currentUserid();
184
185 completer.completerName = "";
186 popup.close();
187
188 if (currentCompletion) {
189 messageInput.insertCompletion(currentCompletion);
190 if (userid) {
191 console.log(userid);
192 room.input.addMention(userid, currentCompletion);
193 }
194 event.accepted = true;
195 return;
196 }
197 }
198 if (!Settings.invertEnterKey) {
199 room.input.send();
200 event.accepted = true;
201 }
202 } else if (event.key == Qt.Key_Tab && (event.modifiers == Qt.NoModifier || event.modifiers == Qt.ShiftModifier)) {
203 event.accepted = true;
204 if (popup.opened) {
205 if (event.modifiers & Qt.ShiftModifier)
206 completer.down();
207 else
208 completer.up();
209 } else {
210 var pos = cursorPosition - 1;
211 while (pos > -1) {
212 var t = messageInput.getText(pos, pos + 1);
213 console.log('"' + t + '"');
214 if (t == '@') {
215 messageInput.openCompleter(pos, "user");
216 return;
217 } else if (t == ' ' || t == '\t') {
218 messageInput.openCompleter(pos + 1, "user");
219 return;
220 } else if (t == ':') {
221 messageInput.openCompleter(pos, "emoji");
222 return;
223 } else if (t == '~') {
224 messageInput.openCompleter(pos, "customEmoji");
225 return;
226 }
227 pos = pos - 1;
228 }
229 // At start of input
230 messageInput.openCompleter(0, "user");
231 }
232 } else if (event.key == Qt.Key_Up && popup.opened) {
233 event.accepted = true;
234 completer.up();
235 } else if ((event.key == Qt.Key_Down || event.key == Qt.Key_Backtab) && popup.opened) {
236 event.accepted = true;
237 completer.down();
238 } else if (event.key == Qt.Key_Up && (event.modifiers == Qt.NoModifier || event.modifiers == Qt.KeypadModifier)) {
239 if (cursorPosition == 0) {
240 event.accepted = true;
241 var idx = room.edit ? room.idToIndex(room.edit) + 1 : 0;
242 while (true) {
243 var id = room.indexToId(idx);
244 if (!id || room.getDump(id, "").isEditable) {
245 room.edit = id;
246 cursorPosition = 0;
247 Qt.callLater(positionCursorAtEnd);
248 break;
249 }
250 idx++;
251 }
252 } else if (positionAt(0, cursorRectangle.y + cursorRectangle.height / 2) === 0) {
253 event.accepted = true;
254 positionCursorAtStart();
255 }
256 } else if (event.key == Qt.Key_Down && (event.modifiers == Qt.NoModifier || event.modifiers == Qt.KeypadModifier)) {
257 if (cursorPosition == messageInput.length && room.edit) {
258 event.accepted = true;
259 var idx = room.idToIndex(room.edit) - 1;
260 while (true) {
261 var id = room.indexToId(idx);
262 if (!id || room.getDump(id, "").isEditable) {
263 room.edit = id;
264 Qt.callLater(positionCursorAtStart);
265 break;
266 }
267 idx--;
268 }
269 } else if (positionAt(width, cursorRectangle.y + cursorRectangle.height / 2) === messageInput.length) {
270 event.accepted = true;
271 positionCursorAtEnd();
272 }
273 }
274 }
275 // Ensure that we get escape key press events first.
276 Keys.onShortcutOverride: event => event.accepted = (popup.opened && (event.key === Qt.Key_Escape || event.key === Qt.Key_Tab || event.key === Qt.Key_Enter || event.key === Qt.Key_Space))
277 onCursorPositionChanged: {
278 if (!room)
279 return;
280 room.input.updateState(selectionStart, selectionEnd, cursorPosition, text);
281 if (popup.opened && cursorPosition <= completerTriggeredAt)
282 popup.close();
283 if (popup.opened)
284 completer.completer.setSearchString(messageInput.getText(completerTriggeredAt, cursorPosition) + messageInput.preeditText);
285 }
286 onPreeditTextChanged: {
287 if (popup.opened)
288 completer.completer.setSearchString(messageInput.getText(completerTriggeredAt, cursorPosition) + messageInput.preeditText);
289 }
290 onSelectionEndChanged: room.input.updateState(selectionStart, selectionEnd, cursorPosition, text)
291 onSelectionStartChanged: room.input.updateState(selectionStart, selectionEnd, cursorPosition, text)
292 onTextChanged: {
293 if (room)
294 room.input.updateState(selectionStart, selectionEnd, cursorPosition, text);
295 forceActiveFocus();
296 if (cursorPosition > 0)
297 lastChar = text.charAt(cursorPosition - 1);
298 else
299 lastChar = '';
300 if (lastChar == '@') {
301 messageInput.openCompleter(selectionStart - 1, "user");
302 } else if (lastChar == ':') {
303 messageInput.openCompleter(selectionStart - 1, "emoji");
304 } else if (lastChar == '#') {
305 messageInput.openCompleter(selectionStart - 1, "roomAliases");
306 } else if (lastChar == "/" && cursorPosition == 1) {
307 messageInput.openCompleter(selectionStart - 1, "command");
308 }
309 }
310
311 Connections {
312 function onRoomChanged() {
313 messageInput.clear();
314 if (room)
315 messageInput.append(room.input.text);
316 completer.completerName = "";
317 messageInput.forceActiveFocus();
318 }
319
320 target: timelineView
321 }
322 Connections {
323 function onCompletionClicked(completion) {
324 messageInput.insertCompletion(completion);
325 }
326
327 target: completer
328 }
329 Popup {
330 id: popup
331
332 background: null
333 padding: 0
334 x: messageInput.positionToRectangle(messageInput.completerTriggeredAt).x
335 y: messageInput.positionToRectangle(messageInput.completerTriggeredAt).y - height
336
337 enter: Transition {
338 NumberAnimation {
339 duration: 100
340 from: 0
341 property: "opacity"
342 to: 1
343 }
344 }
345 exit: Transition {
346 NumberAnimation {
347 duration: 100
348 from: 1
349 property: "opacity"
350 to: 0
351 }
352 }
353
354 contentItem: Completer {
355 id: completer
356
357 rowMargin: 2
358 rowSpacing: 0
359 }
360 }
361 Connections {
362 function onTextChanged(newText) {
363 messageInput.text = newText;
364 messageInput.cursorPosition = newText.length;
365 }
366
367 ignoreUnknownSignals: true
368 target: room ? room.input : null
369 }
370 Connections {
371 function onEditChanged() {
372 messageInput.forceActiveFocus();
373 }
374 function onReplyChanged() {
375 messageInput.forceActiveFocus();
376 }
377 function onThreadChanged() {
378 messageInput.forceActiveFocus();
379 }
380
381 ignoreUnknownSignals: true
382 target: room
383 }
384 Connections {
385 function onFocusInput() {
386 messageInput.forceActiveFocus();
387 }
388
389 target: TimelineManager
390 }
391 MouseArea {
392 acceptedButtons: Qt.MiddleButton
393 // workaround for wrong cursor shape on some platforms
394 anchors.fill: parent
395 cursorShape: Qt.IBeamCursor
396
397 onPressed: mouse => mouse.accepted = room.input.tryPasteAttachment(true)
398 }
399 }
400 }
401 ImageButton {
402 id: stickerButton
403
404 Layout.alignment: Qt.AlignRight | Qt.AlignBottom
405 Layout.margins: 8
406 ToolTip.text: qsTr("Stickers")
407 ToolTip.visible: hovered
408 Layout.preferredHeight: 22
409 hoverEnabled: true
410 image: ":/icons/icons/ui/sticky-note-solid.svg"
411 visible: showAllButtons
412 Layout.preferredWidth: 22
413
414 onClicked: stickerPopup.visible ? stickerPopup.close() : stickerPopup.show(stickerButton, room.roomId, function (row) {
415 room.input.sticker(row);
416 TimelineManager.focusMessageInput();
417 })
418
419 StickerPicker {
420 id: stickerPopup
421
422 emoji: false
423 }
424 }
425 ImageButton {
426 id: emojiButton
427
428 Layout.alignment: Qt.AlignRight | Qt.AlignBottom
429 Layout.margins: 8
430 ToolTip.text: qsTr("Emoji")
431 ToolTip.visible: hovered
432 Layout.preferredHeight: 22
433 hoverEnabled: true
434 image: ":/icons/icons/ui/smile.svg"
435 Layout.preferredWidth: 22
436
437 onClicked: emojiPopup.visible ? emojiPopup.close() : emojiPopup.show(emojiButton, room.roomId, function (plaintext, markdown) {
438 messageInput.insert(messageInput.cursorPosition, markdown);
439 TimelineManager.focusMessageInput();
440 })
441
442 StickerPicker {
443 id: emojiPopup
444
445 emoji: true
446 }
447 }
448 ImageButton {
449 Layout.alignment: Qt.AlignRight | Qt.AlignBottom
450 Layout.margins: 8
451 Layout.rightMargin: 8
452 ToolTip.text: qsTr("Send")
453 ToolTip.visible: hovered
454 Layout.preferredHeight: 22
455 hoverEnabled: true
456 image: ":/icons/icons/ui/send.svg"
457 Layout.preferredWidth: 22
458
459 onClicked: {
460 room.input.send();
461 }
462 }
463 }
464 Label {
465 anchors.centerIn: parent
466 color: palette.placeholderText
467 text: qsTr("You don't have permission to send messages in this room")
468 visible: room ? (!room.permissions.canSend(MtxEvent.TextMessage)) : false
469 }
470 }