source_element.py 3.3 KB

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