Add GObject reactive utility

This commit is contained in:
Jan Hamal Dvořák 2025-03-14 19:51:49 +01:00
parent 9e818dab35
commit 32584313b0
Signed by: mordae
GPG key ID: 1782BCC23EE007B9
2 changed files with 27 additions and 0 deletions
lazy_player/reactive

View file

@ -0,0 +1,27 @@
from __future__ import annotations
from typing import Any, TypeVar
from gi.repository import GObject
from . import Ref
__all__ = ["GRef"]
T = TypeVar("T")
class GRef(Ref[T]):
_prop_name: str
_gobject: GObject.Object
def __init__(self, gobj: GObject.Object, prop_name: str):
super().__init__(gobj.get_property(prop_name))
self._prop_name = prop_name
gobj.connect(f"notify::{prop_name}", self._on_notify)
def _on_notify(self, *args: Any):
self.value = self._gobject.get_property(self._prop_name)
def __repr__(self):
return f"GRef(gobj={self._gobject!r}, prop_name={self._prop_name!r})"