-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
150 lines (119 loc) · 4.13 KB
/
base.py
File metadata and controls
150 lines (119 loc) · 4.13 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
"""Shared base classes and utilities for native view handlers.
Provides the :class:`ViewHandler` abstract base class and common helper
functions used by both Android and iOS platform implementations.
"""
from typing import Any, Dict, Union
class ViewHandler:
"""Protocol for creating, updating, and managing children of a native view type."""
def create(self, props: Dict[str, Any]) -> Any:
raise NotImplementedError
def update(self, native_view: Any, changed_props: Dict[str, Any]) -> None:
raise NotImplementedError
def add_child(self, parent: Any, child: Any) -> None:
pass
def remove_child(self, parent: Any, child: Any) -> None:
pass
def insert_child(self, parent: Any, child: Any, index: int) -> None:
self.add_child(parent, child)
# ======================================================================
# Color parsing
# ======================================================================
def parse_color_int(color: Union[str, int]) -> int:
"""Parse ``#RRGGBB`` / ``#AARRGGBB`` hex string or raw int to a *signed* ARGB int.
Java's ``setBackgroundColor`` et al. expect a signed 32-bit int, so values
with a high alpha byte (e.g. 0xFF…) must be converted to negative ints.
"""
if isinstance(color, int):
val = color
else:
c = color.strip().lstrip("#")
if len(c) == 6:
c = "FF" + c
val = int(c, 16)
if val > 0x7FFFFFFF:
val -= 0x100000000
return val
# ======================================================================
# Padding / margin helpers
# ======================================================================
def resolve_padding(padding: Any) -> tuple:
"""Normalise various padding representations to ``(left, top, right, bottom)``."""
if padding is None:
return (0, 0, 0, 0)
if isinstance(padding, (int, float)):
v = int(padding)
return (v, v, v, v)
if isinstance(padding, dict):
h = int(padding.get("horizontal", 0))
v = int(padding.get("vertical", 0))
left = int(padding.get("left", h))
right = int(padding.get("right", h))
top = int(padding.get("top", v))
bottom = int(padding.get("bottom", v))
a = int(padding.get("all", 0))
if a:
left = left or a
right = right or a
top = top or a
bottom = bottom or a
return (left, top, right, bottom)
return (0, 0, 0, 0)
# ======================================================================
# Flex layout constants
# ======================================================================
FLEX_DIRECTION_COLUMN = "column"
FLEX_DIRECTION_ROW = "row"
FLEX_DIRECTION_COLUMN_REVERSE = "column_reverse"
FLEX_DIRECTION_ROW_REVERSE = "row_reverse"
JUSTIFY_FLEX_START = "flex_start"
JUSTIFY_CENTER = "center"
JUSTIFY_FLEX_END = "flex_end"
JUSTIFY_SPACE_BETWEEN = "space_between"
JUSTIFY_SPACE_AROUND = "space_around"
JUSTIFY_SPACE_EVENLY = "space_evenly"
ALIGN_STRETCH = "stretch"
ALIGN_FLEX_START = "flex_start"
ALIGN_CENTER = "center"
ALIGN_FLEX_END = "flex_end"
POSITION_RELATIVE = "relative"
POSITION_ABSOLUTE = "absolute"
OVERFLOW_VISIBLE = "visible"
OVERFLOW_HIDDEN = "hidden"
OVERFLOW_SCROLL = "scroll"
def is_vertical(direction: str) -> bool:
"""Return ``True`` if *direction* represents a vertical (column) axis."""
return direction in (FLEX_DIRECTION_COLUMN, FLEX_DIRECTION_COLUMN_REVERSE)
# ======================================================================
# Layout property keys
# ======================================================================
LAYOUT_KEYS = frozenset(
{
"width",
"height",
"flex",
"flex_grow",
"flex_shrink",
"margin",
"min_width",
"max_width",
"min_height",
"max_height",
"align_self",
"position",
"top",
"right",
"bottom",
"left",
}
)
CONTAINER_KEYS = frozenset(
{
"flex_direction",
"justify_content",
"align_items",
"overflow",
"spacing",
"padding",
"background_color",
}
)