Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

live preview: Update selection when size of UI changes #4644

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions tools/lsp/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ pub fn set_contents(url: &VersionedUrl, content: String) {
}
}

pub fn reselect_element() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't this belong in the selection module?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it belongs into element selection, but it also needs PREVIEW_STATE and I am trying to keep that in preview.rs. So I moved the re-selection code there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can always use super::PREVIEW_STATE

let Some(selected) = PREVIEW_STATE.with(|preview_state| {
let mut preview_state = preview_state.borrow_mut();
preview_state.selected.take()
}) else {
return;
};
let Some(component_instance) = component_instance() else {
return;
};
let positions = component_instance.component_positions(&selected.path, selected.offset);

set_selected_element(Some(selected), positions, false);
}

// triggered from the UI, running in UI thread
fn can_drop_component(_component_name: slint::SharedString, x: f32, y: f32) -> bool {
drop_location::can_drop_at(x, y)
Expand Down
1 change: 1 addition & 0 deletions tools/lsp/preview/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub fn create_ui(style: String, experimental: bool) -> Result<PreviewUi, Platfor
super::ask_editor_to_show_document(&file, Range::new(pos, pos))
});
ui.on_unselect(super::element_selection::unselect_element);
ui.on_reselect(super::reselect_element);
ui.on_select_at(super::element_selection::select_element_at);
ui.on_select_behind(super::element_selection::select_element_behind);
ui.on_can_drop(super::can_drop_component);
Expand Down
2 changes: 2 additions & 0 deletions tools/lsp/ui/draw-area.slint
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export component DrawArea {
callback select-at(/* x */ length, /* y */ length, /* enter_component? */ bool);
callback select-behind(/* x */ length, /* y */ length, /* enter_component? */ bool, /* reverse */ bool);
callback show-document(/* url */ string, /* line */ int, /* column */ int);
callback reselect(); // Reselect element e.g. after changing the window size (which may move the element)
callback unselect();
callback selected-element-update-geometry(/* x */ length, /* y */ length, /* width */ length, /* height */ length);

Expand Down Expand Up @@ -130,6 +131,7 @@ export component DrawArea {
update-geometry(_, _, w, h) => {
i-preview-area-container.width = clamp(w, i-preview-area-container.min-width, i-preview-area-container.max-width);
i-preview-area-container.height = clamp(h, i-preview-area-container.min-height, i-preview-area-container.max-height);
reselect();
}

width: i-preview-area-container.width;
Expand Down
2 changes: 2 additions & 0 deletions tools/lsp/ui/main.slint
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export component PreviewUi inherits Window {
callback select-behind(/* x */ length, /* y */ length, /* enter_component* */ bool, /* reverse */ bool);
callback show-document(/* url */ string, /* line */ int, /* column */ int);
callback style-changed();
callback reselect();
callback unselect();

property <length> border: 20px;
Expand Down Expand Up @@ -120,6 +121,7 @@ export component PreviewUi inherits Window {
select-behind(x, y, stay_in_file, reverse) => { root.select-behind(x, y, stay_in_file, reverse); }
show-document(url, line, column) => { root.show-document(url, line, column); }
unselect() => { root.unselect(); }
reselect() => { root.reselect(); }
}

preferred-width: draw-area.preferred-width + root.side-bar-width /* for left-side-bar */;
Expand Down
Loading