mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-01 22:31:14 +00:00
Rename Rectangle.color to Rectangle.background
Add support for built-in property aliases and rename `color` to `background` - in preparation for it also changing to type brush. Right now the alias is silent, a deprecation and overall change will come in a subsequent change.
This commit is contained in:
parent
c479fd132d
commit
1f091cb1c0
16 changed files with 250 additions and 131 deletions
|
@ -7,8 +7,10 @@
|
|||
This file is also available under commercial licensing terms.
|
||||
Please contact info@sixtyfps.io for more information.
|
||||
LICENSE END */
|
||||
use std::borrow::Cow;
|
||||
use std::collections::{BTreeMap, HashMap, HashSet};
|
||||
use std::{fmt::Display, rc::Rc};
|
||||
use std::fmt::Display;
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::expression_tree::{Expression, Unit};
|
||||
use crate::object_tree::Component;
|
||||
|
@ -193,18 +195,39 @@ impl Type {
|
|||
!matches!(self, Self::Duration | Self::Easing | Self::Angle)
|
||||
}
|
||||
|
||||
pub fn lookup_property(&self, name: &str) -> Type {
|
||||
pub fn lookup_property<'a>(&self, name: &'a str) -> PropertyLookupResult<'a> {
|
||||
match self {
|
||||
Type::Component(c) => c.root_element.borrow().lookup_property(name),
|
||||
Type::Builtin(b) => b.properties.get(name).cloned().unwrap_or_else(|| {
|
||||
if b.is_non_item_type {
|
||||
Type::Invalid
|
||||
Type::Builtin(b) => {
|
||||
let resolved_name =
|
||||
if let Some(alias_name) = b.native_class.lookup_alias(name.as_ref()) {
|
||||
Cow::Owned(alias_name.to_string())
|
||||
} else {
|
||||
Cow::Borrowed(name)
|
||||
};
|
||||
let property_type =
|
||||
b.properties.get(resolved_name.as_ref()).cloned().unwrap_or_else(|| {
|
||||
if b.is_non_item_type {
|
||||
Type::Invalid
|
||||
} else {
|
||||
crate::typeregister::reserved_property(resolved_name.as_ref())
|
||||
}
|
||||
});
|
||||
PropertyLookupResult { resolved_name, property_type }
|
||||
}
|
||||
Type::Native(n) => {
|
||||
let resolved_name = if let Some(alias_name) = n.lookup_alias(name.as_ref()) {
|
||||
Cow::Owned(alias_name.to_string())
|
||||
} else {
|
||||
crate::typeregister::reserved_property(name)
|
||||
}
|
||||
}),
|
||||
Type::Native(n) => n.lookup_property(name).unwrap_or_default(),
|
||||
_ => Type::Invalid,
|
||||
Cow::Borrowed(name)
|
||||
};
|
||||
let property_type = n.lookup_property(resolved_name.as_ref()).unwrap_or_default();
|
||||
PropertyLookupResult { resolved_name, property_type }
|
||||
}
|
||||
_ => PropertyLookupResult {
|
||||
resolved_name: Cow::Borrowed(name),
|
||||
property_type: Type::Invalid,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -392,6 +415,7 @@ pub struct NativeClass {
|
|||
pub class_name: String,
|
||||
pub vtable_symbol: String,
|
||||
pub properties: HashMap<String, Type>,
|
||||
pub deprecated_aliases: HashMap<String, String>,
|
||||
pub cpp_type: Option<String>,
|
||||
pub rust_type_constructor: Option<String>,
|
||||
}
|
||||
|
@ -441,6 +465,18 @@ impl NativeClass {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn lookup_alias(&self, name: &str) -> Option<&str> {
|
||||
if let Some(alias_target) = self.deprecated_aliases.get(name) {
|
||||
Some(alias_target)
|
||||
} else if self.properties.contains_key(name) {
|
||||
None
|
||||
} else if let Some(parent_class) = &self.parent {
|
||||
parent_class.lookup_alias(name)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn lookup_property_distance(self: Rc<Self>, name: &str) -> (usize, Rc<Self>) {
|
||||
let mut distance = 0;
|
||||
let mut class = self;
|
||||
|
@ -559,6 +595,11 @@ fn test_select_minimal_class_based_on_property_usage() {
|
|||
assert_eq!(reduce_to_second.class_name, second.class_name);
|
||||
}
|
||||
|
||||
pub struct PropertyLookupResult<'a> {
|
||||
pub resolved_name: std::borrow::Cow<'a, str>,
|
||||
pub property_type: Type,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Enumeration {
|
||||
pub name: String,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue