From 5035c29b87deda9cd7386df2b7da61cdce76e04f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Wed, 11 Jan 2023 18:52:00 +0000 Subject: [PATCH] Add early return in view_position calculation. When an input is instantiated without an argument for the parameter 'value', the first time the watchers associated with its reactive attributes run, the attribute self.view_position is set to 1, which makes no sense because there isn't even any value in the input. Thus, when the value is later modified programmatically, e.g., because it is set with 'some_input_widget.value = some_value', the attribute self.view_position will still be 1 and the text will be rendered starting from the first character. As two example situations of this, consider #1477 and #1443. --- src/textual/widgets/_input.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/textual/widgets/_input.py b/src/textual/widgets/_input.py index c2bfa638a6..ec68be8cd6 100644 --- a/src/textual/widgets/_input.py +++ b/src/textual/widgets/_input.py @@ -148,6 +148,11 @@ def validate_view_position(self, view_position: int) -> int: def watch_cursor_position(self, cursor_position: int) -> None: width = self.content_size.width + if width == 0: + # If the input has no width the view position can't be elsewhere. + self.view_position = 0 + return + view_start = self.view_position view_end = view_start + width cursor_offset = self._cursor_offset