-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfirmation_dialog.py
More file actions
90 lines (72 loc) · 2.28 KB
/
confirmation_dialog.py
File metadata and controls
90 lines (72 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from threading import Condition
from textual import on, work
from textual.app import App, ComposeResult
from textual.containers import Center, Horizontal, Vertical
from textual.screen import ModalScreen
from textual.widgets import Button, Label
from textual.worker import Worker, WorkerState
class ConfirmationDialog(ModalScreen):
def compose(self) -> ComposeResult:
with Horizontal():
yield Button("Yes", id="yes")
yield Button("No", id="no")
@on(Button.Pressed)
def confirm(self, event: Button.Pressed) -> None:
if event.button.id == "yes":
self.dismiss(True)
else:
self.dismiss(False)
print("DONE")
class ConfirmApp(App):
CSS = """
Screen, ConfirmationDialog {
align: center middle;
}
Screen > Vertical {
width: auto;
height: auto;
}
ConfirmationDialog > Horizontal {
width: auto;
height: auto;
}
#my_button .success {
border: solid green;
}
"""
def compose(self) -> ComposeResult:
with Vertical():
with Center():
yield Label("Click to start the action!")
with Center():
yield Button("Click", id="mybutton")
@on(Button.Pressed)
def confirm(self):
self.run_task()
@on(Worker.StateChanged)
def worker_state(self, event: Worker.StateChanged) -> None:
if event.state == WorkerState.SUCCESS:
print("SUCCESS")
@work(thread=True)
def run_task(self):
confirmed = Condition()
confirmation = None
def callback(value: bool) -> None:
nonlocal confirmation
with confirmed:
confirmation = value
confirmed.notify()
self.call_from_thread(self.push_screen, ConfirmationDialog(), callback)
print("WAITING...")
with confirmed:
confirmed.wait()
print(f"Confirmation is {confirmation}")
if confirmation is True:
print("Clicked YES")
elif confirmation is False:
print("Clicked NO")
else:
print("WTF?")
if __name__ == "__main__":
app = ConfirmApp()
app.run()