Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | SF123 |
I’m working on a chat app, but it is tough to figure out how to have it scroll down when there is a new message, but only when it is already scrolled down to the bottom of the chat.
Along with this, I would like to have it display a scroll down button if the user is scrolled up.
My code is as follows:
func load_messages():
if len(Messenger.messages[str(conversation_id)]["messages"]) != len(messages_container.get_children()):
clear_messages()
for message in Messenger.messages[conversation_id]["messages"]:
var msg = Messenger.messages[conversation_id]["messages"][message]
var hb = HBoxContainer.new()
var msg_label = Label.new()
messages_container.add_child(hb)
hb.add_child(msg_label)
if msg["sender_id"] == Account.account["id"]:
msg_label.theme = load("res://resources/themes/message/message_you.tres")
hb.alignment = BoxContainer.ALIGN_END
else:
msg_label.theme = load("res://resources/themes/message/message_them.tres")
hb.alignment = BoxContainer.ALIGN_BEGIN
msg_label.text = msg["content"]
if scrolling:
var sb = scroll_msg_container.get_v_scrollbar()
scroll_msg_container.scroll_vertical = sb.max_value
The load_messages() function gets called every time there is a new message, and I’m thinking before I call this that it should check if the user is scrolled all the way down, and if they are, scroll them down again once it adds the messages, and if it is scrolled up at all, it won’t and it will display a ⌄ (scroll down) button that when clicked scrolls down to the bottom.
Thanks in advance!