| 1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
| 2 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 |
| 3 | |
| 4 | //! Flickable pass |
| 5 | //! |
| 6 | //! The Flickable element is special in the sense that it has a viewport |
| 7 | //! which is not exposed. This passes create the viewport and fixes all property access |
| 8 | //! |
| 9 | //! It will also initialize proper geometry |
| 10 | //! This pass must be called before the materialize_fake_properties as it going to be generate |
| 11 | //! binding reference to fake properties |
| 12 | |
| 13 | use crate::expression_tree::{BindingExpression, Expression, MinMaxOp, NamedReference}; |
| 14 | use crate::langtype::{ElementType, NativeClass, Type}; |
| 15 | use crate::layout::is_layout; |
| 16 | use crate::object_tree::{Component, Element, ElementRc}; |
| 17 | use crate::typeregister::TypeRegister; |
| 18 | use core::cell::RefCell; |
| 19 | use smol_str::{format_smolstr, SmolStr}; |
| 20 | use std::rc::Rc; |
| 21 | |
| 22 | pub fn is_flickable_element(element: &ElementRc) -> bool { |
| 23 | matches!(&element.borrow().base_type, ElementType::Builtin(n) if n.name == "Flickable" ) |
| 24 | } |
| 25 | |
| 26 | pub fn handle_flickable(root_component: &Rc<Component>, tr: &TypeRegister) { |
| 27 | let mut native_empty: Rc = tr.empty_type().as_builtin().native_class.clone(); |
| 28 | while let Some(p: Rc) = native_empty.parent.clone() { |
| 29 | native_empty = p; |
| 30 | } |
| 31 | |
| 32 | crate::object_tree::recurse_elem_including_sub_components( |
| 33 | root_component, |
| 34 | &(), |
| 35 | &mut |elem: &ElementRc, _| { |
| 36 | if !is_flickable_element(elem) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | fixup_geometry(elem); |
| 41 | create_viewport_element(flickable:elem, &native_empty); |
| 42 | }, |
| 43 | ) |
| 44 | } |
| 45 | |
| 46 | fn create_viewport_element(flickable: &ElementRc, native_empty: &Rc<NativeClass>) { |
| 47 | let children = std::mem::take(&mut flickable.borrow_mut().children); |
| 48 | let is_listview = children |
| 49 | .iter() |
| 50 | .any(|c| c.borrow().repeated.as_ref().is_some_and(|r| r.is_listview.is_some())); |
| 51 | |
| 52 | if is_listview { |
| 53 | // Fox Listview, we don't bind the y property to the geometry because for large listview, we want to support coordinate with more precision than f32 |
| 54 | // so the actual geometry is relative to the Flickable instead of the viewport |
| 55 | // We still assign a binding to the y property in case it is read by someone |
| 56 | for c in &children { |
| 57 | if c.borrow().repeated.is_none() { |
| 58 | // Normally should not happen, listview should only have one children, and it should be repeated |
| 59 | continue; |
| 60 | } |
| 61 | let ElementType::Component(base) = c.borrow().base_type.clone() else { continue }; |
| 62 | let inner_elem = &base.root_element; |
| 63 | let new_y = crate::layout::create_new_prop( |
| 64 | inner_elem, |
| 65 | SmolStr::new_static("actual-y" ), |
| 66 | Type::LogicalLength, |
| 67 | ); |
| 68 | new_y.mark_as_set(); |
| 69 | inner_elem.borrow_mut().bindings.insert( |
| 70 | "y" .into(), |
| 71 | RefCell::new( |
| 72 | Expression::BinaryExpression { |
| 73 | lhs: Expression::PropertyReference(new_y.clone()).into(), |
| 74 | rhs: Expression::PropertyReference(NamedReference::new( |
| 75 | flickable, |
| 76 | SmolStr::new_static("viewport-y" ), |
| 77 | )) |
| 78 | .into(), |
| 79 | op: '-' , |
| 80 | } |
| 81 | .into(), |
| 82 | ), |
| 83 | ); |
| 84 | inner_elem.borrow_mut().geometry_props.as_mut().unwrap().y = new_y; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | let viewport = Element::make_rc(Element { |
| 89 | id: format_smolstr!(" {}-viewport" , flickable.borrow().id), |
| 90 | base_type: ElementType::Native(native_empty.clone()), |
| 91 | children, |
| 92 | enclosing_component: flickable.borrow().enclosing_component.clone(), |
| 93 | is_flickable_viewport: true, |
| 94 | ..Element::default() |
| 95 | }); |
| 96 | let element_type = flickable.borrow().base_type.clone(); |
| 97 | for prop in element_type.as_builtin().properties.keys() { |
| 98 | // bind the viewport's property to the flickable property, such as: `width <=> parent.viewport-width` |
| 99 | if let Some(vp_prop) = prop.strip_prefix("viewport-" ) { |
| 100 | if is_listview && matches!(vp_prop, "y" | "height" ) { |
| 101 | //don't bind viewport-y for ListView because the layout is handled by the runtime |
| 102 | continue; |
| 103 | } |
| 104 | viewport.borrow_mut().bindings.insert( |
| 105 | vp_prop.into(), |
| 106 | BindingExpression::new_two_way(NamedReference::new(flickable, prop.clone())).into(), |
| 107 | ); |
| 108 | } |
| 109 | } |
| 110 | viewport |
| 111 | .borrow() |
| 112 | .property_analysis |
| 113 | .borrow_mut() |
| 114 | .entry("y" .into()) |
| 115 | .or_default() |
| 116 | .is_set_externally = true; |
| 117 | viewport |
| 118 | .borrow() |
| 119 | .property_analysis |
| 120 | .borrow_mut() |
| 121 | .entry("x" .into()) |
| 122 | .or_default() |
| 123 | .is_set_externally = true; |
| 124 | |
| 125 | let enclosing_component = flickable.borrow().enclosing_component.upgrade().unwrap(); |
| 126 | if let Some((insertion_point, _, _)) = |
| 127 | &mut *enclosing_component.child_insertion_point.borrow_mut() |
| 128 | { |
| 129 | if std::rc::Rc::ptr_eq(insertion_point, flickable) { |
| 130 | *insertion_point = viewport.clone() |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | flickable.borrow_mut().children.push(viewport); |
| 135 | } |
| 136 | |
| 137 | fn fixup_geometry(flickable_elem: &ElementRc) { |
| 138 | let forward_minmax_of = |prop: &'static str, op: MinMaxOp| { |
| 139 | set_binding_if_not_explicit(flickable_elem, prop, || { |
| 140 | flickable_elem |
| 141 | .borrow() |
| 142 | .children |
| 143 | .iter() |
| 144 | .filter(|x| is_layout(&x.borrow().base_type)) |
| 145 | // FIXME: we should ideally add runtime code to merge layout info of all elements that are repeated (#407) |
| 146 | .filter(|x| x.borrow().repeated.is_none()) |
| 147 | .map(|x| { |
| 148 | Expression::PropertyReference(NamedReference::new(x, SmolStr::new_static(prop))) |
| 149 | }) |
| 150 | .reduce(|lhs, rhs| crate::builtin_macros::min_max_expression(lhs, rhs, op)) |
| 151 | }) |
| 152 | }; |
| 153 | |
| 154 | if !flickable_elem.borrow().bindings.contains_key("height" ) { |
| 155 | forward_minmax_of("max-height" , MinMaxOp::Min); |
| 156 | forward_minmax_of("preferred-height" , MinMaxOp::Min); |
| 157 | } |
| 158 | if !flickable_elem.borrow().bindings.contains_key("width" ) { |
| 159 | forward_minmax_of("max-width" , MinMaxOp::Min); |
| 160 | forward_minmax_of("preferred-width" , MinMaxOp::Min); |
| 161 | } |
| 162 | set_binding_if_not_explicit(flickable_elem, "viewport-width" , || { |
| 163 | Some( |
| 164 | flickable_elem |
| 165 | .borrow() |
| 166 | .children |
| 167 | .iter() |
| 168 | .filter(|x| is_layout(&x.borrow().base_type)) |
| 169 | // FIXME: (#407) |
| 170 | .filter(|x| x.borrow().repeated.is_none()) |
| 171 | .map(|x| { |
| 172 | Expression::PropertyReference(NamedReference::new( |
| 173 | x, |
| 174 | SmolStr::new_static("min-width" ), |
| 175 | )) |
| 176 | }) |
| 177 | .fold( |
| 178 | Expression::PropertyReference(NamedReference::new( |
| 179 | flickable_elem, |
| 180 | SmolStr::new_static("width" ), |
| 181 | )), |
| 182 | |lhs, rhs| crate::builtin_macros::min_max_expression(lhs, rhs, MinMaxOp::Max), |
| 183 | ), |
| 184 | ) |
| 185 | }); |
| 186 | set_binding_if_not_explicit(flickable_elem, "viewport-height" , || { |
| 187 | Some( |
| 188 | flickable_elem |
| 189 | .borrow() |
| 190 | .children |
| 191 | .iter() |
| 192 | .filter(|x| is_layout(&x.borrow().base_type)) |
| 193 | // FIXME: (#407) |
| 194 | .filter(|x| x.borrow().repeated.is_none()) |
| 195 | .map(|x| { |
| 196 | Expression::PropertyReference(NamedReference::new( |
| 197 | x, |
| 198 | SmolStr::new_static("min-height" ), |
| 199 | )) |
| 200 | }) |
| 201 | .fold( |
| 202 | Expression::PropertyReference(NamedReference::new( |
| 203 | flickable_elem, |
| 204 | SmolStr::new_static("height" ), |
| 205 | )), |
| 206 | |lhs, rhs| crate::builtin_macros::min_max_expression(lhs, rhs, MinMaxOp::Max), |
| 207 | ), |
| 208 | ) |
| 209 | }); |
| 210 | } |
| 211 | |
| 212 | /// Set the property binding on the given element to the given expression (computed lazily). |
| 213 | /// The parameter to the lazily calculation is the element's children |
| 214 | fn set_binding_if_not_explicit( |
| 215 | elem: &ElementRc, |
| 216 | property: &str, |
| 217 | expression: impl FnOnce() -> Option<Expression>, |
| 218 | ) { |
| 219 | // we can't use `set_binding_if_not_set` directly because `expression()` may borrow `elem` |
| 220 | if elem.borrow().bindings.get(property).map_or(default:true, |b: &RefCell| !b.borrow().has_binding()) { |
| 221 | if let Some(e: Expression) = expression() { |
| 222 | elem.borrow_mut().set_binding_if_not_set(property.into(), || e); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |