Skip to content

Commit

Permalink
live preview: Re-enable moving and resizing when no layout is in sight
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hunger committed Feb 28, 2024
1 parent e668a0a commit b0f2e8c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
16 changes: 13 additions & 3 deletions tools/lsp/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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::<Vec<_>>();
let model = Rc::new(slint::VecModel::from(values));
Expand All @@ -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,
);

Expand Down
23 changes: 23 additions & 0 deletions tools/lsp/preview/element_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b0f2e8c

Please sign in to comment.