Skip to content

Commit

Permalink
Blur (#3645)
Browse files Browse the repository at this point in the history
* added blur

* add blur pseudo class

* added blur psuedo class

* added test
  • Loading branch information
willmcgugan authored Nov 7, 2023
1 parent c7cf028 commit c667c86
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed

- Duplicate CSS errors when parsing CSS from a screen https://github.com/Textualize/textual/issues/3581
- Added missing `blur` pseudo class https://github.com/Textualize/textual/issues/3439
- Fixed visual glitched characters on Windows due to Python limitation https://github.com/Textualize/textual/issues/2548

### Changed
Expand Down
1 change: 1 addition & 0 deletions docs/guide/CSS.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ Here are some other pseudo classes:
- `:disabled` Matches widgets which are in a disabled state.
- `:enabled` Matches widgets which are in an enabled state.
- `:focus` Matches widgets which have input focus.
- `:blur` Matches widgets which *do not* have input focus.
- `:focus-within` Matches widgets with a focused child widget.
- `:dark` Matches widgets in dark mode (where `App.dark == True`).
- `:light` Matches widgets in dark mode (where `App.dark == False`).
Expand Down
2 changes: 2 additions & 0 deletions src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -2779,6 +2779,8 @@ def get_pseudo_classes(self) -> Iterable[str]:
yield "hover"
if self.has_focus:
yield "focus"
else:
yield "blur"
if self.can_focus:
yield "can-focus"
try:
Expand Down
15 changes: 13 additions & 2 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,25 @@ async def test_hover_update_styles():
app = MyApp()
async with app.run_test() as pilot:
button = app.query_one(Button)
assert button.pseudo_classes == {"enabled", "can-focus", "dark"}
assert button.pseudo_classes == {
"blur",
"can-focus",
"dark",
"enabled",
}

# Take note of the initial background colour
initial_background = button.styles.background
await pilot.hover(Button)

# We've hovered, so ensure the pseudoclass is present and background changed
assert button.pseudo_classes == {"enabled", "hover", "can-focus", "dark"}
assert button.pseudo_classes == {
"blur",
"can-focus",
"dark",
"enabled",
"hover",
}
assert button.styles.background != initial_background


Expand Down
30 changes: 29 additions & 1 deletion tests/test_focus.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from textual.app import App
from textual.app import App, ComposeResult
from textual.containers import Container
from textual.screen import Screen
from textual.widget import Widget
Expand Down Expand Up @@ -309,3 +309,31 @@ def compose(self):
w11,
w12,
]


async def test_focus_pseudo_class():
"""Test focus and blue pseudo classes"""

# https://github.com/Textualize/textual/pull/3645
class FocusApp(App):
AUTO_FOCUS = None

def compose(self) -> ComposeResult:
yield Button("Hello")

app = FocusApp()
async with app.run_test() as pilot:
button = app.query_one(Button)
classes = list(button.get_pseudo_classes())
# Blurred, not focused
assert "blur" in classes
assert "focus" not in classes

# Focus the button
button.focus()
await pilot.pause()

# Focused, not blurred
classes = list(button.get_pseudo_classes())
assert "blur" not in classes
assert "focus" in classes

0 comments on commit c667c86

Please sign in to comment.