live-preview: Send telemetry to VSCode

Send telemetry data from the live-preview to vscode.

It will process that data according to telemetry settings in VScode
This commit is contained in:
Tobias Hunger 2025-05-09 15:13:06 +00:00 committed by Tobias Hunger
parent 5a9dc2152b
commit 24e24df737
5 changed files with 34 additions and 0 deletions

View file

@ -532,6 +532,8 @@ pub enum PreviewToLspMessage {
SendWorkspaceEdit { label: Option<String>, edit: lsp_types::WorkspaceEdit },
/// Pass a `ShowMessage` notification on to the editor
SendShowMessage { message: lsp_types::ShowMessageParams },
/// Senf a telemetry event
TelemetryEvent(serde_json::Map<String, serde_json::Value>),
}
/// Information on the Element types available

View file

@ -581,6 +581,11 @@ async fn handle_preview_to_lsp_message(
ctx.server_notifier
.send_notification::<lsp_types::notification::ShowMessage>(message)?;
}
M::TelemetryEvent(object) => {
ctx.server_notifier.send_notification::<lsp_types::notification::TelemetryEvent>(
lsp_types::OneOf::Left(object),
)?
}
}
Ok(())
}

View file

@ -538,6 +538,8 @@ fn set_code_binding(
property_name: slint::SharedString,
property_value: slint::SharedString,
) {
send_telemetry(&mut [("type".to_string(), serde_json::to_value("property_changed").unwrap())]);
set_binding(
element_url,
element_version,
@ -685,6 +687,8 @@ fn can_drop_component(component_index: i32, x: f32, y: f32, on_drop_area: bool)
// triggered from the UI, running in UI thread
fn drop_component(component_index: i32, x: f32, y: f32) {
send_telemetry(&mut [("type".to_string(), serde_json::to_value("component_dropped").unwrap())]);
let Some(document_cache) = document_cache() else {
return;
};
@ -1886,6 +1890,17 @@ pub fn lsp_to_preview_message(message: crate::common::LspToPreviewMessage) {
}
}
pub fn send_telemetry(data: &mut [(String, serde_json::Value)]) {
let object = {
let mut object = serde_json::Map::new();
for (name, value) in data.iter_mut() {
object.insert(std::mem::take(name), std::mem::take(value));
}
object
};
send_message_to_lsp(crate::common::PreviewToLspMessage::TelemetryEvent(object));
}
#[cfg(test)]
pub mod test {
use std::{collections::HashMap, path::PathBuf, rc::Rc};

View file

@ -176,6 +176,10 @@ pub(super) fn open_ui_impl(preview_state: &mut PreviewState) -> Result<(), slint
Some(ui) => ui,
None => {
let ui = super::ui::create_ui(default_style, experimental)?;
crate::preview::send_telemetry(&mut [(
"type".to_string(),
serde_json::to_value("preview_opened").unwrap(),
)]);
preview_state.ui.insert(ui)
}
};

View file

@ -302,6 +302,14 @@ impl SlintServer {
.server_notifier
.send_notification::<lsp_types::notification::ShowMessage>(message);
}
M::TelemetryEvent(object) => {
let _ = self
.ctx
.server_notifier
.send_notification::<lsp_types::notification::TelemetryEvent>(
lsp_types::OneOf::Left(object),
);
}
}
Ok(())
}