disableable_element.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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) -> 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 = lambda x: x) -> Self:
  20. """Bind the enabled state 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, 'enabled', target_object, target_name, forward)
  27. return self
  28. def bind_enabled_from(self,
  29. target_object: Any,
  30. target_name: str = 'enabled',
  31. backward: Callable = lambda x: x) -> Self:
  32. """Bind the enabled state of this element from the target object's target_name property.
  33. The binding works one way only, from the target to this element.
  34. :param target_object: The object to bind from.
  35. :param target_name: The name of the property to bind from.
  36. :param backward: A function to apply to the value before applying it to this element.
  37. """
  38. bind_from(self, 'enabled', target_object, target_name, backward)
  39. return self
  40. def bind_enabled(self,
  41. target_object: Any,
  42. target_name: str = 'enabled', *,
  43. forward: Callable = lambda x: x,
  44. backward: Callable = lambda x: x) -> Self:
  45. """Bind the enabled state of this element to the target object's target_name property.
  46. The binding works both ways, from this element to the target and from the target to this element.
  47. :param target_object: The object to bind to.
  48. :param target_name: The name of the property to bind to.
  49. :param forward: A function to apply to the value before applying it to the target.
  50. :param backward: A function to apply to the value before applying it to this element.
  51. """
  52. bind(self, 'enabled', target_object, target_name, forward=forward, backward=backward)
  53. return self
  54. def set_enabled(self, value: bool) -> None:
  55. """Set the enabled state of the element."""
  56. self.enabled = value
  57. def on_enabled_change(self, enabled: bool) -> None:
  58. """Called when the element is enabled or disabled.
  59. :param enabled: The new state.
  60. """
  61. self._props['disable'] = not enabled
  62. self.update()