From 0ba3ffb1718bdd01a5136fd1bc30e8ed58e6a47c Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Mon, 4 Jul 2022 21:15:24 +0100 Subject: [PATCH] size properties --- old examples/easing.py | 4 ++-- src/textual/css/scalar_animation.py | 2 +- src/textual/layouts/horizontal.py | 2 +- src/textual/layouts/vertical.py | 2 +- src/textual/screen.py | 2 +- src/textual/widget.py | 20 ++++++++++---------- src/textual/widgets/_data_table.py | 4 ++-- tests/css/test_styles.py | 2 +- tests/test_integration_layout.py | 6 +++--- tests/utilities/test_app.py | 2 +- 10 files changed, 23 insertions(+), 23 deletions(-) diff --git a/old examples/easing.py b/old examples/easing.py index 447f69e5f..e70620e05 100644 --- a/old examples/easing.py +++ b/old examples/easing.py @@ -14,8 +14,8 @@ class EasingApp(App): def watch_side(self, side: bool) -> None: """Animate when the side changes (False for left, True for right).""" - width = self.easing_view.size.width - animate_x = (width - self.placeholder.size.width) if side else 0 + width = self.easing_view.outer_size.width + animate_x = (width - self.placeholder.outer_size.width) if side else 0 self.placeholder.animate( "layout_offset_x", animate_x, easing=self.easing, duration=1 ) diff --git a/src/textual/css/scalar_animation.py b/src/textual/css/scalar_animation.py index 58cc4252f..ae37249f9 100644 --- a/src/textual/css/scalar_animation.py +++ b/src/textual/css/scalar_animation.py @@ -36,7 +36,7 @@ class ScalarAnimation(Animation): self.final_value = value self.easing = easing - size = widget.size + size = widget.outer_size viewport = widget.app.size self.start: Offset = getattr(styles, attribute).resolve(size, viewport) diff --git a/src/textual/layouts/horizontal.py b/src/textual/layouts/horizontal.py index 47c6788e7..4f6588048 100644 --- a/src/textual/layouts/horizontal.py +++ b/src/textual/layouts/horizontal.py @@ -22,7 +22,7 @@ class HorizontalLayout(Layout): add_placement = placements.append x = max_width = max_height = Fraction(0) - parent_size = parent.size + parent_size = parent.outer_size children = list(parent.children) styles = [child.styles for child in children if child.styles.width is not None] diff --git a/src/textual/layouts/vertical.py b/src/textual/layouts/vertical.py index 023995a6a..f8ed4f152 100644 --- a/src/textual/layouts/vertical.py +++ b/src/textual/layouts/vertical.py @@ -20,7 +20,7 @@ class VerticalLayout(Layout): placements: list[WidgetPlacement] = [] add_placement = placements.append - parent_size = parent.size + parent_size = parent.outer_size children = list(parent.children) styles = [child.styles for child in children if child.styles.height is not None] diff --git a/src/textual/screen.py b/src/textual/screen.py index acf9552ff..7ad6d3e02 100644 --- a/src/textual/screen.py +++ b/src/textual/screen.py @@ -131,7 +131,7 @@ class Screen(Widget): def _refresh_layout(self, size: Size | None = None, full: bool = False) -> None: """Refresh the layout (can change size and positions of widgets).""" - size = self.size if size is None else size + size = self.outer_size if size is None else size if not size: return diff --git a/src/textual/widget.py b/src/textual/widget.py index 753f74821..08abce3e8 100644 --- a/src/textual/widget.py +++ b/src/textual/widget.py @@ -434,15 +434,18 @@ class Widget(DOMNode): return self.styles.gutter + self.scrollbar_gutter @property - def content_size(self) -> Size: + def size(self) -> Size: + """The size of the content area.""" return self.content_region.size @property - def size(self) -> Size: + def outer_size(self) -> Size: + """The size of the widget (including padding and border).""" return self._size @property def container_size(self) -> Size: + """The size of the container (parent widget).""" return self._container_size @property @@ -535,17 +538,14 @@ class Widget(DOMNode): """ if regions: - widget_regions = [ - region.translate(self.content_offset) for region in regions - ] + content_offset = self.content_offset + widget_regions = [region.translate(content_offset) for region in regions] self._dirty_regions.update(widget_regions) self._styles_cache.set_dirty(*widget_regions) else: self._dirty_regions.clear() self._styles_cache.clear() - # TODO: Does this need to be content region? - # self._dirty_regions.append(self.size.region) - self._dirty_regions.add(self.size.region) + self._dirty_regions.add(self.outer_size.region) def get_dirty_regions(self) -> Collection[Region]: """Get regions which require a repaint. @@ -976,13 +976,13 @@ class Widget(DOMNode): def _render_content(self) -> None: """Render all lines.""" - width, height = self.content_size + width, height = self.size renderable = self.render_styled() options = self.console.options.update_dimensions(width, height).update( highlight=False ) lines = self.console.render_lines(renderable, options, style=self.rich_style) - self._render_cache = RenderCache(self.content_size, lines) + self._render_cache = RenderCache(self.size, lines) self._dirty_regions.clear() def render_line(self, y: int) -> list[Segment]: diff --git a/src/textual/widgets/_data_table.py b/src/textual/widgets/_data_table.py index 1b04ac790..472c98bc1 100644 --- a/src/textual/widgets/_data_table.py +++ b/src/textual/widgets/_data_table.py @@ -462,7 +462,7 @@ class DataTable(ScrollView, Generic[CellType]): list[Segment]: List of segments for rendering. """ - width = self.content_size.width + width = self.size.width try: row_index, line_no = self._get_offsets(y) @@ -507,7 +507,7 @@ class DataTable(ScrollView, Generic[CellType]): Returns: list[Segment]: A rendered line. """ - width, height = self.content_size + width, height = self.size scroll_x, scroll_y = self.scroll_offset fixed_top_row_count = sum( self.get_row_height(row_index) for row_index in range(self.fixed_rows) diff --git a/tests/css/test_styles.py b/tests/css/test_styles.py index 0d1319ae6..3ad0585cd 100644 --- a/tests/css/test_styles.py +++ b/tests/css/test_styles.py @@ -277,5 +277,5 @@ async def test_scrollbar_gutter( app = MyTestApp(test_name="scrollbar_gutter", size=Size(80, 10)) await app.boot_and_shutdown() - assert text_widget.size.width == expected_text_widget_width + assert text_widget.outer_size.width == expected_text_widget_width assert container.scrollbars_enabled[0] is expects_vertical_scrollbar diff --git a/tests/test_integration_layout.py b/tests/test_integration_layout.py index 2046a37b8..85990497f 100644 --- a/tests/test_integration_layout.py +++ b/tests/test_integration_layout.py @@ -144,7 +144,7 @@ async def test_composition_of_vertical_container_with_children( async with app.in_running_state(): # root widget checks: root_widget = cast(Widget, app.get_child("root")) - assert root_widget.size == expected_screen_size + assert root_widget.outer_size == expected_screen_size root_widget_region = app.screen.find_widget(root_widget).region assert root_widget_region == ( 0, @@ -158,7 +158,7 @@ async def test_composition_of_vertical_container_with_children( # placeholder widgets checks: for placeholder in app_placeholders: - assert placeholder.size == expected_placeholders_size + assert placeholder.outer_size == expected_placeholders_size assert placeholder.styles.offset.x.value == 0.0 assert app.screen.get_offset(placeholder).x == expected_placeholders_offset_x @@ -224,7 +224,7 @@ async def test_border_edge_types_impact_on_widget_size( ) assert box_inner_size == expected_box_inner_size - assert border_target.size == expected_box_size + assert border_target.outer_size == expected_box_size top_left_edge_style = app.screen.get_style_at(0, 0) top_left_edge_color = top_left_edge_style.color.name diff --git a/tests/utilities/test_app.py b/tests/utilities/test_app.py index a327a54c9..8e422f2e7 100644 --- a/tests/utilities/test_app.py +++ b/tests/utilities/test_app.py @@ -159,7 +159,7 @@ class AppTest(App): # We artificially tell the Compositor that the whole area should be refreshed screen._compositor._dirty_regions = { - Region(0, 0, screen.size.width, screen.size.height), + Region(0, 0, screen.outer_size.width, screen.outer_size.height), } screen.refresh(repaint=repaint, layout=layout) # We also have to make sure we have at least one dirty widget, or `screen._on_update()` will early return: