Merge pull request #1548 from Textualize/fix-1477

Improve `Input` view position calculation
This commit is contained in:
Rodrigo Girão Serrão
2023-01-12 15:13:03 +00:00
committed by GitHub
3 changed files with 105 additions and 0 deletions

View File

@@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- The styles `scrollbar-background-active` and `scrollbar-color-hover` are no longer ignored https://github.com/Textualize/textual/pull/1480
- The widget `Placeholder` can now have its width set to `auto` https://github.com/Textualize/textual/pull/1508
- Behavior of widget `Input` when rendering after programmatic value change and related scenarios https://github.com/Textualize/textual/issues/1477 https://github.com/Textualize/textual/issues/1443
## [0.9.1] - 2022-12-30

View File

@@ -148,6 +148,11 @@ class Input(Widget, can_focus=True):
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

99
tests/test_input.py Normal file
View File

@@ -0,0 +1,99 @@
from rich.console import Console
from textual.app import App
from textual.widgets import Input
async def test_input_value_visible_on_instantiation():
"""Check if the full input value is rendered if the input is instantiated with it."""
class MyApp(App):
def compose(self):
yield Input(value="value")
app = MyApp()
async with app.run_test():
console = Console(width=5)
with console.capture() as capture:
console.print(app.query_one(Input).render())
assert capture.get() == "value"
async def test_input_value_visible_after_value_assignment():
"""Check if the full input value is rendered if the value is assigned to programmatically."""
class MyApp(App):
def compose(self):
yield Input()
def on_mount(self):
self.query_one(Input).value = "value"
app = MyApp()
async with app.run_test():
console = Console(width=5)
with console.capture() as capture:
console.print(app.query_one(Input).render())
assert capture.get() == "value"
async def test_input_value_visible_if_mounted_later():
"""Check if full input value is rendered if the widget is mounted later."""
class MyApp(App):
BINDINGS = [("a", "add_input", "add_input")]
def action_add_input(self):
self.mount(Input(value="value"))
app = MyApp()
async with app.run_test() as pilot:
await pilot.press("a")
console = Console(width=5)
with console.capture() as capture:
console.print(app.query_one(Input).render())
assert capture.get() == "value"
async def test_input_value_visible_if_mounted_later_and_focused():
"""Check if full input value is rendered if the widget is mounted later and immediately focused."""
class MyApp(App):
BINDINGS = [("a", "add_input", "add_input")]
def action_add_input(self):
inp = Input(value="value")
self.mount(inp)
inp.focus()
app = MyApp()
async with app.run_test() as pilot:
await pilot.press("a")
console = Console(width=5)
with console.capture() as capture:
console.print(app.query_one(Input).render())
assert capture.get() == "value"
async def test_input_value_visible_if_mounted_later_and_assigned_after():
"""Check if full value rendered if the widget is mounted later and the value is then assigned to."""
class MyApp(App):
BINDINGS = [
("a", "add_input", "add_input"),
("v", "set_value", "set_value"),
]
def action_add_input(self):
self.mount(Input())
def action_set_value(self):
self.query_one(Input).value = "value"
app = MyApp()
async with app.run_test() as pilot:
await pilot.press("a")
await pilot.press("v")
console = Console(width=5)
with console.capture() as capture:
console.print(app.query_one(Input).render())
assert capture.get() == "value"