From 589ec018c7880b741777032a3c3da555114ef12b Mon Sep 17 00:00:00 2001 From: Lucas Fryzek Date: Sat, 5 Oct 2024 20:41:39 +0100 Subject: chat: Get auto-scroll working, and fix ssh conn --- src/chat.py | 36 +++++++++++++++++++++++++++++++----- src/gtk/chat.ui | 13 ++++++++++--- src/main.py | 2 +- src/message.py | 35 +++++++++++++++++++++-------------- src/relay/network.py | 5 +++-- 5 files changed, 66 insertions(+), 25 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): diff --git a/src/gtk/chat.ui b/src/gtk/chat.ui index 23477ad..1b7c52a 100644 --- a/src/gtk/chat.ui +++ b/src/gtk/chat.ui @@ -19,21 +19,28 @@ True - 3 - + never + + 750 550 + True + True 1 + item + log - + diff --git a/src/main.py b/src/main.py index 8997ab5..1b45586 100644 --- a/src/main.py +++ b/src/main.py @@ -324,7 +324,7 @@ class WeegtkApplication(Adw.Application): buf_name = buf.data['short_name'] # Only make chats visible on main screen - if buf.is_chat(): + if True or buf.is_chat(): self.props.active_window.stack.add_titled(buf, name=buf_name, title=buf_name) def buffer_input(self, source_object, full_name, text): diff --git a/src/message.py b/src/message.py index a6e69a4..241ceab 100644 --- a/src/message.py +++ b/src/message.py @@ -30,19 +30,26 @@ class WeegtkMessage(Gtk.Box): def __init__(self, username="username", messages=[], **kwargs): super().__init__(**kwargs) - self.set_contents(username, messages) - - def set_contents(self, username, messages): - self.username.set_label(username) - self.avatar.set_text(username) - - first = True - for message in messages: - margin = 5 if not first else 0 - first = False - msg = Gtk.Label(label=message, selectable=True, - wrap=True, halign=0, hexpand=True, hexpand_set=True, - xalign=0, margin_top=margin) - self.message_list.append(msg) + data = { + "type": "empty" + } + self.set_contents(data) + + def set_contents(self, data): + if data["type"] == "empty": + self.set_visible(False) + else: + self.set_visible(True) + self.username.set_label(data["username"]) + self.avatar.set_text(data["username"]) + + first = True + for message in data["text"]: + margin = 5 if not first else 0 + first = False + msg = Gtk.Label(label=message, selectable=True, + wrap=True, halign=0, hexpand=True, hexpand_set=True, + xalign=0, margin_top=margin) + self.message_list.append(msg) diff --git a/src/relay/network.py b/src/relay/network.py index 1071f1a..4308ecf 100644 --- a/src/relay/network.py +++ b/src/relay/network.py @@ -256,7 +256,7 @@ class Network(GObject.GObject): def connect_weechat_ssh(self, ssh_host, ssh_port, ssh_username, ssh_key, relay_host, relay_port, relay_pw): self.ssh_tunnel = SSHTunnelForwarder( - ssh_host, + (ssh_host, int(ssh_port)), ssh_username=ssh_username, ssh_pkey=ssh_key, remote_bind_address=(relay_host, int(relay_port))) @@ -294,7 +294,7 @@ class Network(GObject.GObject): self._socket = self._socketclient.connect_finish(res) except GLib.Error as err: print("Connection failed:\n{}".format(err.message)) - self.set_status(STATUS_NOT_CONNECTED) + self.set_status(STATUS_DISCONNECTED) return else: print("Connected") @@ -401,3 +401,4 @@ class Network(GObject.GObject): self.debug_dialog.display_lines(self.debug_lines) self.debug_dialog.chat.scroll_bottom() + -- cgit