|
17 | 17 | StyleT = TypeVar("StyleT", bound=BaseStyle)
|
18 | 18 |
|
19 | 19 |
|
| 20 | +TAB = " " |
| 21 | + |
| 22 | + |
20 | 23 | class Widget(Node):
|
21 | 24 | _MIN_WIDTH = 100
|
22 | 25 | _MIN_HEIGHT = 100
|
@@ -319,19 +322,57 @@ def enabled(self) -> bool:
|
319 | 322 | def enabled(self, value: bool) -> None:
|
320 | 323 | self._impl.set_enabled(bool(value))
|
321 | 324 |
|
| 325 | + _layouts = 0 |
| 326 | + _level = 0 |
| 327 | + |
322 | 328 | def refresh(self) -> None:
|
| 329 | + name = type(self).__name__ |
| 330 | + # print(TAB * self._level + f"Refresh requested on {name}") |
| 331 | + |
| 332 | + if not self.window: |
| 333 | + # No need to do anything if the widget hasn't been added to a window. |
| 334 | + return |
| 335 | + |
323 | 336 | self._impl.refresh()
|
324 | 337 |
|
325 | 338 | # Refresh the layout
|
326 | 339 | if self._root:
|
327 | 340 | # We're not the root of the node hierarchy;
|
328 | 341 | # defer the refresh call to the root node.
|
329 | 342 | self._root.refresh()
|
| 343 | + |
330 | 344 | else:
|
331 |
| - # We can't compute a layout until we have a container |
332 |
| - if self._impl.container: |
333 |
| - super().refresh(self._impl.container) |
334 |
| - self._impl.container.refreshed() |
| 345 | + # Uncomment to always compute layout: |
| 346 | + |
| 347 | + # self._refresh_layout() |
| 348 | + # return |
| 349 | + |
| 350 | + if self.window._currently_laying_out: |
| 351 | + self._refresh_layout() |
| 352 | + return |
| 353 | + |
| 354 | + print(TAB * self._level + f"Adding {name} to dirty set") |
| 355 | + self.window._dirty_root_widgets.add(self) |
| 356 | + |
| 357 | + if self.window._pending_layout is None: |
| 358 | + self.window._pending_layout = self.app.loop.call_soon( |
| 359 | + self.window._refresh_layouts |
| 360 | + ) |
| 361 | + |
| 362 | + def _refresh_layout(self): |
| 363 | + # print(self._level) |
| 364 | + name = type(self).__name__ |
| 365 | + |
| 366 | + Widget._layouts += 1 |
| 367 | + print(TAB * self._level + f"#{self._layouts}. Laying out {name}") |
| 368 | + |
| 369 | + Widget._level += 1 |
| 370 | + |
| 371 | + super().refresh(self._impl.container) |
| 372 | + self._impl.container.refreshed() |
| 373 | + |
| 374 | + Widget._level -= 1 |
| 375 | + # print(TAB * self._level + f"Done laying out {name}\n") |
335 | 376 |
|
336 | 377 | def focus(self) -> None:
|
337 | 378 | """Give this widget the input focus.
|
|
0 commit comments