From 8151946f380acacb8daa39b9c5b705033f5263d6 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Wed, 24 May 2023 20:16:11 +0100 Subject: [PATCH] add blur (#2645) * add blur * docstring * blur on disabled * snapshot test * Add test --- CHANGELOG.md | 1 + src/textual/app.py | 2 +- src/textual/screen.py | 1 + src/textual/widget.py | 9 +- .../__snapshots__/test_snapshots.ambr | 274 ++++++++++++++---- .../snapshot_apps/blur_on_disabled.py | 20 ++ tests/snapshot_tests/test_snapshots.py | 8 + 7 files changed, 253 insertions(+), 62 deletions(-) create mode 100644 tests/snapshot_tests/snapshot_apps/blur_on_disabled.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 3085106ddf..f43453c25e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/textual/app.py b/src/textual/app.py index aca3be101c..fe30f2e1bc 100644 --- a/src/textual/app.py +++ b/src/textual/app.py @@ -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() diff --git a/src/textual/screen.py b/src/textual/screen.py index 796f9eb84a..29c6ab95b0 100644 --- a/src/textual/screen.py +++ b/src/textual/screen.py @@ -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: diff --git a/src/textual/widget.py b/src/textual/widget.py index d1e7bc64fc..a67c3fe274 100644 --- a/src/textual/widget.py +++ b/src/textual/widget.py @@ -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( @@ -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. @@ -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) diff --git a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr index e39be06d6b..7013a574ac 100644 --- a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr +++ b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr @@ -709,6 +709,164 @@ ''' # --- +# name: test_blur_on_disabled + ''' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + BlurApp + + + + + + + + + + ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ + foo  + ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ + + + + + + + + + + + + + + + + + + + + + + + + + + ''' +# --- # name: test_border_alpha ''' @@ -22270,134 +22428,134 @@ font-weight: 700; } - .terminal-2821039707-matrix { + .terminal-1349433035-matrix { font-family: Fira Code, monospace; font-size: 20px; line-height: 24.4px; font-variant-east-asian: full-width; } - .terminal-2821039707-title { + .terminal-1349433035-title { font-size: 18px; font-weight: bold; font-family: arial; } - .terminal-2821039707-r1 { fill: #004578 } - .terminal-2821039707-r2 { fill: #c5c8c6 } - .terminal-2821039707-r3 { fill: #e1e1e1 } - .terminal-2821039707-r4 { fill: #23568b } + .terminal-1349433035-r1 { fill: #004578 } + .terminal-1349433035-r2 { fill: #c5c8c6 } + .terminal-1349433035-r3 { fill: #e1e1e1 } + .terminal-1349433035-r4 { fill: #14191f } - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - MyApp + MyApp - - - - ────────────────────────────────────────────────────────────────────────────── - SPAM - SPAM - SPAM - SPAM - SPAM - SPAM - SPAM - SPAM - SPAM - SPAM - SPAM - SPAM - SPAM - SPAM - SPAM - SPAM - SPAM - SPAM - SPAM - SPAM - SPAM▇▇ - SPAM - ────────────────────────────────────────────────────────────────────────────── + + + + ────────────────────────────────────────────────────────────────────────────── + SPAM + SPAM▁▁ + SPAM + SPAM + SPAM + SPAM + SPAM + SPAM + SPAM + SPAM + SPAM + SPAM + SPAM + SPAM + SPAM + SPAM + SPAM + SPAM + SPAM + SPAM + SPAM + SPAM + ────────────────────────────────────────────────────────────────────────────── diff --git a/tests/snapshot_tests/snapshot_apps/blur_on_disabled.py b/tests/snapshot_tests/snapshot_apps/blur_on_disabled.py new file mode 100644 index 0000000000..3bfa0e1226 --- /dev/null +++ b/tests/snapshot_tests/snapshot_apps/blur_on_disabled.py @@ -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() diff --git a/tests/snapshot_tests/test_snapshots.py b/tests/snapshot_tests/test_snapshots.py index 874f096d99..fd22c0ca30 100644 --- a/tests/snapshot_tests/test_snapshots.py +++ b/tests/snapshot_tests/test_snapshots.py @@ -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"], + )