value_element.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from typing import Any, Callable, Dict, List, Optional
  2. from typing_extensions import Self
  3. from ...binding import BindableProperty, bind, bind_from, bind_to
  4. from ...element import Element
  5. from ...events import ValueChangeEventArguments, handle_event
  6. class ValueElement(Element):
  7. VALUE_PROP: str = 'model-value'
  8. EVENT_ARGS: Optional[List[str]] = ['value']
  9. LOOPBACK: bool = True
  10. value = BindableProperty(on_change=lambda sender, value: sender.on_value_change(value))
  11. def __init__(self, *,
  12. value: Any,
  13. on_value_change: Optional[Callable[..., Any]],
  14. throttle: float = 0,
  15. **kwargs: Any,
  16. ) -> None:
  17. super().__init__(**kwargs)
  18. self.set_value(value)
  19. self._props[self.VALUE_PROP] = self._value_to_model_value(value)
  20. self._props['loopback'] = self.LOOPBACK
  21. self._send_update_on_value_change = True
  22. self.change_handler = on_value_change
  23. def handle_change(msg: Dict) -> None:
  24. self._send_update_on_value_change = self.LOOPBACK
  25. self.set_value(self._msg_to_value(msg))
  26. self._send_update_on_value_change = True
  27. self.on(f'update:{self.VALUE_PROP}', handle_change, self.EVENT_ARGS, throttle=throttle)
  28. def bind_value_to(self,
  29. target_object: Any,
  30. target_name: str = 'value',
  31. forward: Callable[..., Any] = lambda x: x,
  32. ) -> Self:
  33. """Bind the value of this element to the target object's target_name property.
  34. The binding works one way only, from this element to the target.
  35. :param target_object: The object to bind to.
  36. :param target_name: The name of the property to bind to.
  37. :param forward: A function to apply to the value before applying it to the target.
  38. """
  39. bind_to(self, 'value', target_object, target_name, forward)
  40. return self
  41. def bind_value_from(self,
  42. target_object: Any,
  43. target_name: str = 'value',
  44. backward: Callable[..., Any] = lambda x: x,
  45. ) -> Self:
  46. """Bind the value of this element from the target object's target_name property.
  47. The binding works one way only, from the target to this element.
  48. :param target_object: The object to bind from.
  49. :param target_name: The name of the property to bind from.
  50. :param backward: A function to apply to the value before applying it to this element.
  51. """
  52. bind_from(self, 'value', target_object, target_name, backward)
  53. return self
  54. def bind_value(self,
  55. target_object: Any,
  56. target_name: str = 'value', *,
  57. forward: Callable[..., Any] = lambda x: x,
  58. backward: Callable[..., Any] = lambda x: x,
  59. ) -> Self:
  60. """Bind the value of this element to the target object's target_name property.
  61. The binding works both ways, from this element to the target and from the target to this element.
  62. :param target_object: The object to bind to.
  63. :param target_name: The name of the property to bind to.
  64. :param forward: A function to apply to the value before applying it to the target.
  65. :param backward: A function to apply to the value before applying it to this element.
  66. """
  67. bind(self, 'value', target_object, target_name, forward=forward, backward=backward)
  68. return self
  69. def set_value(self, value: Any) -> None:
  70. """Set the value of this element.
  71. :param value: The value to set.
  72. """
  73. self.value = value
  74. def on_value_change(self, value: Any) -> None:
  75. """Called when the value of this element changes.
  76. :param value: The new value.
  77. """
  78. self._props[self.VALUE_PROP] = self._value_to_model_value(value)
  79. if self._send_update_on_value_change:
  80. self.update()
  81. args = ValueChangeEventArguments(sender=self, client=self.client, value=self._value_to_event_value(value))
  82. handle_event(self.change_handler, args)
  83. def _msg_to_value(self, msg: Dict) -> Any:
  84. return msg['args']
  85. def _value_to_model_value(self, value: Any) -> Any:
  86. return value
  87. def _value_to_event_value(self, value: Any) -> Any:
  88. return value