|
@@ -239,21 +239,39 @@ impl<T: UserEvent> Context<T> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-#[derive(Clone)]
|
|
|
+#[cfg(feature = "tracing")]
|
|
|
+#[derive(Debug, Clone, Default)]
|
|
|
+pub struct ActiveTraceSpanStore(Rc<RefCell<Vec<ActiveTracingSpan>>>);
|
|
|
+
|
|
|
+#[cfg(feature = "tracing")]
|
|
|
+impl ActiveTraceSpanStore {
|
|
|
+ pub fn remove_window_draw(&self, window_id: WindowId) {
|
|
|
+ let mut store = self.0.borrow_mut();
|
|
|
+ if let Some(index) = store
|
|
|
+ .iter()
|
|
|
+ .position(|t| matches!(t, ActiveTracingSpan::WindowDraw { id, span: _ } if id == &window_id))
|
|
|
+ {
|
|
|
+ store.remove(index);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#[cfg(feature = "tracing")]
|
|
|
+#[derive(Debug)]
|
|
|
+pub enum ActiveTracingSpan {
|
|
|
+ WindowDraw {
|
|
|
+ id: WindowId,
|
|
|
+ span: tracing::span::EnteredSpan,
|
|
|
+ },
|
|
|
+}
|
|
|
+
|
|
|
+#[derive(Debug, Clone)]
|
|
|
pub struct DispatcherMainThreadContext<T: UserEvent> {
|
|
|
pub window_target: EventLoopWindowTarget<Message<T>>,
|
|
|
pub web_context: WebContextStore,
|
|
|
pub windows: Rc<RefCell<HashMap<WebviewId, WindowWrapper>>>,
|
|
|
-}
|
|
|
-
|
|
|
-impl<T: UserEvent> std::fmt::Debug for DispatcherMainThreadContext<T> {
|
|
|
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
- f.debug_struct("DispatcherMainThreadContext")
|
|
|
- .field("window_target", &self.window_target)
|
|
|
- .field("web_context", &self.web_context)
|
|
|
- .field("windows", &self.windows)
|
|
|
- .finish()
|
|
|
- }
|
|
|
+ #[cfg(feature = "tracing")]
|
|
|
+ pub active_tracing_spans: ActiveTraceSpanStore,
|
|
|
}
|
|
|
|
|
|
// SAFETY: we ensure this type is only used on the main thread.
|
|
@@ -1051,7 +1069,10 @@ pub enum WindowMessage {
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
pub enum WebviewMessage {
|
|
|
+ #[cfg(not(feature = "tracing"))]
|
|
|
EvaluateScript(String),
|
|
|
+ #[cfg(feature = "tracing")]
|
|
|
+ EvaluateScript(String, Sender<()>, tracing::Span),
|
|
|
#[allow(dead_code)]
|
|
|
WebviewEvent(WebviewEvent),
|
|
|
Print,
|
|
@@ -1560,6 +1581,21 @@ impl<T: UserEvent> Dispatch<T> for WryDispatcher<T> {
|
|
|
)
|
|
|
}
|
|
|
|
|
|
+ #[cfg(feature = "tracing")]
|
|
|
+ fn eval_script<S: Into<String>>(&self, script: S) -> Result<()> {
|
|
|
+ // use a channel so the EvaluateScript task uses the current span as parent
|
|
|
+ let (tx, rx) = channel();
|
|
|
+ getter!(
|
|
|
+ self,
|
|
|
+ rx,
|
|
|
+ Message::Webview(
|
|
|
+ self.window_id,
|
|
|
+ WebviewMessage::EvaluateScript(script.into(), tx, tracing::Span::current()),
|
|
|
+ )
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ #[cfg(not(feature = "tracing"))]
|
|
|
fn eval_script<S: Into<String>>(&self, script: S) -> Result<()> {
|
|
|
send_user_message(
|
|
|
&self.context,
|
|
@@ -1867,6 +1903,8 @@ impl<T: UserEvent> Wry<T> {
|
|
|
window_target: event_loop.deref().clone(),
|
|
|
web_context,
|
|
|
windows,
|
|
|
+ #[cfg(feature = "tracing")]
|
|
|
+ active_tracing_spans: Default::default(),
|
|
|
},
|
|
|
plugins: Default::default(),
|
|
|
next_window_id: Default::default(),
|
|
@@ -2009,6 +2047,9 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
|
|
|
let web_context = &self.context.main_thread.web_context;
|
|
|
let plugins = self.context.plugins.clone();
|
|
|
|
|
|
+ #[cfg(feature = "tracing")]
|
|
|
+ let active_tracing_spans = self.context.main_thread.active_tracing_spans.clone();
|
|
|
+
|
|
|
let mut iteration = RunIteration::default();
|
|
|
|
|
|
let proxy = self.event_loop.create_proxy();
|
|
@@ -2031,6 +2072,8 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
|
|
|
callback: &mut callback,
|
|
|
webview_id_map: webview_id_map.clone(),
|
|
|
windows: windows.clone(),
|
|
|
+ #[cfg(feature = "tracing")]
|
|
|
+ active_tracing_spans: active_tracing_spans.clone(),
|
|
|
},
|
|
|
web_context,
|
|
|
);
|
|
@@ -2047,6 +2090,8 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
|
|
|
callback: &mut callback,
|
|
|
windows: windows.clone(),
|
|
|
webview_id_map: webview_id_map.clone(),
|
|
|
+ #[cfg(feature = "tracing")]
|
|
|
+ active_tracing_spans: active_tracing_spans.clone(),
|
|
|
},
|
|
|
web_context,
|
|
|
);
|
|
@@ -2061,6 +2106,8 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
|
|
|
let web_context = self.context.main_thread.web_context;
|
|
|
let plugins = self.context.plugins.clone();
|
|
|
|
|
|
+ #[cfg(feature = "tracing")]
|
|
|
+ let active_tracing_spans = self.context.main_thread.active_tracing_spans.clone();
|
|
|
let proxy = self.event_loop.create_proxy();
|
|
|
|
|
|
self.event_loop.run(move |event, event_loop, control_flow| {
|
|
@@ -2074,6 +2121,8 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
|
|
|
callback: &mut callback,
|
|
|
webview_id_map: webview_id_map.clone(),
|
|
|
windows: windows.clone(),
|
|
|
+ #[cfg(feature = "tracing")]
|
|
|
+ active_tracing_spans: active_tracing_spans.clone(),
|
|
|
},
|
|
|
&web_context,
|
|
|
);
|
|
@@ -2089,6 +2138,8 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
|
|
|
callback: &mut callback,
|
|
|
webview_id_map: webview_id_map.clone(),
|
|
|
windows: windows.clone(),
|
|
|
+ #[cfg(feature = "tracing")]
|
|
|
+ active_tracing_spans: active_tracing_spans.clone(),
|
|
|
},
|
|
|
&web_context,
|
|
|
);
|
|
@@ -2100,6 +2151,8 @@ pub struct EventLoopIterationContext<'a, T: UserEvent> {
|
|
|
pub callback: &'a mut (dyn FnMut(RunEvent<T>) + 'static),
|
|
|
pub webview_id_map: WebviewIdStore,
|
|
|
pub windows: Rc<RefCell<HashMap<WebviewId, WindowWrapper>>>,
|
|
|
+ #[cfg(feature = "tracing")]
|
|
|
+ pub active_tracing_spans: ActiveTraceSpanStore,
|
|
|
}
|
|
|
|
|
|
struct UserMessageContext {
|
|
@@ -2374,6 +2427,19 @@ fn handle_user_message<T: UserEvent>(
|
|
|
}
|
|
|
}
|
|
|
Message::Webview(id, webview_message) => match webview_message {
|
|
|
+ #[cfg(feature = "tracing")]
|
|
|
+ WebviewMessage::EvaluateScript(script, tx, span) => {
|
|
|
+ let _span = span.entered();
|
|
|
+ if let Some(WindowHandle::Webview { inner: webview, .. }) =
|
|
|
+ windows.borrow().get(&id).and_then(|w| w.inner.as_ref())
|
|
|
+ {
|
|
|
+ if let Err(e) = webview.evaluate_script(&script) {
|
|
|
+ debug_eprintln!("{}", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ tx.send(()).unwrap();
|
|
|
+ }
|
|
|
+ #[cfg(not(feature = "tracing"))]
|
|
|
WebviewMessage::EvaluateScript(script) => {
|
|
|
if let Some(WindowHandle::Webview { inner: webview, .. }) =
|
|
|
windows.borrow().get(&id).and_then(|w| w.inner.as_ref())
|
|
@@ -2441,6 +2507,8 @@ fn handle_event_loop<T: UserEvent>(
|
|
|
callback,
|
|
|
webview_id_map,
|
|
|
windows,
|
|
|
+ #[cfg(feature = "tracing")]
|
|
|
+ active_tracing_spans,
|
|
|
} = context;
|
|
|
if *control_flow != ControlFlow::Exit {
|
|
|
*control_flow = ControlFlow::Wait;
|
|
@@ -2463,6 +2531,11 @@ fn handle_event_loop<T: UserEvent>(
|
|
|
callback(RunEvent::Exit);
|
|
|
}
|
|
|
|
|
|
+ #[cfg(feature = "tracing")]
|
|
|
+ Event::RedrawRequested(id) => {
|
|
|
+ active_tracing_spans.remove_window_draw(id);
|
|
|
+ }
|
|
|
+
|
|
|
Event::UserEvent(Message::Webview(id, WebviewMessage::WebviewEvent(event))) => {
|
|
|
if let Some(event) = WindowEventWrapper::from(&event).0 {
|
|
|
let windows = windows.borrow();
|
|
@@ -2650,6 +2723,14 @@ fn create_webview<T: UserEvent, F: Fn(RawWindow) + Send + 'static>(
|
|
|
..
|
|
|
} = pending;
|
|
|
|
|
|
+ #[cfg(feature = "tracing")]
|
|
|
+ let _webview_create_span = tracing::debug_span!("wry::webview::create").entered();
|
|
|
+ #[cfg(feature = "tracing")]
|
|
|
+ let window_draw_span = tracing::debug_span!("wry::window::draw").entered();
|
|
|
+ #[cfg(feature = "tracing")]
|
|
|
+ let window_create_span =
|
|
|
+ tracing::debug_span!(parent: &window_draw_span, "wry::window::create").entered();
|
|
|
+
|
|
|
let window_event_listeners = WindowEventListeners::default();
|
|
|
|
|
|
#[cfg(windows)]
|
|
@@ -2678,6 +2759,21 @@ fn create_webview<T: UserEvent, F: Fn(RawWindow) + Send + 'static>(
|
|
|
let focused = window_builder.inner.window.focused;
|
|
|
let window = window_builder.inner.build(event_loop).unwrap();
|
|
|
|
|
|
+ #[cfg(feature = "tracing")]
|
|
|
+ {
|
|
|
+ drop(window_create_span);
|
|
|
+
|
|
|
+ context
|
|
|
+ .main_thread
|
|
|
+ .active_tracing_spans
|
|
|
+ .0
|
|
|
+ .borrow_mut()
|
|
|
+ .push(ActiveTracingSpan::WindowDraw {
|
|
|
+ id: window.id(),
|
|
|
+ span: window_draw_span,
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
context.webview_id_map.insert(window.id(), window_id);
|
|
|
|
|
|
if window_builder.center {
|