Debian Codegraph @

resources/qml/Reactions.qml

raw
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 import QtQuick 2.6
6 import QtQuick.Controls 2.2
7 import im.nheko 1.0
8
9 // This class is for showing Reactions in the timeline row, not for
10 // adding new reactions via the emoji picker
11 Flow {
12 id: reactionFlow
13
14 property string eventId
15
16 // lower-contrast colors to avoid distracting from text & to enhance hover effect
17 property color gentleHighlight: Qt.hsla(palette.highlight.hslHue, palette.highlight.hslSaturation, palette.highlight.hslLightness, 0.8)
18 property color gentleText: Qt.hsla(palette.text.hslHue, palette.text.hslSaturation, palette.text.hslLightness, 0.6)
19 property alias reactions: repeater.model
20
21 spacing: 4
22
23 Repeater {
24 id: repeater
25
26 delegate: AbstractButton {
27 id: reaction
28
29 ToolTip.delay: Nheko.tooltipDelay
30 ToolTip.visible: hovered
31 hoverEnabled: true
32 leftPadding: textMetrics.height / 2
33 rightPadding: textMetrics.height / 2
34
35 background: Rectangle {
36 anchors.centerIn: parent
37 border.color: reaction.hovered ? palette.text : gentleText
38 border.width: 1
39 color: reaction.hovered ? palette.highlight : (modelData.selfReactedEvent !== '' ? gentleHighlight : palette.window)
40 implicitHeight: reaction.implicitHeight
41 implicitWidth: reaction.implicitWidth
42 radius: reaction.height / 2
43 }
44 contentItem: Row {
45 spacing: textMetrics.height / 4
46
47 TextMetrics {
48 id: textMetrics
49
50 elide: Text.ElideRight
51 elideWidth: 150
52 font.family: Settings.emojiFont != "" ? Settings.emojiFont : undefined
53 text: modelData.displayKey
54 }
55 Text {
56 id: reactionText
57
58 anchors.baseline: reactionCounter.baseline
59 color: (reaction.hovered || modelData.selfReactedEvent !== '') ? palette.highlightedText : palette.text
60 font.family: Settings.emojiFont != "" ? Settings.emojiFont : undefined
61 textFormat: TextEdit.RichText
62 maximumLineCount: 1
63 text: {
64 // When an emoji font is selected that doesn't have …, it is dropped from elidedText. So we add it back.
65 if (textMetrics.elidedText !== modelData.displayKey) {
66 if (!textMetrics.elidedText.endsWith("…")) {
67 return textMetrics.elidedText + "…";
68 }
69 }
70 return textMetrics.elidedText;
71 }
72 visible: !modelData.key.startsWith("mxc://")
73 }
74 Image {
75 anchors.verticalCenter: divider.verticalCenter
76 fillMode: Image.PreserveAspectFit
77 height: textMetrics.height
78 mipmap: true
79 source: modelData.key.startsWith("mxc://") ? (modelData.key.replace("mxc://", "image://MxcImage/") + "?scale") : ""
80 visible: modelData.key.startsWith("mxc://")
81 width: textMetrics.height
82 }
83 Rectangle {
84 id: divider
85
86 color: reaction.hovered ? palette.text : gentleText
87 height: Math.floor(reactionCounter.implicitHeight * 1.4)
88 width: 1
89 }
90 Text {
91 id: reactionCounter
92
93 anchors.verticalCenter: divider.verticalCenter
94 color: (reaction.hovered || modelData.selfReactedEvent !== '') ? palette.highlightedText : palette.windowText
95 font: reaction.font
96 text: modelData.count
97 }
98 }
99
100 Component.onCompleted: {
101 ToolTip.text = Qt.binding(function () {
102 if (textMetrics.elidedText === textMetrics.text) {
103 return modelData.users;
104 }
105 return modelData.displayKey + "\n" + modelData.users;
106 });
107 }
108 onClicked: {
109 console.debug("Picked " + modelData.key + "in response to " + reactionFlow.eventId + ". selfReactedEvent: " + modelData.selfReactedEvent);
110 room.input.reaction(reactionFlow.eventId, modelData.key);
111 }
112 }
113 }
114 }