Spectral/qml/form/RoomForm.qml

528 lines
18 KiB
QML
Raw Normal View History

2018-07-12 01:44:41 +00:00
import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import QtQuick.Controls.Material 2.2
import Spectral 0.1
import Spectral.Settings 0.1
2018-09-26 23:37:09 +00:00
import SortFilterProxyModel 0.2
2018-09-26 23:37:09 +00:00
import "../component"
import "../menu"
2018-07-09 14:00:27 +00:00
import "qrc:/js/md.js" as Markdown
2018-09-10 01:51:02 +00:00
import "qrc:/js/util.js" as Util
2018-02-23 14:39:14 +00:00
Item {
property var currentRoom: null
2018-03-05 11:11:55 +00:00
id: item
2018-09-26 23:37:09 +00:00
MessageEventModel {
id: messageEventModel
room: currentRoom
}
2018-09-06 05:25:39 +00:00
RoomDrawer {
width: Math.min(item.width * 0.7, 480)
height: item.height
2018-02-23 14:39:14 +00:00
2018-09-04 13:13:14 +00:00
id: roomDrawer
2018-09-06 05:25:39 +00:00
room: currentRoom
}
2018-09-27 11:44:34 +00:00
Label {
anchors.centerIn: parent
visible: !currentRoom
text: "Please choose a room."
}
ColumnLayout {
anchors.fill: parent
2018-09-27 11:44:34 +00:00
spacing: 0
2018-09-27 11:44:34 +00:00
visible: currentRoom
2018-02-23 14:39:14 +00:00
2018-09-27 11:44:34 +00:00
Rectangle {
Layout.fillWidth: true
Layout.preferredHeight: 64
z: 10
2018-09-27 11:44:34 +00:00
color: Material.accent
2018-09-27 11:44:34 +00:00
ItemDelegate {
anchors.fill: parent
2018-02-23 14:39:14 +00:00
2018-09-27 11:44:34 +00:00
onClicked: roomDrawer.open()
2018-09-26 23:37:09 +00:00
2018-09-27 11:44:34 +00:00
RowLayout {
anchors.fill: parent
2018-09-27 11:44:34 +00:00
anchors.margins: 12
2018-09-27 11:44:34 +00:00
spacing: 12
2018-09-27 11:44:34 +00:00
ImageItem {
Layout.preferredWidth: height
Layout.fillHeight: true
2018-09-27 11:44:34 +00:00
hint: currentRoom ? currentRoom.displayName : "No name"
image: spectralController.safeImage(currentRoom ? currentRoom.avatar : null)
}
2018-09-27 11:44:34 +00:00
ColumnLayout {
Layout.fillWidth: true
Layout.fillHeight: true
2018-09-04 13:13:14 +00:00
2018-09-27 11:44:34 +00:00
visible: parent.width > 64
2018-09-27 11:44:34 +00:00
Label {
Layout.fillWidth: true
Layout.fillHeight: true
2018-09-27 11:44:34 +00:00
text: currentRoom ? currentRoom.displayName : ""
color: "white"
font.pointSize: 12
elide: Text.ElideRight
wrapMode: Text.NoWrap
}
2018-09-27 11:44:34 +00:00
Label {
Layout.fillWidth: true
Layout.fillHeight: true
2018-09-27 11:44:34 +00:00
text: currentRoom ? (currentRoom.topic).replace(/(\r\n\t|\n|\r\t)/gm,"") : ""
color: "white"
elide: Text.ElideRight
wrapMode: Text.NoWrap
}
2018-02-23 14:39:14 +00:00
}
}
}
2018-09-27 11:44:34 +00:00
}
2018-02-23 14:39:14 +00:00
ListView {
2018-09-27 11:44:34 +00:00
Layout.fillWidth: true
Layout.fillHeight: true
Layout.margins: 16
2018-07-09 02:45:26 +00:00
id: messageListView
displayMarginBeginning: 40
displayMarginEnd: 40
verticalLayoutDirection: ListView.BottomToTop
spacing: 8
2018-07-09 02:45:26 +00:00
flickDeceleration: 4096
2018-09-04 13:13:14 +00:00
boundsBehavior: Flickable.DragOverBounds
2018-09-04 13:13:14 +00:00
property int largestVisibleIndex: count > 0 ? indexAt(contentX, contentY + height - 1) : -1
2018-07-30 05:52:20 +00:00
onContentYChanged: {
// Check whether we're about to bump into the ceiling in 2 seconds
var curVelocity = verticalVelocity // Snapshot the current speed
if(curVelocity < 0 && contentY + curVelocity*2 < originY)
{
// Request the amount of messages enough to scroll at this
// rate for 3 more seconds
2018-09-30 09:09:58 +00:00
currentRoom.getPreviousContent(20);
}
}
2018-08-04 16:15:58 +00:00
onMovementEnded: currentRoom.saveViewport(sortedMessageEventModel.mapToSource(indexAt(contentX, contentY)), sortedMessageEventModel.mapToSource(largestVisibleIndex))
2018-09-30 09:09:58 +00:00
displaced: Transition {
NumberAnimation {
property: "y"; duration: 200
easing.type: Easing.OutQuad
2018-09-30 09:09:58 +00:00
}
}
2018-09-06 04:34:15 +00:00
model: SortFilterProxyModel {
id: sortedMessageEventModel
2018-09-26 23:37:09 +00:00
sourceModel: messageEventModel
2018-09-26 23:37:09 +00:00
filters: ExpressionFilter {
expression: marks !== 0x08 && marks !== 0x10
2018-09-27 11:44:34 +00:00
}
onModelReset: {
if (currentRoom)
{
var lastScrollPosition = mapFromSource(currentRoom.savedTopVisibleIndex())
if (lastScrollPosition === 0)
messageListView.positionViewAtBeginning()
else
{
console.log("Scrolling to position", lastScrollPosition)
messageListView.currentIndex = lastScrollPosition
}
if (messageListView.contentY < messageListView.originY + 10)
currentRoom.getPreviousContent(100)
}
console.log("Model timeline reset")
}
}
2018-07-30 05:52:20 +00:00
delegate: ColumnLayout {
width: parent.width
2018-09-04 06:58:41 +00:00
id: delegateColumn
2018-09-04 12:49:53 +00:00
spacing: 8
Label {
Layout.alignment: Qt.AlignHCenter
visible: section !== aboveSection
text: section
color: "white"
verticalAlignment: Text.AlignVCenter
leftPadding: 8
rightPadding: 8
topPadding: 4
bottomPadding: 4
2018-08-04 16:15:58 +00:00
background: Rectangle {
color: MSettings.darkTheme ? "#484848" : "grey"
2018-09-27 11:44:34 +00:00
}
}
2018-08-13 05:07:35 +00:00
MessageDelegate {
visible: eventType === "notice" || eventType === "message" || eventType === "image" || eventType === "video" || eventType === "audio" || eventType === "file"
}
2018-09-04 13:13:14 +00:00
StateDelegate {
Layout.maximumWidth: messageListView.width * 0.8
2018-09-07 04:26:09 +00:00
visible: eventType === "emote" || eventType === "state"
}
2018-09-07 04:26:09 +00:00
Label {
Layout.alignment: Qt.AlignHCenter
2018-09-07 04:26:09 +00:00
visible: eventType === "other"
2018-09-23 13:14:12 +00:00
text: display
color: "grey"
font.italic: true
}
2018-09-23 13:14:12 +00:00
Label {
Layout.alignment: Qt.AlignHCenter
2018-09-23 13:14:12 +00:00
visible: readMarker === true && index !== 0
2018-09-23 13:14:12 +00:00
text: "And Now"
color: "white"
verticalAlignment: Text.AlignVCenter
leftPadding: 8
rightPadding: 8
topPadding: 4
bottomPadding: 4
2018-09-27 11:44:34 +00:00
background: Rectangle { color: MSettings.darkTheme ? "#484848" : "grey" }
2018-09-27 11:44:34 +00:00
}
}
RoundButton {
width: 64
height: 64
2018-09-04 13:13:14 +00:00
id: goTopFab
2018-09-04 13:13:14 +00:00
visible: !(parent.atYEnd || messageListView.moving)
2018-07-09 05:36:28 +00:00
anchors.right: parent.right
anchors.bottom: parent.bottom
2018-07-09 05:36:28 +00:00
contentItem: MaterialIcon {
anchors.fill: parent
2018-09-02 13:26:42 +00:00
icon: "\ue313"
color: "white"
}
2018-08-04 16:15:58 +00:00
Material.background: Material.accent
2018-08-04 16:15:58 +00:00
onClicked: parent.positionViewAtBeginning()
2018-07-09 05:36:28 +00:00
Behavior on opacity { NumberAnimation { duration: 200 } }
}
2018-09-11 05:14:56 +00:00
MessageContextMenu { id: messageContextMenu }
2018-09-11 05:14:56 +00:00
Popup {
property string sourceText
2018-09-11 05:14:56 +00:00
x: (window.width - width) / 2
y: (window.height - height) / 2
width: 480
2018-09-11 05:14:56 +00:00
id: sourceDialog
2018-09-11 05:14:56 +00:00
parent: ApplicationWindow.overlay
2018-09-11 05:14:56 +00:00
modal: true
2018-09-11 05:14:56 +00:00
padding: 16
2018-09-11 05:14:56 +00:00
closePolicy: Dialog.CloseOnEscape | Dialog.CloseOnPressOutside
2018-09-11 05:14:56 +00:00
contentItem: ScrollView {
TextArea {
readOnly: true
selectByMouse: true
2018-09-11 05:14:56 +00:00
text: sourceDialog.sourceText
2018-09-11 05:14:56 +00:00
}
2018-09-27 11:44:34 +00:00
}
}
2018-09-15 11:07:38 +00:00
Popup {
property alias listModel: readMarkerListView.model
2018-09-15 11:07:38 +00:00
x: (window.width - width) / 2
y: (window.height - height) / 2
width: 320
2018-09-15 11:07:38 +00:00
id: readMarkerDialog
2018-09-15 11:07:38 +00:00
parent: ApplicationWindow.overlay
2018-09-15 11:07:38 +00:00
modal: true
padding: 16
2018-09-15 11:07:38 +00:00
closePolicy: Dialog.CloseOnEscape | Dialog.CloseOnPressOutside
2018-09-15 11:07:38 +00:00
contentItem: ListView {
implicitHeight: Math.min(window.height - 64, readMarkerListView.contentHeight)
2018-09-15 11:07:38 +00:00
id: readMarkerListView
2018-09-15 11:07:38 +00:00
clip: true
boundsBehavior: Flickable.DragOverBounds
2018-09-15 11:07:38 +00:00
delegate: ItemDelegate {
width: parent.width
height: 48
2018-09-15 11:07:38 +00:00
RowLayout {
anchors.fill: parent
anchors.margins: 8
spacing: 12
2018-09-15 11:07:38 +00:00
ImageItem {
Layout.preferredWidth: height
Layout.fillHeight: true
2018-09-15 11:07:38 +00:00
image: modelData.avatar
hint: modelData.displayName
}
2018-09-15 11:07:38 +00:00
Label {
Layout.fillWidth: true
2018-09-15 11:07:38 +00:00
text: modelData.displayName
2018-09-15 11:07:38 +00:00
}
}
}
2018-07-09 05:36:28 +00:00
ScrollBar.vertical: ScrollBar {}
}
}
2018-09-27 11:44:34 +00:00
}
2018-02-23 14:39:14 +00:00
2018-09-27 11:44:34 +00:00
Item {
Layout.fillWidth: true
Layout.preferredHeight: 40
}
2018-09-27 11:44:34 +00:00
Rectangle {
Layout.fillWidth: true
Layout.preferredHeight: 40
Layout.leftMargin: 16
Layout.rightMargin: 16
2018-09-27 11:44:34 +00:00
color: Material.background
2018-09-27 11:44:34 +00:00
Rectangle {
anchors.verticalCenter: parent.top
width: parent.width
height: 48
2018-09-27 11:44:34 +00:00
color: MSettings.darkTheme ? "#303030" : "#fafafa"
2018-09-27 11:44:34 +00:00
layer.enabled: true
layer.effect: ElevationEffect {
elevation: 2
}
2018-09-04 13:13:14 +00:00
2018-09-27 11:44:34 +00:00
RowLayout {
anchors.fill: parent
2018-09-19 23:01:55 +00:00
2018-09-27 11:44:34 +00:00
spacing: 0
2018-09-04 13:13:14 +00:00
2018-09-27 11:44:34 +00:00
ItemDelegate {
Layout.preferredWidth: 48
Layout.preferredHeight: 48
2018-09-02 13:26:42 +00:00
2018-09-27 11:44:34 +00:00
contentItem: MaterialIcon { icon: "\ue226" }
2018-09-02 13:26:42 +00:00
2018-09-27 11:44:34 +00:00
onClicked: currentRoom.chooseAndUploadFile()
}
2018-09-27 11:44:34 +00:00
ScrollView {
Layout.fillWidth: true
Layout.preferredHeight: 48
2018-09-27 11:44:34 +00:00
ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
2018-02-23 14:39:14 +00:00
2018-09-27 11:44:34 +00:00
clip: true
2018-08-19 06:32:18 +00:00
2018-09-27 11:44:34 +00:00
TextArea {
property real progress: 0
2018-09-27 11:44:34 +00:00
id: inputField
2018-08-20 14:40:36 +00:00
2018-09-27 11:44:34 +00:00
wrapMode: Text.Wrap
placeholderText: "Send a Message"
leftPadding: 16
topPadding: 0
bottomPadding: 0
selectByMouse: true
verticalAlignment: TextEdit.AlignVCenter
2018-07-09 14:00:27 +00:00
2018-09-27 11:44:34 +00:00
text: currentRoom ? currentRoom.cachedInput : ""
2018-07-11 08:07:04 +00:00
2018-09-27 11:44:34 +00:00
background: Item {}
2018-07-09 14:00:27 +00:00
2018-09-27 11:44:34 +00:00
onTextChanged: {
timeoutTimer.restart()
repeatTimer.start()
currentRoom.cachedInput = text
}
2018-09-02 13:26:42 +00:00
2018-09-27 11:44:34 +00:00
ToolTip.visible: currentRoom && currentRoom.hasUsersTyping
ToolTip.text: currentRoom ? currentRoom.usersTyping : ""
2018-09-02 13:26:42 +00:00
2018-09-27 11:44:34 +00:00
Keys.onReturnPressed: {
if (event.modifiers & Qt.ShiftModifier) {
inputField.insert(inputField.cursorPosition, "\n")
} else {
inputField.postMessage(inputField.text)
inputField.text = ""
2018-09-26 23:37:09 +00:00
}
2018-09-27 11:44:34 +00:00
}
2018-09-26 23:37:09 +00:00
2018-09-27 11:44:34 +00:00
Timer {
id: timeoutTimer
2018-09-17 03:58:02 +00:00
2018-09-27 11:44:34 +00:00
repeat: false
interval: 2000
onTriggered: {
repeatTimer.stop()
currentRoom.sendTypingNotification(false)
2018-09-17 03:58:02 +00:00
}
2018-09-27 11:44:34 +00:00
}
Timer {
id: repeatTimer
2018-09-17 03:58:02 +00:00
2018-09-27 11:44:34 +00:00
repeat: true
interval: 5000
triggeredOnStart: true
onTriggered: currentRoom.sendTypingNotification(true)
}
2018-02-23 14:39:14 +00:00
2018-09-27 11:44:34 +00:00
function postMessage(text) {
if (text.trim().length === 0) { return }
if(!currentRoom) { return }
var PREFIX_ME = '/me '
var PREFIX_NOTICE = '/notice '
var PREFIX_RAINBOW = '/rainbow '
var PREFIX_HTML = '/html '
var PREFIX_MARKDOWN = '/md '
var replyRe = new RegExp("^> <(.*)><(.*)> (.*)\n\n(.*)")
if (text.match(replyRe)) {
var matches = text.match(replyRe)
currentRoom.sendReply(matches[1], matches[2], matches[3], matches[4])
return
2018-09-26 23:37:09 +00:00
}
2018-09-27 11:44:34 +00:00
if (text.indexOf(PREFIX_ME) === 0) {
text = text.substr(PREFIX_ME.length)
currentRoom.postMessage(text, RoomMessageEvent.Emote)
return
}
if (text.indexOf(PREFIX_NOTICE) === 0) {
text = text.substr(PREFIX_NOTICE.length)
currentRoom.postMessage(text, RoomMessageEvent.Notice)
return
}
if (text.indexOf(PREFIX_RAINBOW) === 0) {
text = text.substr(PREFIX_RAINBOW.length)
2018-09-26 23:37:09 +00:00
2018-09-27 11:44:34 +00:00
var parsedText = ""
var rainbowColor = ["#ff2b00", "#ff5500", "#ff8000", "#ffaa00", "#ffd500", "#ffff00", "#d4ff00", "#aaff00", "#80ff00", "#55ff00", "#2bff00", "#00ff00", "#00ff2b", "#00ff55", "#00ff80", "#00ffaa", "#00ffd5", "#00ffff", "#00d4ff", "#00aaff", "#007fff", "#0055ff", "#002bff", "#0000ff", "#2a00ff", "#5500ff", "#7f00ff", "#aa00ff", "#d400ff", "#ff00ff", "#ff00d4", "#ff00aa", "#ff0080", "#ff0055", "#ff002b", "#ff0000"]
for (var i = 0; i < text.length; i++) {
parsedText = parsedText + "<font color='" + rainbowColor[i % rainbowColor.length] + "'>" + text.charAt(i) + "</font>"
2018-09-26 23:37:09 +00:00
}
2018-09-27 11:44:34 +00:00
currentRoom.postHtmlMessage(text, parsedText, RoomMessageEvent.Text)
return
}
if (text.indexOf(PREFIX_HTML) === 0) {
text = text.substr(PREFIX_HTML.length)
var re = new RegExp("<.*?>")
var plainText = text.replace(re, "")
currentRoom.postHtmlMessage(plainText, text, RoomMessageEvent.Text)
return
}
if (text.indexOf(PREFIX_MARKDOWN) === 0) {
text = text.substr(PREFIX_MARKDOWN.length)
var parsedText = Markdown.markdown_parser(text)
currentRoom.postHtmlMessage(text, parsedText, RoomMessageEvent.Text)
return
2018-09-26 23:37:09 +00:00
}
2018-09-27 11:44:34 +00:00
currentRoom.postPlainText(text)
2018-09-26 23:37:09 +00:00
}
}
2018-09-27 11:44:34 +00:00
}
2018-09-27 11:44:34 +00:00
ItemDelegate {
Layout.preferredWidth: 48
Layout.preferredHeight: 48
2018-09-04 13:13:14 +00:00
2018-09-27 11:44:34 +00:00
id: emojiButton
2018-09-27 11:44:34 +00:00
contentItem: MaterialIcon { icon: "\ue24e" }
2018-08-11 04:42:13 +00:00
2018-09-27 11:44:34 +00:00
onClicked: emojiPicker.visible ? emojiPicker.close() : emojiPicker.open()
2018-08-11 04:42:13 +00:00
2018-09-27 11:44:34 +00:00
EmojiPicker {
x: window.width - 370
y: window.height - 400
2018-08-11 04:42:13 +00:00
2018-09-27 11:44:34 +00:00
width: 360
height: 320
2018-08-11 04:42:13 +00:00
2018-09-27 11:44:34 +00:00
id: emojiPicker
2018-09-04 13:13:14 +00:00
2018-09-27 11:44:34 +00:00
parent: ApplicationWindow.overlay
2018-09-04 13:13:14 +00:00
2018-09-27 11:44:34 +00:00
Material.elevation: 2
2018-09-14 04:16:25 +00:00
2018-09-27 11:44:34 +00:00
textArea: inputField
2018-09-26 23:37:09 +00:00
}
2018-02-23 14:39:14 +00:00
}
}
}
}
}
}