-
Notifications
You must be signed in to change notification settings - Fork 8
/
ChatView.qml
192 lines (164 loc) · 5.33 KB
/
ChatView.qml
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import Intellicute
import core
ListView {
id: root
signal speak(message: string)
signal removeMessage(message: OpenAIMessage)
property bool ttsEnabled: true
property bool ttsActive: false
component FontButton: RoundButton {
implicitWidth: 32
implicitHeight: implicitWidth
text: "\uf0c5"
font.pixelSize: 16
font.family: Style.iconFont
}
spacing: Style.spacing
delegate: Rectangle {
id: item
required property OpenAIMessage message
width: root.width
height: Math.max(messageText.implicitHeight, copyButton.implicitHeight) + 20
radius: 10
color: item?.message?.role === OpenAIMessage.User ? "lightblue" : (item?.message?.role === OpenAIMessage.System ? "orange" : "lightgreen")
border.color: color.darker(0.8)
border.width: 1
RowLayout {
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
anchors.margins: Style.doubleMargin
TextEdit {
id: messageText
Layout.fillWidth: true
Layout.fillHeight: true
text: item?.message?.content ?? ""
textFormat: markdownRadio.checked ? TextEdit.MarkdownText : (htmlRadio.checked ? TextEdit.RichText : TextEdit.PlainText)
wrapMode: TextEdit.WordWrap
verticalAlignment: TextEdit.AlignVCenter
onEditingFinished: {
item.message.content = messageText.text
}
}
RowLayout {
id: buttonLayout
Layout.alignment: Qt.AlignBottom
opacity: mouseArea.containsMouse ? 1.0 : 0.0
Behavior on opacity { NumberAnimation { duration: 250 } }
FontButton {
id: copyButton
text: "\uf0c5"
onClicked: {
messageText.selectAll()
messageText.copy()
messageText.deselect()
}
}
FontButton {
id: playButton
visible: (item?.message?.role === OpenAIMessage.Assistant) && root.ttsEnabled
enabled: !root.ttsActive
text: "\uf04b"
onClicked: root.speak(messageText.getText(0, messageText.length))
}
FontButton {
id: deleteButton
text: "\uf1f8"
onClicked: {
root.model.removeMessage(item.message)
}
}
}
}
MouseArea {
id: mouseArea
anchors.fill: parent
acceptedButtons: Qt.RightButton
hoverEnabled: true
onClicked: contextMenu.popup()
}
Menu {
id: contextMenu
MenuItem {
text: qsTr("Copy Selection")
onClicked: {
messageText.copy()
}
}
MenuItem {
text: qsTr("Copy All")
onClicked: {
messageText.selectAll()
messageText.copy()
messageText.deselect()
}
}
MenuSeparator {}
RadioButton {
id: markdownRadio
text: qsTr("Render Markdown")
checked: true
onClicked: contextMenu.close()
}
RadioButton {
id: htmlRadio
text: qsTr("Render HTML")
checked: false
onClicked: contextMenu.close()
}
RadioButton {
id: plainRadio
text: qsTr("Render Plain Text")
checked: false
onClicked: contextMenu.close()
}
}
}
ColumnLayout {
anchors.bottom: parent.bottom
anchors.right: parent.right
FontButton {
id: addButton
text: "\u002b"
onClicked: {
addMessageMenu.popup()
}
Menu {
id: addMessageMenu
MenuItem {
text: qsTr("User")
onClicked: {
root.model.addMessage(OpenAIMessage.User, "")
}
}
MenuItem {
text: qsTr("Assistant")
onClicked: {
root.model.addMessage(OpenAIMessage.Assistant, "")
}
}
MenuItem {
text: qsTr("System")
onClicked: {
root.model.addMessage(OpenAIMessage.System, "")
}
}
}
}
}
// move to bottom
onCountChanged: {
if (count > 0) {
root.positionViewAtEnd()
}
}
add: Transition {
NumberAnimation { properties: "opacity"; from: 0; to: 1.0; duration: 500 }
}
remove: Transition {
NumberAnimation { properties: "opacity"; to: 0; duration: 500 }
}
}