Python: Expose Slint structs

Structs declared and exported in Slint are now available in the module namespace
with a constructor.

Fixes #5708
This commit is contained in:
Simon Hausmann 2024-07-08 22:20:29 +02:00 committed by Simon Hausmann
parent 048c0eaf08
commit 1e3f05c983
11 changed files with 144 additions and 12 deletions

View file

@ -8,6 +8,7 @@ from . import slint as native
import types
import logging
import importlib
import copy
from . import models
@ -191,6 +192,23 @@ def _build_class(compdef):
return type("SlintClassWrapper", (Component,), properties_and_callbacks)
def _build_struct(name, struct_prototype):
def new_struct(cls, *args, **kwargs):
inst = copy.copy(struct_prototype)
for prop, val in kwargs.items():
setattr(inst, prop, val)
return inst
type_dict = {
"__new__": new_struct,
}
return type(name, (), type_dict)
def load_file(path, quiet=False, style=None, include_paths=None, library_paths=None, translation_domain=None):
compiler = native.Compiler()
@ -223,6 +241,10 @@ def load_file(path, quiet=False, style=None, include_paths=None, library_paths=N
setattr(module, comp_name, wrapper_class)
for name, struct_or_enum_prototype in result.structs_and_enums.items():
struct_wrapper = _build_struct(name, struct_or_enum_prototype)
setattr(module, name, struct_wrapper)
return module