popup.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # -*- coding: utf-8 -*-
  2. """
  3. Popup-Fenster (Modal) fuer die TUI.
  4. """
  5. from textual.screen import ModalScreen
  6. from textual.containers import Vertical, Horizontal
  7. from textual.widgets import Static, Button
  8. from textual.app import ComposeResult
  9. class PopupWindow(ModalScreen[bool]):
  10. """
  11. Modales Popup-Fenster.
  12. Zeigt eine Nachricht mit OK-Button.
  13. """
  14. DEFAULT_CSS = """
  15. PopupWindow {
  16. align: center middle;
  17. }
  18. PopupWindow > Vertical {
  19. width: 60;
  20. height: auto;
  21. max-height: 20;
  22. border: thick $accent;
  23. background: $surface;
  24. padding: 1 2;
  25. }
  26. PopupWindow > Vertical > Static {
  27. margin-bottom: 1;
  28. }
  29. PopupWindow > Vertical > Button {
  30. width: 100%;
  31. }
  32. """
  33. def __init__(self, title: str, message: str, **kwargs) -> None:
  34. super().__init__(**kwargs)
  35. self._title = title
  36. self._message = message
  37. def compose(self) -> ComposeResult:
  38. with Vertical():
  39. yield Static(f"[bold]{self._title}[/bold]")
  40. yield Static(self._message)
  41. yield Button("OK", variant="primary", id="popup-ok")
  42. def on_button_pressed(self, event: Button.Pressed) -> None:
  43. """Schliesst das Popup."""
  44. self.dismiss(True)
  45. class ConfirmDialog(ModalScreen[bool]):
  46. """
  47. Bestaetigungs-Dialog mit OK und Abbrechen.
  48. Gibt True bei OK, False bei Abbrechen zurueck.
  49. """
  50. DEFAULT_CSS = """
  51. ConfirmDialog {
  52. align: center middle;
  53. }
  54. ConfirmDialog > Vertical {
  55. width: 70;
  56. height: auto;
  57. max-height: 24;
  58. border: thick $accent;
  59. background: $surface;
  60. padding: 1 2;
  61. }
  62. ConfirmDialog > Vertical > Static {
  63. margin-bottom: 1;
  64. }
  65. ConfirmDialog > Vertical > Horizontal {
  66. height: 3;
  67. margin-top: 1;
  68. }
  69. ConfirmDialog > Vertical > Horizontal > Button {
  70. width: 1fr;
  71. margin: 0 1;
  72. }
  73. """
  74. def __init__(self, title: str, message: str, **kwargs) -> None:
  75. super().__init__(**kwargs)
  76. self._title = title
  77. self._message = message
  78. def compose(self) -> ComposeResult:
  79. with Vertical():
  80. yield Static(f"[bold]{self._title}[/bold]")
  81. yield Static(self._message)
  82. with Horizontal():
  83. yield Button("OK", variant="warning", id="confirm-ok")
  84. yield Button("Abbrechen", variant="default", id="confirm-cancel")
  85. def on_button_pressed(self, event: Button.Pressed) -> None:
  86. if event.button.id == "confirm-ok":
  87. self.dismiss(True)
  88. else:
  89. self.dismiss(False)