FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnative/src/pythonnative/components/controls.py at main · pythonnative/pythonnative · GitHub
pythonnative
/
pythonnative
Public
Notifications
You must be signed in to change notification settings
Fork
23
Star
131
Code
Issues
11
Pull requests
4
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnative
/
src
/
pythonnative
/
components
/
controls.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
487 lines (431 loc) · 15.9 KB
Breadcrumbs
pythonnative
/
src
/
pythonnative
/
components
/
controls.py
Copy path
File metadata and controls
487 lines (431 loc) · 15.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
"""Form controls and side-effect elements.
``Switch``, ``Slider``, ``ProgressBar``, ``ActivityIndicator``,
``Checkbox``, ``SegmentedControl``, ``DatePicker``, ``Picker``, the
``RefreshControl``, and ``StatusBar``.
"""
from
typing
import
Any
,
Callable
,
Dict
,
List
,
Literal
,
Optional
,
Unpack
from
..
element
import
Element
from
..
hooks
import
Ref
from
..
style
import
(
Color
,
StyleProp
,
)
from
.
_base
import
REFRESH_CONTROL_TYPE
,
_make_element
from
.
props
import
AccessibilityProps
def
Switch
(
*
,
value
:
bool
=
False
,
on_change
:
Optional
[
Callable
[[
bool
],
Any
]]
=
None
,
disabled
:
bool
=
False
,
on_tint_color
:
Optional
[
Color
]
=
None
,
thumb_color
:
Optional
[
Color
]
=
None
,
style
:
StyleProp
=
None
,
key
:
Optional
[
str
]
=
None
,
**
props
:
Unpack
[
AccessibilityProps
],
)
->
Element
:
"""Display a toggle switch.
Args:
value: Current on/off state.
on_change: Callback invoked with the new boolean state.
disabled: Prevent changes while keeping the switch visible.
on_tint_color: Track color when the switch is on.
thumb_color: Thumb color, or the platform default.
style: Style dict (or list of dicts).
key: Stable identity for keyed reconciliation.
**props: Shared accessibility and test keywords; see
[`AccessibilityProps`][pythonnative.AccessibilityProps].
Returns:
An [`Element`][pythonnative.Element] of type ``"Switch"``.
"""
return
_make_element
(
"Switch"
,
style
=
style
,
key
=
key
,
value
=
value
,
on_change
=
on_change
,
disabled
=
disabled
,
on_tint_color
=
on_tint_color
,
thumb_color
=
thumb_color
,
**
props
,
)
def
Slider
(
*
,
value
:
float
=
0.0
,
min_value
:
float
=
0.0
,
max_value
:
float
=
1.0
,
on_change
:
Optional
[
Callable
[[
float
],
Any
]]
=
None
,
disabled
:
bool
=
False
,
step
:
float
=
0.0
,
minimum_track_color
:
Optional
[
Color
]
=
None
,
maximum_track_color
:
Optional
[
Color
]
=
None
,
thumb_color
:
Optional
[
Color
]
=
None
,
on_sliding_start
:
Optional
[
Callable
[[
float
],
Any
]]
=
None
,
on_sliding_complete
:
Optional
[
Callable
[[
float
],
Any
]]
=
None
,
style
:
StyleProp
=
None
,
key
:
Optional
[
str
]
=
None
,
**
props
:
Unpack
[
AccessibilityProps
],
)
->
Element
:
"""Continuous-value slider between ``min_value`` and ``max_value``.
Args:
value: Current slider value.
min_value: Lower bound.
max_value: Upper bound.
on_change: Callback invoked with the new value as the user
drags.
disabled: Prevent dragging the slider.
step: Snap values to this increment, or zero for continuous movement.
minimum_track_color: Filled track color.
maximum_track_color: Unfilled track color.
thumb_color: Thumb color, or the platform default.
on_sliding_start: Callback when the user starts dragging.
on_sliding_complete: Callback when the user finishes dragging.
style: Style dict (or list of dicts).
key: Stable identity for keyed reconciliation.
**props: Shared accessibility and test keywords; see
[`AccessibilityProps`][pythonnative.AccessibilityProps].
Returns:
An [`Element`][pythonnative.Element] of type ``"Slider"``.
"""
return
_make_element
(
"Slider"
,
style
=
style
,
key
=
key
,
value
=
value
,
min_value
=
min_value
,
max_value
=
max_value
,
on_change
=
on_change
,
disabled
=
disabled
,
step
=
step
,
minimum_track_color
=
minimum_track_color
,
maximum_track_color
=
maximum_track_color
,
thumb_color
=
thumb_color
,
on_sliding_start
=
on_sliding_start
,
on_sliding_complete
=
on_sliding_complete
,
**
props
,
)
def
ProgressBar
(
*
,
value
:
float
=
0.0
,
color
:
Optional
[
Color
]
=
None
,
track_color
:
Optional
[
Color
]
=
None
,
indeterminate
:
bool
=
False
,
style
:
StyleProp
=
None
,
key
:
Optional
[
str
]
=
None
,
)
->
Element
:
"""Show determinate progress as a value between ``0.0`` and ``1.0``.
For a spinner instead of a bar, use
[`ActivityIndicator`][pythonnative.ActivityIndicator]; for an
indeterminate *bar* pass ``indeterminate=True``.
Args:
value: Fraction complete (clamped to ``[0.0, 1.0]`` by the
platform handler).
color: Color of the filled portion of the bar.
track_color: Color of the unfilled track behind the fill.
indeterminate: When ``True``, the bar animates continuously and
``value`` is ignored.
style: Style dict (or list of dicts).
key: Stable identity for keyed reconciliation.
Returns:
An [`Element`][pythonnative.Element] of type ``"ProgressBar"``.
"""
return
_make_element
(
"ProgressBar"
,
style
=
style
,
key
=
key
,
value
=
value
,
color
=
color
,
track_color
=
track_color
,
indeterminate
=
indeterminate
or
None
,
)
def
ActivityIndicator
(
*
,
animating
:
bool
=
True
,
color
:
Optional
[
Color
]
=
None
,
size
:
Literal
[
"small"
,
"large"
]
=
"small"
,
style
:
StyleProp
=
None
,
key
:
Optional
[
str
]
=
None
,
)
->
Element
:
"""Show an indeterminate loading spinner.
Args:
animating: When ``False``, the spinner is hidden.
color: Spinner color.
size: ``"small"`` (default) or ``"large"``.
style: Style dict (or list of dicts).
key: Stable identity for keyed reconciliation.
Returns:
An [`Element`][pythonnative.Element] of type
``"ActivityIndicator"``.
"""
return
_make_element
(
"ActivityIndicator"
,
style
=
style
,
key
=
key
,
animating
=
animating
,
color
=
color
,
size
=
size
,
)
def
Checkbox
(
*
,
value
:
bool
=
False
,
on_change
:
Optional
[
Callable
[[
bool
],
Any
]]
=
None
,
label
:
Optional
[
str
]
=
None
,
disabled
:
bool
=
False
,
color
:
Optional
[
Color
]
=
None
,
style
:
StyleProp
=
None
,
key
:
Optional
[
str
]
=
None
,
**
props
:
Unpack
[
AccessibilityProps
],
)
->
Element
:
"""A boolean checkbox with an optional inline label.
Backed by ``android.widget.CheckBox`` on Android and a checkmark
``UIButton`` on iOS. Tapping the control (or its label) toggles the
value and fires ``on_change(new_value)``.
Args:
value: Current checked state.
on_change: Callback invoked with the new boolean state.
label: Optional text shown beside the box (also tappable).
disabled: When ``True``, the control is greyed out and inert.
color: Tint applied to the checked box.
style: Style dict (or list of dicts).
key: Stable identity for keyed reconciliation.
**props: Shared accessibility and test keywords; see
[`AccessibilityProps`][pythonnative.AccessibilityProps].
Returns:
An [`Element`][pythonnative.Element] of type ``"Checkbox"``.
"""
return
_make_element
(
"Checkbox"
,
style
=
style
,
key
=
key
,
value
=
value
,
on_change
=
on_change
,
label
=
label
,
disabled
=
disabled
or
None
,
color
=
color
,
_defaults
=
{
"accessibility_role"
:
"checkbox"
},
**
props
,
)
def
SegmentedControl
(
*
,
segments
:
Optional
[
List
[
str
]]
=
None
,
selected_index
:
int
=
0
,
on_change
:
Optional
[
Callable
[[
int
],
Any
]]
=
None
,
disabled
:
bool
=
False
,
tint_color
:
Optional
[
Color
]
=
None
,
style
:
StyleProp
=
None
,
key
:
Optional
[
str
]
=
None
,
**
props
:
Unpack
[
AccessibilityProps
],
)
->
Element
:
"""A horizontal multi-choice control (one selected segment at a time).
Backed by ``UISegmentedControl`` on iOS and a styled toggle row on
Android. Selecting a segment fires ``on_change(index)``.
Args:
segments: Ordered list of segment labels.
selected_index: Index of the currently selected segment.
on_change: Callback invoked with the newly selected index.
disabled: When ``True``, the control is disabled.
tint_color: Accent color for the selected segment.
style: Style dict (or list of dicts).
key: Stable identity for keyed reconciliation.
**props: Shared accessibility and test keywords; see
[`AccessibilityProps`][pythonnative.AccessibilityProps].
Returns:
An [`Element`][pythonnative.Element] of type
``"SegmentedControl"``.
"""
return
_make_element
(
"SegmentedControl"
,
style
=
style
,
key
=
key
,
segments
=
list
(
segments
)
if
segments
is
not
None
else
[],
selected_index
=
selected_index
,
on_change
=
on_change
,
disabled
=
disabled
or
None
,
tint_color
=
tint_color
,
**
props
,
)
def
DatePicker
(
*
,
value
:
Optional
[
str
]
=
None
,
mode
:
Literal
[
"date"
,
"time"
,
"datetime"
]
=
"date"
,
on_change
:
Optional
[
Callable
[[
str
],
Any
]]
=
None
,
minimum
:
Optional
[
str
]
=
None
,
maximum
:
Optional
[
str
]
=
None
,
disabled
:
bool
=
False
,
style
:
StyleProp
=
None
,
key
:
Optional
[
str
]
=
None
,
**
props
:
Unpack
[
AccessibilityProps
],
)
->
Element
:
"""A native date / time picker.
Backed by ``UIDatePicker`` on iOS and a trigger button that opens
the platform ``DatePickerDialog`` / ``TimePickerDialog`` on
Android. ``value`` and the value reported to ``on_change`` are
ISO-8601 strings (``"2026-05-31"`` for ``mode="date"``, ``"14:30"``
for ``mode="time"``, ``"2026-05-31T14:30"`` for
``mode="datetime"``), so values stay JSON-serializable and
platform-agnostic.
Args:
value: Currently selected value as an ISO-8601 string.
mode: ``"date"`` (default), ``"time"``, or ``"datetime"``.
on_change: Callback invoked with the new ISO-8601 string.
minimum: Earliest selectable value (ISO-8601), if any.
maximum: Latest selectable value (ISO-8601), if any.
disabled: When ``True``, the picker is disabled.
style: Style dict (or list of dicts).
key: Stable identity for keyed reconciliation.
**props: Shared accessibility and test keywords; see
[`AccessibilityProps`][pythonnative.AccessibilityProps].
Returns:
An [`Element`][pythonnative.Element] of type ``"DatePicker"``.
"""
return
_make_element
(
"DatePicker"
,
style
=
style
,
key
=
key
,
value
=
value
,
mode
=
mode
,
on_change
=
on_change
,
minimum
=
minimum
,
maximum
=
maximum
,
disabled
=
disabled
or
None
,
_defaults
=
{
"accessibility_role"
:
"button"
},
**
props
,
)
def
Picker
(
*
,
value
:
Any
=
None
,
disabled
:
bool
=
False
,
items
:
Optional
[
List
[
Dict
[
str
,
Any
]]]
=
None
,
on_change
:
Optional
[
Callable
[[
Any
],
Any
]]
=
None
,
placeholder
:
str
=
"Select…"
,
style
:
StyleProp
=
None
,
ref
:
Optional
[
Ref
]
=
None
,
key
:
Optional
[
str
]
=
None
,
**
props
:
Unpack
[
AccessibilityProps
],
)
->
Element
:
"""A real native dropdown / select widget.
Renders a tappable trigger labelled with the selected item; the
iOS handler attaches a ``UIMenu`` (system dropdown) and the Android
handler uses a native ``Spinner``. Selecting an item fires
``on_change(value)``.
``items`` is an ordered list of ``{"value": Any, "label": str}``
entries (``label`` defaults to ``str(value)`` when omitted).
Args:
disabled: Whether the control ignores user input.
value: Currently selected value (matched against
``items[i]["value"]``).
items: Selectable options.
on_change: Callback invoked with the new value.
placeholder: Label shown when no item matches ``value``.
style: Style dict applied to the trigger.
ref: Optional [`Ref`][pythonnative.Ref] from ``use_ref()``.
key: Stable identity for keyed reconciliation.
**props: Shared accessibility and test keywords; see
[`AccessibilityProps`][pythonnative.AccessibilityProps].
Returns:
An [`Element`][pythonnative.Element] of type ``"Picker"``.
"""
return
_make_element
(
"Picker"
,
style
=
style
,
ref
=
ref
,
key
=
key
,
value
=
value
,
disabled
=
disabled
or
None
,
items
=
list
(
items
)
if
items
is
not
None
else
[],
on_change
=
on_change
,
placeholder
=
placeholder
,
_defaults
=
{
"accessibility_role"
:
"button"
},
**
props
,
)
def
RefreshControl
(
*
,
refreshing
:
bool
=
False
,
on_refresh
:
Optional
[
Callable
[[],
Any
]]
=
None
,
tint_color
:
Optional
[
Color
]
=
None
,
)
->
Element
:
"""Pull-to-refresh control for [`ScrollView`][pythonnative.ScrollView] and the list components.
Pass the result as the ``refresh_control=`` prop of a
[`ScrollView`][pythonnative.ScrollView],
[`FlatList`][pythonnative.FlatList], or
[`SectionList`][pythonnative.SectionList]. It is a regular
[`Element`][pythonnative.Element] (type ``"RefreshControl"``) built
like every other piece of UI; the scroll container attaches it to
its native scroll view rather than rendering it as a child, and
rejects anything else with a ``TypeError``.
Args:
refreshing: Drive the spinner's visibility from a use_state
value.
on_refresh: Callback invoked when the user pulls down past the
threshold. Set ``refreshing`` to ``True`` for the duration
of the work, then back to ``False`` on completion.
tint_color: Color of the spinner.
Returns:
An [`Element`][pythonnative.Element] of type ``"RefreshControl"``.
Example:
```python
import pythonnative as pn
@pn.component
def MyList():
refreshing, set_refreshing = pn.use_state(False)
def reload():
set_refreshing(True)
# ... fetch data ...
set_refreshing(False)
return pn.ScrollView(
pn.Text("Pull me!"),
refresh_control=pn.RefreshControl(
refreshing=refreshing, on_refresh=reload
),
)
```
"""
return
_make_element
(
REFRESH_CONTROL_TYPE
,
refreshing
=
bool
(
refreshing
),
on_refresh
=
on_refresh
,
tint_color
=
tint_color
,
)
def
StatusBar
(
*
,
bar_style
:
Optional
[
Literal
[
"light"
,
"dark"
,
"default"
]]
=
None
,
background_color
:
Optional
[
Color
]
=
None
,
hidden
:
Optional
[
bool
]
=
None
,
translucent
:
bool
=
False
,
animated
:
bool
=
False
,
key
:
Optional
[
str
]
=
None
,
)
->
Element
:
"""Configure the device's status bar appearance.
StatusBar is a side-effect element: it doesn't render any visible
content but applies its props to the host platform's status bar.
Mount one near the top of your tree.
The ``bar_style`` parameter is named separately from the universal
``style`` kwarg (which is unused here) to avoid the conflict that
``style="light"`` would create with the visual-style dict used
elsewhere.
Args:
bar_style: ``"light"`` (light icons over dark backgrounds),
``"dark"`` (dark icons over light backgrounds), or
``"default"`` (system default).
background_color: Color of the status-bar background (Android
only; iOS draws the bar transparent over your content).
hidden: When ``True``, the status bar is hidden.
translucent: Draw your content under the status bar instead of
below it (Android only; iOS always draws underneath).
animated: Animate ``bar_style`` and ``hidden`` changes (iOS
only).
key: Stable identity for keyed reconciliation.
Returns:
An [`Element`][pythonnative.Element] of type ``"StatusBar"``.
"""
props
:
Dict
[
str
,
Any
]
=
{}
if
bar_style
is
not
None
:
props
[
"bar_style"
]
=
bar_style
if
background_color
is
not
None
:
props
[
"background_color"
]
=
background_color
if
hidden
is
not
None
:
props
[
"hidden"
]
=
hidden
if
translucent
:
props
[
"translucent"
]
=
True
if
animated
:
props
[
"animated"
]
=
True
return
Element
(
"StatusBar"
,
props
, [],
key
=
key
)
Back
|
FazBrowse Home
|
New Git URL