-
Notifications
You must be signed in to change notification settings - Fork 0
/
coupled_switches_on_screens.py
59 lines (44 loc) · 1.56 KB
/
coupled_switches_on_screens.py
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
from textual import on
from textual.app import App, ComposeResult
from textual.message import Message
from textual.screen import Screen
from textual.widgets import Footer, Header, Switch
class MasterScreen(Screen):
TITLE = "Master Screen"
def compose(self) -> ComposeResult:
yield Header()
yield Footer()
yield Switch(value=False, id="master-switch")
class SlaveScreen(Screen):
class MasterSwitchChanged(Message):
def __init__(self, value: bool) -> None:
super().__init__()
self.value = value
TITLE = "Slave Screen"
def compose(self) -> ComposeResult:
yield Header()
yield Footer()
yield Switch(value=False, id="slave-switch")
@on(MasterSwitchChanged)
def set_slave_from_master(self, event: MasterSwitchChanged) -> None:
self.query_one("#slave-switch").value = event.value
class CoupledSwitchesApp(App[None]):
CSS = """
Screen {
align: center top;
}
"""
SCREENS = {"master": MasterScreen(), "slave": SlaveScreen()}
BINDINGS = [
("m", "app.switch_screen('master')", "Switch to Master"),
("s", "app.switch_screen('slave')", "Switch to Slave"),
("q", "quit", "Quit"),
]
def on_mount(self) -> None:
self.push_screen("master")
@on(Switch.Changed, "#master-switch")
def store_master_switch_value(self, event: Switch.Changed) -> None:
self.SCREENS["slave"].post_message(
SlaveScreen.MasterSwitchChanged(value=event.value)
)
CoupledSwitchesApp().run()