-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
131 lines (115 loc) · 3.3 KB
/
__init__.py
File metadata and controls
131 lines (115 loc) · 3.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
"""Public extension surface for PythonNative.
The ``pythonnative.sdk`` package collects the *stable* extension
contract that third-party packages rely on: the
[`ViewHandler`][pythonnative.sdk.ViewHandler] protocol, the
[`Style`][pythonnative.sdk.Style] type, the
[`@native_component`][pythonnative.sdk.native_component] registration
decorator, and an
[`element_factory`][pythonnative.sdk.element_factory] helper for
producing strongly-typed element constructors.
A custom native component is three things:
1. A typed, frozen [`Props`][pythonnative.sdk.Props] dataclass listing
the public properties the component accepts.
2. One or more
[`ViewHandler`][pythonnative.sdk.ViewHandler] subclasses (one per
target platform) implementing creation, update, and child management
for the underlying native widget.
3. A registration call (the
[`@native_component`][pythonnative.sdk.native_component] decorator,
or
[`register_component`][pythonnative.sdk.register_component] for
imperative use) that binds the props type and handler into the
process-wide registry.
Once registered, the component appears alongside the built-ins: the
reconciler, layout engine, and Fast Refresh treat it identically.
PyPI packages can ship handlers without users importing them
explicitly by declaring an entry point in the
``pythonnative.handlers`` group; PythonNative discovers and imports
those modules the first time the registry is asked for a handler.
Example:
```python
from dataclasses import dataclass
import pythonnative as pn
from pythonnative.sdk import (
Props,
ViewHandler,
element_factory,
native_component,
)
@dataclass(frozen=True)
class BadgeProps(Props):
text: str = ""
color: str = "#FF3B30"
style: pn.StyleProp = None
@native_component("Badge", props=BadgeProps, platforms=("ios",))
class IOSBadgeHandler(ViewHandler):
def create(self, props):
...
def update(self, view, changed):
...
Badge = element_factory("Badge")
@pn.component
def App():
return pn.Column(
Badge(text="3", color="#0A84FF"),
pn.Text("Inbox"),
)
```
"""
from ..element import Element
from ..native_views.base import ViewHandler, parse_color_int
from ..style import (
Color,
Dimension,
EdgeInsets,
EdgeValue,
FlexDirection,
JustifyContent,
Overflow,
Position,
Style,
StyleProp,
TransformSpec,
style,
)
from ._components import (
ENTRY_POINT_GROUP,
Props,
element_factory,
get_props_type,
install_into_registry,
list_components,
native_component,
register_component,
unregister_component,
)
__all__ = [
# Core types
"Element",
"ViewHandler",
# Style types
"Color",
"Dimension",
"EdgeInsets",
"EdgeValue",
"FlexDirection",
"JustifyContent",
"Overflow",
"Position",
"Style",
"StyleProp",
"TransformSpec",
"style",
# SDK helpers (re-exported so users only import from one place)
"parse_color_int",
# Native-component SDK
"ENTRY_POINT_GROUP",
"Props",
"element_factory",
"get_props_type",
"install_into_registry",
"list_components",
"native_component",
"register_component",
"unregister_component",
]