-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
87 lines (61 loc) · 2.91 KB
/
__init__.py
File metadata and controls
87 lines (61 loc) · 2.91 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
"""Platform-specific native view creation and update logic.
This package provides the :class:`NativeViewRegistry` that maps element type
names to platform-specific :class:`~.base.ViewHandler` implementations.
Platform handlers live in dedicated submodules:
- :mod:`~.base` — shared :class:`~.base.ViewHandler` protocol and utilities
- :mod:`~.android` — Android handlers (Chaquopy / Java bridge)
- :mod:`~.ios` — iOS handlers (rubicon-objc)
All platform-branching is handled at registration time via lazy imports,
so the package can be imported on any platform for testing.
"""
from typing import Any, Dict, Optional
from .base import ViewHandler
class NativeViewRegistry:
"""Maps element type names to platform-specific :class:`ViewHandler` instances."""
def __init__(self) -> None:
self._handlers: Dict[str, ViewHandler] = {}
def register(self, type_name: str, handler: ViewHandler) -> None:
self._handlers[type_name] = handler
def create_view(self, type_name: str, props: Dict[str, Any]) -> Any:
handler = self._handlers.get(type_name)
if handler is None:
raise ValueError(f"Unknown element type: {type_name!r}")
return handler.create(props)
def update_view(self, native_view: Any, type_name: str, changed_props: Dict[str, Any]) -> None:
handler = self._handlers.get(type_name)
if handler is not None:
handler.update(native_view, changed_props)
def add_child(self, parent: Any, child: Any, parent_type: str) -> None:
handler = self._handlers.get(parent_type)
if handler is not None:
handler.add_child(parent, child)
def remove_child(self, parent: Any, child: Any, parent_type: str) -> None:
handler = self._handlers.get(parent_type)
if handler is not None:
handler.remove_child(parent, child)
def insert_child(self, parent: Any, child: Any, parent_type: str, index: int) -> None:
handler = self._handlers.get(parent_type)
if handler is not None:
handler.insert_child(parent, child, index)
# ======================================================================
# Singleton registry
# ======================================================================
_registry: Optional[NativeViewRegistry] = None
def get_registry() -> NativeViewRegistry:
"""Return the singleton registry, lazily creating platform handlers."""
global _registry
if _registry is not None:
return _registry
_registry = NativeViewRegistry()
from ..utils import IS_ANDROID
if IS_ANDROID:
from .android import register_handlers
register_handlers(_registry)
else:
from .ios import register_handlers
register_handlers(_registry)
return _registry
def set_registry(registry: NativeViewRegistry) -> None:
"""Inject a custom or mock registry (primarily for testing)."""
global _registry
_registry = registry