* add blur

* docstring

* blur on disabled

* snapshot test

* Add test
This commit is contained in:
Will McGugan
2023-05-24 20:16:11 +01:00
committed by GitHub
parent a89c199409
commit 8151946f38
7 changed files with 253 additions and 62 deletions

View File

@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Removed ### Removed
- `Placeholder.reset_color_cycle` - `Placeholder.reset_color_cycle`
- Removed `Widget.reset_focus` (now called `Widget.blur`) https://github.com/Textualize/textual/issues/2642
## [0.26.0] - 2023-05-20 ## [0.26.0] - 2023-05-20

View File

@@ -2075,7 +2075,7 @@ class App(Generic[ReturnType], DOMNode):
Args: Args:
widget: A Widget to unregister widget: A Widget to unregister
""" """
widget.reset_focus() widget.blur()
if isinstance(widget._parent, Widget): if isinstance(widget._parent, Widget):
widget._parent._nodes._remove(widget) widget._parent._nodes._remove(widget)
widget._detach() widget._detach()

View File

@@ -386,6 +386,7 @@ class Screen(Generic[ScreenResultType], Widget):
focusable_widgets = self.focus_chain focusable_widgets = self.focus_chain
if not focusable_widgets: if not focusable_widgets:
# If there's nothing to focus... give up now. # If there's nothing to focus... give up now.
self.set_focus(None)
return return
try: try:

View File

@@ -2712,6 +2712,7 @@ class Widget(DOMNode):
def watch_disabled(self) -> None: def watch_disabled(self) -> None:
"""Update the styles of the widget and its children when disabled is toggled.""" """Update the styles of the widget and its children when disabled is toggled."""
self.blur()
self._update_styles() self._update_styles()
def _size_updated( def _size_updated(
@@ -3017,8 +3018,10 @@ class Widget(DOMNode):
self.app.call_later(set_focus, self) self.app.call_later(set_focus, self)
return self return self
def reset_focus(self) -> Self: def blur(self) -> Self:
"""Reset the focus (move it to the next available widget). """Blur (un-focus) the widget.
Focus will be moved to the next available widget in the focus chain..
Returns: Returns:
The `Widget` instance. The `Widget` instance.
@@ -3172,7 +3175,7 @@ class Widget(DOMNode):
def _on_hide(self, event: events.Hide) -> None: def _on_hide(self, event: events.Hide) -> None:
if self.has_focus: if self.has_focus:
self.reset_focus() self.blur()
def _on_scroll_to_region(self, message: messages.ScrollToRegion) -> None: def _on_scroll_to_region(self, message: messages.ScrollToRegion) -> None:
self.scroll_to_region(message.region, animate=True) self.scroll_to_region(message.region, animate=True)

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,20 @@
from textual.app import App, ComposeResult
from textual.widgets import Input
class BlurApp(App):
BINDINGS = [("f3", "disable")]
def compose(self) -> ComposeResult:
yield Input()
def on_ready(self) -> None:
self.query_one(Input).focus()
def action_disable(self) -> None:
self.query_one(Input).disabled = True
if __name__ == "__main__":
app = BlurApp()
app.run()

View File

@@ -514,3 +514,11 @@ def test_select_rebuild(snap_compare):
SNAPSHOT_APPS_DIR / "select_rebuild.py", SNAPSHOT_APPS_DIR / "select_rebuild.py",
press=["space", "escape", "tab", "enter", "tab", "space"], press=["space", "escape", "tab", "enter", "tab", "space"],
) )
def test_blur_on_disabled(snap_compare):
# https://github.com/Textualize/textual/issues/2641
assert snap_compare(
SNAPSHOT_APPS_DIR / "blur_on_disabled.py",
press=[*"foo", "f3", *"this should not appear"],
)