Skip to content

Commit

Permalink
add blur (#2645)
Browse files Browse the repository at this point in the history
* add blur

* docstring

* blur on disabled

* snapshot test

* Add test
  • Loading branch information
willmcgugan authored May 24, 2023
1 parent a89c199 commit 8151946
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 62 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Removed

- `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
Expand Down
2 changes: 1 addition & 1 deletion src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2075,7 +2075,7 @@ def _unregister(self, widget: Widget) -> None:
Args:
widget: A Widget to unregister
"""
widget.reset_focus()
widget.blur()
if isinstance(widget._parent, Widget):
widget._parent._nodes._remove(widget)
widget._detach()
Expand Down
1 change: 1 addition & 0 deletions src/textual/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ def _reset_focus(
focusable_widgets = self.focus_chain
if not focusable_widgets:
# If there's nothing to focus... give up now.
self.set_focus(None)
return

try:
Expand Down
9 changes: 6 additions & 3 deletions src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -2712,6 +2712,7 @@ def watch_has_focus(self, value: bool) -> None:

def watch_disabled(self) -> None:
"""Update the styles of the widget and its children when disabled is toggled."""
self.blur()
self._update_styles()

def _size_updated(
Expand Down Expand Up @@ -3017,8 +3018,10 @@ def set_focus(widget: Widget):
self.app.call_later(set_focus, self)
return self

def reset_focus(self) -> Self:
"""Reset the focus (move it to the next available widget).
def blur(self) -> Self:
"""Blur (un-focus) the widget.
Focus will be moved to the next available widget in the focus chain..
Returns:
The `Widget` instance.
Expand Down Expand Up @@ -3172,7 +3175,7 @@ def _on_scroll_right(self, event: ScrollRight) -> None:

def _on_hide(self, event: events.Hide) -> None:
if self.has_focus:
self.reset_focus()
self.blur()

def _on_scroll_to_region(self, message: messages.ScrollToRegion) -> None:
self.scroll_to_region(message.region, animate=True)
Expand Down
274 changes: 216 additions & 58 deletions tests/snapshot_tests/__snapshots__/test_snapshots.ambr

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions tests/snapshot_tests/snapshot_apps/blur_on_disabled.py
Original file line number Diff line number Diff line change
@@ -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()
8 changes: 8 additions & 0 deletions tests/snapshot_tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,11 @@ def test_select_rebuild(snap_compare):
SNAPSHOT_APPS_DIR / "select_rebuild.py",
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"],
)

0 comments on commit 8151946

Please sign in to comment.