diff options
author | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2024-10-05 20:41:39 +0100 |
---|---|---|
committer | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2024-10-05 20:41:39 +0100 |
commit | 589ec018c7880b741777032a3c3da555114ef12b (patch) | |
tree | a84a09796d0ae5a699d5d23b05d2d37a4f37295e /src/chat.py | |
parent | be71bf80d290c98c0f6a66d3dcab28515b4ea371 (diff) |
chat: Get auto-scroll working, and fix ssh conn
Diffstat (limited to 'src/chat.py')
-rw-r--r-- | src/chat.py | 36 |
1 files changed, 31 insertions, 5 deletions
diff --git a/src/chat.py b/src/chat.py index f876854..bc42717 100644 --- a/src/chat.py +++ b/src/chat.py @@ -60,9 +60,36 @@ class WeegtkChat(Adw.Bin): self.color = Color(config.color_options(), False) + self.auto_scroll = True + self.sticky = True + adj = self.window.get_vadjustment() + adj.connect("value-changed", self.scroll_changes) + adj.connect("notify::upper", self.upper_notify) + + def is_at_bottom(self): + adj = self.window.get_vadjustment() + return (adj.get_value() + adj.get_page_size()) == adj.get_upper() + + def scroll_changes(self, *args): + is_at_bottom = self.is_at_bottom() + if self.auto_scroll: + if is_at_bottom: + self.auto_scroll = False + self.sticky = True + else: + self.scroll_bottom() + else: + self.sticky = is_at_bottom + + def upper_notify(self, *args): + if self.sticky: + self.scroll_bottom() + def scroll_bottom(self, *args): n_items = self.model.get_n_items() + self.auto_scroll = True self.messages.scroll_to(n_items - 1, Gtk.ListScrollFlags.FOCUS) + self.window.emit("scroll_child", Gtk.ScrollType.END, False) def setup_list_item(self, factory, list_item, *user_data): message = WeegtkMessage() @@ -73,8 +100,7 @@ class WeegtkChat(Adw.Bin): text = list_item.get_item().get_string() data = json.loads(text) message = list_item.get_child() - message.set_contents(data["username"], data["text"]) - pass + message.set_contents(data) def is_chat(self): buf_type = self.data['local_variables']['type'] if 'type' in self.data['local_variables'] else None @@ -99,17 +125,17 @@ class WeegtkChat(Adw.Bin): if count != 0: last = self.model.get_string(count - 1) last_data = json.loads(last) - if last_data["username"] == user: + if last_data["type"] == "message" and last_data["username"] == user: last_data["text"].append(msg) self.model.splice(count - 1, 1, [json.dumps(last_data)]) return data = { "username": user, - "text": [msg] + "text": [msg], + "type": "msg" } self.model.append(json.dumps(data)) - self.scroll_bottom() @Gtk.Template.Callback() def entry_activate(self, *args): |