live-preview: Add a "show preview" button into the component list

... It is *UGLY*, really *UGLY*. But it works ;-)
This commit is contained in:
Tobias Hunger 2024-06-20 12:51:10 +02:00 committed by Tobias Hunger
parent 35b61fa852
commit 1b88f70086
4 changed files with 29 additions and 0 deletions

View file

@ -296,6 +296,18 @@ fn show_component(name: slint::SharedString, file: slint::SharedString) {
ask_editor_to_show_document(&file.to_string_lossy(), lsp_types::Range::new(start, start))
}
// triggered from the UI, running in UI thread
fn show_preview_for(name: slint::SharedString, url: slint::SharedString) {
let name = name.to_string();
let Ok(url) = Url::parse(&url.to_string()) else {
return;
};
let current = PreviewComponent { url, component: Some(name), style: String::new() };
load_preview(current);
}
// triggered from the UI, running in UI thread
fn can_drop_component(
component_type: slint::SharedString,

View file

@ -43,6 +43,7 @@ pub fn create_ui(style: String, experimental: bool) -> Result<PreviewUi, Platfor
let pos = Position::new((line as u32).saturating_sub(1), (column as u32).saturating_sub(1));
super::ask_editor_to_show_document(&file, Range::new(pos, pos))
});
ui.on_show_preview_for(super::show_preview_for);
ui.on_unselect(super::element_selection::unselect_element);
ui.on_reselect(super::element_selection::reselect_element);
ui.on_select_at(super::element_selection::select_element_at);

View file

@ -31,6 +31,7 @@ export component ComponentList {
callback drop(string/* component_type */, length/* x */, length/* y */);
callback add_new_component();
callback rename_component(string/* old_name */, string/* defined_at */, string/* new_name */);
callback show_preview_for(string/* name */, string/*url*/);
private property <bool> preview-visible: preview-area-width > 0px && preview-area-height > 0px;
private property <int> editing-in-category: -1;
@ -104,6 +105,17 @@ export component ComponentList {
}
}
if ci.is_user_defined && !ci.is-currently-shown: preview-button := Button {
x: parent.x + parent.width - self.width - 4px;
text: "P";
width: self.min-width;
height: self.min-height;
clicked() => {
root.show_preview_for(component.data.name, component.data.defined_at)
}
}
pointer-event(event) => {
if (self.can-drop-here && event.kind == PointerEventKind.up && event.button == PointerEventButton.left) {
root.drop(self.data.name, drop-x, drop-y);

View file

@ -39,6 +39,7 @@ export component PreviewUi inherits Window {
callback show-component(/* name */ string, /* file */ string);
// Show a position consisiting of `line` and `column` in a `file` in the editor
callback show-document(/* file */ string, /* line */ int, /* column */ int);
callback show_preview_for(string/* name */, string/*url*/);
callback style-changed();
callback unselect();
@ -123,6 +124,9 @@ export component PreviewUi inherits Window {
rename_component(on, op, nn) => {
return root.rename_component(on, op, nn);
}
show_preview_for(cn, cp) => {
return root.show_preview_for(cn, cp);
}
}
}