Skip to content

Commit

Permalink
Merge pull request #4769 from Textualize/hide-send-all
Browse files Browse the repository at this point in the history
Send Hide messages when toggling visibility
  • Loading branch information
willmcgugan authored Jul 18, 2024
2 parents 158e0b3 + 921702b commit fb6fc06
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/textual/_compositor.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,8 @@ def _arrange_root(
map: CompositorMap = {}
widgets: set[Widget] = set()
add_new_widget = widgets.add
invisible_widgets: set[Widget] = set()
add_new_invisible_widget = invisible_widgets.add
layer_order: int = 0

no_clip = size.region
Expand Down Expand Up @@ -595,6 +597,8 @@ def add_widget(

if visible:
add_new_widget(widget)
else:
add_new_invisible_widget(widget)
styles_offset = styles.offset
layout_offset = (
styles_offset.resolve(region.size, clip.size)
Expand Down Expand Up @@ -740,6 +744,7 @@ def add_widget(
True,
NULL_SPACING,
)
widgets -= invisible_widgets
return map, widgets

@property
Expand Down
34 changes: 34 additions & 0 deletions tests/test_widget_visibility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from textual.app import App, ComposeResult
from textual.widgets import Label


async def test_hide() -> None:
"""Check that setting visibility produces Hide messages."""
# https://github.com/Textualize/textual/issues/3460
visibility: list[bool] = []

class ShowHideLabel(Label):
def on_show(self) -> None:
visibility.append(True)

def on_hide(self) -> None:
visibility.append(False)

class ShowHideApp(App[None]):
BINDINGS = [("space", "toggle_label")]

def compose(self) -> ComposeResult:
yield ShowHideLabel("Here I am")

def action_toggle_label(self) -> None:
self.query_one(ShowHideLabel).visible = not self.query_one(
ShowHideLabel
).visible

app = ShowHideApp()
async with app.run_test() as pilot:
assert visibility == [True]
await pilot.press("space")
assert visibility == [True, False]
await pilot.press("space")
assert visibility == [True, False, True]

0 comments on commit fb6fc06

Please sign in to comment.