-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponents.py
More file actions
241 lines (205 loc) · 6.6 KB
/
components.py
File metadata and controls
241 lines (205 loc) · 6.6 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"""Built-in element-creating functions for declarative UI composition.
Each function returns an :class:`Element` describing a native UI widget.
These are pure data — no native views are created until the reconciler
mounts the element tree.
Naming follows React Native conventions:
- ``Text`` (was *Label*)
- ``Button``
- ``Column`` / ``Row`` (was *StackView* vertical/horizontal)
- ``ScrollView``
- ``TextInput`` (was *TextField*)
- ``Image`` (was *ImageView*)
- ``Switch``
- ``ProgressBar`` (was *ProgressView*)
- ``ActivityIndicator`` (was *ActivityIndicatorView*)
- ``WebView``
- ``Spacer`` (new)
"""
from typing import Any, Callable, Dict, Optional, Union
from .element import Element
def _filter_none(**kwargs: Any) -> Dict[str, Any]:
"""Return *kwargs* with ``None``-valued entries removed."""
return {k: v for k, v in kwargs.items() if v is not None}
# ---------------------------------------------------------------------------
# Leaf components
# ---------------------------------------------------------------------------
def Text(
text: str = "",
*,
font_size: Optional[float] = None,
color: Optional[str] = None,
bold: bool = False,
text_align: Optional[str] = None,
background_color: Optional[str] = None,
max_lines: Optional[int] = None,
key: Optional[str] = None,
) -> Element:
"""Display text."""
props = _filter_none(
text=text,
font_size=font_size,
color=color,
bold=bold or None,
text_align=text_align,
background_color=background_color,
max_lines=max_lines,
)
return Element("Text", props, [], key=key)
def Button(
title: str = "",
*,
on_click: Optional[Callable[[], None]] = None,
color: Optional[str] = None,
background_color: Optional[str] = None,
font_size: Optional[float] = None,
enabled: bool = True,
key: Optional[str] = None,
) -> Element:
"""Create a tappable button."""
props: Dict[str, Any] = {"title": title}
if on_click is not None:
props["on_click"] = on_click
if color is not None:
props["color"] = color
if background_color is not None:
props["background_color"] = background_color
if font_size is not None:
props["font_size"] = font_size
if not enabled:
props["enabled"] = False
return Element("Button", props, [], key=key)
def TextInput(
*,
value: str = "",
placeholder: str = "",
on_change: Optional[Callable[[str], None]] = None,
secure: bool = False,
font_size: Optional[float] = None,
color: Optional[str] = None,
background_color: Optional[str] = None,
key: Optional[str] = None,
) -> Element:
"""Create a single-line text entry field."""
props: Dict[str, Any] = {"value": value}
if placeholder:
props["placeholder"] = placeholder
if on_change is not None:
props["on_change"] = on_change
if secure:
props["secure"] = True
if font_size is not None:
props["font_size"] = font_size
if color is not None:
props["color"] = color
if background_color is not None:
props["background_color"] = background_color
return Element("TextInput", props, [], key=key)
def Image(
source: str = "",
*,
width: Optional[float] = None,
height: Optional[float] = None,
scale_type: Optional[str] = None,
background_color: Optional[str] = None,
key: Optional[str] = None,
) -> Element:
"""Display an image from a resource path or URL."""
props = _filter_none(
source=source or None,
width=width,
height=height,
scale_type=scale_type,
background_color=background_color,
)
return Element("Image", props, [], key=key)
def Switch(
*,
value: bool = False,
on_change: Optional[Callable[[bool], None]] = None,
key: Optional[str] = None,
) -> Element:
"""Create a toggle switch."""
props: Dict[str, Any] = {"value": value}
if on_change is not None:
props["on_change"] = on_change
return Element("Switch", props, [], key=key)
def ProgressBar(
*,
value: float = 0.0,
background_color: Optional[str] = None,
key: Optional[str] = None,
) -> Element:
"""Show determinate progress (0.0 – 1.0)."""
props = _filter_none(value=value, background_color=background_color)
return Element("ProgressBar", props, [], key=key)
def ActivityIndicator(
*,
animating: bool = True,
key: Optional[str] = None,
) -> Element:
"""Show an indeterminate loading spinner."""
return Element("ActivityIndicator", {"animating": animating}, [], key=key)
def WebView(
*,
url: str = "",
key: Optional[str] = None,
) -> Element:
"""Embed web content."""
props: Dict[str, Any] = {}
if url:
props["url"] = url
return Element("WebView", props, [], key=key)
def Spacer(
*,
size: Optional[float] = None,
key: Optional[str] = None,
) -> Element:
"""Insert empty space with an optional fixed size."""
props = _filter_none(size=size)
return Element("Spacer", props, [], key=key)
# ---------------------------------------------------------------------------
# Container components
# ---------------------------------------------------------------------------
PaddingValue = Union[int, float, Dict[str, Union[int, float]]]
def Column(
*children: Element,
spacing: float = 0,
padding: Optional[PaddingValue] = None,
alignment: Optional[str] = None,
background_color: Optional[str] = None,
key: Optional[str] = None,
) -> Element:
"""Arrange children vertically."""
props = _filter_none(
spacing=spacing or None,
padding=padding,
alignment=alignment,
background_color=background_color,
)
return Element("Column", props, list(children), key=key)
def Row(
*children: Element,
spacing: float = 0,
padding: Optional[PaddingValue] = None,
alignment: Optional[str] = None,
background_color: Optional[str] = None,
key: Optional[str] = None,
) -> Element:
"""Arrange children horizontally."""
props = _filter_none(
spacing=spacing or None,
padding=padding,
alignment=alignment,
background_color=background_color,
)
return Element("Row", props, list(children), key=key)
def ScrollView(
child: Optional[Element] = None,
*,
background_color: Optional[str] = None,
key: Optional[str] = None,
) -> Element:
"""Wrap a single child in a scrollable container."""
children = [child] if child is not None else []
props = _filter_none(background_color=background_color)
return Element("ScrollView", props, children, key=key)