~nicohman/signal-rs

ref: 21d519db83d3c672c89259ac11661434f0dbdc34 signal-rs/qml/MessageUI.qml -rw-r--r-- 7.3 KiB
21d519db — nicohman Save to try kirigami 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import QtQuick 2.15
import QtQuick.Controls 2.5
import QtQuick.Layouts 1.15

ItemDelegate {
    id: message
    x: 5
    height: rectangle.height + 10
    property string messagetext: "Lorem ipsum"
    property bool outgoing: true
    property var message: null
    property bool group: false
    property int oneLineWidth: textLabel.implicitWidth
    property bool oneLine: oneLineWidth <= maxDelegateWidth
    readonly property int maxDelegateWidth: 27 * 8
    width: parent ? parent.width : 0
    property ListModel attachmentModel: ListModel {
    }
    property ListModel reactionModel : ListModel {
    }
    Component.onCompleted : {
        if (message.message.attachment && message.message.attachment !== "[]") {
            var attachments  = JSON.parse(new String(message.message.attachment));
            attachments.forEach(function(at) {
                message.attachmentModel.append(at);
            });
        }
        if (message.message.reactions && message.message.reactions !== "[]") {
            var reactions = JSON.parse(new String(message.message.reactions));
            reactions.forEach(function(r) {
                message.reactionModel.append(r);
            });
        }
    }
    Row {
        id: row
        x: 0
        y: 0
        Layout.topMargin: 0
        anchors.fill: parent
        anchors.bottomMargin: 0
        layoutDirection: message.message.outgoing ? Qt.RightToLeft : Qt.LeftToRight
        Layout.rightMargin: 5
        Rectangle {
            MouseArea {
                anchors.fill: parent
                acceptedButtons: Qt.RightButton
                onClicked : {
                    if (mouse.button === Qt.RightButton) {
                        msgRightClick.popup();
                    }
                }
                Menu {
                    id: msgRightClick
                    MenuItem {
                        onClicked: {
                            signal.del_message(signalState.current, message.message.sent_at);
                        }
                        text: "Delete"
                    }
                }
            }
            id: rectangle
            width: Math.min(
                       message.maxDelegateWidth, Math.max(
                           message.oneLine ? message.oneLineWidth : textLabel.implicitWidth,
                           dateLabel.implicitWidth, attachmentList.implicitWidth, dynName.implicitWidth)) + 10
            height: textLabel.implicitHeight + textLabel.anchors.topMargin + 15
                    + dateLabel.implicitHeight + atRow.implicitHeight + dynName.implicitHeight + dynName.anchors.topMargin + attachmentList.anchors.topMargin + dateLabel.anchors.topMargin
            color: message.message.outgoing ? theme.outgoing : theme.message
            radius: 10
            anchors.top: parent.top
            anchors.topMargin: 10
            anchors.leftMargin: 5
            anchors.rightMargin: 5
            ColumnLayout {
                Row {
                    leftPadding: 5
                    Label {
                        MouseArea {
                            anchors.fill: parent
                            cursorShape: parent.hoveredLink ? Qt.PointingHandCursor : Qt.ArrowCursor
                            acceptedButtons: Qt.NoButton
                        }
                        onLinkActivated: Qt.openUrlExternally(link)
                    	color: theme.highlightedText
                        font.pixelSize: Qt.application.font.pixelSize
                        font.bold: false
                        id: textLabel
                        width: message.oneLine ? textLabel.implicitWidth : message.maxDelegateWidth
                        text: {
                            var regex = new RegExp("http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+");
                            return message.message.message.toString().replace(regex, "<a href=\"$&\">$&</a>");
                        }
                        wrapMode: Text.Wrap
                    }
                }
                Row {
                    topPadding: {
                       return message.attachmentModel.count === 0 ? 0 : 5;
                    }
                    Layout.leftMargin: {
                       return message.attachmentModel.count === 0 ? 0 : 5;
                    }
                    id: atRow
                    ListView {
                    	id:attachmentList
                        model: message.attachmentModel
                        delegate: ItemDelegate {
                            id: atDel
                            Component.onCompleted: {
                                switch (CType) {
                                    case 2:
                                        var obj = Qt.createQmlObject(`import QtQuick 2.15; Image { width: 350\nfillMode: Image.PreserveAspectFit\nsource: "file://${File}"}`,atRow,  "imageDel");
                                        if (obj.width > rectangle.width) {
                                            rectangle.width = obj.width + 10;
                                        }
                                        break;
                                    default:
                                        var obj = Qt.createQmlObject('import QtQuick 2.15; import QtQuick.Controls 2.5; Label { text: "Can\'t display attachment"}', atDel, "defaultDel");
                                        attachmentList.height += obj.implicitHeight;
                                        break;
                                }
                            }
                        }
                    }
                }
                Row {
                	id: dynName
                	leftPadding: 5
                	Label {
                		color: theme.highlightedText
                		text: signal.resolve_name(message.message.source)//signalState.contact(message.message.source).Name || message.message.source
                		visible: signalState.currentChat && signalState.currentChat.is_group
                	}
                }
                Row {
                    leftPadding: 5
                    Label {
                        color: theme.highlightedText
                        Layout.leftMargin: 5
                        id: dateLabel
                        text: {
                            var now = new Date();
                            var dateobj = new Date(message.message.sent_at);
                            if (dateobj.getDate() === now.getDate()
                                    && dateobj.getMonth() === now.getMonth()) {
                                return dateobj.toLocaleTimeString(
                                            Qt.locale("en_US"), Locale.ShortFormat);
                            } else {
                                return dateobj.toLocaleDateString(
                                            Qt.locale("en_US"),
                                            Locale.ShortFormat);
                            }
                        }
                    }
                }
                Row {
                    leftPadding: 5
                    ListView {
                        orientation: ListView.Horizontal
                        model: message.reactionModel
                        delegate: Label {
                            text: emoji
                        }
                    }
                }
            }
        }
    }
}