From 69971dae3cd9dd0054aa92fae59ffc4f82718ab6 Mon Sep 17 00:00:00 2001
From: Nico Alt <nicoalt@posteo.org>
Date: Wed, 30 Dec 2020 12:00:00 +0000
Subject: [PATCH] Let chat input adapt to small heights

Using a hack from Dino:
https://github.com/dino/dino/blob/v0.2.0/main/src/ui/chat_input/chat_text_view.vala#L51
---
 .../briar_gtk/presenters/private_chat.py      | 24 +++++++++++++++++--
 briar-gtk/data/ui/main_window.ui              |  2 +-
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/briar-gtk/briar_gtk/presenters/private_chat.py b/briar-gtk/briar_gtk/presenters/private_chat.py
index b3258b1..4235fe6 100644
--- a/briar-gtk/briar_gtk/presenters/private_chat.py
+++ b/briar-gtk/briar_gtk/presenters/private_chat.py
@@ -193,10 +193,30 @@ class PrivateChatPresenter:
         self._chat_input_signal_id = chat_input.connect(
             "key-press-event", self._on_chat_input_activate
         )
-        # TODO: Activate vscrollbar only if needed (to save space)
-        # https://github.com/dino/dino/blob/231df1/main/src/ui/chat_input/chat_text_view.vala#L51
+        self._setup_vscrollbar_listener()
         chat_input.grab_focus()
 
+    def _setup_vscrollbar_listener(self):
+        chat_input_scroll = self._view.builder.get_object("chat_input_scroll")
+        vscollbar = chat_input_scroll.get_vscrollbar()
+        self._vscrollbar_min_height = vscollbar.get_preferred_height()[0]
+
+        vadjustment = chat_input_scroll.get_vadjustment()
+        vadjustment.connect_after("notify", self._on_upper_notify)
+
+    # pylint: disable=unused-argument, line-too-long
+    def _on_upper_notify(self, g_object, param_spec):  # noqa
+        """
+        https://github.com/dino/dino/blob/v0.2.0/main/src/ui/chat_input/chat_text_view.vala#L51
+        """
+        chat_input_scroll = self._view.builder.get_object("chat_input_scroll")
+        vadjustment = chat_input_scroll.get_vadjustment()
+        vadjustment.value = vadjustment.get_upper() - vadjustment.get_page_size()  # noqa
+
+        # Hack for vscrollbar not requiring space and making TextView higher
+        vscollbar = chat_input_scroll.get_vscrollbar()
+        vscollbar.set_visible(vadjustment.get_upper() > chat_input_scroll.get_max_content_height() - 2 * self._vscrollbar_min_height)  # noqa
+
     @staticmethod
     def _hide_chat_view(main_content_stack):
         chat_view = main_content_stack.get_child_by_name("chat_view")
diff --git a/briar-gtk/data/ui/main_window.ui b/briar-gtk/data/ui/main_window.ui
index c19d374..0e061ab 100644
--- a/briar-gtk/data/ui/main_window.ui
+++ b/briar-gtk/data/ui/main_window.ui
@@ -126,7 +126,7 @@
                                   <object class="GtkBox">
                                     <property name="visible">True</property>
                                     <child>
-                                      <object class="GtkScrolledWindow">
+                                      <object class="GtkScrolledWindow" id="chat_input_scroll">
                                         <property name="visible">True</property>
                                         <property name="margin">6</property>
                                         <property name="propagate_natural_height">True</property>
-- 
GitLab