| 1 |
// SPDX-FileCopyrightText: Nheko Contributors |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
import ".." |
| 6 |
import "../components" |
| 7 |
import QtQuick |
| 8 |
import QtQuick.Controls |
| 9 |
import QtQuick.Layouts |
| 10 |
import im.nheko |
| 11 |
|
| 12 |
ApplicationWindow { |
| 13 |
id: inviteDialogRoot |
| 14 |
|
| 15 |
property InviteesModel invitees |
| 16 |
property var friendsCompleter |
| 17 |
property var profile |
| 18 |
minimumWidth: 300 |
| 19 |
|
| 20 |
Component.onCompleted: { |
| 21 |
friendsCompleter = TimelineManager.completerFor("user", "friends") |
| 22 |
width = 600 |
| 23 |
} |
| 24 |
|
| 25 |
function addInvite(mxid, displayName, avatarUrl) { |
| 26 |
if (mxid.match("@.+?:.{3,}")) { |
| 27 |
invitees.addUser(mxid, displayName, avatarUrl); |
| 28 |
} else |
| 29 |
console.log("invalid mxid: " + mxid) |
| 30 |
} |
| 31 |
|
| 32 |
function cleanUpAndClose() { |
| 33 |
if (inviteeEntry.isValidMxid) |
| 34 |
addInvite(inviteeEntry.text, "", ""); |
| 35 |
|
| 36 |
invitees.accept(); |
| 37 |
close(); |
| 38 |
} |
| 39 |
|
| 40 |
title: qsTr("Invite users to %1").arg(invitees.room.plainRoomName) |
| 41 |
height: 380 |
| 42 |
width: 340 |
| 43 |
color: palette.window |
| 44 |
flags: Qt.Dialog | Qt.WindowCloseButtonHint | Qt.WindowTitleHint |
| 45 |
|
| 46 |
Shortcut { |
| 47 |
sequence: "Ctrl+Enter" |
| 48 |
onActivated: cleanUpAndClose() |
| 49 |
} |
| 50 |
|
| 51 |
Shortcut { |
| 52 |
sequence: StandardKey.Cancel |
| 53 |
onActivated: inviteDialogRoot.close() |
| 54 |
} |
| 55 |
|
| 56 |
ColumnLayout { |
| 57 |
anchors.fill: parent |
| 58 |
anchors.margins: Nheko.paddingMedium |
| 59 |
spacing: Nheko.paddingMedium |
| 60 |
Flow { |
| 61 |
layoutDirection: Qt.LeftToRight |
| 62 |
Layout.fillWidth: true |
| 63 |
Layout.preferredHeight: implicitHeight |
| 64 |
spacing: 4 |
| 65 |
visible: !inviteesList.visible |
| 66 |
Repeater { |
| 67 |
id: inviteesRepeater |
| 68 |
model: invitees |
| 69 |
delegate: ItemDelegate { |
| 70 |
onClicked: invitees.removeUser(model.mxid) |
| 71 |
id: inviteeButton |
| 72 |
contentItem: Label { |
| 73 |
anchors.centerIn: parent |
| 74 |
id: inviteeUserid |
| 75 |
text: model.displayName != "" ? model.displayName : model.userid |
| 76 |
color: inviteeButton.hovered ? palette.highlightedText: palette.text |
| 77 |
maximumLineCount: 1 |
| 78 |
} |
| 79 |
background: Rectangle { |
| 80 |
border.color: palette.text |
| 81 |
color: inviteeButton.hovered ? palette.highlight : palette.window |
| 82 |
border.width: 1 |
| 83 |
radius: inviteeButton.height / 2 |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
Label { |
| 90 |
text: qsTr("Search user") |
| 91 |
Layout.fillWidth: true |
| 92 |
color: palette.text |
| 93 |
} |
| 94 |
RowLayout { |
| 95 |
spacing: Nheko.paddingMedium |
| 96 |
|
| 97 |
MatrixTextField { |
| 98 |
id: inviteeEntry |
| 99 |
|
| 100 |
property bool isValidMxid: text.match("@.+?:.{3,}") |
| 101 |
|
| 102 |
backgroundColor: palette.window |
| 103 |
placeholderText: qsTr("@user:yourserver.example.com", "Example user id. The name 'user' can be localized however you want.") |
| 104 |
Layout.fillWidth: true |
| 105 |
onAccepted: { |
| 106 |
if (isValidMxid) { |
| 107 |
addInvite(text, "", ""); |
| 108 |
clear() |
| 109 |
} |
| 110 |
else if (userSearch.count > 0) { |
| 111 |
addInvite(userSearch.itemAtIndex(0).userid, userSearch.itemAtIndex(0).displayName, userSearch.itemAtIndex(0).avatarUrl) |
| 112 |
clear() |
| 113 |
} |
| 114 |
} |
| 115 |
Component.onCompleted: forceActiveFocus() |
| 116 |
Keys.onShortcutOverride: event.accepted = ((event.key === Qt.Key_Return || event.key === Qt.Key_Enter) && (event.modifiers & Qt.ControlModifier)) |
| 117 |
Keys.onPressed: { |
| 118 |
if ((event.key === Qt.Key_Return || event.key === Qt.Key_Enter) && (event.modifiers === Qt.ControlModifier)) |
| 119 |
cleanUpAndClose(); |
| 120 |
|
| 121 |
} |
| 122 |
onTextChanged: { |
| 123 |
searchTimer.restart() |
| 124 |
if(isValidMxid) { |
| 125 |
profile = TimelineManager.getGlobalUserProfile(text); |
| 126 |
} else |
| 127 |
profile = null; |
| 128 |
} |
| 129 |
Timer { |
| 130 |
id: searchTimer |
| 131 |
|
| 132 |
interval: 350 |
| 133 |
onTriggered: { |
| 134 |
userSearch.model.setSearchString(parent.text) |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
ToggleButton { |
| 140 |
id: searchOnServer |
| 141 |
checked: false |
| 142 |
onClicked: userSearch.model.setSearchString(inviteeEntry.text) |
| 143 |
} |
| 144 |
MatrixText { |
| 145 |
text: qsTr("Search on Server") |
| 146 |
} |
| 147 |
|
| 148 |
} |
| 149 |
RowLayout { |
| 150 |
UserListRow { |
| 151 |
visible: inviteeEntry.isValidMxid |
| 152 |
id: del3 |
| 153 |
Layout.preferredWidth: inviteDialogRoot.width/2 |
| 154 |
Layout.alignment: Qt.AlignTop |
| 155 |
Layout.preferredHeight: implicitHeight |
| 156 |
displayName: profile? profile.displayName : "" |
| 157 |
avatarUrl: profile? profile.avatarUrl : "" |
| 158 |
userid: inviteeEntry.text |
| 159 |
onClicked: addInvite(inviteeEntry.text, displayName, avatarUrl) |
| 160 |
bgColor: del3.hovered ? palette.dark : inviteDialogRoot.color |
| 161 |
} |
| 162 |
ListView { |
| 163 |
visible: !inviteeEntry.isValidMxid |
| 164 |
id: userSearch |
| 165 |
model: searchOnServer.checked? userDirectory : friendsCompleter |
| 166 |
Layout.fillWidth: true |
| 167 |
Layout.fillHeight: true |
| 168 |
clip: true |
| 169 |
delegate: UserListRow { |
| 170 |
id: del2 |
| 171 |
width: ListView.view.width |
| 172 |
height: implicitHeight |
| 173 |
displayName: model.displayName |
| 174 |
userid: model.userid |
| 175 |
avatarUrl: model.avatarUrl |
| 176 |
onClicked: addInvite(userid, displayName, avatarUrl) |
| 177 |
bgColor: del2.hovered ? palette.dark : inviteDialogRoot.color |
| 178 |
} |
| 179 |
} |
| 180 |
Rectangle { |
| 181 |
Layout.fillHeight: true |
| 182 |
visible: inviteesList.visible |
| 183 |
Layout.preferredWidth: 1 |
| 184 |
color: Nheko.theme.separator |
| 185 |
} |
| 186 |
ListView { |
| 187 |
id: inviteesList |
| 188 |
|
| 189 |
Layout.fillWidth: true |
| 190 |
Layout.fillHeight: true |
| 191 |
model: invitees |
| 192 |
clip: true |
| 193 |
visible: inviteDialogRoot.width >= 500 |
| 194 |
|
| 195 |
delegate: UserListRow { |
| 196 |
id: del |
| 197 |
hoverEnabled: true |
| 198 |
width: ListView.view.width |
| 199 |
height: implicitHeight |
| 200 |
onClicked: TimelineManager.openGlobalUserProfile(model.mxid) |
| 201 |
userid: model.mxid |
| 202 |
avatarUrl: model.avatarUrl |
| 203 |
displayName: model.displayName |
| 204 |
bgColor: del.hovered ? palette.dark : inviteDialogRoot.color |
| 205 |
ImageButton { |
| 206 |
anchors.right: parent.right |
| 207 |
anchors.rightMargin: Nheko.paddingSmall |
| 208 |
anchors.top: parent.top |
| 209 |
anchors.topMargin: Nheko.paddingSmall |
| 210 |
id: removeButton |
| 211 |
image: ":/icons/icons/ui/dismiss.svg" |
| 212 |
onClicked: invitees.removeUser(model.mxid) |
| 213 |
} |
| 214 |
|
| 215 |
NhekoCursorShape { |
| 216 |
anchors.fill: parent |
| 217 |
cursorShape: Qt.PointingHandCursor |
| 218 |
} |
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
footer: DialogButtonBox { |
| 228 |
id: buttons |
| 229 |
|
| 230 |
Button { |
| 231 |
text: qsTr("Invite") |
| 232 |
DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole |
| 233 |
enabled: invitees.count > 0 |
| 234 |
onClicked: cleanUpAndClose() |
| 235 |
} |
| 236 |
|
| 237 |
Button { |
| 238 |
text: qsTr("Cancel") |
| 239 |
DialogButtonBox.buttonRole: DialogButtonBox.DestructiveRole |
| 240 |
onClicked: inviteDialogRoot.close() |
| 241 |
} |
| 242 |
|
| 243 |
} |
| 244 |
|
| 245 |
} |