source_element.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from pathlib import Path
  2. from typing import Any, Callable, Union
  3. from typing_extensions import Self
  4. from ... import globals
  5. from ...binding import BindableProperty, bind, bind_from, bind_to
  6. from ...element import Element
  7. from ...helpers import is_file
  8. class SourceElement(Element):
  9. source = BindableProperty(on_change=lambda sender, source: sender.on_source_change(source))
  10. def __init__(self, *, source: Union[str, Path], **kwargs: Any) -> None:
  11. super().__init__(**kwargs)
  12. if is_file(source):
  13. source = globals.app.add_static_file(local_file=source)
  14. self.source = source
  15. self._props['src'] = source
  16. def bind_source_to(self,
  17. target_object: Any,
  18. target_name: str = 'source',
  19. forward: Callable[..., Any] = lambda x: x,
  20. ) -> Self:
  21. """Bind the source of this element to the target object's target_name property.
  22. The binding works one way only, from this element to the target.
  23. :param target_object: The object to bind to.
  24. :param target_name: The name of the property to bind to.
  25. :param forward: A function to apply to the value before applying it to the target.
  26. """
  27. bind_to(self, 'source', target_object, target_name, forward)
  28. return self
  29. def bind_source_from(self,
  30. target_object: Any,
  31. target_name: str = 'source',
  32. backward: Callable[..., Any] = lambda x: x,
  33. ) -> Self:
  34. """Bind the source of this element from the target object's target_name property.
  35. The binding works one way only, from the target to this element.
  36. :param target_object: The object to bind from.
  37. :param target_name: The name of the property to bind from.
  38. :param backward: A function to apply to the value before applying it to this element.
  39. """
  40. bind_from(self, 'source', target_object, target_name, backward)
  41. return self
  42. def bind_source(self,
  43. target_object: Any,
  44. target_name: str = 'source', *,
  45. forward: Callable[..., Any] = lambda x: x,
  46. backward: Callable[..., Any] = lambda x: x,
  47. ) -> Self:
  48. """Bind the source of this element to the target object's target_name property.
  49. The binding works both ways, from this element to the target and from the target to this element.
  50. :param target_object: The object to bind to.
  51. :param target_name: The name of the property to bind to.
  52. :param forward: A function to apply to the value before applying it to the target.
  53. :param backward: A function to apply to the value before applying it to this element.
  54. """
  55. bind(self, 'source', target_object, target_name, forward=forward, backward=backward)
  56. return self
  57. def set_source(self, source: Union[str, Path]) -> None:
  58. """Set the source of this element.
  59. :param source: The new source.
  60. """
  61. self.source = source
  62. def on_source_change(self, source: Union[str, Path]) -> None:
  63. """Called when the source of this element changes.
  64. :param source: The new source.
  65. """
  66. self._props['src'] = source
  67. self.update()