Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blur #3645

Merged
merged 5 commits into from
Nov 7, 2023
Merged

Blur #3645

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading