value_element.py 4.1 KB

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