| 2 | from puppets import DesktopPuppet |
| 3 | from wminterfaces import WMInterface |
| 4 | |
| 5 | class ModelManager: |
| 6 | def __init__(self, puppet: DesktopPuppet, wm_interface: WMInterface) -> None: |
| 7 | self.puppet = puppet |
| 8 | self.wm_interface = wm_interface |
| 9 | self._last_cursor: tuple[int, int] | None = None |
| 10 | |
| 11 | def get_gtk_widget(self): |
| 12 | return self.puppet.get_gtk_widget() |
| 13 | |
| 14 | def get_updated_area(self) -> tuple[int, int, int, int]: |
| 15 | """Return the last known model area and queue a refresh on the GTK thread.""" |
| 16 | GLib.idle_add(self._update_area) |
| 17 | return self.puppet.model_x, self.puppet.model_y, self.puppet.model_width, self.puppet.model_height |
| 18 | |
| 19 | def _update_area(self) -> bool: |
| 20 | self.puppet.update_area() |
| 21 | return GLib.SOURCE_REMOVE |
| 22 | |
| 23 | def update_cursor_position(self) -> bool: |
| 24 | """Poll the cursor and make the puppet look at it. |
| 25 | |
| 26 | Safe to call from a worker thread: the puppet is only updated on the |
| 27 | GTK thread, and only when the cursor moved. |
| 28 | |
| 29 | Returns: |
| 30 | bool: True if the cursor moved since the last poll |
| 31 | """ |
| 32 | position = self.wm_interface.get_cursor_position() |
| 33 | if position == self._last_cursor: |
| 34 | return False |
| 35 | self._last_cursor = position |
| 36 | GLib.idle_add(self._look, *position) |
| 37 | return True |
| 38 | |
| 39 | def _look(self, x: int, y: int) -> bool: |
| 40 | # A puppet that is still loading drops the position, so poll it again |
| 41 | # instead of waiting for the cursor to move. |
| 42 | if self.puppet.look(x, y) is False: |