From b0f2e8cd28de507a49df1ac4cce192985b948a58 Mon Sep 17 00:00:00 2001 From: Tobias Hunger Date: Wed, 28 Feb 2024 17:32:35 +0100 Subject: [PATCH] live preview: Re-enable moving and resizing when no layout is in sight Allow moving and resizing of elements provided: * The element is not layout. This approximates "fills its parent", which is what we aim for once we can query type info rom within the Preview. * The element is not *inside* a layout --- tools/lsp/preview.rs | 16 +++++++++++++--- tools/lsp/preview/element_selection.rs | 23 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/tools/lsp/preview.rs b/tools/lsp/preview.rs index 8ea2f2d283f..e2f26c01eb1 100644 --- a/tools/lsp/preview.rs +++ b/tools/lsp/preview.rs @@ -606,6 +606,8 @@ fn set_selections( ui: Option<&ui::PreviewUi>, main_index: usize, is_layout: bool, + is_moveable: bool, + is_resizable: bool, positions: ComponentPositions, ) { let Some(ui) = ui else { @@ -634,8 +636,8 @@ fn set_selections( y: g.origin.y, border_color: if i == main_index { border_color } else { secondary_border_color }, is_primary: i == main_index, - is_moveable: false, - is_resizable: false, + is_moveable, + is_resizable, }) .collect::>(); let model = Rc::new(slint::VecModel::from(values)); @@ -647,13 +649,21 @@ fn set_selected_element( positions: slint_interpreter::highlight::ComponentPositions, notify_editor_about_selection_after_update: bool, ) { + let (is_layout, is_in_layout) = selection + .as_ref() + .and_then(|s| s.as_element_node()) + .map(|en| (en.is_layout(), element_selection::is_element_node_in_layout(&en))) + .unwrap_or((false, false)); + PREVIEW_STATE.with(move |preview_state| { let mut preview_state = preview_state.borrow_mut(); set_selections( preview_state.ui.as_ref(), selection.as_ref().map(|s| s.instance_index).unwrap_or_default(), - selection.as_ref().map(|s| s.is_layout).unwrap_or_default(), + is_layout, + !is_in_layout && !is_layout, + !is_in_layout && !is_layout, positions, ); diff --git a/tools/lsp/preview/element_selection.rs b/tools/lsp/preview/element_selection.rs index c9eee64a643..22620d70691 100644 --- a/tools/lsp/preview/element_selection.rs +++ b/tools/lsp/preview/element_selection.rs @@ -392,6 +392,29 @@ pub fn select_element_at(x: f32, y: f32, enter_component: bool) { } } +pub fn is_element_node_in_layout(element: &ElementRcNode) -> bool { + if element.debug_index > 0 { + // If we are not the first node, then we might have been inlined right + // after a layout managing us + element + .element + .borrow() + .debug + .get(element.debug_index - 1) + .map(|d| d.1.is_some()) + .unwrap_or_default() + } else { + // If we are the first node, then we might be a child of a layout stored + // in the last node of our parent element. + let Some(parent) = i_slint_compiler::object_tree::find_parent_element(&element.element) + else { + return false; + }; + let r = parent.borrow().debug.last().map(|d| d.1.is_some()).unwrap_or_default(); + r + } +} + pub fn select_element_behind(x: f32, y: f32, enter_component: bool, reverse: bool) { let Some(component_instance) = super::component_instance() else { return;