Skip to content

Commit

Permalink
Add a render_mode option for App.
Browse files Browse the repository at this point in the history
  • Loading branch information
salt-die committed Oct 6, 2023
1 parent 42ce00f commit f703add
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/basic/spinners.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ async def on_start(self):


if __name__ == "__main__":
SpinnersApp(title="Spinners").run()
SpinnersApp(title="Spinners", render_mode="painter").run()
18 changes: 18 additions & 0 deletions nurses_2/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pathlib import Path
from time import monotonic
from types import ModuleType
from typing import Literal

from .colors import BLACK_ON_BLACK, DEFAULT_COLOR_THEME, ColorPair, ColorTheme
from .io import (
Expand Down Expand Up @@ -54,6 +55,10 @@ class App(ABC):
the asciicast format -- doing so will corrupt the recording.
redirect_stderr : Path | None, default: None
If provided, stderr is written to this path.
render_mode : Literal["regions", "painter"], default: "regions"
Determines how the widget tree is rendered. "painter" fully paints every widget
back-to-front. "regions" only paints the visible portion of each widget.
"painter" may be more efficient for a large number of non-overlapping widgets.
Attributes
----------
Expand Down Expand Up @@ -106,6 +111,7 @@ def __init__(
color_theme: ColorTheme = DEFAULT_COLOR_THEME,
asciicast_path: Path | None = None,
redirect_stderr: Path | None = None,
render_mode: Literal["regions", "painter"] = "regions",
):
self.root = None

Expand All @@ -117,6 +123,7 @@ def __init__(
self.color_theme = color_theme
self.asciicast_path = asciicast_path
self.redirect_stderr = redirect_stderr
self.render_mode = render_mode

@property
def color_theme(self) -> ColorTheme:
Expand Down Expand Up @@ -151,6 +158,16 @@ def background_color_pair(self, background_color_pair: str):
if self.root is not None:
self.root.background_color_pair = background_color_pair

@property
def render_mode(self) -> Literal["regions", "painter"]:
return self._render_mode

@render_mode.setter
def render_mode(self, render_mode: Literal["regions", "painter"]):
self._render_mode = render_mode
if self.root is not None:
self.root.render_mode = render_mode

@abstractmethod
async def on_start(self):
"""
Expand Down Expand Up @@ -216,6 +233,7 @@ async def _run_async(self):
self.root = root = _Root(
background_char=self.background_char,
background_color_pair=self.background_color_pair,
render_mode=self.render_mode,
size=env_out.get_size(),
)

Expand Down
22 changes: 14 additions & 8 deletions nurses_2/widgets/_root.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""
Root widget.
"""
from typing import Literal

import numpy as np

from .widget import ColorPair, Point, Region, Size, Widget, style_char

USE_PAINTERS = True


class _Root(Widget):
"""
Expand All @@ -16,11 +16,16 @@ class _Root(Widget):
"""

def __init__(
self, background_char: str, background_color_pair: ColorPair, size: Size
self,
background_char: str,
background_color_pair: ColorPair,
render_mode: Literal["regions", "painter"],
size: Size,
):
self.children = []
self.background_char = background_char
self.background_color_pair = background_color_pair
self.render_mode = render_mode
self._size = size
self.on_size()

Expand Down Expand Up @@ -93,11 +98,12 @@ def render(self):
else Region()
)

for child in self.walk_reverse():
if child.is_enabled:
child.region &= self.region
if child.is_visible and not child.is_transparent:
self.region -= child.region
if self.render_mode == "regions":
for child in self.walk_reverse():
if child.is_enabled:
child.region &= self.region
if child.is_visible and not child.is_transparent:
self.region -= child.region

self.canvas, self._last_canvas = self._last_canvas, self.canvas
self.colors, self._last_colors = self._last_colors, self.colors
Expand Down

0 comments on commit f703add

Please sign in to comment.