source_element.py 1.2 KB

1234567891011121314151617181920212223242526272829303132
  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_source(self, source: str) -> None:
  7. self.source = source
  8. self._props['src'] = source
  9. def bind_source_to(self, target_object: Any, target_name: str = 'source', forward: Callable = lambda x: x):
  10. bind_to(self, 'source', target_object, target_name, forward)
  11. return self
  12. def bind_source_from(self, target_object: Any, target_name: str = 'source', backward: Callable = lambda x: x):
  13. bind_from(self, 'source', target_object, target_name, backward)
  14. return self
  15. def bind_source(self, target_object: Any, target_name: str = 'source', *,
  16. forward: Callable = lambda x: x, backward: Callable = lambda x: x):
  17. bind(self, 'source', target_object, target_name, forward=forward, backward=backward)
  18. return self
  19. def set_source(self, source: str) -> None:
  20. self.source = source
  21. def on_source_change(self, source: str) -> None:
  22. self._props['src'] = source
  23. self.update()