mirror of
https://github.com/slint-ui/slint.git
synced 2025-08-04 02:39:28 +00:00
Simplify popup coordinate handling
Instead of replacing the existing x/y properties with "invisible" ones, let's just keep those that we have and merely zero out the geometry x/y. This has the bonus that access to x/y within the PopupWindow will provide the right value, i.e. popup := PopupWindow { x: 42px; Text { text: popup.text / 1px; } } will show "42" instead of zero. Since x/y are now merely referenced via the NamedReferences in PopupWindow, this patch also fixes the property materialization pass to use visit_all_named_references(component) to also visit the x/y named refs, instead of visit_all_named_references_in_element() that skips them.
This commit is contained in:
parent
c81ab5cd6d
commit
5f0d0937f0
2 changed files with 30 additions and 30 deletions
|
@ -8,7 +8,6 @@ use crate::expression_tree::{Expression, NamedReference};
|
|||
use crate::langtype::{ElementType, Type};
|
||||
use crate::object_tree::*;
|
||||
use crate::typeregister::TypeRegister;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub fn lower_popups(
|
||||
|
@ -120,10 +119,24 @@ fn lower_popup_window(
|
|||
e.borrow_mut().enclosing_component = weak.clone()
|
||||
});
|
||||
|
||||
// Generate a x and y property, relative to the window coordinate
|
||||
// FIXME: this is a hack that doesn't always work, perhaps should we store an item ref or something
|
||||
let coord_x = create_coordinate(&popup_comp.root_element, "x");
|
||||
let coord_y = create_coordinate(&popup_comp.root_element, "y");
|
||||
// Take a reference to the x/y coordinates, to be read when calling show_popup(), and
|
||||
// converted to absolute coordinates in the run-time library.
|
||||
let coord_x = NamedReference::new(&popup_comp.root_element, "x");
|
||||
let coord_y = NamedReference::new(&popup_comp.root_element, "y");
|
||||
|
||||
// Meanwhile, set the geometry x/y to zero, because we'll be shown as a top-level and
|
||||
// children should be rendered starting with a (0, 0) offset.
|
||||
{
|
||||
let mut popup_mut = popup_comp.root_element.borrow_mut();
|
||||
let name = format!("popup-{}-dummy", popup_mut.id);
|
||||
popup_mut.property_declarations.insert(name.clone(), Type::LogicalLength.into());
|
||||
drop(popup_mut);
|
||||
let dummy1 = NamedReference::new(&popup_comp.root_element, &name);
|
||||
let dummy2 = NamedReference::new(&popup_comp.root_element, &name);
|
||||
let mut popup_mut = popup_comp.root_element.borrow_mut();
|
||||
popup_mut.geometry_props.as_mut().unwrap().x = dummy1;
|
||||
popup_mut.geometry_props.as_mut().unwrap().y = dummy2;
|
||||
}
|
||||
|
||||
// Throw error when accessing the popup from outside
|
||||
// FIXME:
|
||||
|
@ -149,17 +162,3 @@ fn lower_popup_window(
|
|||
parent_element: parent_element.clone(),
|
||||
});
|
||||
}
|
||||
|
||||
fn create_coordinate(popup_root_element: &ElementRc, coord: &str) -> NamedReference {
|
||||
let mut elem = popup_root_element.borrow_mut();
|
||||
let expression = elem
|
||||
.bindings
|
||||
.remove(coord)
|
||||
.map(|e| e.into_inner().expression)
|
||||
.unwrap_or(Expression::NumberLiteral(0., crate::expression_tree::Unit::Phx));
|
||||
let property_name = format!("{}-popup-{}", elem.id, coord);
|
||||
elem.property_declarations.insert(property_name.clone(), Type::LogicalLength.into());
|
||||
elem.bindings.insert(property_name.clone(), RefCell::new(expression.into()));
|
||||
drop(elem);
|
||||
NamedReference::new(popup_root_element, &property_name)
|
||||
}
|
||||
|
|
|
@ -17,18 +17,19 @@ use std::rc::Rc;
|
|||
pub fn materialize_fake_properties(component: &Rc<Component>) {
|
||||
let mut to_materialize = std::collections::HashMap::new();
|
||||
|
||||
recurse_elem_including_sub_components_no_borrow(component, &(), &mut |elem, _| {
|
||||
visit_all_named_references_in_element(elem, |nr| {
|
||||
let elem = nr.element();
|
||||
let elem = elem.borrow();
|
||||
if !to_materialize.contains_key(nr) {
|
||||
if let Some(ty) =
|
||||
should_materialize(&elem.property_declarations, &elem.base_type, nr.name())
|
||||
{
|
||||
to_materialize.insert(nr.clone(), ty);
|
||||
}
|
||||
visit_all_named_references(component, &mut |nr| {
|
||||
let elem = nr.element();
|
||||
let elem = elem.borrow();
|
||||
if !to_materialize.contains_key(nr) {
|
||||
if let Some(ty) =
|
||||
should_materialize(&elem.property_declarations, &elem.base_type, nr.name())
|
||||
{
|
||||
to_materialize.insert(nr.clone(), ty);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
recurse_elem_including_sub_components_no_borrow(component, &(), &mut |elem, _| {
|
||||
for prop in elem.borrow().bindings.keys() {
|
||||
let nr = NamedReference::new(elem, prop);
|
||||
if let std::collections::hash_map::Entry::Vacant(e) = to_materialize.entry(nr) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue