Well I went off on a bit of a tangent tonight , yet again. But I was thinking about options to connect my views to Custom Classes as you can with loading a UIFile with bindings. I am sure there are simpler ways, but I wanted to check it out anyway.
So the idea was I could create a simple view using v=ui.View() and bind that to a custom class. Anyway, that was not going to work itself. But I thought what if I create a json Str that ui.load_view_str() expects and pass the bindings. Btw. ui.load_view_str() is not documented as far as I know. But you can see it in the ui module.
Anyway, below is what I come up with. I don't really understand it 100% and maybe it's a horrible idea. I am surprised what it does. Regardless also got me started thinking about a memory ui object to dict -> json str, so it can be written out as a pyui file. As far as I know, no one has written this. If they have, please let me know. Would be so handy to have a function like that.
Anyway, this is what I did, maybe I went around the block 5 times to do something a simple assignment can do...
import ui, json
d = \
[
{
"selected" : False,
"frame" : "{{0, 0}, {600, 800}}",
"class" : "View",
"nodes" : [],
"attributes" : {
"custom_class" : "Panel",
"enabled" : True,
"background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
"tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)",
"border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)",
"flex" : ""
}
}
]
class Panel(ui.View):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.bg_color = 'purple'
self.xxxxxxxx = 'dynamic Attr'
def h(self):
print('hello from Panel class')
def pyui_bindings(obj):
# JonB
def WrapInstance(obj):
class Wrapper(obj.__class__):
def __new__(cls):
return obj
return Wrapper
bindings = globals().copy()
bindings[obj.__class__.__name__]=WrapInstance(obj)
return bindings
j_str = json.dumps(d)
v = ui.load_view_str(j_str, pyui_bindings(Panel))
v.present('sheet')
# dir(v) shows the xxxxxxxx attr
print(dir(v))
# v links to Panel.h method
v.h()