moment/src/gui/Pages/Chat/Timeline/EventContent.qml

315 lines
10 KiB
QML
Raw Normal View History

2019-12-19 22:46:16 +11:00
// SPDX-License-Identifier: LGPL-3.0-or-later
import QtQuick 2.12
import QtQuick.Layouts 1.12
2019-12-18 19:53:08 +11:00
import "../../../Base"
import "../../.."
2019-04-15 02:56:30 +10:00
HRowLayout {
2019-07-18 21:22:41 +10:00
id: eventContent
spacing: theme.chat.message.horizontalSpacing
layoutDirection: onRight ? Qt.RightToLeft: Qt.LeftToRight
2020-03-24 04:30:40 +11:00
readonly property var mentions: JSON.parse(model.mentions)
readonly property string mentionsCSS: {
const lines = []
for (const [name, link] of mentions) {
if (! link.match(/^https?:\/\/matrix.to\/#\/@.+/)) continue
2020-03-24 04:30:40 +11:00
lines.push(
`.mention[data-mention='${name}'] { color: ` +
utils.nameColor(name) +
"}"
)
}
return "<style type='text/css'>" + lines.join("\n") + "</style>"
}
readonly property string senderText:
hideNameLine ? "" : (
2020-03-23 04:05:35 +11:00
`<${smallAvatar ? "span" : "div"} class='sender'>` +
utils.coloredNameHtml(model.sender_name, model.sender_id) +
2020-03-23 04:05:35 +11:00
(smallAvatar ? ": " : "") +
(smallAvatar ? "</span>" : "</div>")
)
property string contentText: utils.processedEventText(model)
readonly property string timeText: utils.formatTime(model.date, false)
readonly property string localEchoText:
model.is_local_echo ?
2019-11-18 18:57:13 +11:00
`&nbsp;<font size=${theme.fontSize.small}px></font>` :
""
readonly property bool pureMedia: ! contentText && linksRepeater.count
2019-04-15 02:56:30 +10:00
readonly property string hoveredLink: contentLabel.hoveredLink
readonly property bool hoveredSelectable: contentHover.hovered
2019-09-20 09:28:28 +10:00
readonly property int xOffset:
onRight ?
Math.min(
contentColumn.width - contentLabel.paintedWidth -
contentLabel.leftPadding - contentLabel.rightPadding,
contentColumn.width - linksRepeater.widestChild -
(
pureMedia ?
0 : contentLabel.leftPadding + contentLabel.rightPadding
),
) :
2019-09-20 09:28:28 +10:00
0
readonly property int maxMessageWidth:
contentText.includes("<pre>") ?
-1 :
window.settings.maxMessageCharactersPerLine < 0 ?
-1 :
Math.ceil(
mainUI.fontMetrics.averageCharacterWidth *
window.settings.maxMessageCharactersPerLine
)
readonly property alias selectedText: contentLabel.selectedText
Item {
id: avatarWrapper
opacity: collapseAvatar ? 0 : 1
visible: ! hideAvatar
2020-03-23 04:05:35 +11:00
Layout.minimumWidth:
smallAvatar ?
theme.chat.message.collapsedAvatarSize :
theme.chat.message.avatarSize
Layout.minimumHeight: collapseAvatar ? 1 : Layout.minimumWidth
2019-12-05 00:08:38 +11:00
Layout.maximumWidth: Layout.minimumWidth
Layout.maximumHeight: Layout.minimumHeight
Layout.alignment: Qt.AlignTop
HUserAvatar {
id: avatar
Big performance refactoring & various improvements Instead of passing all sorts of events for the JS to handle and manually add to different data models, we now handle everything we can in Python. For any change, the python models send a sync event with their contents (no more than 4 times per second) to JS, and the QSyncable library's JsonListModel takes care of converting it to a QML ListModel and sending the appropriate signals. The SortFilterProxyModel library is not used anymore, the only case where we need to filter/sort something now is when the user interacts with the "Filter rooms" or "Filter members" fields. These cases are handled by a simple JS function. We now keep separated room and timeline models for different accounts, the previous approach of sharing all the data we could between accounts created a lot of complications (local echoes, decrypted messages replacing others, etc). The users's own account profile changes are now hidden in the timeline. On startup, if all events for a room were only own profile changes, more events will be loaded. Any kind of image format supported by Qt is now handled by the pyotherside image provider, instead of just PNG/JPG. SVGs which previously caused errors are supported as well. The typing members bar paddings/margins are fixed. The behavior of the avatar/"upload a profile picture" overlay is fixed. Config files read from disk are now cached (TODO: make them reloadable again). Pylint is not used anymore because of all its annoying false warnings and lack of understanding for dataclasses, it is replaced by flake8 with a custom config and various plugins. Debug mode is now considered on if the program was compiled with the right option, instead of taking an argument from CLI. When on, C++ will set a flag in the Window QML component. The loading screen is now unloaded after the UI is ready, where previously it just stayed in the background invisible and wasted CPU. The overall refactoring and improvements make us now able to handle rooms with thousand of members and no lazy-loading, where previously everything would freeze and simply scrolling up to load past events in any room would block the UI for a few seconds.
2019-08-11 22:01:22 +10:00
userId: model.sender_id
displayName: model.sender_name
mxc: model.sender_avatar
width: parent.width
2020-03-23 04:05:35 +11:00
height: collapseAvatar ? 1 : parent.Layout.minimumWidth
radius: theme.chat.message.avatarRadius
}
}
HColumnLayout {
id: contentColumn
Layout.fillWidth: true
Layout.alignment: Qt.AlignVCenter
HSelectableLabel {
id: contentLabel
visible: ! pureMedia
enableLinkActivation: ! eventList.selectedCount
selectByMouse:
eventList.selectedCount <= 1 &&
eventDelegate.checked &&
textSelectionBlocker.point.scenePosition === Qt.point(0, 0)
topPadding: theme.chat.message.verticalSpacing
bottomPadding: topPadding
leftPadding: eventContent.spacing
rightPadding: leftPadding
2019-11-30 22:10:48 +11:00
color: model.event_type === "RoomMessageNotice" ?
theme.chat.message.noticeBody :
theme.chat.message.body
font.italic: model.event_type === "RoomMessageEmote"
wrapMode: TextEdit.Wrap
textFormat: Text.RichText
text:
// CSS
2020-03-24 04:30:40 +11:00
theme.chat.message.styleInclude + mentionsCSS +
// Sender name & message body
(
smallAvatar && contentText.match(/^\s*<(p|h[1-6])>/) ?
contentText.replace(
/(^\s*<(p|h[1-6])>)/, "$1" + senderText,
) :
senderText + contentText
) +
// Time
// For some reason, if there's only one space,
// times will be on their own lines most of the time.
" " +
2019-11-18 18:57:13 +11:00
`<font size=${theme.fontSize.small}px ` +
`color=${theme.chat.message.date}>` +
timeText +
"</font>" +
// Local echo icon
(model.is_local_echo ?
2019-11-18 18:57:13 +11:00
`&nbsp;<font size=${theme.fontSize.small}px></font>` : "")
2019-09-20 09:28:28 +10:00
transform: Translate { x: xOffset }
Layout.maximumWidth: eventContent.maxMessageWidth
Layout.fillWidth: true
onSelectedTextChanged: if (selectedText) {
eventList.delegateWithSelectedText = model.id
eventList.selectedText = selectedText
} else if (eventList.delegateWithSelectedText === model.id) {
eventList.delegateWithSelectedText = ""
eventList.selectedText = ""
}
Connections {
target: eventList
function onCheckedChanged() {
contentLabel.deselect()
}
function onDelegateWithSelectedTextChanged() {
if (eventList.delegateWithSelectedText !== model.id)
contentLabel.deselect()
}
}
HoverHandler { id: contentHover }
PointHandler {
id: mousePointHandler
acceptedButtons: Qt.LeftButton
acceptedModifiers: Qt.NoModifier
acceptedPointerTypes:
PointerDevice.GenericPointer | PointerDevice.Eraser
onActiveChanged: {
if (active &&
! eventDelegate.checked &&
(! parent.hoveredLink ||
! parent.enableLinkActivation)) {
eventList.check(model.index)
checkedNow = true
}
if (! active && eventDelegate.checked) {
checkedNow ?
checkedNow = false :
eventList.uncheck(model.index)
}
}
property bool checkedNow: false
}
PointHandler {
id: mouseShiftPointHandler
acceptedButtons: Qt.LeftButton
acceptedModifiers: Qt.ShiftModifier
acceptedPointerTypes:
PointerDevice.GenericPointer | PointerDevice.Eraser
onActiveChanged: {
if (active &&
! eventDelegate.checked &&
(! parent.hoveredLink ||
! parent.enableLinkActivation)) {
eventList.checkFromLastToHere(model.index)
}
}
}
TapHandler {
id: touchTapHandler
acceptedButtons: Qt.LeftButton
acceptedPointerTypes: PointerDevice.Finger | PointerDevice.Pen
onTapped:
if (! parent.hoveredLink || ! parent.enableLinkActivation)
eventDelegate.toggleChecked()
}
TapHandler {
id: textSelectionBlocker
acceptedPointerTypes: PointerDevice.Finger | PointerDevice.Pen
}
Rectangle {
id: contentBackground
width: Math.max(
parent.paintedWidth +
parent.leftPadding + parent.rightPadding,
2019-12-20 06:56:07 +11:00
linksRepeater.summedWidth +
(pureMedia ? 0 : parent.leftPadding + parent.rightPadding),
)
height: contentColumn.height
radius: theme.chat.message.radius
2020-03-13 16:09:04 +11:00
z: -100
color: eventDelegate.checked &&
! contentLabel.selectedText &&
! mousePointHandler.active &&
! mouseShiftPointHandler.active ?
2020-03-27 12:01:36 +11:00
theme.chat.message.checkedBackground :
isOwn?
theme.chat.message.ownBackground :
theme.chat.message.background
2019-11-30 22:10:48 +11:00
Behavior on color { HColorAnimation {} }
2019-11-30 22:10:48 +11:00
Rectangle {
visible: model.event_type === "RoomMessageNotice"
2020-03-16 06:29:57 +11:00
// y: parent.height / 2 - height / 2
2019-11-30 22:10:48 +11:00
width: theme.chat.message.noticeLineWidth
height: parent.height
radius: parent.radius
color: utils.nameColor(
2019-11-30 22:10:48 +11:00
model.sender_name || model.sender_id.substring(1),
)
}
}
}
HRepeater {
2019-09-20 09:28:28 +10:00
id: linksRepeater
2020-05-11 08:30:20 +10:00
model: {
const links = JSON.parse(eventDelegate.currentModel.links)
if (eventDelegate.currentModel.media_url)
links.push(eventDelegate.currentModel.media_url)
return links
}
EventMediaLoader {
singleMediaInfo: eventDelegate.currentModel
mediaUrl: modelData
showSender: pureMedia ? senderText : ""
showDate: pureMedia ? timeText : ""
showLocalEcho: pureMedia ? localEchoText : ""
2019-09-20 09:28:28 +10:00
transform: Translate { x: xOffset }
2019-10-28 04:26:00 +11:00
Layout.bottomMargin: pureMedia ? 0 : contentLabel.bottomPadding
Layout.leftMargin: pureMedia ? 0 : eventContent.spacing
Layout.rightMargin: pureMedia ? 0 : eventContent.spacing
2019-11-06 00:19:48 +11:00
Layout.preferredWidth: item ? item.width : -1
Layout.preferredHeight: item ? item.height : -1
}
2019-04-15 02:56:30 +10:00
}
}
HSpacer {}
2019-04-15 02:56:30 +10:00
}