Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidgetnode.py
More file actions
Latest commit
111 lines (82 loc) · 3.5 KB
/
Copy pathwidgetnode.py
File metadata and controls
111 lines (82 loc) · 3.5 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
from __future__ importannotations
fromabcimportABC, abstractmethod
fromdataclassesimportdataclass, field
fromenumimportEnum
importjson
fromrx.subjectimportBehaviorSubject
fromthemeimportNodeStyle, NodeStyleDef, WidgetStyle, WidgetStyleDef
fromwidgettypesimportWidgetTypes
fromtypingimportAny, Callable, Dict, List, Optional, Union
classBaseComponent(ABC):
def__init__(self, props: Dict[str, Any]):
self.props=BehaviorSubject(props)
@abstractmethod
defrender(self):
raiseNotImplementedError
@dataclass
classWidgetNode:
type: WidgetTypes
props: BehaviorSubject[Dict[str, Any]] =field(default_factory=lambda: BehaviorSubject({}))
children: BehaviorSubject[List[Renderable]] =field(default_factory=lambda: BehaviorSubject([]))
@dataclass
classRawChildlessWidgetNodeWithId:
id: int
type: WidgetTypes
props: Dict[str, Any] =field(default_factory=dict)
defto_serializable_dict(self):
out= {
"id": self.id,
"type": self.type.value,
}
forkey, valueinself.props.items():
ifnotcallable(value):
out[key] =value
returnout
defwidget_node_factory(widgetType: WidgetTypes, props: Dict[str, Any], children: List[Renderable]) ->WidgetNode:
returnWidgetNode(widgetType, BehaviorSubject(props), BehaviorSubject(children))
defcreate_raw_childless_widget_node_with_id(id: int, node: WidgetNode) ->RawChildlessWidgetNodeWithId:
returnRawChildlessWidgetNodeWithId(id=id, type=node.type, props=node.props.value)
definit_props_with_style(style: Optional[Union[NodeStyle, WidgetStyle]]):
props: Dict[str, Any] = {}
ifstyle:
ifstyle.style:
props["style"] =style.style
ifstyle.activeStyle:
props["activeStyle"] =style.activeStyle
ifstyle.hoverStyle:
props["hoverStyle"] =style.hoverStyle
ifstyle.disabledStyle:
props["disabledStyle"] =style.disabledStyle
returnprops
defroot_node(children: List[Renderable], style: Optional[NodeStyle] =None) ->WidgetNode:
props=init_props_with_style(style)
props["root"] =True
returnwidget_node_factory(WidgetTypes.Node, props, children)
defnode (children: List[Renderable], style: Optional[NodeStyle] =None) ->WidgetNode:
props=init_props_with_style(style)
props["root"] =False
returnwidget_node_factory(WidgetTypes.Node, props, children)
defunformatted_text (text: str, style: Optional[WidgetStyle] =None) ->WidgetNode:
props=init_props_with_style(style)
props["text"] =text
returnwidget_node_factory(WidgetTypes.UnformattedText, props, [])
defbutton (label: str, on_click: Optional[Callable] =None, style: Optional[WidgetStyle] =None) ->WidgetNode:
ifon_clickisnotNoneandnotcallable(on_click):
raiseTypeError("on_click must be a callable")
props=init_props_with_style(style)
props["label"] =label
ifon_click:
props["on_click"] =on_click
returnwidget_node_factory(WidgetTypes.Button, props, [])
Renderable=Union[BaseComponent, WidgetNode]
classRawChildlessWidgetNodeWithIdEncoder(json.JSONEncoder):
defdefault(self, obj):
ifisinstance(obj, Enum):
returnobj.value
ifcallable(obj):
returnNone
ifisinstance(obj, WidgetStyleDef):
returnobj.to_dict()
ifisinstance(obj, NodeStyleDef):
returnobj.to_dict()
returnsuper().default(obj)