disableable_element.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from typing import Any, Callable
  2. from typing_extensions import Self
  3. from ...binding import BindableProperty, bind, bind_from, bind_to
  4. from ...element import Element
  5. class DisableableElement(Element):
  6. enabled = BindableProperty(on_change=lambda sender, value: sender.on_enabled_change(value))
  7. def __init__(self, **kwargs: Any) -> None:
  8. super().__init__(**kwargs)
  9. self.enabled = True
  10. def enable(self) -> None:
  11. """Enable the element."""
  12. self.enabled = True
  13. def disable(self) -> None:
  14. """Disable the element."""
  15. self.enabled = False
  16. def bind_enabled_to(self,
  17. target_object: Any,
  18. target_name: str = 'enabled',
  19. forward: Callable[..., Any] = lambda x: x,
  20. ) -> Self:
  21. """Bind the enabled state 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, 'enabled', target_object, target_name, forward)
  28. return self
  29. def bind_enabled_from(self,
  30. target_object: Any,
  31. target_name: str = 'enabled',
  32. backward: Callable[..., Any] = lambda x: x,
  33. ) -> Self:
  34. """Bind the enabled state 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, 'enabled', target_object, target_name, backward)
  41. return self
  42. def bind_enabled(self,
  43. target_object: Any,
  44. target_name: str = 'enabled', *,
  45. forward: Callable[..., Any] = lambda x: x,
  46. backward: Callable[..., Any] = lambda x: x,
  47. ) -> Self:
  48. """Bind the enabled state 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, 'enabled', target_object, target_name, forward=forward, backward=backward)
  56. return self
  57. def set_enabled(self, value: bool) -> None:
  58. """Set the enabled state of the element."""
  59. self.enabled = value
  60. def on_enabled_change(self, enabled: bool) -> None:
  61. """Called when the element is enabled or disabled.
  62. :param enabled: The new state.
  63. """
  64. self._props['disable'] = not enabled
  65. self.update()