From cb2ff26daacde20e9d347bc0e720ed27ea2bea52 Mon Sep 17 00:00:00 2001 From: Lucas Fryzek Date: Sun, 6 Oct 2024 11:04:12 +0100 Subject: chat: Add autoscroll to bottom button --- src/chat.py | 17 ++++++++++++++--- src/gtk/chat.ui | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/chat.py b/src/chat.py index bb498c3..3c61b00 100644 --- a/src/chat.py +++ b/src/chat.py @@ -43,6 +43,7 @@ class WeegtkChat(Adw.Bin): window = Gtk.Template.Child() messages = Gtk.Template.Child() text_entry = Gtk.Template.Child() + scroll_button_revealer = Gtk.Template.Child() def __init__(self, data=None, **kwargs): super().__init__(**kwargs) @@ -62,30 +63,40 @@ class WeegtkChat(Adw.Bin): self.color = Color(config.color_options(), False) self.auto_scroll = True - self.sticky = True + self.set_sticky(True) adj = self.window.get_vadjustment() adj.connect("value-changed", self.scroll_changes) adj.connect("notify::upper", self.upper_notify) + # TODO figure out why style is not being taken from ui file + self.add_css_class("view") + def is_at_bottom(self): adj = self.window.get_vadjustment() return (adj.get_value() + adj.get_page_size()) == adj.get_upper() + def set_sticky(self, is_sticky): + if not is_sticky: + self.scroll_button_revealer.set_visible(True) + self.scroll_button_revealer.set_reveal_child(not is_sticky) + self.sticky = is_sticky + 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 + self.set_sticky(True) else: self.scroll_bottom() else: - self.sticky = is_at_bottom + self.set_sticky(is_at_bottom) def upper_notify(self, *args): if self.sticky: self.scroll_bottom() + @Gtk.Template.Callback() def scroll_bottom(self, *args): n_items = self.model.get_n_items() self.auto_scroll = True diff --git a/src/gtk/chat.ui b/src/gtk/chat.ui index a768f5c..65765e5 100644 --- a/src/gtk/chat.ui +++ b/src/gtk/chat.ui @@ -43,6 +43,38 @@ + + + 750 + 500 + + + + + False + crossfade + end + end + 5 + 5 + + + go-bottom-symbolic + Scroll to Bottom + + + + + + + + + + 750 -- cgit