~nicohman/signal-rs

ref: 8d72ad30e0e3ebb149982a17f8b6f45fbbfd6325 signal-rs/qml/MessageUI.qml -rw-r--r-- 6.2 KiB
8d72ad30nicohman Add theme support and general improvements 9 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
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 {
    }
    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);
            });
        }
    }
    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.currentChat.tel, 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 {
                    	color: theme.highlightedText
                        font.pixelSize: Qt.application.font.pixelSize
                        font.bold: false
                        id: textLabel
                        width: message.oneLine ? textLabel.implicitWidth : message.maxDelegateWidth
                        text: message.message.message
                        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");
                                        //atRow.Layout.minimumHeight += obj.implicitHeight;
                                        //atRow.forceLayout();
                                        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: 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);
                            }
                        }
                    }
                }
            }
        }
    }
}