-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidgets.py
138 lines (109 loc) · 3.93 KB
/
widgets.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from textual.app import ComposeResult, Widget
from textual.widgets import (
Input,
RadioButton,
RadioSet,
Select,
SelectionList,
Switch,
)
from functions import SettingsMixin
class MyInput(Input, SettingsMixin):
def __init__(self, options, *args, **kwargs):
super().__init__(*args, **kwargs)
namespace, key = self.name.split(".")
self.value = self.read_settings(key=key, namespace=namespace)
def on_mount(self):
print(self.name)
def on_input_submitted(self, event):
namespace, key = self.name.split(".")
self.write_settings(key=key, value=event.value, namespace=namespace)
self.notify("Saved.")
class MySwitch(Switch, SettingsMixin):
def __init__(self, options, *args, **kwargs):
super().__init__(*args, **kwargs)
namespace, key = self.name.split(".")
self.value = self.read_settings(key=key, namespace=namespace)
def on_mount(self):
print(self.name)
def on_switch_changed(self, event):
namespace, key = self.name.split(".")
self.write_settings(key=key, value=event.value, namespace=namespace)
self.notify("Saved.")
class MyRadioSet(Widget, SettingsMixin):
def __init__(
self,
options: list,
*args,
**kwargs,
):
super().__init__(*args, **kwargs)
namespace, key = self.name.split(".")
self.value = self.read_settings(key=key, namespace=namespace)
self.options = options
def compose(self) -> ComposeResult:
with RadioSet():
index = self.options.index(self.value)
for e, i in enumerate(self.options):
if e == index:
yield RadioButton(str(i), value=True)
else:
yield RadioButton(str(i))
def on_mount(self):
print(self.name)
def on_radio_set_changed(self, event: RadioSet.Changed) -> None:
namespace, key = self.name.split(".")
self.write_settings(
key=key,
value=self.options[event.radio_set.pressed_index],
namespace=namespace,
)
self.notify("Saved.")
class MySelectionList(Widget, SettingsMixin):
def __init__(
self,
options: list,
*args,
**kwargs,
):
super().__init__(*args, **kwargs)
namespace, key = self.name.split(".")
self.value = self.read_settings(key=key, namespace=namespace)
self.options = options
def compose(self) -> ComposeResult:
indexes = [self.options.index(i) for i in self.value]
selection_list = []
for e, i in enumerate(self.options):
if e in indexes:
selection_list.append((str(i), e, True))
else:
selection_list.append((str(i), e))
yield SelectionList(*selection_list)
def on_mount(self):
print(self.name)
def on_selection_list_selected_changed(self):
namespace, key = self.name.split(".")
value = [self.options[i] for i in self.query_one(SelectionList).selected]
self.write_settings(
key=key,
value=value,
namespace=namespace,
)
self.notify("Saved.")
class MySelect(Select, SettingsMixin):
def __init__(self, options: list, *args, **kwargs):
options = [(str(option), option) for option in options]
super().__init__(*args, options=options, **kwargs)
namespace, key = self.name.split(".")
self.value = self.read_settings(key=key, namespace=namespace)
self.prompt = str(self.value)
def on_mount(self):
print(self.name)
def on_show(self):
self.prompt = "Select"
def on_select_changed(self, event: Select.Changed):
if event.value == Select.BLANK:
return
namespace, key = self.name.split(".")
self.write_settings(key=key, value=event.value, namespace=namespace)
self.notify("Saved.")