Skip to content

Commit

Permalink
allow binding multiple properties using a list and a transform
Browse files Browse the repository at this point in the history
  • Loading branch information
PacificViking committed Jan 6, 2025
1 parent db78fb9 commit fd0ea29
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions ignis/gobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ class Binding(GObject.Object):
def __init__(
self,
target: GObject.Object,
target_property: str,
target_properties: list[str],
transform: Callable | None = None,
):
self._target = target
self._target_property = target_property
self._target_properties = target_properties
self._transform = transform
super().__init__()

Expand All @@ -28,13 +28,13 @@ def target(self) -> GObject.Object:
return self._target

@GObject.Property
def target_property(self) -> str:
def target_properties(self) -> list[str]:
"""
- required, read-only
The property on the target GObject.
The properties on the target GObject to bind.
"""
return self._target_property
return self._target_properties

@GObject.Property
def transform(self) -> Callable | None:
Expand Down Expand Up @@ -106,7 +106,7 @@ def set_property(self, property_name: str, value: Any) -> None:
self.bind_property2(
source_property=property_name,
target=value.target,
target_property=value.target_property,
target_properties=value.target_properties,
transform=value.transform,
)
else:
Expand All @@ -116,26 +116,34 @@ def bind_property2(
self,
source_property: str,
target: GObject.Object,
target_property: str,
target_properties: list[str],
transform: Callable | None = None,
) -> None:
"""
Bind ``source_property`` on ``self`` with ``target_property`` on ``target``.
Bind ``source_property`` on ``self`` with ``target_properties`` on ``target``.
Args:
source_property: The property on ``self`` to bind.
target: the target ``GObject.Object``.
target_property: the property on ``target`` to bind.
target_properties: the properties on ``target`` to bind.
transform: The function that accepts a new property value and returns the processed value.
"""

def callback(*args):
value = target.get_property(target_property.replace("-", "_"))
values = []
for target_property in target_properties:
values.append(target.get_property(target_property.replace("-", "_")))

if transform:
value = transform(value)
value = transform(*values)
else:
if len(values) != 1:
raise IndexError("No transform function on muliple binding")
value = values[0]
self.set_property(source_property, value)

target.connect(f"notify::{target_property.replace('_', '-')}", callback)
for target_property in target_properties:
target.connect(f"notify::{target_property.replace('_', '-')}", callback)
callback()

def bind(self, property_name: str, transform: Callable | None = None) -> Binding:
Expand All @@ -148,7 +156,19 @@ def bind(self, property_name: str, transform: Callable | None = None) -> Binding
Returns:
:class:`~ignis.gobject.Binding`
"""
return Binding(self, property_name, transform)
return Binding(self, [property_name], transform)

def bind_many(self, property_names: list[str], transform: Callable) -> Binding:
"""
Creates ``Binding`` from property name on ``self``.
Args:
property_names: List of property names of ``self``.
transform: The function that accepts a new property value and returns the processed value.
Returns:
:class:`~ignis.gobject.Binding`
"""
return Binding(self, property_names, transform)

def __getattribute__(self, name: str) -> Any:
# This modified __getattribute__ method redirect all "set_" methods to set_property method to provive bindings support.
Expand Down

0 comments on commit fd0ea29

Please sign in to comment.