Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Jun 17, 2024
1 parent a566558 commit dd3ff84
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/textual/_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ def compose(node: App | Widget) -> list[Widget]:
return nodes


def _pending_children(
node: Widget, children_by_id: dict[str, Widget]
) -> dict[str, Widget]:
def recurse_children(widget: Widget):
children: list[Widget] = list(
child
for child in widget._pending_children
if not child.has_class("-textual-system")
)

if widget.id is not None and widget.id not in children_by_id:
children_by_id[widget.id] = widget
for child in children:
recurse_children(child)

recurse_children(node)

return children_by_id


def recompose(
node: App | Widget, compose_node: App | Widget | None = None
) -> tuple[list[Widget], set[Widget]]:
Expand All @@ -96,7 +116,25 @@ def recompose(
children_by_id = {child.id: child for child in children if child.id is not None}
new_children: list[Widget] = []
remove_children: set[Widget] = set(children)

composed = compose(node if compose_node is None else compose_node)

for compose_node in compose(node if compose_node is None else compose_node):
_pending_children(compose_node, children_by_id)

def recurse_pending(node: Widget) -> None:
print("recurse", node, children_by_id)
for child in list(node._nodes):
if child.id is not None and child.id in children_by_id:
print(1)
existing_child = children_by_id.pop(child.id)
if node._nodes._replace(child, existing_child):
remove_children.add(child)
else:
print(2)
recurse_pending(child)

for compose_node in composed:
if (
compose_node.id is not None
and (existing_child := children_by_id.pop(compose_node.id, None))
Expand All @@ -107,4 +145,9 @@ def recompose(
existing_child.copy_state(compose_node)
else:
new_children.append(compose_node)
recurse_pending(node)

node.log(children_by_id)
print(new_children, remove_children)

return (new_children, remove_children)
10 changes: 10 additions & 0 deletions src/textual/_node_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ def _move_to_end(self, widget: Widget) -> None:
if widget in self._nodes_set:
self._nodes.remove(widget)
self._nodes.append(widget)
self._updates += 1

def _replace(self, widget: Widget, new_widget: Widget) -> bool:
try:
index = self._nodes.index(widget)
except ValueError:
return False
else:
self._nodes[index] = new_widget
return True

def _ensure_unique_id(self, widget_id: str) -> None:
if widget_id in self._nodes_by_id:
Expand Down
6 changes: 5 additions & 1 deletion src/textual/widgets/_footer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ..app import ComposeResult
from ..binding import Binding
from ..containers import ScrollableContainer
from ..dom import NoScreen
from ..reactive import reactive
from ..widget import Widget

Expand Down Expand Up @@ -173,7 +174,10 @@ async def bindings_changed(screen: Screen) -> None:
self.screen.bindings_updated_signal.subscribe(self, bindings_changed)

def on_unmount(self) -> None:
self.screen.bindings_updated_signal.unsubscribe(self)
try:
self.screen.bindings_updated_signal.unsubscribe(self)
except NoScreen:
pass

def watch_compact(self, compact: bool) -> None:
self.set_class(compact, "-compact")
1 change: 1 addition & 0 deletions src/textual/widgets/_placeholder.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def render(self) -> RenderResult:
Returns:
The value to render.
"""
return str(id(self))
return self._renderables[self.variant]

def cycle_variant(self) -> Self:
Expand Down

0 comments on commit dd3ff84

Please sign in to comment.