1// SPDX-FileCopyrightText: 2021 Nheko Contributors
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5import "./delegates/"
6import QtQuick 2.9
7import QtQuick.Controls 2.3
8import im.nheko 1.0
9
10Popup {
11    id: forwardMessagePopup
12
13    property var mid
14
15    function setMessageEventId(mid_in) {
16        mid = mid_in;
17    }
18
19    x: Math.round(parent.width / 2 - width / 2)
20    y: Math.round(parent.height / 2 - height / 2)
21    modal: true
22    palette: Nheko.colors
23    parent: Overlay.overlay
24    width: implicitWidth >= (timelineRoot.width * 0.8) ? implicitWidth : (timelineRoot.width * 0.8)
25    height: implicitHeight + completerPopup.height + padding * 2
26    leftPadding: 10
27    rightPadding: 10
28    onOpened: {
29        completerPopup.open();
30        roomTextInput.forceActiveFocus();
31    }
32    onClosed: {
33        completerPopup.close();
34    }
35
36    Column {
37        id: forwardColumn
38
39        spacing: 5
40
41        Label {
42            id: titleLabel
43
44            text: qsTr("Forward Message")
45            font.bold: true
46            bottomPadding: 10
47            color: Nheko.colors.text
48        }
49
50        Reply {
51            id: replyPreview
52
53            property var modelData: room ? room.getDump(mid, "") : {
54            }
55
56            userColor: TimelineManager.userColor(modelData.userId, Nheko.colors.window)
57            blurhash: modelData.blurhash ?? ""
58            body: modelData.body ?? ""
59            formattedBody: modelData.formattedBody ?? ""
60            eventId: modelData.eventId ?? ""
61            filename: modelData.filename ?? ""
62            filesize: modelData.filesize ?? ""
63            proportionalHeight: modelData.proportionalHeight ?? 1
64            type: modelData.type ?? MtxEvent.UnknownMessage
65            typeString: modelData.typeString ?? ""
66            url: modelData.url ?? ""
67            originalWidth: modelData.originalWidth ?? 0
68            isOnlyEmoji: modelData.isOnlyEmoji ?? false
69            userId: modelData.userId ?? ""
70            userName: modelData.userName ?? ""
71            encryptionError: modelData.encryptionError ?? ""
72        }
73
74        MatrixTextField {
75            id: roomTextInput
76
77            width: forwardMessagePopup.width - forwardMessagePopup.leftPadding * 2
78            color: Nheko.colors.text
79            onTextEdited: {
80                completerPopup.completer.searchString = text;
81            }
82            Keys.onPressed: {
83                if ((event.key == Qt.Key_Up || event.key == Qt.Key_Backtab) && completerPopup.opened) {
84                    event.accepted = true;
85                    completerPopup.up();
86                } else if ((event.key == Qt.Key_Down || event.key == Qt.Key_Tab) && completerPopup.opened) {
87                    event.accepted = true;
88                    if (event.key == Qt.Key_Tab && (event.modifiers & Qt.ShiftModifier))
89                        completerPopup.up();
90                    else
91                        completerPopup.down();
92                } else if (event.matches(StandardKey.InsertParagraphSeparator)) {
93                    completerPopup.finishCompletion();
94                    event.accepted = true;
95                }
96            }
97        }
98
99    }
100
101    Completer {
102        id: completerPopup
103
104        y: titleLabel.height + replyPreview.height + roomTextInput.height + roomTextInput.bottomPadding + forwardColumn.spacing * 3
105        width: forwardMessagePopup.width - forwardMessagePopup.leftPadding * 2
106        completerName: "room"
107        fullWidth: true
108        centerRowContent: false
109        avatarHeight: 24
110        avatarWidth: 24
111        bottomToTop: false
112        closePolicy: Popup.NoAutoClose
113    }
114
115    Connections {
116        function onCompletionSelected(id) {
117            room.forwardMessage(messageContextMenu.eventId, id);
118            forwardMessagePopup.close();
119        }
120
121        function onCountChanged() {
122            if (completerPopup.count > 0 && (completerPopup.currentIndex < 0 || completerPopup.currentIndex >= completerPopup.count))
123                completerPopup.currentIndex = 0;
124
125        }
126
127        target: completerPopup
128    }
129
130    background: Rectangle {
131        color: Nheko.colors.window
132    }
133
134    Overlay.modal: Rectangle {
135        color: Qt.rgba(Nheko.colors.window.r, Nheko.colors.window.g, Nheko.colors.window.b, 0.7)
136    }
137
138}
139