From e80b61cf9df54ae80666cc35d7db06d386fd5910 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Silva?= Date: Sat, 27 Apr 2019 04:03:57 +0100 Subject: [PATCH] Work-around for unwanted scroll while resizing the editor The problem: When a TextView *with vertical margins set* is resized, it scrolls upwards automatically. It's not entirely clear why this happens, but removing the top/bottom margins fixes the issue entirely. The work-around: enforcing the scroll scale between a resize starting and the UI becoming idle again. This is a hack, and the experience is not great (the scroll is visibly unstable for a few ms), but it patches and old bug in UberWriter. The better solution: Figuring out how to prevent it from happening, either by somehow ensuring the TextView does not do this, or by approaching the layout differently where the margin is not set on the TextView itself. --- uberwriter/text_view.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/uberwriter/text_view.py b/uberwriter/text_view.py index abbd6ec..51ad6af 100644 --- a/uberwriter/text_view.py +++ b/uberwriter/text_view.py @@ -1,4 +1,5 @@ import gi +from gi.repository.GObject import SignalMatchType from uberwriter.inline_preview import InlinePreview from uberwriter.text_view_format_inserter import FormatInserter @@ -106,6 +107,12 @@ class TextView(Gtk.TextView): self.hemingway_mode = False self.connect('key-press-event', self.on_key_press_event) + # While resizing the TextView, there is unwanted scroll upwards if a top margin is present. + # When a size allocation is detected, this variable will hold the scroll to re-set until the + # UI is idle again. + # TODO: Find a better way to handle unwanted scroll. + self.frozen_scroll_scale = None + def get_text(self): text_buffer = self.get_buffer() start_iter = text_buffer.get_start_iter() @@ -127,6 +134,11 @@ class TextView(Gtk.TextView): self.update_horizontal_margin() self.update_vertical_margin() self.markup.update_margins_indents() + self.queue_draw() + + # TODO: Find a better way to handle unwanted scroll on resize. + self.frozen_scroll_scale = self.get_scroll_scale() + GLib.idle_add(self.unfreeze_scroll_scale) def on_text_changed(self, *_): self.markup.apply() @@ -156,7 +168,14 @@ class TextView(Gtk.TextView): return False def on_scroll_scale_changed(self, *_): - self.emit("scroll-scale-changed", self.get_scroll_scale()) + if self.frozen_scroll_scale is not None: + self.set_scroll_scale(self.frozen_scroll_scale) + else: + self.emit("scroll-scale-changed", self.get_scroll_scale()) + + def unfreeze_scroll_scale(self): + self.frozen_scroll_scale = None + self.queue_draw() def set_focus_mode(self, focus_mode): """Toggle focus mode.