Skip to content

Commit

Permalink
easing example
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Sep 12, 2021
1 parent 4b5f039 commit ff64e14
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.1.11] - Unreleased
## [0.1.11] - 2021-09-12

### Changed

Expand Down
1 change: 1 addition & 0 deletions examples/easing.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ async def on_mount(self) -> None:

self.placeholder = Placeholder()
self.easing_view = DockView()
self.placeholder.style = "white on dark_blue"

tree = TreeControl("Easing", {})
for easing_key in sorted(EASING.keys()):
Expand Down
4 changes: 0 additions & 4 deletions src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,6 @@ async def on_event(self, event: events.Event) -> None:
else:
await super().on_event(event)

# async def on_idle(self, event: events.Idle) -> None:
# if self.view.check_layout():
# await self.view.refresh_layout()

async def action(
self,
action: str,
Expand Down
4 changes: 2 additions & 2 deletions src/textual/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def blend(self, destination: Offset, factor: float) -> Offset:
class Size(NamedTuple):
"""An area defined by its width and height."""

width: int
height: int
width: int = 0
height: int = 0

def __bool__(self) -> bool:
"""A Size is Falsey if it has area 0."""
Expand Down
7 changes: 4 additions & 3 deletions src/textual/layout_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ def add_widget(
self.widgets[widget] = RenderRegion(region + widget.layout_offset, order, clip)

if isinstance(widget, View):
scroll = widget.scroll
view: View = widget
scroll = view.scroll
total_region = region.size.region
sub_clip = clip.intersection(region)

arrangement = widget.layout.arrange(region.size, scroll)
arrangement = view.get_arrangement(region.size, scroll)
for sub_region, sub_widget, sub_order in arrangement:
total_region = total_region.union(sub_region)
if sub_widget is not None:
Expand All @@ -65,4 +66,4 @@ def add_widget(
sub_order,
sub_clip,
)
widget.virtual_size = total_region.size
view.virtual_size = total_region.size
10 changes: 2 additions & 8 deletions src/textual/layouts/vertical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

from typing import Iterable

from rich.console import Console
from rich.measure import Measurement

from .. import log
from ..geometry import Offset, Region, Size
from ..layout import Layout, WidgetPlacement
from ..layout_map import LayoutMap
from ..widget import Widget


Expand Down Expand Up @@ -51,8 +46,7 @@ def arrange(self, size: Size, scroll: Offset) -> Iterable[WidgetPlacement]:
x = gutter_width
y = gutter_height

total_region = size.region

total_region = Region()
for widget in self._widgets:
if (
not widget.render_cache
Expand All @@ -65,4 +59,4 @@ def arrange(self, size: Size, scroll: Offset) -> Iterable[WidgetPlacement]:
yield WidgetPlacement(region, widget, (self.z, index))
total_region = total_region.union(region)

yield WidgetPlacement(total_region.expand(self.gutter))
yield WidgetPlacement(total_region)
18 changes: 17 additions & 1 deletion src/textual/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from . import events
from . import log
from . import messages
from .layout import Layout, NoWidget
from .layout import Layout, NoWidget, WidgetPlacement
from .geometry import Size, Offset, Region
from .reactive import Reactive, watch

Expand All @@ -33,6 +33,13 @@ def __init__(self, layout: Layout = None, name: str | None = None) -> None:
self.named_widgets: dict[str, Widget] = {}
self._mouse_style: Style = Style()
self._mouse_widget: Widget | None = None

self._cached_arrangement: tuple[Size, Offset, list[WidgetPlacement]] = (
Size(),
Offset(),
[],
)

super().__init__(name=name)

def __init_subclass__(
Expand Down Expand Up @@ -84,6 +91,14 @@ def render(self) -> RenderableType:
def get_offset(self, widget: Widget) -> Offset:
return self.layout.get_offset(widget)

def get_arrangement(self, size: Size, scroll: Offset) -> Iterable[WidgetPlacement]:
cached_size, cached_scroll, arrangement = self._cached_arrangement
if cached_size == size and cached_scroll == scroll:
return arrangement
arrangement = list(self.layout.arrange(size, scroll))
self._cached_arrangement = (size, scroll, arrangement)
return arrangement

async def handle_update(self, message: messages.Update) -> None:
if self.is_root_view:
message.stop()
Expand Down Expand Up @@ -116,6 +131,7 @@ async def mount(self, *anon_widgets: Widget, **widgets: Widget) -> None:
self.refresh()

async def refresh_layout(self) -> None:
self._cached_arrangement = (Size(), Offset(), [])
try:
await self.layout.mount_all(self)
if not self.is_root_view:
Expand Down

0 comments on commit ff64e14

Please sign in to comment.