Skip to content

Commit

Permalink
Merge pull request #1210 from Armored-Dragon/fix/ac_scrolling
Browse files Browse the repository at this point in the history
Fix ArmoredChat scrolling
  • Loading branch information
daleglass authored Nov 2, 2024
2 parents 9d15fc4 + 7c8ca85 commit 620be77
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 49 deletions.
3 changes: 2 additions & 1 deletion scripts/communityScripts/armored-chat/armored_chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@
case "action":
switch (event.action) {
case "erase_history":
Settings.setValue("ArmoredChat-Messages", []);
Settings.setValue("ArmoredChat-Messages", null);
messageHistory = [];
_emitEvent({
type: "clear_messages",
});
Expand Down
114 changes: 66 additions & 48 deletions scripts/communityScripts/armored-chat/armored_chat.qml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.3
import controlsUit 1.0 as HifiControlsUit

Expand Down Expand Up @@ -30,7 +30,7 @@ Rectangle {
running: false
repeat: false
onTriggered: {
scrollToBottom();
scrollToBottom(true);
}
}

Expand Down Expand Up @@ -145,49 +145,60 @@ Rectangle {
anchors.top: navigation_bar.bottom
visible: ["local", "domain"].includes(pageVal) ? true : false


// Chat Message History
ListView {
Flickable {
width: parent.width
height: parent.height - 40
contentWidth: parent.width
contentHeight: listview.height
clip: true
interactive: true
spacing: 5
id: listview

delegate: Loader {
property int delegateIndex: index
property string delegateText: model.text
property string delegateUsername: model.username
property string delegateDate: model.date
width: listview.width

sourceComponent: {
if (model.type === "chat") {
return template_chat_message;
} else if (model.type === "notification") {
return template_notification;
id: messageViewFlickable

ColumnLayout {
id: listview
Layout.fillWidth: true

Repeater {
model: getChannel(pageVal)
delegate: Loader {
property int delegateIndex: model.index
property string delegateText: model.text
property string delegateUsername: model.username
property string delegateDate: model.date

sourceComponent: {
if (model.type === "chat") {
return template_chat_message;
} else if (model.type === "notification") {
return template_notification;
}
}

}
}
}

ScrollBar.vertical: ScrollBar {
id: chat_scrollbar
height: 100
size: 0.05
ScrollBar.vertical: ScrollBar {
size: 100
minimumSize: 0.1
}

model: getChannel(pageVal)

rebound: Transition {
NumberAnimation {
properties: "x,y"
duration: 1
}
}
}


ListModel {
id: local
}

ListModel {
id: domain
}
}

// Chat Entry
Rectangle {
Expand Down Expand Up @@ -346,14 +357,16 @@ Rectangle {
Component {
id: template_chat_message

Rectangle{
Rectangle {
property int index: delegateIndex
property string texttest: delegateText
property string username: delegateUsername
property string date: delegateDate

height: Math.max(65, children[1].height + 30)
color: index % 2 === 0 ? "transparent" : Qt.rgba(0.15,0.15,0.15,1)
width: listview.parent.parent.width
Layout.fillWidth: true

Item {
width: parent.width - 10
Expand All @@ -372,7 +385,7 @@ Rectangle {
}
}

TextEdit{
TextEdit {
anchors.top: parent.children[0].bottom
x: 5
text: texttest
Expand Down Expand Up @@ -453,16 +466,25 @@ Rectangle {

}



property var channels: {
"local": local,
"domain": domain,
}

function scrollToBottom() {
if (listview.count == 0) return;
listview.positionViewAtEnd();
function scrollToBottom(bypassDistanceCheck = false, extraMoveDistance = 0) {
const totalHeight = listview.height; // Total height of the content
const currentPosition = messageViewFlickable.contentY; // Current position of the view
const windowHeight = listview.parent.parent.height; // Total height of the window
const bottomPosition = currentPosition + windowHeight;

// Check if the view is within 300 units from the bottom
const closeEnoughToBottom = totalHeight - bottomPosition <= 300;
if (!bypassDistanceCheck && !closeEnoughToBottom) return;
if (totalHeight < windowHeight) return; // No reason to scroll, we don't have an overflow.
if (bottomPosition == totalHeight) return; // At the bottom, do nothing.

messageViewFlickable.contentY = listview.height - listview.parent.parent.height;
messageViewFlickable.returnToBounds();
}


Expand All @@ -471,13 +493,13 @@ Rectangle {

// Format content
message = formatContent(message);

message = embedImages(message);

if (type === "notification"){
channel.append({ text: message, date: date, type: "notification" });
last_message_user = "";
scrollToBottom();
scrollToBottom(null, 30);

last_message_time = new Date();
return;
}
Expand All @@ -489,22 +511,18 @@ Rectangle {
var last_item_index = channel.count - 1;
var last_item = channel.get(last_item_index);

// FIXME: When adding a new message this would check to see if we could append the incoming message
// to the bottom of the last message. This current implimentation causes issues with scrollToBottom()
// Specifically, scrolling to the bottom does not like image embeds.
// This needs to be reworked entirely before it can be reimplimented
// if (last_message_user === username && elapsed_minutes < 1 && last_item){
// message = "<br>" + message
// last_item.text = last_item.text += "\n" + message;
// scrollToBottom()
// last_message_time = new Date();
// return;
// }
if (last_message_user === username && elapsed_minutes < 1 && last_item){
message = "<br>" + message
last_item.text = last_item.text += "\n" + message;
load_scroll_timer.running = true;
last_message_time = new Date();
return;
}

last_message_user = username;
last_message_time = new Date();
channel.append({ text: message, username: username, date: date, type: type });
scrollToBottom();
load_scroll_timer.running = true;
}

function getChannel(id) {
Expand Down

0 comments on commit 620be77

Please sign in to comment.