From a34196f2fa184755e0dc336341b8ed92e5d0a398 Mon Sep 17 00:00:00 2001 From: Lucas Fryzek Date: Wed, 15 Jan 2025 21:14:35 +0000 Subject: chat: Improve rendering of messages Reduces the slowdown from receiving lots of messages at once. TODO look to see if adding messages can be done without freezing the UI. --- src/chat.py | 8 +++++--- src/gtk/message.ui | 1 + src/main.py | 2 +- src/message.py | 10 +++++++++- 4 files changed, 16 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/chat.py b/src/chat.py index b659395..3ee0438 100644 --- a/src/chat.py +++ b/src/chat.py @@ -166,9 +166,7 @@ class WeegtkChat(Adw.Bin): last = self.model.get_string(count - 1) last_data = json.loads(last) if last_data["username"] == user: - last_data["text"].append(msg) - self.model.splice(count - 1, 1, [json.dumps(last_data)]) - return + msg_type = "message_append" data = { "username": user, @@ -177,6 +175,10 @@ class WeegtkChat(Adw.Bin): } self.model.append(json.dumps(data)) + def clear(self): + # TODO clear chat buffer + pass + @Gtk.Template.Callback() def entry_activate(self, *args): entry_buffer = self.text_entry.get_buffer() diff --git a/src/gtk/message.ui b/src/gtk/message.ui index 9619342..66d1769 100644 --- a/src/gtk/message.ui +++ b/src/gtk/message.ui @@ -21,6 +21,7 @@ vertical start + center Username diff --git a/src/main.py b/src/main.py index a120031..ea387eb 100644 --- a/src/main.py +++ b/src/main.py @@ -320,7 +320,7 @@ class WeegtkApplication(Adw.Application): self.buffers[index].data['title'] = item['title'] ##self.pages[index].set_title(item['title']) elif message.msgid == '_buffer_cleared': - self.buffers[index].widget.chat.clear() + self.buffers[index].clear() elif message.msgid.startswith('_buffer_localvar_'): self.buffers[index].data['local_variables'] = \ item['local_variables'] diff --git a/src/message.py b/src/message.py index 7a14483..db50829 100644 --- a/src/message.py +++ b/src/message.py @@ -65,14 +65,22 @@ class WeegtkMessage(Gtk.Box): else: self.avatar.set_text(data["username"]) + if data["type"] == "message_append": + self.avatar.set_visible(False) + self.username.set_visible(False) + first = True + padding_size = self.avatar.get_size() + self.avatar.get_margin_start() + self.avatar.get_margin_end() + # TODO messages no longer should be a list, and there should only be one message per entry + # Remove this for loop and make sure chat only sets message contents as a single string for message in data["text"]: margin = 5 if not first else 0 first = False markuped = self.parse_message(message) msg = Gtk.Label(label=markuped, selectable=True, wrap=True, wrap_mode=Pango.WrapMode.WORD_CHAR, xalign=0, - margin_top=margin, use_markup=True) + margin_top=margin, use_markup=True, + margin_start= padding_size if data["type"] == "message_append" else 0) self.message_list.append(msg) -- cgit