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

Updating styles on demand instead of on_idle #2304

Merged
merged 7 commits into from
Apr 18, 2023
Merged
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Changed signature of Driver. Technically a breaking change, but unlikely to affect anyone.
- Breaking change: Timer.start is now private, and returns No
- A clicked tab will now be scrolled to the center of its tab container https://github.com/Textualize/textual/pull/2276
- Style updates are now done immediately rather than on_idle https://github.com/Textualize/textual/pull/2304
- `ButtonVariant` is now exported from `textual.widgets.button` https://github.com/Textualize/textual/issues/2264

### Added
Expand All @@ -28,9 +29,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Fixed order styles are applied in DataTable - allows combining of renderable styles and component classes https://github.com/Textualize/textual/pull/2272
- Fix empty ListView preventing bindings from firing https://github.com/Textualize/textual/pull/2281
- Fix `get_component_styles` returning incorrect values on first call when combined with pseudoclasses https://github.com/Textualize/textual/pull/2304
- Fixed `active_message_pump.get` sometimes resulting in a `LookupError` https://github.com/Textualize/textual/issues/2301


## [0.19.1] - 2023-04-10

### Fixed
Expand Down
4 changes: 1 addition & 3 deletions src/textual/_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ def __len__(self) -> int:
return len(self._cache)

def __repr__(self) -> str:
return (
f"<LRUCache maxsize={self._maxsize} hits={self.hits} misses={self.misses}>"
)
return f"<LRUCache size={len(self)} maxsize={self._maxsize} hits={self.hits} misses={self.misses}>"

def grow(self, maxsize: int) -> None:
"""Grow the maximum size to at least `maxsize` elements.
Expand Down
19 changes: 6 additions & 13 deletions src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ def __init__(
self.design = DEFAULT_COLORS

self.stylesheet = Stylesheet(variables=self.get_css_variables())
self._require_stylesheet_update: set[DOMNode] = set()

css_path = css_path or self.CSS_PATH
if css_path is not None:
Expand Down Expand Up @@ -1229,13 +1228,15 @@ def get_child_by_type(self, expect_type: type[ExpectType]) -> ExpectType:
return self.screen.get_child_by_type(expect_type)

def update_styles(self, node: DOMNode | None = None) -> None:
"""Request update of styles.
"""Immediately update the styles of this node and all descendant nodes.

Should be called whenever CSS classes / pseudo classes change.

For example, when you hover over a button, the :hover pseudo class
will be added, and this method is called to apply the corresponding
:hover styles.
"""
self._require_stylesheet_update.add(self.screen if node is None else node)
self.check_idle()
descendants = node.walk_children(with_self=True)
self.stylesheet.update_nodes(descendants, animate=True)

def mount(
self,
Expand Down Expand Up @@ -1773,14 +1774,6 @@ async def _on_compose(self) -> None:

def _on_idle(self) -> None:
"""Perform actions when there are no messages in the queue."""
if self._require_stylesheet_update and not self._batch_count:
nodes: set[DOMNode] = {
child
for node in self._require_stylesheet_update
for child in node.walk_children(with_self=True)
}
self._require_stylesheet_update.clear()
self.stylesheet.update_nodes(nodes, animate=True)

def _register_child(
self, parent: DOMNode, child: Widget, before: int | None, after: int | None
Expand Down
1 change: 0 additions & 1 deletion src/textual/css/stylesheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,6 @@ def update_nodes(self, nodes: Iterable[DOMNode], animate: bool = False) -> None:
nodes: Nodes to update.
animate: Enable CSS animation.
"""

rules_map = self.rules_map
apply = self.apply

Expand Down
1 change: 1 addition & 0 deletions src/textual/devtools/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ async def run(self) -> WebSocketResponse:
):
await self.incoming_queue.put(message)
elif websocket_message.type == WSMsgType.ERROR:
self.service.console.print(websocket_message.data)
self.service.console.print(
DevConsoleNotice("Websocket error occurred", level="error")
)
Expand Down
2 changes: 1 addition & 1 deletion src/textual/widgets/_data_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
background: $primary 10%;
}

DataTable > .datatable--cursor {
DataTable > .datatable--cursor {
background: $secondary;
color: $text;
}
Expand Down
23 changes: 22 additions & 1 deletion tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from textual.app import App
from textual.app import App, ComposeResult
from textual.widgets import Button


def test_batch_update():
Expand All @@ -15,3 +16,23 @@ def test_batch_update():
assert app._batch_count == 1 # Exiting decrements

assert app._batch_count == 0 # Back to zero


class MyApp(App):
def compose(self) -> ComposeResult:
yield Button("Click me!")


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"}

# 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"}
assert button.styles.background != initial_background
4 changes: 2 additions & 2 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
def test_lru_cache():
cache = LRUCache(3)

assert str(cache) == "<LRUCache maxsize=3 hits=0 misses=0>"
assert str(cache) == "<LRUCache size=0 maxsize=3 hits=0 misses=0>"

# insert some values
cache["foo"] = 1
Expand Down Expand Up @@ -65,7 +65,7 @@ def test_lru_cache_hits():
assert cache.hits == 3
assert cache.misses == 2

assert str(cache) == "<LRUCache maxsize=4 hits=3 misses=2>"
assert str(cache) == "<LRUCache size=1 maxsize=4 hits=3 misses=2>"


def test_lru_cache_get():
Expand Down