-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponents.py
More file actions
351 lines (291 loc) · 10.3 KB
/
components.py
File metadata and controls
351 lines (291 loc) · 10.3 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
"""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.
All visual and layout properties are passed via the ``style`` parameter,
which accepts a dict or a list of dicts (later entries override earlier).
Layout properties supported by all components::
width, height, flex, margin, min_width, max_width, min_height,
max_height, align_self
Container-specific layout properties (Column / Row)::
spacing, padding, align_items, justify_content
"""
from typing import Any, Callable, Dict, List, Optional
from .element import Element
from .style import StyleValue, resolve_style
# ======================================================================
# Leaf components
# ======================================================================
def Text(
text: str = "",
*,
style: StyleValue = None,
key: Optional[str] = None,
) -> Element:
"""Display text.
Style properties: ``font_size``, ``color``, ``bold``, ``text_align``,
``background_color``, ``max_lines``, plus common layout props.
"""
props: Dict[str, Any] = {"text": text}
props.update(resolve_style(style))
return Element("Text", props, [], key=key)
def Button(
title: str = "",
*,
on_click: Optional[Callable[[], None]] = None,
enabled: bool = True,
style: StyleValue = None,
key: Optional[str] = None,
) -> Element:
"""Create a tappable button.
Style properties: ``color``, ``background_color``, ``font_size``,
plus common layout props.
"""
props: Dict[str, Any] = {"title": title}
if on_click is not None:
props["on_click"] = on_click
if not enabled:
props["enabled"] = False
props.update(resolve_style(style))
return Element("Button", props, [], key=key)
def TextInput(
*,
value: str = "",
placeholder: str = "",
on_change: Optional[Callable[[str], None]] = None,
secure: bool = False,
style: StyleValue = None,
key: Optional[str] = None,
) -> Element:
"""Create a single-line text entry field.
Style properties: ``font_size``, ``color``, ``background_color``,
plus common layout props.
"""
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
props.update(resolve_style(style))
return Element("TextInput", props, [], key=key)
def Image(
source: str = "",
*,
scale_type: Optional[str] = None,
style: StyleValue = None,
key: Optional[str] = None,
) -> Element:
"""Display an image from a resource path or URL.
Style properties: ``background_color``, plus common layout props.
"""
props: Dict[str, Any] = {}
if source:
props["source"] = source
if scale_type is not None:
props["scale_type"] = scale_type
props.update(resolve_style(style))
return Element("Image", props, [], key=key)
def Switch(
*,
value: bool = False,
on_change: Optional[Callable[[bool], None]] = None,
style: StyleValue = 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
props.update(resolve_style(style))
return Element("Switch", props, [], key=key)
def ProgressBar(
*,
value: float = 0.0,
style: StyleValue = None,
key: Optional[str] = None,
) -> Element:
"""Show determinate progress (0.0 – 1.0)."""
props: Dict[str, Any] = {"value": value}
props.update(resolve_style(style))
return Element("ProgressBar", props, [], key=key)
def ActivityIndicator(
*,
animating: bool = True,
style: StyleValue = None,
key: Optional[str] = None,
) -> Element:
"""Show an indeterminate loading spinner."""
props: Dict[str, Any] = {"animating": animating}
props.update(resolve_style(style))
return Element("ActivityIndicator", props, [], key=key)
def WebView(
*,
url: str = "",
style: StyleValue = None,
key: Optional[str] = None,
) -> Element:
"""Embed web content."""
props: Dict[str, Any] = {}
if url:
props["url"] = url
props.update(resolve_style(style))
return Element("WebView", props, [], key=key)
def Spacer(
*,
size: Optional[float] = None,
flex: Optional[float] = None,
key: Optional[str] = None,
) -> Element:
"""Insert empty space with an optional fixed size or flex weight."""
props: Dict[str, Any] = {}
if size is not None:
props["size"] = size
if flex is not None:
props["flex"] = flex
return Element("Spacer", props, [], key=key)
def Slider(
*,
value: float = 0.0,
min_value: float = 0.0,
max_value: float = 1.0,
on_change: Optional[Callable[[float], None]] = None,
style: StyleValue = None,
key: Optional[str] = None,
) -> Element:
"""Continuous value slider."""
props: Dict[str, Any] = {
"value": value,
"min_value": min_value,
"max_value": max_value,
}
if on_change is not None:
props["on_change"] = on_change
props.update(resolve_style(style))
return Element("Slider", props, [], key=key)
# ======================================================================
# Container components
# ======================================================================
def Column(
*children: Element,
style: StyleValue = None,
key: Optional[str] = None,
) -> Element:
"""Arrange children vertically.
Style properties: ``spacing``, ``padding``, ``align_items``,
``justify_content``, ``background_color``, plus common layout props.
``align_items`` controls cross-axis (horizontal) alignment:
``"stretch"`` (default), ``"flex_start"``/``"leading"``,
``"center"``, ``"flex_end"``/``"trailing"``.
``justify_content`` controls main-axis (vertical) distribution:
``"flex_start"`` (default), ``"center"``, ``"flex_end"``,
``"space_between"``, ``"space_around"``, ``"space_evenly"``.
"""
props: Dict[str, Any] = {}
props.update(resolve_style(style))
return Element("Column", props, list(children), key=key)
def Row(
*children: Element,
style: StyleValue = None,
key: Optional[str] = None,
) -> Element:
"""Arrange children horizontally.
Style properties: ``spacing``, ``padding``, ``align_items``,
``justify_content``, ``background_color``, plus common layout props.
``align_items`` controls cross-axis (vertical) alignment:
``"stretch"`` (default), ``"flex_start"``/``"top"``,
``"center"``, ``"flex_end"``/``"bottom"``.
``justify_content`` controls main-axis (horizontal) distribution:
``"flex_start"`` (default), ``"center"``, ``"flex_end"``,
``"space_between"``, ``"space_around"``, ``"space_evenly"``.
"""
props: Dict[str, Any] = {}
props.update(resolve_style(style))
return Element("Row", props, list(children), key=key)
def ScrollView(
child: Optional[Element] = None,
*,
style: StyleValue = None,
key: Optional[str] = None,
) -> Element:
"""Wrap a single child in a scrollable container."""
children = [child] if child is not None else []
props: Dict[str, Any] = {}
props.update(resolve_style(style))
return Element("ScrollView", props, children, key=key)
def View(
*children: Element,
style: StyleValue = None,
key: Optional[str] = None,
) -> Element:
"""Generic container view (``UIView`` / ``android.view.View``)."""
props: Dict[str, Any] = {}
props.update(resolve_style(style))
return Element("View", props, list(children), key=key)
def SafeAreaView(
*children: Element,
style: StyleValue = None,
key: Optional[str] = None,
) -> Element:
"""Container that respects safe area insets (notch, status bar)."""
props: Dict[str, Any] = {}
props.update(resolve_style(style))
return Element("SafeAreaView", props, list(children), key=key)
def Modal(
*children: Element,
visible: bool = False,
on_dismiss: Optional[Callable[[], None]] = None,
title: Optional[str] = None,
style: StyleValue = None,
key: Optional[str] = None,
) -> Element:
"""Overlay modal dialog.
The modal is shown when ``visible=True`` and hidden when ``False``.
"""
props: Dict[str, Any] = {"visible": visible}
if on_dismiss is not None:
props["on_dismiss"] = on_dismiss
if title is not None:
props["title"] = title
props.update(resolve_style(style))
return Element("Modal", props, list(children), key=key)
def Pressable(
child: Optional[Element] = None,
*,
on_press: Optional[Callable[[], None]] = None,
on_long_press: Optional[Callable[[], None]] = None,
key: Optional[str] = None,
) -> Element:
"""Wrapper that adds press handling to any child element."""
props: Dict[str, Any] = {}
if on_press is not None:
props["on_press"] = on_press
if on_long_press is not None:
props["on_long_press"] = on_long_press
children = [child] if child is not None else []
return Element("Pressable", props, children, key=key)
def FlatList(
*,
data: Optional[List[Any]] = None,
render_item: Optional[Callable[[Any, int], Element]] = None,
key_extractor: Optional[Callable[[Any, int], str]] = None,
separator_height: float = 0,
style: StyleValue = None,
key: Optional[str] = None,
) -> Element:
"""Scrollable list that renders items from *data* using *render_item*.
Each item is rendered by calling ``render_item(item, index)``. If
``key_extractor`` is provided, it is called as ``key_extractor(item, index)``
to produce a stable key for each child element.
"""
items: List[Element] = []
for i, item in enumerate(data or []):
el = render_item(item, i) if render_item else Text(str(item))
if key_extractor is not None:
el = Element(el.type, el.props, el.children, key=key_extractor(item, i))
items.append(el)
inner = Column(*items, style={"spacing": separator_height} if separator_height else None)
sv_props: Dict[str, Any] = {}
sv_props.update(resolve_style(style))
return Element("ScrollView", sv_props, [inner], key=key)