|
@@ -13,7 +13,7 @@ use tauri_runtime::{
|
|
|
dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size},
|
|
|
DetachedWindow, PendingWindow, WindowEvent,
|
|
|
},
|
|
|
- Dispatch, Error, Icon, Params, Result, RunIteration, Runtime, RuntimeHandle,
|
|
|
+ Dispatch, Error, Icon, Params, Result, RunEvent, RunIteration, Runtime, RuntimeHandle,
|
|
|
};
|
|
|
|
|
|
#[cfg(feature = "menu")]
|
|
@@ -951,6 +951,7 @@ struct TrayContext {
|
|
|
}
|
|
|
|
|
|
struct WebviewWrapper {
|
|
|
+ label: String,
|
|
|
inner: WebView,
|
|
|
#[cfg(feature = "menu")]
|
|
|
menu_items: HashMap<u32, WryCustomMenuItem>,
|
|
@@ -1130,7 +1131,7 @@ impl Runtime for Wry {
|
|
|
}
|
|
|
|
|
|
#[cfg(any(target_os = "windows", target_os = "macos"))]
|
|
|
- fn run_iteration(&mut self) -> RunIteration {
|
|
|
+ fn run_iteration<F: Fn(RunEvent) + 'static>(&mut self, callback: F) -> RunIteration {
|
|
|
use wry::application::platform::run_return::EventLoopExtRunReturn;
|
|
|
let webviews = self.webviews.clone();
|
|
|
let window_event_listeners = self.window_event_listeners.clone();
|
|
@@ -1153,7 +1154,7 @@ impl Runtime for Wry {
|
|
|
event_loop,
|
|
|
control_flow,
|
|
|
EventLoopIterationContext {
|
|
|
- callback: None,
|
|
|
+ callback: &callback,
|
|
|
webviews: webviews.lock().expect("poisoned webview collection"),
|
|
|
window_event_listeners: window_event_listeners.clone(),
|
|
|
#[cfg(feature = "menu")]
|
|
@@ -1168,7 +1169,7 @@ impl Runtime for Wry {
|
|
|
iteration
|
|
|
}
|
|
|
|
|
|
- fn run<F: Fn() + 'static>(self, callback: F) {
|
|
|
+ fn run<F: Fn(RunEvent) + 'static>(self, callback: F) {
|
|
|
self.is_event_loop_running.store(true, Ordering::Relaxed);
|
|
|
let webviews = self.webviews.clone();
|
|
|
let window_event_listeners = self.window_event_listeners.clone();
|
|
@@ -1183,7 +1184,7 @@ impl Runtime for Wry {
|
|
|
event_loop,
|
|
|
control_flow,
|
|
|
EventLoopIterationContext {
|
|
|
- callback: Some(&callback),
|
|
|
+ callback: &callback,
|
|
|
webviews: webviews.lock().expect("poisoned webview collection"),
|
|
|
window_event_listeners: window_event_listeners.clone(),
|
|
|
#[cfg(feature = "menu")]
|
|
@@ -1197,7 +1198,7 @@ impl Runtime for Wry {
|
|
|
}
|
|
|
|
|
|
struct EventLoopIterationContext<'a> {
|
|
|
- callback: Option<&'a (dyn Fn() + 'static)>,
|
|
|
+ callback: &'a (dyn Fn(RunEvent) + 'static),
|
|
|
webviews: MutexGuard<'a, HashMap<WindowId, WebviewWrapper>>,
|
|
|
window_event_listeners: WindowEventListeners,
|
|
|
#[cfg(feature = "menu")]
|
|
@@ -1273,13 +1274,7 @@ fn handle_event_loop(
|
|
|
}
|
|
|
match event {
|
|
|
WryWindowEvent::CloseRequested => {
|
|
|
- webviews.remove(&window_id);
|
|
|
- if webviews.is_empty() {
|
|
|
- *control_flow = ControlFlow::Exit;
|
|
|
- if let Some(callback) = callback {
|
|
|
- callback();
|
|
|
- }
|
|
|
- }
|
|
|
+ on_window_close(callback, window_id, &mut webviews, control_flow);
|
|
|
}
|
|
|
WryWindowEvent::Resized(_) => {
|
|
|
if let Err(e) = webviews[&window_id].inner.resize() {
|
|
@@ -1365,10 +1360,7 @@ fn handle_event_loop(
|
|
|
WindowMessage::Show => window.set_visible(true),
|
|
|
WindowMessage::Hide => window.set_visible(false),
|
|
|
WindowMessage::Close => {
|
|
|
- webviews.remove(&id);
|
|
|
- if webviews.is_empty() {
|
|
|
- *control_flow = ControlFlow::Exit;
|
|
|
- }
|
|
|
+ on_window_close(callback, id, &mut webviews, control_flow);
|
|
|
}
|
|
|
WindowMessage::SetDecorations(decorations) => window.set_decorations(decorations),
|
|
|
WindowMessage::SetAlwaysOnTop(always_on_top) => window.set_always_on_top(always_on_top),
|
|
@@ -1489,6 +1481,21 @@ fn handle_event_loop(
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+fn on_window_close<'a>(
|
|
|
+ callback: &'a (dyn Fn(RunEvent) + 'static),
|
|
|
+ window_id: WindowId,
|
|
|
+ webviews: &mut MutexGuard<'a, HashMap<WindowId, WebviewWrapper>>,
|
|
|
+ control_flow: &mut ControlFlow,
|
|
|
+) {
|
|
|
+ if let Some(webview) = webviews.remove(&window_id) {
|
|
|
+ callback(RunEvent::WindowClose(webview.label));
|
|
|
+ }
|
|
|
+ if webviews.is_empty() {
|
|
|
+ *control_flow = ControlFlow::Exit;
|
|
|
+ callback(RunEvent::Exit);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
fn center_window(window: &Window) -> Result<()> {
|
|
|
if let Some(monitor) = window.current_monitor() {
|
|
|
let screen_size = monitor.size();
|
|
@@ -1534,8 +1541,11 @@ fn create_webview<P: Params<Runtime = Wry>>(
|
|
|
webview_builder.with_rpc_handler(create_rpc_handler(context.clone(), label.clone(), handler));
|
|
|
}
|
|
|
if let Some(handler) = file_drop_handler {
|
|
|
- webview_builder =
|
|
|
- webview_builder.with_file_drop_handler(create_file_drop_handler(context, label, handler));
|
|
|
+ webview_builder = webview_builder.with_file_drop_handler(create_file_drop_handler(
|
|
|
+ context,
|
|
|
+ label.clone(),
|
|
|
+ handler,
|
|
|
+ ));
|
|
|
}
|
|
|
for (scheme, protocol) in webview_attributes.uri_scheme_protocols {
|
|
|
webview_builder = webview_builder.with_custom_protocol(scheme, move |_window, url| {
|
|
@@ -1558,6 +1568,7 @@ fn create_webview<P: Params<Runtime = Wry>>(
|
|
|
.map_err(|e| Error::CreateWebview(Box::new(e)))?;
|
|
|
|
|
|
Ok(WebviewWrapper {
|
|
|
+ label: format!("{}", label),
|
|
|
inner: webview,
|
|
|
#[cfg(feature = "menu")]
|
|
|
menu_items,
|