Skip to content

Commit

Permalink
Add early return in view_position calculation.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rodrigogiraoserrao committed Jan 11, 2023
1 parent 328733e commit 5035c29
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/textual/widgets/_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5035c29

Please sign in to comment.