wasm preview ui: Implement highlight from editor

This commit is contained in:
Tobias Hunger 2023-09-08 15:56:55 +02:00 committed by Olivier Goffart
parent 7d4a73b76c
commit 4310969b2a
3 changed files with 41 additions and 24 deletions

View file

@ -170,3 +170,20 @@ pub fn set_preview_factory(
});
ui.set_preview_area(factory);
}
/// Highlight the element pointed at the offset in the path.
/// When path is None, remove the highlight.
pub fn highlight(path: Option<PathBuf>, offset: u32) {
let highlight = path.clone().map(|x| (x, offset));
let mut cache = CONTENT_CACHE.get_or_init(Default::default).lock().unwrap();
if cache.highlight == highlight {
return;
}
cache.highlight = highlight;
if cache.highlight.as_ref().map_or(true, |(path, _)| cache.dependency.contains(path)) {
let path = path.unwrap_or_default();
update_highlight(path, offset);
}
}

View file

@ -348,28 +348,15 @@ pub fn notify_diagnostics(diagnostics: &[slint_interpreter::Diagnostic]) -> Opti
/// Highlight the element pointed at the offset in the path.
/// When path is None, remove the highlight.
pub fn highlight(path: Option<PathBuf>, offset: u32) {
let highlight = path.map(|x| (x, offset));
let mut cache = super::CONTENT_CACHE.get_or_init(Default::default).lock().unwrap();
if cache.highlight == highlight {
return;
}
cache.highlight = highlight;
if cache.highlight.as_ref().map_or(true, |(path, _)| cache.dependency.contains(path)) {
run_in_ui_thread(move || async move {
PREVIEW_STATE.with(|preview_state| {
let preview_state = preview_state.borrow();
let handle = preview_state.handle.borrow();
if let (Some(cache), Some(handle)) = (super::CONTENT_CACHE.get(), &*handle) {
if let Some((path, offset)) = cache.lock().unwrap().highlight.clone() {
handle.highlight(path, offset);
} else {
handle.highlight(PathBuf::default(), 0);
}
}
})
pub fn update_highlight(path: PathBuf, offset: u32) {
let path = path.to_path_buf();
run_in_ui_thread(move || async move {
PREVIEW_STATE.with(|preview_state| {
let preview_state = preview_state.borrow();
let handle = preview_state.handle.borrow();
if let Some(handle) = &*handle {
handle.highlight(path, offset);
}
})
}
})
}

View file

@ -503,7 +503,7 @@ impl PreviewConnector {
Ok(())
}
M::HighlightFromEditor { path, offset } => {
log(&format!("highlight {path:?}:{offset}"));
super::highlight(path.map(|s| PathBuf::from(s)), offset);
Ok(())
}
}
@ -646,3 +646,16 @@ pub fn update_preview_area(compiled: slint_interpreter::ComponentDefinition) {
);
})
}
pub fn update_highlight(path: PathBuf, offset: u32) {
slint::invoke_from_event_loop(move || {
PREVIEW_STATE.with(|preview_state| {
let preview_state = preview_state.borrow();
let handle = preview_state.handle.borrow();
if let Some(handle) = &*handle {
handle.highlight(path, offset);
}
})
})
.unwrap();
}