Skip to content

Commit

Permalink
fix: avoid rendering nested HeadlessWidgets and avoid triggering re…
Browse files Browse the repository at this point in the history
…render of the parent `HeadlessWidget` when a nested `HeadlessWidget` is rendered
  • Loading branch information
sassanh committed Oct 25, 2024
1 parent 8626a75 commit 2106d97
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 37 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Upcoming

- fix: avoid rendering nested `HeadlessWidget`s and avoid triggering rerender of the parent `HeadlessWidget` when a nested `HeadlessWidget` is rendered

## Version 0.10.1

- chore: set dependencies for `[test]` extra
Expand Down
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@ the last rendered frame.
pip install headless-kivy
```

To work on a non-RPi environment, run this:
To use its test tools, you can install it with the following command:

```sh
# pip:
pip install headless-kivy[dev]
# poetry:
poetry --group dev headless-kivy
```

## 🛠 Usage
Expand Down Expand Up @@ -131,13 +128,12 @@ If set to `True`, it will flip the display vertically.

## 🤝 Contributing

You need to have [Poetry](https://python-poetry.org/) installed on your machine.
You need to have [uv](https://github.com/astral-sh/uv) installed on your machine.

After having poetry, to install the required dependencies, run the following command
in the root directory of the project:
To install the required dependencies, run the following command in the root directory of the project:

```sh
poetry install
uv sync
```

## ⚠️ Important Note
Expand Down
33 changes: 4 additions & 29 deletions headless_kivy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from kivy.graphics.context_instructions import Color
from kivy.graphics.fbo import Fbo
from kivy.graphics.gl_instructions import ClearBuffers, ClearColor
from kivy.graphics.instructions import Canvas
from kivy.graphics.vertex_instructions import Rectangle
from kivy.metrics import dp
from kivy.properties import ObjectProperty
Expand Down Expand Up @@ -86,11 +85,9 @@ def __init__(self: HeadlessWidget, **kwargs: dict[str, object]) -> None:
self.last_change = time.time()
self.fps = config.max_fps()

self.canvas = Canvas()
with self.canvas:
self.fbo = Fbo(size=self.size, with_stencilbuffer=True)
self.fbo_color = Color(1, 1, 1, 1)
self.fbo_rect = Rectangle()
self.canvas = self.fbo = Fbo(size=self.size, with_stencilbuffer=True)
self.fbo_color = Color(1, 1, 1, 1)
self.fbo_rect = Rectangle()

with self.fbo:
ClearColor(0, 0, 0, 0)
Expand All @@ -114,28 +111,6 @@ def clear(*_: object) -> None:
if app:
app.bind(on_stop=clear)

def add_widget(
self: HeadlessWidget,
*args: object,
**kwargs: object,
) -> None:
"""Extend `Widget.add_widget` and handle `canvas`."""
canvas = self.canvas
self.canvas = self.fbo
super().add_widget(*args, **kwargs)
self.canvas = canvas

def remove_widget(
self: HeadlessWidget,
*args: object,
**kwargs: object,
) -> None:
"""Extend `Widget.remove_widget` and handle `canvas`."""
canvas = self.canvas
self.canvas = self.fbo
super().remove_widget(*args, **kwargs)
self.canvas = canvas

def on_size(
self: HeadlessWidget,
_: HeadlessWidget,
Expand Down Expand Up @@ -269,7 +244,7 @@ def render_on_display(self: HeadlessWidget, *_: object) -> None: # noqa: C901
thread = Thread(
target=config.callback(),
kwargs={
'rectangle': (self.x, self.y, width, height),
'rectangle': (x, y, x + width - 1, y + height - 1),
'data': data,
'data_hash': data_hash,
'last_render_thread': last_thread,
Expand Down

0 comments on commit 2106d97

Please sign in to comment.