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

fix for tabs #3059

Merged
merged 12 commits into from
Aug 7, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Fixed unintuitive sizing behaviour of TabbedContent https://github.com/Textualize/textual/issues/2411
- Fixed relative units not always expanding auto containers https://github.com/Textualize/textual/pull/3059
- Fixed background refresh https://github.com/Textualize/textual/issues/3055


Expand Down
1 change: 1 addition & 0 deletions src/textual/css/scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ def from_number(cls, value: float) -> Scalar:
return cls(float(value), Unit.CELLS, Unit.WIDTH)

@classmethod
@lru_cache(maxsize=1024)
def parse(cls, token: str, percent_unit: Unit = Unit.WIDTH) -> Scalar:
"""Parse a string in to a Scalar

Expand Down
5 changes: 1 addition & 4 deletions src/textual/css/styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,7 @@ def auto_dimensions(self) -> bool:
def is_relative_width(self) -> bool:
"""Does the node have a relative width?"""
width = self.width
return width is not None and width.unit in (
Unit.FRACTION,
Unit.PERCENT,
)
return width is not None and width.unit in (Unit.FRACTION, Unit.PERCENT)

@property
def is_relative_height(self) -> bool:
Expand Down
2 changes: 0 additions & 2 deletions src/textual/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,8 +848,6 @@ def _handle_mouse_move(self, event: events.MouseMove) -> None:
except NoMatches:
pass
else:
tooltip.styles.offset = event.screen_offset

if self._tooltip_widget != widget or not tooltip.display:
self._tooltip_widget = widget
if self._tooltip_timer is not None:
Expand Down
31 changes: 27 additions & 4 deletions src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -1537,17 +1537,40 @@ def _console(self) -> Console:

@property
def _has_relative_children_width(self) -> bool:
"""Do any children have a relative width?"""
"""Do any children (or progeny) have a relative width?"""
if not self.is_container:
return False
return any(widget.styles.is_relative_width for widget in self.children)
for child in self.children:
styles = child.styles
if styles.display == "none":
continue
width = styles.width
if width is None:
continue
if styles.is_relative_width or (
width.is_auto and child._has_relative_children_width
):
return True
return False

@property
def _has_relative_children_height(self) -> bool:
"""Do any children have a relative height?"""
"""Do any children (or progeny) have a relative height?"""

if not self.is_container:
return False
return any(widget.styles.is_relative_height for widget in self.children)
for child in self.children:
styles = child.styles
if styles.display == "none":
continue
height = styles.height
if height is None:
continue
if styles.is_relative_height or (
height.is_auto and child._has_relative_children_height
):
return True
return False

def animate(
self,
Expand Down
7 changes: 7 additions & 0 deletions src/textual/widgets/_content_switcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class ContentSwitcher(Container):
Children that have no ID will be hidden and ignored.
"""

DEFAULT_CSS = """
ContentSwitcher {
height: auto;
}

"""

current: reactive[str | None] = reactive[Optional[str]](None, init=False)
"""The ID of the currently-displayed widget.

Expand Down
5 changes: 3 additions & 2 deletions src/textual/widgets/_tabbed_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ class TabbedContent(Widget):
"""A container with associated tabs to toggle content visibility."""

DEFAULT_CSS = """

TabbedContent {
height: auto;
}
TabbedContent > ContentSwitcher {
height: auto;
TabbedContent Tabs {
dock: top;
}
"""

Expand Down
2 changes: 1 addition & 1 deletion src/textual/widgets/_tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class Tabs(Widget, can_focus=True):
DEFAULT_CSS = """
Tabs {
width: 100%;
height:3;
height: 3;
}
Tabs > #tabs-scroll {
overflow: hidden;
Expand Down
309 changes: 233 additions & 76 deletions tests/snapshot_tests/__snapshots__/test_snapshots.ambr

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions tests/snapshot_tests/snapshot_apps/nested_fr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from textual.app import App, ComposeResult
from textual.containers import Vertical
from textual.widgets import Static


class AutoApp(App):
"""The innermost container should push its parents outwards, to fill the screen."""

CSS = """
#outer {
background: blue;
height: auto;
border: solid white;
}

#inner {
background: green;
height: auto;
border: solid yellow;
}

#innermost {
background: cyan;
height: 1fr;
color: auto;
}

"""

def compose(self) -> ComposeResult:
with Vertical(id="outer"):
with Vertical(id="inner"):
with Vertical(id="innermost"):
yield Static("Hello\nWorld!\nfoo", id="helloworld")


if __name__ == "__main__":
app = AutoApp()
app.run()
5 changes: 5 additions & 0 deletions tests/snapshot_tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,3 +605,8 @@ def test_print_capture(snap_compare) -> None:

def test_text_log_blank_write(snap_compare) -> None:
assert snap_compare(SNAPSHOT_APPS_DIR / "text_log_blank_write.py")


def test_nested_fr(snap_compare) -> None:
# https://github.com/Textualize/textual/pull/3059
assert snap_compare(SNAPSHOT_APPS_DIR / "nested_fr.py")
Loading