LSP: Remove design mode custom commands

This commit is contained in:
Tobias Hunger 2023-08-31 18:43:49 +02:00 committed by Olivier Goffart
parent 97f3046dbd
commit a0cc85bfb4
6 changed files with 14 additions and 72 deletions

View file

@ -14,8 +14,6 @@ pub type Result<T> = std::result::Result<T, Error>;
/// API used by the LSP to talk to the Preview. The other direction uses the /// API used by the LSP to talk to the Preview. The other direction uses the
/// ServerNotifier /// ServerNotifier
pub trait PreviewApi { pub trait PreviewApi {
fn set_design_mode(&self, enable: bool);
fn design_mode(&self) -> bool;
fn set_contents(&self, path: &Path, contents: &str); fn set_contents(&self, path: &Path, contents: &str);
fn load_preview(&self, component: PreviewComponent, behavior: PostLoadBehavior); fn load_preview(&self, component: PreviewComponent, behavior: PostLoadBehavior);
fn config_changed( fn config_changed(

View file

@ -47,8 +47,6 @@ const QUERY_PROPERTIES_COMMAND: &str = "slint/queryProperties";
const REMOVE_BINDING_COMMAND: &str = "slint/removeBinding"; const REMOVE_BINDING_COMMAND: &str = "slint/removeBinding";
const SHOW_PREVIEW_COMMAND: &str = "slint/showPreview"; const SHOW_PREVIEW_COMMAND: &str = "slint/showPreview";
const SET_BINDING_COMMAND: &str = "slint/setBinding"; const SET_BINDING_COMMAND: &str = "slint/setBinding";
const SET_DESIGN_MODE_COMMAND: &str = "slint/setDesignMode";
const TOGGLE_DESIGN_MODE_COMMAND: &str = "slint/toggleDesignMode";
pub fn uri_to_file(uri: &lsp_types::Url) -> Option<PathBuf> { pub fn uri_to_file(uri: &lsp_types::Url) -> Option<PathBuf> {
let Ok(path) = uri.to_file_path() else { return None }; let Ok(path) = uri.to_file_path() else { return None };
@ -62,11 +60,7 @@ fn command_list() -> Vec<String> {
REMOVE_BINDING_COMMAND.into(), REMOVE_BINDING_COMMAND.into(),
#[cfg(any(feature = "preview", feature = "preview-lense"))] #[cfg(any(feature = "preview", feature = "preview-lense"))]
SHOW_PREVIEW_COMMAND.into(), SHOW_PREVIEW_COMMAND.into(),
#[cfg(any(feature = "preview", feature = "preview-lense"))]
SET_DESIGN_MODE_COMMAND.into(),
SET_BINDING_COMMAND.into(), SET_BINDING_COMMAND.into(),
#[cfg(any(feature = "preview", feature = "preview-lense"))]
TOGGLE_DESIGN_MODE_COMMAND.into(),
] ]
} }
@ -266,16 +260,6 @@ pub fn register_request_handlers(rh: &mut RequestHandler) {
show_preview_command(&params.arguments, &ctx)?; show_preview_command(&params.arguments, &ctx)?;
return Ok(None::<serde_json::Value>); return Ok(None::<serde_json::Value>);
} }
if params.command.as_str() == SET_DESIGN_MODE_COMMAND {
#[cfg(feature = "preview")]
set_design_mode(&params.arguments, &ctx)?;
return Ok(None::<serde_json::Value>);
}
if params.command.as_str() == TOGGLE_DESIGN_MODE_COMMAND {
#[cfg(feature = "preview")]
toggle_design_mode(&params.arguments, &ctx)?;
return Ok(None::<serde_json::Value>);
}
if params.command.as_str() == QUERY_PROPERTIES_COMMAND { if params.command.as_str() == QUERY_PROPERTIES_COMMAND {
return Ok(Some(query_properties_command(&params.arguments, &ctx)?)); return Ok(Some(query_properties_command(&params.arguments, &ctx)?));
} }
@ -328,7 +312,6 @@ pub fn register_request_handlers(rh: &mut RequestHandler) {
Ok(semantic_tokens::get_semantic_tokens(document_cache, &params.text_document)) Ok(semantic_tokens::get_semantic_tokens(document_cache, &params.text_document))
}); });
rh.register::<DocumentHighlightRequest, _>(|_params, ctx| async move { rh.register::<DocumentHighlightRequest, _>(|_params, ctx| async move {
eprintln!("DOCUMENTHIGHLIGHTREQUEST RECEIVED");
let document_cache = &mut ctx.document_cache.borrow_mut(); let document_cache = &mut ctx.document_cache.borrow_mut();
let uri = _params.text_document_position_params.text_document.uri; let uri = _params.text_document_position_params.text_document.uri;
if let Some((tk, _off)) = if let Some((tk, _off)) =
@ -424,25 +407,6 @@ pub fn show_preview_command(params: &[serde_json::Value], ctx: &Rc<Context>) ->
Ok(()) Ok(())
} }
#[cfg(feature = "preview")]
pub fn set_design_mode(params: &[serde_json::Value], ctx: &Rc<Context>) -> Result<()> {
let e = || "InvalidParameter";
let enable = if let serde_json::Value::Bool(b) = params.get(0).ok_or_else(e)? {
b
} else {
return Err(e().into());
};
ctx.preview.set_design_mode(*enable);
Ok(())
}
#[cfg(feature = "preview")]
pub fn toggle_design_mode(_params: &[serde_json::Value], ctx: &Rc<Context>) -> Result<()> {
ctx.preview.set_design_mode(!ctx.preview.design_mode());
Ok(())
}
pub fn query_properties_command( pub fn query_properties_command(
params: &[serde_json::Value], params: &[serde_json::Value],
ctx: &Rc<Context>, ctx: &Rc<Context>,

View file

@ -36,18 +36,6 @@ struct Previewer {
} }
impl PreviewApi for Previewer { impl PreviewApi for Previewer {
fn set_design_mode(&self, _enable: bool) {
#[cfg(feature = "preview")]
preview::set_design_mode(_enable);
}
fn design_mode(&self) -> bool {
#[cfg(not(feature = "preview"))]
return false;
#[cfg(feature = "preview")]
return preview::design_mode();
}
fn set_contents(&self, _path: &std::path::Path, _contents: &str) { fn set_contents(&self, _path: &std::path::Path, _contents: &str) {
#[cfg(feature = "preview")] #[cfg(feature = "preview")]
preview::set_contents(_path, _contents.to_string()); preview::set_contents(_path, _contents.to_string());

View file

@ -131,18 +131,19 @@ pub fn open_ui(sender: &ServerNotifier) {
cache.sender = Some(sender.clone()); cache.sender = Some(sender.clone());
} }
let r = i_slint_core::api::invoke_from_event_loop(move || { i_slint_core::api::invoke_from_event_loop(move || {
PREVIEW_STATE.with(|preview_state| { PREVIEW_STATE.with(|preview_state| {
let mut preview_state = preview_state.borrow_mut(); let mut preview_state = preview_state.borrow_mut();
open_ui_impl(&mut preview_state) open_ui_impl(&mut preview_state)
}); });
}); })
r.unwrap(); .unwrap();
} }
fn open_ui_impl(preview_state: &mut PreviewState) { fn open_ui_impl(preview_state: &mut PreviewState) {
// TODO: Handle Error! // TODO: Handle Error!
let ui = preview_state.ui.get_or_insert_with(|| super::ui::PreviewUi::new().unwrap()); let ui = preview_state.ui.get_or_insert_with(|| super::ui::PreviewUi::new().unwrap());
ui.on_design_mode_changed(|design_mode| set_design_mode(design_mode));
ui.show().unwrap(); ui.show().unwrap();
} }
@ -255,12 +256,7 @@ struct PreviewState {
} }
thread_local! {static PREVIEW_STATE: std::cell::RefCell<PreviewState> = Default::default();} thread_local! {static PREVIEW_STATE: std::cell::RefCell<PreviewState> = Default::default();}
pub fn design_mode() -> bool { fn set_design_mode(enable: bool) {
let cache = CONTENT_CACHE.get_or_init(Default::default).lock().unwrap();
cache.design_mode
}
pub fn set_design_mode(enable: bool) {
let mut cache = CONTENT_CACHE.get_or_init(Default::default).lock().unwrap(); let mut cache = CONTENT_CACHE.get_or_init(Default::default).lock().unwrap();
let Some(sender) = cache.sender.clone() else { let Some(sender) = cache.sender.clone() else {
return; return;

View file

@ -2,14 +2,19 @@
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
slint::slint!( slint::slint!(
import { VerticalBox } from "std-widgets.slint"; import { Button, VerticalBox } from "std-widgets.slint";
export component PreviewUi inherits Window { export component PreviewUi inherits Window {
in property<component-factory> preview_area <=> cc.component-factory; in property<component-factory> preview_area <=> preview_area_container.component-factory;
callback design_mode_changed(bool);
VerticalBox { VerticalBox {
Text { text: "Welcome to the Slint Preview"; } design_mode_toggle := Button {
cc := ComponentContainer {} text: "Design Mode";
checkable: true;
clicked => { root.design_mode_changed(self.checked); }
}
preview_area_container := ComponentContainer {}
} }
} }
); );

View file

@ -48,15 +48,6 @@ struct Previewer {
} }
impl PreviewApi for Previewer { impl PreviewApi for Previewer {
fn set_design_mode(&self, _enable: bool) {
// do nothing!
}
fn design_mode(&self) -> bool {
// do nothing!
false
}
fn set_contents(&self, _path: &std::path::Path, _contents: &str) { fn set_contents(&self, _path: &std::path::Path, _contents: &str) {
// do nothing! // do nothing!
} }