Przeglądaj źródła

#109: factored out bind_* methods into mixin classes

Hannes Römer 2 lat temu
rodzic
commit
5a126110e6

+ 77 - 8
nicegui/binding.py

@@ -103,20 +103,89 @@ class BindableProperty:
             self.on_change(owner, value)
 
 
-class BindTextMixin:
+class BindMixin:
     """
-    Mixin providing bind text methods.
+    Mixin providing bind methods for target object attributes.
     """
 
-    def bind_text_to(self, target_object, target_name, forward=lambda x: x):
-        bind_to(self, 'text', target_object, target_name, forward=forward)
+    def _bind_from(self, target_object, target_name, *, attr: str = 'value', backward=lambda x: x):
+        bind_from(self, attr, target_object, target_name, backward=backward)
+
+    def _bind_to(self, target_object, target_name, *, attr: str = 'value', forward=lambda x: x):
+        bind_to(self, attr, target_object, target_name, forward=forward)
         return self
 
-    def bind_text_from(self, target_object, target_name, backward=lambda x: x):
-        bind_from(self, 'text', target_object, target_name, backward=backward)
+    def _bind(self, target_object, target_name, *, attr: str = 'value', forward=lambda x: x, backward=lambda x: x):
+        self._bind_from(target_object, target_name, attr=attr, backward=backward)
+        self._bind_to(target_object, target_name, attr=attr, forward=forward)
         return self
 
+
+class BindTextMixin(BindMixin):
+    """
+    Mixin providing bind methods for attribute text.
+    """
+
+    def bind_text_to(self, target_object, target_name, forward=lambda x: x):
+        return super()._bind_to(attr='text', target_object=target_object, target_name=target_name, forward=forward)
+
+    def bind_text_from(self, target_object, target_name, backward=lambda x: x):
+        return super()._bind_from(attr='text', target_object=target_object, target_name=target_name, backward=backward)
+
     def bind_text(self, target_object, target_name, forward=lambda x: x, backward=lambda x: x):
-        bind_from(self, 'text', target_object, target_name, backward=backward)
-        bind_to(self, 'text', target_object, target_name, forward=forward)
+        self.bind_text_from(target_object=target_object, target_name=target_name, backward=backward)
+        self.bind_text_to(target_object=target_object, target_name=target_name, forward=forward)
+        return self
+
+
+class BindValueMixin(BindMixin):
+    """
+    Mixin providing bind methods for attribute value.
+    """
+
+    def bind_value_to(self, target_object, target_name, forward=lambda x: x):
+        return super()._bind_to(attr='value', target_object=target_object, target_name=target_name, forward=forward)
+
+    def bind_value_from(self, target_object, target_name, backward=lambda x: x):
+        return super()._bind_from(attr='value', target_object=target_object, target_name=target_name, backward=backward)
+
+    def bind_value(self, target_object, target_name, forward=lambda x: x, backward=lambda x: x):
+        self.bind_value_from(target_object=target_object, target_name=target_name, backward=backward)
+        self.bind_value_to(target_object=target_object, target_name=target_name, forward=forward)
+        return self
+
+
+class BindVisibilityMixin(BindMixin):
+    """
+    Mixin providing bind methods for attribute visible.
+    """
+
+    def bind_visibility_to(self, target_object, target_name, forward=lambda x: x):
+        return super()._bind_to(attr='visible', target_object=target_object, target_name=target_name, forward=forward)
+
+    def bind_visibility_from(self, target_object, target_name, backward=lambda x: x):
+        return super()._bind_from(attr='visible', target_object=target_object, target_name=target_name,
+                                  backward=backward)
+
+    def bind_value(self, target_object, target_name, forward=lambda x: x, backward=lambda x: x):
+        self.bind_visibility_from(target_object=target_object, target_name=target_name, backward=backward)
+        self.bind_visibility_to(target_object=target_object, target_name=target_name, forward=forward)
+        return self
+
+
+class BindSourceMixin(BindMixin):
+    """
+    Mixin providing bind methods for attribute source.
+    """
+
+    def bind_source_to(self, target_object, target_name, forward=lambda x: x):
+        return super()._bind_to(attr='source', target_object=target_object, target_name=target_name, forward=forward)
+
+    def bind_source_from(self, target_object, target_name, backward=lambda x: x):
+        return super()._bind_from(attr='source', target_object=target_object, target_name=target_name,
+                                  backward=backward)
+
+    def bind_source(self, target_object, target_name, forward=lambda x: x, backward=lambda x: x):
+        self.bind_source_from(target_object=target_object, target_name=target_name, backward=backward)
+        self.bind_source_to(target_object=target_object, target_name=target_name, forward=forward)
         return self

