-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks.py
More file actions
1177 lines (896 loc) · 37.1 KB
/
hooks.py
File metadata and controls
1177 lines (896 loc) · 37.1 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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Hook primitives for function components.
Provides React-like hooks for managing state, effects, memoization,
context, and navigation within function components decorated with
[`component`][pythonnative.component]. Hooks must be called at the top
level of a component (not inside conditionals or loops) so they map to
the same slot across renders.
Effects are queued during the render phase and flushed *after* the
reconciler commits native-view mutations. This ordering guarantees that
effect callbacks can safely measure layout or interact with the
committed native tree.
Example:
```python
import pythonnative as pn
@pn.component
def Counter(initial=0):
count, set_count = pn.use_state(initial)
return pn.Column(
pn.Text(f"Count: {count}"),
pn.Button("+", on_click=lambda: set_count(count + 1)),
)
```
"""
import asyncio
import inspect
import threading
from contextlib import contextmanager
from dataclasses import dataclass, field, replace
from typing import Any, Awaitable, Callable, Dict, Generator, Generic, List, Optional, Tuple, TypeVar
from .element import Element
T = TypeVar("T")
_SENTINEL = object()
_hook_context: threading.local = threading.local()
_batch_context: threading.local = threading.local()
# ======================================================================
# Hook state container
# ======================================================================
class HookState:
"""Per-instance storage for one component's hooks.
Each `@component` instance owns one `HookState`. Hooks are matched
to slots by call order, so they must always be called in the same
order across renders. Effects scheduled during render are deferred
into `_pending_effects` and flushed after the reconciler commits
native mutations, which guarantees effect callbacks can safely
interact with the committed native tree.
Attributes:
states: One entry per `use_state` / `use_reducer` call.
effects: One `(deps, cleanup)` tuple per `use_effect` call.
memos: One `(deps, value)` tuple per `use_memo` / `use_callback`.
refs: One mutable dict per `use_ref` call.
"""
__slots__ = (
"states",
"effects",
"memos",
"refs",
"state_index",
"effect_index",
"memo_index",
"ref_index",
"_trigger_render",
"_pending_effects",
"_dirty",
)
def __init__(self) -> None:
self.states: List[Any] = []
self.effects: List[Tuple[Any, Any]] = []
self.memos: List[Tuple[Any, Any]] = []
self.refs: List[dict] = []
self.state_index: int = 0
self.effect_index: int = 0
self.memo_index: int = 0
self.ref_index: int = 0
self._trigger_render: Optional[Callable[[], None]] = None
self._pending_effects: List[Tuple[int, Callable, Any]] = []
# Cleared by the reconciler after each successful render.
# ``use_state`` / ``use_reducer`` setters flip it to ``True``
# whenever they actually mutate state, so [`memo`][pythonnative.memo]
# knows that a memoized component still needs to re-render even
# when its props didn't change.
self._dirty: bool = False
def reset_index(self) -> None:
"""Reset every per-hook cursor to ``0``.
Called by the reconciler at the start of every render pass so
the next render reads slots in the same order they were
written.
"""
self.state_index = 0
self.effect_index = 0
self.memo_index = 0
self.ref_index = 0
def flush_pending_effects(self) -> None:
"""Run effects queued during render, after native commit.
For each pending effect, the previous cleanup is invoked first
(if any), then the new effect callback. The new return value
becomes the next cleanup.
"""
pending = self._pending_effects
self._pending_effects = []
for idx, effect_fn, deps in pending:
_, prev_cleanup = self.effects[idx]
if callable(prev_cleanup):
try:
prev_cleanup()
except Exception:
pass
cleanup = effect_fn()
self.effects[idx] = (list(deps) if deps is not None else None, cleanup)
def cleanup_all_effects(self) -> None:
"""Run every outstanding cleanup function, then clear state.
Called when the component instance is unmounted by the
reconciler.
"""
for i, (deps, cleanup) in enumerate(self.effects):
if callable(cleanup):
try:
cleanup()
except Exception:
pass
self.effects[i] = (_SENTINEL, None)
self._pending_effects = []
# ======================================================================
# Thread-local context helpers
# ======================================================================
def _get_hook_state() -> Optional[HookState]:
"""Return the active `HookState`, or `None` if no render is in flight."""
return getattr(_hook_context, "current", None)
def _set_hook_state(state: Optional[HookState]) -> None:
"""Install `state` as the active `HookState` for the current thread."""
_hook_context.current = state
def _deps_changed(prev: Any, current: Any) -> bool:
"""Return whether the dependency arrays differ enough to re-run an effect."""
if prev is _SENTINEL:
return True
if prev is None or current is None:
return True
if len(prev) != len(current):
return True
return any(p is not c and p != c for p, c in zip(prev, current))
# ======================================================================
# Batching helpers
# ======================================================================
def _schedule_trigger(trigger: Callable[[], None]) -> None:
"""Run ``trigger`` immediately, or defer it inside a `batch_updates` block."""
if getattr(_batch_context, "depth", 0) > 0:
_batch_context.pending_trigger = trigger
else:
trigger()
@contextmanager
def batch_updates() -> Generator[None, None, None]:
"""Coalesce multiple state updates into a single re-render.
State setters called inside the `with` block defer their
re-render trigger until the block exits, so any number of
`set_*` calls produce at most one render pass.
Yields:
None. The block executes normally; deferred renders fire on
exit.
Example:
```python
import pythonnative as pn
with pn.batch_updates():
set_count(1)
set_name("hello")
```
"""
depth = getattr(_batch_context, "depth", 0)
_batch_context.depth = depth + 1
if depth == 0:
_batch_context.pending_trigger = None
try:
yield
finally:
_batch_context.depth -= 1
if _batch_context.depth == 0:
trigger = _batch_context.pending_trigger
_batch_context.pending_trigger = None
if trigger is not None:
trigger()
# ======================================================================
# Public hooks
# ======================================================================
def use_state(initial: Any = None) -> Tuple[Any, Callable]:
"""Return ``(value, setter)`` for component-local state.
State persists across re-renders of the same component instance.
The setter accepts a value or a ``current -> new`` callable; calling
it with an unchanged value is a no-op (no re-render).
Args:
initial: Initial state value. If callable, it is invoked once on
the first render (lazy initialization).
Returns:
A 2-tuple ``(value, setter)`` where ``value`` is the current
state and ``setter`` updates it (and triggers a re-render).
Raises:
RuntimeError: If called outside a `@component` function.
Example:
```python
import pythonnative as pn
@pn.component
def Counter():
count, set_count = pn.use_state(0)
return pn.Button(
f"Count: {count}",
on_click=lambda: set_count(count + 1),
)
```
"""
ctx = _get_hook_state()
if ctx is None:
raise RuntimeError("use_state must be called inside a @component function")
idx = ctx.state_index
ctx.state_index += 1
if idx >= len(ctx.states):
val = initial() if callable(initial) else initial
ctx.states.append(val)
current = ctx.states[idx]
def setter(new_value: Any) -> None:
if callable(new_value):
new_value = new_value(ctx.states[idx])
if ctx.states[idx] is not new_value and ctx.states[idx] != new_value:
ctx.states[idx] = new_value
ctx._dirty = True
if ctx._trigger_render:
_schedule_trigger(ctx._trigger_render)
return current, setter
def use_reducer(reducer: Callable[[Any, Any], Any], initial_state: Any) -> Tuple[Any, Callable]:
"""Return ``(state, dispatch)`` for reducer-based state management.
A reducer is a pure function that takes the current state and an
action and returns the next state. Use it instead of
[`use_state`][pythonnative.use_state] when state transitions are
complex enough that centralizing them in one function aids
readability and testing.
Args:
reducer: ``reducer(current_state, action) -> new_state``.
The component re-renders only when `reducer` returns a
value different from the current state.
initial_state: Initial state value, or a callable invoked once
on the first render.
Returns:
A 2-tuple ``(state, dispatch)`` where `dispatch` runs the
reducer with the supplied action.
Raises:
RuntimeError: If called outside a `@component` function.
Example:
```python
import pythonnative as pn
def reducer(state, action):
if action == "increment":
return state + 1
if action == "reset":
return 0
return state
@pn.component
def Counter():
count, dispatch = pn.use_reducer(reducer, 0)
return pn.Row(
pn.Button("+", on_click=lambda: dispatch("increment")),
pn.Button("Reset", on_click=lambda: dispatch("reset")),
)
```
"""
ctx = _get_hook_state()
if ctx is None:
raise RuntimeError("use_reducer must be called inside a @component function")
idx = ctx.state_index
ctx.state_index += 1
if idx >= len(ctx.states):
val = initial_state() if callable(initial_state) else initial_state
ctx.states.append(val)
current = ctx.states[idx]
def dispatch(action: Any) -> None:
new_state = reducer(ctx.states[idx], action)
if ctx.states[idx] is not new_state and ctx.states[idx] != new_state:
ctx.states[idx] = new_state
ctx._dirty = True
if ctx._trigger_render:
_schedule_trigger(ctx._trigger_render)
return current, dispatch
def use_effect(effect: Callable, deps: Optional[list] = None) -> None:
"""Schedule a side effect to run after the native commit.
Effects are queued during the render pass and flushed once the
reconciler has finished applying all native-view mutations, which
means effect callbacks can safely measure layout or interact with
committed native views.
The `deps` argument controls when the effect re-runs:
- `None`: every render.
- `[]`: mount only.
- `[a, b]`: when `a` or `b` change (compared by identity, then `==`).
`effect` may return a cleanup callable; the previous cleanup runs
before the next effect (and on unmount).
Args:
effect: A zero-arg callable invoked after commit. Optionally
returns a cleanup callable.
deps: Dependency list, or `None` to run on every render.
Raises:
RuntimeError: If called outside a `@component` function.
Example:
```python
import pythonnative as pn
@pn.component
def Timer():
seconds, set_seconds = pn.use_state(0)
def tick():
import threading
t = threading.Timer(1.0, lambda: set_seconds(seconds + 1))
t.start()
return t.cancel
pn.use_effect(tick, [seconds])
return pn.Text(f"Elapsed: {seconds}s")
```
"""
ctx = _get_hook_state()
if ctx is None:
raise RuntimeError("use_effect must be called inside a @component function")
idx = ctx.effect_index
ctx.effect_index += 1
if idx >= len(ctx.effects):
ctx.effects.append((_SENTINEL, None))
ctx._pending_effects.append((idx, effect, deps))
return
prev_deps, _prev_cleanup = ctx.effects[idx]
if _deps_changed(prev_deps, deps):
ctx._pending_effects.append((idx, effect, deps))
def use_memo(factory: Callable[[], T], deps: list) -> T:
"""Return a memoized value that is recomputed only when `deps` change.
Use this for expensive computations whose inputs change rarely. For
cheap computations, plain inline code is faster (memoization itself
has overhead).
Args:
factory: Zero-arg callable returning the value.
deps: Dependency list. The value is recomputed when any element
differs from the previous render.
Returns:
The cached or freshly computed value.
Raises:
RuntimeError: If called outside a `@component` function.
"""
ctx = _get_hook_state()
if ctx is None:
raise RuntimeError("use_memo must be called inside a @component function")
idx = ctx.memo_index
ctx.memo_index += 1
if idx >= len(ctx.memos):
value = factory()
ctx.memos.append((list(deps), value))
return value
prev_deps, prev_value = ctx.memos[idx]
if not _deps_changed(prev_deps, deps):
return prev_value
value = factory()
ctx.memos[idx] = (list(deps), value)
return value
def use_callback(callback: Callable, deps: list) -> Callable:
"""Return a stable reference to ``callback``, refreshed when ``deps`` change.
Equivalent to `use_memo(lambda: callback, deps)`. Useful when passing
a function as a prop to a memoized child component, so the child
doesn't see a fresh function identity on every render.
Args:
callback: The callable to memoize.
deps: Dependency list controlling when the reference refreshes.
Returns:
A callable with stable identity across renders (until `deps` change).
"""
return use_memo(lambda: callback, deps)
def use_ref(initial: Any = None) -> dict:
"""Return a mutable ref dict ``{"current": initial}`` that persists across renders.
Refs are useful for storing values that must survive renders without
triggering them: timers, last-seen values, native handles, and so on.
The ``current`` key is also populated by the reconciler with the
underlying native view (`UIView` on iOS, `android.view.View` on
Android) when the ref is passed via the ``ref=`` prop on a built-in
element. This is how ``Animated.View`` obtains a handle to the
native view it animates without going through the reconciler.
Args:
initial: Value placed at `ref["current"]` on first render.
Returns:
A dict with a single `"current"` key. Mutations to the dict do
*not* trigger re-renders.
Raises:
RuntimeError: If called outside a `@component` function.
"""
ctx = _get_hook_state()
if ctx is None:
raise RuntimeError("use_ref must be called inside a @component function")
idx = ctx.ref_index
ctx.ref_index += 1
if idx >= len(ctx.refs):
ref: dict = {"current": initial}
ctx.refs.append(ref)
return ref
return ctx.refs[idx]
# ======================================================================
# Async hooks
# ======================================================================
def use_async_effect(
effect: Callable[[], Awaitable[None]],
deps: Optional[list] = None,
) -> None:
"""Schedule an async effect that's cancelled on re-run / unmount.
Like [`use_effect`][pythonnative.use_effect] but takes an
``async def`` (or any zero-arg callable returning an awaitable).
The coroutine is scheduled on the framework runtime via
[`run_async`][pythonnative.runtime.run_async] after the native
commit. When ``deps`` change (or the component unmounts), the
in-flight future is cancelled.
Args:
effect: A zero-arg callable returning an awaitable. Typically
an ``async def`` defined inside the component.
deps: Dependency list, or ``None`` to re-run on every render.
Raises:
RuntimeError: If called outside a ``@component`` function.
Example:
```python
import pythonnative as pn
@pn.component
def Posts(user_id):
posts, set_posts = pn.use_state([])
async def load():
set_posts(await api.get_posts(user_id))
pn.use_async_effect(load, [user_id])
return pn.FlatList(posts, render_item=...)
```
"""
from .runtime import run_async
def _sync_effect() -> Callable[[], None]:
future = run_async(effect())
def _cancel() -> None:
future.cancel()
return _cancel
use_effect(_sync_effect, deps)
@dataclass(frozen=True)
class QueryResult(Generic[T]):
"""Snapshot of a [`use_query`][pythonnative.use_query] subscription.
Attributes:
data: The most recent successful result, or the ``initial``
value before the first fetch completes.
loading: ``True`` while a fetch is in flight (including the
initial fetch and any refetches).
error: The exception raised by the most recent failed fetch,
or ``None`` if no fetch has failed since the last success.
refetch: A zero-arg callable that triggers a refetch. Stable
across renders.
"""
data: Optional[T] = None
loading: bool = True
error: Optional[BaseException] = None
refetch: Callable[[], None] = field(default=lambda: None)
def use_query(
fetcher: Callable[[], Awaitable[T]],
deps: Optional[list] = None,
*,
initial: Optional[T] = None,
) -> QueryResult[T]:
"""Subscribe to an async fetcher and re-render when its result changes.
The fetcher is called on mount and any time ``deps`` change, with
cancellation propagated when the component unmounts mid-fetch.
Args:
fetcher: Zero-arg ``async`` callable that resolves to the
current data.
deps: Dependency list — refetches whenever any entry changes.
initial: Optional starting value for ``data`` before the
first fetch completes.
Returns:
A frozen [`QueryResult`][pythonnative.QueryResult] with
``data`` / ``loading`` / ``error`` / ``refetch``.
Raises:
RuntimeError: If called outside a ``@component`` function.
Example:
```python
import pythonnative as pn
@pn.component
def UserCard(user_id):
q = pn.use_query(lambda: api.get_user(user_id), [user_id])
if q.loading:
return pn.Text("Loading…")
if q.error:
return pn.Text(f"Error: {q.error}")
return pn.Text(q.data["name"])
```
"""
from .runtime import run_async
state, set_state = use_state(lambda: QueryResult[T](data=initial, loading=True))
nonce, set_nonce = use_state(0)
refetch = use_callback(lambda: set_nonce(lambda n: n + 1), [])
# Surface the stable refetch callable on every returned result.
if state.refetch is not refetch:
state = replace(state, refetch=refetch)
def _start_fetch() -> Callable[[], None]:
set_state(lambda s: replace(s, loading=True, error=None))
async def _runner() -> None:
try:
data = await fetcher()
set_state(lambda s: replace(s, data=data, loading=False, error=None))
except asyncio.CancelledError:
raise
except BaseException as exc: # pragma: no cover - surfaced to user
set_state(lambda s, e=exc: replace(s, loading=False, error=e))
future = run_async(_runner())
def _cancel() -> None:
future.cancel()
return _cancel
effect_deps: List[Any] = list(deps or []) + [nonce]
use_effect(_start_fetch, effect_deps)
return state
@dataclass(frozen=True)
class MutationState(Generic[T]):
"""Snapshot of a [`use_mutation`][pythonnative.use_mutation] subscription.
Attributes:
data: The most recent successful return value of the mutator,
or ``None`` if no mutation has succeeded yet.
loading: ``True`` while a mutation is in flight.
error: The exception raised by the most recent failed
mutation, or ``None``.
"""
data: Optional[T] = None
loading: bool = False
error: Optional[BaseException] = None
class MutationCall(Generic[T]):
"""Awaitable handle returned by a mutator trigger.
Returned by the second element of the
[`use_mutation`][pythonnative.use_mutation] tuple. Awaiting the
handle resolves to the mutator's return value (or re-raises its
exception); discarding the handle is safe — Python won't warn
about an unawaited coroutine because this is a plain object.
Example:
```python
# Fire-and-forget:
save_button.on_click = lambda: mutate(post)
# Or await for the result:
async def submit():
try:
created = await mutate(post)
except ApiError as exc:
await pn.Alert.show(title="Save failed", message=str(exc))
```
"""
__slots__ = ("_future",)
def __init__(self, future: "Any") -> None:
self._future = future
def __await__(self) -> Any:
return asyncio.wrap_future(self._future).__await__()
def cancel(self) -> bool:
"""Cancel the underlying mutation. Returns whether cancellation succeeded."""
return self._future.cancel()
def done(self) -> bool:
"""Whether the underlying mutation has finished."""
return self._future.done()
def use_mutation(
mutator: Callable[..., Awaitable[T]],
) -> Tuple[MutationState[T], Callable[..., MutationCall[T]]]:
"""Wrap an async mutator with loading/error state and a trigger.
Returns ``(state, mutate)``. Call ``mutate(*args, **kwargs)`` to
invoke the mutator; ``state`` reflects loading/error/data and
re-renders on each transition. ``mutate`` returns a
[`MutationCall`][pythonnative.MutationCall] you can ``await`` for
the result, or discard for fire-and-forget.
Args:
mutator: An ``async`` callable that performs the side effect
and returns the resulting data.
Returns:
A 2-tuple ``(state, mutate)``.
Example:
```python
import pythonnative as pn
@pn.component
def NewPostForm():
state, save = pn.use_mutation(api.create_post)
return pn.Column(
pn.Button("Save", on_click=lambda: save(post)),
pn.Text("Saving…") if state.loading else pn.Text(""),
pn.Text(str(state.error)) if state.error else pn.Text(""),
)
```
"""
from .runtime import run_async
state, set_state = use_state(lambda: MutationState[T]())
def mutate(*args: Any, **kwargs: Any) -> MutationCall[T]:
set_state(lambda s: replace(s, loading=True, error=None))
async def _runner() -> T:
try:
data = await mutator(*args, **kwargs)
set_state(lambda s: replace(s, data=data, loading=False, error=None))
return data
except asyncio.CancelledError:
set_state(lambda s: replace(s, loading=False))
raise
except BaseException as exc:
set_state(lambda s, e=exc: replace(s, loading=False, error=e))
raise
future = run_async(_runner())
return MutationCall[T](future)
return state, mutate
# ======================================================================
# Platform-metric hooks (window dimensions, safe-area insets, keyboard)
# ======================================================================
def use_window_dimensions() -> Dict[str, float]:
"""Return the current viewport size and re-render when it changes.
Equivalent to React Native's ``useWindowDimensions``. The values
are pushed by the screen host whenever the platform reports a new
size (initial layout, rotation, multitasking split-view).
Returns:
A dict with ``"width"`` and ``"height"`` floats in layout
units (pt on iOS, dp on Android). Both are ``0.0`` until the
screen host has run its first layout pass.
Raises:
RuntimeError: If called outside a `@component` function.
Example:
```python
import pythonnative as pn
@pn.component
def MyView():
dims = pn.use_window_dimensions()
return pn.Text(f"{dims['width']:.0f} x {dims['height']:.0f}")
```
"""
from . import platform_metrics
ctx = _get_hook_state()
if ctx is None:
raise RuntimeError("use_window_dimensions must be called inside a @component function")
_, set_tick = use_state(0)
def subscribe() -> Callable[[], None]:
return platform_metrics.subscribe(lambda: set_tick(lambda n: n + 1))
use_effect(subscribe, [])
dims = platform_metrics.get_window_dimensions()
return {"width": dims.width, "height": dims.height}
def use_safe_area_insets() -> Dict[str, float]:
"""Return the current safe-area insets and re-render on change.
Mirrors ``react-native-safe-area-context``'s ``useSafeAreaInsets``.
Returns:
A dict with ``"top"``, ``"bottom"``, ``"left"``, and ``"right"``
floats in layout units (pt on iOS, dp on Android).
Raises:
RuntimeError: If called outside a `@component` function.
"""
from . import platform_metrics
ctx = _get_hook_state()
if ctx is None:
raise RuntimeError("use_safe_area_insets must be called inside a @component function")
_, set_tick = use_state(0)
def subscribe() -> Callable[[], None]:
return platform_metrics.subscribe(lambda: set_tick(lambda n: n + 1))
use_effect(subscribe, [])
insets = platform_metrics.get_safe_area_insets()
return {
"top": insets.top,
"bottom": insets.bottom,
"left": insets.left,
"right": insets.right,
}
def use_keyboard_height() -> float:
"""Return the on-screen keyboard height (or 0) and re-render on change.
Useful for custom layout that needs to react to keyboard
show/hide events. Most apps should use
[`KeyboardAvoidingView`][pythonnative.KeyboardAvoidingView] instead
of reading this directly.
Raises:
RuntimeError: If called outside a `@component` function.
"""
from . import platform_metrics
ctx = _get_hook_state()
if ctx is None:
raise RuntimeError("use_keyboard_height must be called inside a @component function")
_, set_tick = use_state(0)
def subscribe() -> Callable[[], None]:
return platform_metrics.subscribe(lambda: set_tick(lambda n: n + 1))
use_effect(subscribe, [])
return platform_metrics.get_keyboard_height()
# ======================================================================
# Context
# ======================================================================
class Context:
"""Container for a value shared across a subtree.
Created by [`create_context`][pythonnative.create_context]; consumed
via [`use_context`][pythonnative.use_context]. Use
[`Provider`][pythonnative.Provider] to set the value for a subtree.
Attributes:
default: The value returned when no `Provider` ancestor exists.
"""
def __init__(self, default: Any = None) -> None:
self.default = default
self._stack: List[Any] = []
def _current(self) -> Any:
return self._stack[-1] if self._stack else self.default
def create_context(default: Any = None) -> Context:
"""Create a new context with an optional default value.
Args:
default: Returned by [`use_context`][pythonnative.use_context]
when there is no enclosing
[`Provider`][pythonnative.Provider].
Returns:
A fresh `Context` instance.
Example:
```python
import pythonnative as pn
ThemeContext = pn.create_context({"primary": "#007AFF"})
```
"""
return Context(default)
def use_context(context: Context) -> Any:
"""Read the current value of `context` from the nearest `Provider`.
If no enclosing `Provider` exists, returns the context's default.
Args:
context: The `Context` to read from.
Returns:
The current value for `context`.
Raises:
RuntimeError: If called outside a `@component` function.
"""
ctx = _get_hook_state()
if ctx is None:
raise RuntimeError("use_context must be called inside a @component function")
return context._current()
# ======================================================================
# Provider element helper
# ======================================================================
def Provider(context: "Context", value: Any, *children: Element) -> Element:
"""Provide ``value`` for ``context`` to all descendants of ``children``.
Accepts any number of children (varargs). Multiple children are
grouped under an internal [`Fragment`][pythonnative.Fragment] so
they all share the same provided value without an extra wrapping
native view.
Args:
context: The [`Context`][pythonnative.hooks.Context] to set.
value: Value made available to descendants via
[`use_context`][pythonnative.use_context].
*children: Subtree(s) under which the provider applies.
Returns:
An [`Element`][pythonnative.Element] that the reconciler treats
as a context boundary.
Example:
```python
import pythonnative as pn
ThemeContext = pn.create_context({"primary": "#007AFF"})
@pn.component
def App():
return pn.Provider(
ThemeContext,
{"primary": "#FF0000"},
Header(),
Body(),
)
```
"""
if not children:
kids: List[Element] = []
elif len(children) == 1:
kids = [children[0]]
else:
kids = [Element("__Fragment__", {}, list(children))]
return Element("__Provider__", {"__context__": context, "__value__": value}, kids)
def memo(component_fn: Callable[..., Element]) -> Callable[..., Element]:
"""Skip a function component's render when its props haven't changed.
Decorate a ``@component``-wrapped function to opt into shallow-prop
memoization. When the reconciler re-renders the parent tree, a
memoized child is skipped (its previously-rendered subtree is
reused) iff:
- Its props are shallowly equal to the previous render's props
(callables compared by identity, scalars by ``==``).