From 1b88f700863faa107546e40a2c366b16dde912d0 Mon Sep 17 00:00:00 2001 From: Tobias Hunger Date: Thu, 20 Jun 2024 12:51:10 +0200 Subject: [PATCH] live-preview: Add a "show preview" button into the component list ... It is *UGLY*, really *UGLY*. But it works ;-) --- tools/lsp/preview.rs | 12 ++++++++++++ tools/lsp/preview/ui.rs | 1 + tools/lsp/ui/component-list.slint | 12 ++++++++++++ tools/lsp/ui/main.slint | 4 ++++ 4 files changed, 29 insertions(+) diff --git a/tools/lsp/preview.rs b/tools/lsp/preview.rs index 66286781d..c42dd54dc 100644 --- a/tools/lsp/preview.rs +++ b/tools/lsp/preview.rs @@ -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, diff --git a/tools/lsp/preview/ui.rs b/tools/lsp/preview/ui.rs index cc4acd7f8..dbf3fee67 100644 --- a/tools/lsp/preview/ui.rs +++ b/tools/lsp/preview/ui.rs @@ -43,6 +43,7 @@ pub fn create_ui(style: String, experimental: bool) -> Result preview-visible: preview-area-width > 0px && preview-area-height > 0px; private property 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); diff --git a/tools/lsp/ui/main.slint b/tools/lsp/ui/main.slint index 0320c2205..4fc543bfe 100644 --- a/tools/lsp/ui/main.slint +++ b/tools/lsp/ui/main.slint @@ -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); + } } }