dialog.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import asyncio
  2. from typing import Any, Optional
  3. from .mixins.value_element import ValueElement
  4. class Dialog(ValueElement):
  5. def __init__(self, *, value: bool = False) -> None:
  6. """Dialog
  7. Creates a dialog based on Quasar's `QDialog <https://quasar.dev/vue-components/dialog>`_ component.
  8. By default it is dismissible by clicking or pressing ESC.
  9. To make it persistent, set `.props('persistent')` on the dialog element.
  10. :param value: whether the dialog should be opened on creation (default: `False`)
  11. """
  12. super().__init__(tag='q-dialog', value=value, on_value_change=None)
  13. self._result: Any = None
  14. self._submitted: Optional[asyncio.Event] = None
  15. @property
  16. def submitted(self) -> asyncio.Event:
  17. """An event that is set when the dialog is submitted."""
  18. if self._submitted is None:
  19. self._submitted = asyncio.Event()
  20. return self._submitted
  21. def open(self) -> None:
  22. """Open the dialog."""
  23. self.value = True
  24. def close(self) -> None:
  25. """Close the dialog."""
  26. self.value = False
  27. def __await__(self):
  28. self._result = None
  29. self.submitted.clear()
  30. self.open()
  31. yield from self.submitted.wait().__await__() # pylint: disable=no-member
  32. result = self._result
  33. self.close()
  34. return result
  35. def submit(self, result: Any) -> None:
  36. """Submit the dialog with the given result."""
  37. self._result = result
  38. self.submitted.set()
  39. def _handle_value_change(self, value: Any) -> None:
  40. super()._handle_value_change(value)
  41. if not self.value:
  42. self._result = None
  43. self.submitted.set()