status-desktop/ui/app/AppLayouts/Chat/ChatColumn/ChatMessages.qml

137 lines
3.8 KiB
QML
Raw Normal View History

2020-05-28 22:22:51 +00:00
import QtQuick 2.14
2020-06-04 23:42:11 +00:00
import QtQuick.Controls 2.3
2020-05-28 22:22:51 +00:00
import QtQuick.Controls 2.14 as QQC2
import QtQuick.Layouts 1.3
import Qt.labs.platform 1.1
2020-06-04 23:42:11 +00:00
import QtQml.Models 2.3
import "../../../../shared"
import "../../../../imports"
2020-05-28 17:34:54 +00:00
import "./samples/"
2020-05-28 22:22:51 +00:00
ScrollView {
2020-06-04 23:42:11 +00:00
id: scrollView
2020-05-28 17:34:54 +00:00
property var messageList: MessagesData {}
property bool loadingMessages: false
2020-05-28 22:22:51 +00:00
2020-06-04 23:42:11 +00:00
contentItem: chatLogView
2020-05-28 22:22:51 +00:00
anchors.fill: parent
Layout.fillWidth: true
Layout.fillHeight: true
2020-06-08 22:34:41 +00:00
ScrollBar.vertical.policy: chatLogView.contentHeight > chatLogView.height ? ScrollBar.AlwaysOn : ScrollBar.AlwaysOff
2020-05-28 22:22:51 +00:00
ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
2020-06-04 23:42:11 +00:00
ListView {
anchors.fill: parent
spacing: 4
id: chatLogView
Layout.fillWidth: true
Layout.fillHeight: true
Connections {
target: chatsModel
onMessagesLoaded: {
loadingMessages = false;
}
onActiveChannelChanged: {
Qt.callLater( chatLogView.positionViewAtEnd )
}
onMessagePushed: {
if (!chatLogView.atYEnd) {
// User has scrolled up, we don't want to scroll back
return
}
if(chatLogView.atYEnd)
Qt.callLater( chatLogView.positionViewAtEnd )
}
2020-06-04 19:07:07 +00:00
}
onContentYChanged: {
if(atYBeginning && !loadingMessages){
loadingMessages = true;
chatsModel.loadMoreMessages();
}
}
2020-06-04 23:42:11 +00:00
model: messageListDelegate
section.property: "fromAuthor"
section.criteria: ViewSection.FullString
2020-06-04 23:42:11 +00:00
}
DelegateModel {
id: messageListDelegate
property var lessThan: [
function(left, right) { return left.clock < right.clock }
]
2020-05-28 22:22:51 +00:00
property int sortOrder: 0
onSortOrderChanged: items.setGroups(0, items.count, "unsorted")
2020-06-04 23:42:11 +00:00
function insertPosition(lessThan, item) {
var lower = 0
var upper = items.count
while (lower < upper) {
var middle = Math.floor(lower + (upper - lower) / 2)
var result = lessThan(item.model, items.get(middle).model);
if (result) {
upper = middle
} else {
lower = middle + 1
}
}
return lower
}
function sort(lessThan) {
while (unsortedItems.count > 0) {
var item = unsortedItems.get(0)
var index = insertPosition(lessThan, item)
item.groups = "items"
items.move(item.itemsIndex, index)
2020-05-28 22:22:51 +00:00
}
2020-06-04 23:42:11 +00:00
}
2020-06-04 23:42:11 +00:00
items.includeByDefault: false
groups: DelegateModelGroup {
id: unsortedItems
name: "unsorted"
includeByDefault: true
onChanged: {
if (messageListDelegate.sortOrder == messageListDelegate.lessThan.length)
setGroups(0, count, "items")
else {
messageListDelegate.sort(messageListDelegate.lessThan[messageListDelegate.sortOrder])
}
2020-05-28 22:22:51 +00:00
}
}
model: messageList
Message {
id: msgDelegate
chatId: model.chatId
userName: model.userName
message: model.message
identicon: model.identicon
isCurrentUser: model.isCurrentUser
timestamp: model.timestamp
sticker: model.sticker
contentType: model.contentType
authorCurrentMsg: msgDelegate.ListView.section
authorPrevMsg: msgDelegate.ListView.previousSection
}
}
2020-06-04 23:42:11 +00:00
}
/*##^##
Designer {
D{i:0;autoSize:true;height:480;width:640}
}
##^##*/