+ 2 - 21
nicegui/elements/element.py

@@ -6,7 +6,7 @@ from typing import Dict, Optional
 import justpy as jp
 
 from .. import globals
-from ..binding import BindableProperty, bind_from, bind_to
+from ..binding import BindableProperty, BindVisibilityMixin
 from ..page import Page, find_parent_view
 from ..task_logger import create_task
 
@@ -16,7 +16,7 @@ def _handle_visibility_change(sender: Element, visible: bool) -> None:
     sender.update()
 
 
-class Element:
+class Element(BindVisibilityMixin):
     visible = BindableProperty(on_change=_handle_visibility_change)
 
     def __init__(self, view: jp.HTMLBaseComponent):
@@ -29,25 +29,6 @@ class Element:
 
         self.visible = True
 
-    def bind_visibility_to(self, target_object, target_name, forward=lambda x: x):
-        bind_to(self, 'visible', target_object, target_name, forward=forward)
-        return self
-
-    def bind_visibility_from(self, target_object, target_name, backward=lambda x: x, *, value=None):
-        if value is not None:
-            def backward(x): return x == value
-
-        bind_from(self, 'visible', target_object, target_name, backward=backward)
-        return self
-
-    def bind_visibility(self, target_object, target_name, forward=lambda x: x, backward=None, *, value=None):
-        if value is not None:
-            def backward(x): return x == value
-
-        bind_from(self, 'visible', target_object, target_name, backward=backward)
-        bind_to(self, 'visible', target_object, target_name, forward=forward)
-        return self
-
     def classes(self, add: Optional[str] = None, *, remove: Optional[str] = None, replace: Optional[str] = None):
         '''HTML classes to modify the look of the element.
         Every class in the `remove` parameter will be removed from the element.

+ 2 - 15
nicegui/elements/image.py

@@ -1,10 +1,10 @@
 import justpy as jp
 
-from ..binding import BindableProperty, bind_from, bind_to
+from ..binding import BindableProperty, BindSourceMixin
 from .group import Group
 
 
-class Image(Group):
+class Image(Group, BindSourceMixin):
     source = BindableProperty()
 
     def __init__(self, source: str = ''):
@@ -22,16 +22,3 @@ class Image(Group):
 
     def set_source(self, source: str):
         self.source = source
-
-    def bind_source_to(self, target_object, target_name, forward=lambda x: x):
-        bind_to(self, 'source', target_object, target_name, forward=forward)
-        return self
-
-    def bind_source_from(self, target_object, target_name, backward=lambda x: x):
-        bind_from(self, 'source', target_object, target_name, backward=backward)
-        return self
-
-    def bind_source(self, target_object, target_name, forward=lambda x: x, backward=lambda x: x):
-        bind_from(self, 'source', target_object, target_name, backward=backward)
-        bind_to(self, 'source', target_object, target_name, forward=forward)
-        return self

+ 2 - 16
nicegui/elements/value_element.py

@@ -2,13 +2,12 @@ from typing import Any, Callable, Dict, Optional
 
 import justpy as jp
 
-from ..binding import BindableProperty, bind_from, bind_to
+from ..binding import BindableProperty, BindValueMixin
 from ..events import ValueChangeEventArguments, handle_event
 from .element import Element
 
 
-class ValueElement(Element):
-
+class ValueElement(Element, BindValueMixin):
     value = BindableProperty(on_change=lambda sender, value: handle_event(
         sender.change_handler, ValueChangeEventArguments(sender=sender, socket=None, value=value)))
 
@@ -29,16 +28,3 @@ class ValueElement(Element):
         self.value = msg['value']
         self.update()
         return False
-
-    def bind_value_to(self, target_object, target_name, *, forward=lambda x: x):
-        bind_to(self, 'value', target_object, target_name, forward=forward)
-        return self
-
-    def bind_value_from(self, target_object, target_name, *, backward=lambda x: x):
-        bind_from(self, 'value', target_object, target_name, backward=backward)
-        return self
-
-    def bind_value(self, target_object, target_name, *, forward=lambda x: x, backward=lambda x: x):
-        bind_from(self, 'value', target_object, target_name, backward=backward)
-        bind_to(self, 'value', target_object, target_name, forward=forward)
-        return self