slint/sixtyfps_compiler/passes/resolve_native_classes.rs
Simon Hausmann 88fb491837 Add support for mapping elements to different native classes
The Rectangle element has properties for a border outline. If those are
used, then the generated code should use BorderRectangle. But if they are
not used, then we can fall back to just generating a Rectangle.
2020-07-24 17:48:19 +02:00

37 lines
1.4 KiB
Rust

//! After inlining and moving declarations, all Element::base_type should be Type::BuiltinElement. This pass resolves them
// to NativeClass and picking a variant that only contains the used properties.
use crate::object_tree::{recurse_elem, Component};
use crate::typeregister::Type;
pub fn resolve_native_classes(component: &Component) {
recurse_elem(&component.root_element, &(), &mut |elem, _| {
let new_native_class = {
let elem = elem.borrow();
let base_type = match &elem.base_type {
Type::Component(comp) => {
// Components at this point must be for example repeaters. Recurse.
resolve_native_classes(&comp);
return;
}
Type::Builtin(b) => b,
_ => panic!("This should not happen because of inlining"),
};
let native_properties_used = elem.bindings.keys().filter(|k| {
if elem.property_declarations.contains_key(*k) {
return false;
}
base_type.as_ref().properties.get(*k).is_some()
});
let current_native_class = elem.base_type.as_builtin().native_class.clone();
current_native_class
.select_minimal_class_based_on_property_usage(native_properties_used)
};
elem.borrow_mut().base_type = Type::Native(new_native_class);
})
}