source_element.py 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. from typing import Any, Callable
  2. from ...binding import BindableProperty, bind, bind_from, bind_to
  3. from ...element import Element
  4. class SourceElement(Element):
  5. source = BindableProperty(on_change=lambda sender, source: sender.on_source_change(source))
  6. def __init__(self, *, source: str, **kwargs) -> None:
  7. super().__init__(**kwargs)
  8. self.source = source
  9. self._props['src'] = source
  10. def bind_source_to(self, target_object: Any, target_name: str = 'source', forward: Callable = lambda x: x):
  11. bind_to(self, 'source', target_object, target_name, forward)
  12. return self
  13. def bind_source_from(self, target_object: Any, target_name: str = 'source', backward: Callable = lambda x: x):
  14. bind_from(self, 'source', target_object, target_name, backward)
  15. return self
  16. def bind_source(self, target_object: Any, target_name: str = 'source', *,
  17. forward: Callable = lambda x: x, backward: Callable = lambda x: x):
  18. bind(self, 'source', target_object, target_name, forward=forward, backward=backward)
  19. return self
  20. def set_source(self, source: str) -> None:
  21. self.source = source
  22. def on_source_change(self, source: str) -> None:
  23. self._props['src'] = source
  24. self.update()