From ad8a0cb73a23440eb430acfced1b96603383c128 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Thu, 26 Oct 2023 09:22:52 -0300 Subject: [PATCH] refactor(core): remove `on_page_load` --- .changes/remove-on-page-load.md | 5 + core/tauri/scripts/init.js | 18 +- core/tauri/src/app.rs | 31 ---- core/tauri/src/manager.rs | 21 +-- core/tauri/src/plugin.rs | 47 +---- core/tauri/src/window/mod.rs | 169 ++++++++---------- examples/api/src-tauri/Cargo.lock | 100 ++--------- examples/api/src-tauri/src/lib.rs | 25 ++- examples/multiwindow/index.html | 4 +- examples/multiwindow/main.rs | 16 +- examples/parent-window/index.html | 92 +++++----- examples/parent-window/main.rs | 12 +- .../jest/fixtures/app/src-tauri/src/main.rs | 7 +- 13 files changed, 185 insertions(+), 362 deletions(-) create mode 100644 .changes/remove-on-page-load.md diff --git a/.changes/remove-on-page-load.md b/.changes/remove-on-page-load.md new file mode 100644 index 000000000000..7dd88eb5f62f --- /dev/null +++ b/.changes/remove-on-page-load.md @@ -0,0 +1,5 @@ +--- +"tauri": patch:breaking +--- + +Removed the `on_page_load` hook from `Plugin` and `Builder`. Use `on_navigation` instead. diff --git a/core/tauri/scripts/init.js b/core/tauri/scripts/init.js index f9e0e66716e2..a418130fc14c 100644 --- a/core/tauri/scripts/init.js +++ b/core/tauri/scripts/init.js @@ -2,7 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT -;(function () { +; +(function () { __RAW_freeze_prototype__ __RAW_pattern_script__ @@ -12,21 +13,8 @@ __RAW_core_script__ __RAW_event_initialization_script__ - ;(function () { - __RAW_bundle_script__ - })() - if (window.ipc) { - window.__TAURI_INTERNALS__.invoke('__initialized', { - url: window.location.href - }) - } else { - window.addEventListener('DOMContentLoaded', function () { - window.__TAURI_INTERNALS__.invoke('__initialized', { - url: window.location.href - }) - }) - } + __RAW_bundle_script__ __RAW_plugin_initialization_script__ })() diff --git a/core/tauri/src/app.rs b/core/tauri/src/app.rs index fa993606971b..0d305457ff51 100644 --- a/core/tauri/src/app.rs +++ b/core/tauri/src/app.rs @@ -30,7 +30,6 @@ use crate::tray::{TrayIcon, TrayIconBuilder, TrayIconEvent, TrayIconId}; #[cfg(desktop)] use crate::window::WindowMenu; use raw_window_handle::HasRawDisplayHandle; -use serde::Deserialize; use serialize_to_javascript::{default_template, DefaultTemplate, Template}; use tauri_macros::default_runtime; #[cfg(desktop)] @@ -67,21 +66,6 @@ pub(crate) type GlobalWindowEventListener = Box) /// A closure that is run when the Tauri application is setting up. pub type SetupHook = Box) -> Result<(), Box> + Send>; -/// A closure that is run once every time a window is created and loaded. -pub type OnPageLoad = dyn Fn(Window, PageLoadPayload) + Send + Sync + 'static; - -/// The payload for the [`OnPageLoad`] hook. -#[derive(Debug, Clone, Deserialize)] -pub struct PageLoadPayload { - url: String, -} - -impl PageLoadPayload { - /// The page URL. - pub fn url(&self) -> &str { - &self.url - } -} /// Api exposed on the `ExitRequested` event. #[derive(Debug)] @@ -981,9 +965,6 @@ pub struct Builder { /// The setup hook. setup: SetupHook, - /// Page load hook. - on_page_load: Box>, - /// windows to create when starting up. pending_windows: Vec>, @@ -1040,7 +1021,6 @@ impl Builder { .render_default(&Default::default()) .unwrap() .into_string(), - on_page_load: Box::new(|_, _| ()), pending_windows: Default::default(), plugins: PluginStore::default(), uri_scheme_protocols: Default::default(), @@ -1126,16 +1106,6 @@ impl Builder { self } - /// Defines the page load hook. - #[must_use] - pub fn on_page_load(mut self, on_page_load: F) -> Self - where - F: Fn(Window, PageLoadPayload) + Send + Sync + 'static, - { - self.on_page_load = Box::new(on_page_load); - self - } - /// Adds a Tauri application plugin. /// /// A plugin is created using the [`crate::plugin::Builder`] struct.Check its documentation for more information. @@ -1477,7 +1447,6 @@ impl Builder { context, self.plugins, self.invoke_handler, - self.on_page_load, self.uri_scheme_protocols, self.state, self.window_event_listeners, diff --git a/core/tauri/src/manager.rs b/core/tauri/src/manager.rs index 2154b0979e81..d1d52776af6c 100644 --- a/core/tauri/src/manager.rs +++ b/core/tauri/src/manager.rs @@ -29,10 +29,7 @@ use tauri_utils::{ use crate::event::EmitArgs; use crate::{ - app::{ - AppHandle, GlobalWindowEvent, GlobalWindowEventListener, OnPageLoad, PageLoadPayload, - UriSchemeResponder, - }, + app::{AppHandle, GlobalWindowEvent, GlobalWindowEventListener, UriSchemeResponder}, event::{assert_event_name_is_valid, Event, EventId, Listeners}, ipc::{Invoke, InvokeHandler, InvokeResponder}, pattern::PatternJavascript, @@ -231,9 +228,6 @@ pub struct InnerWindowManager { /// The JS message handler. invoke_handler: Box>, - /// The page load hook, invoked when the webview performs a navigation. - on_page_load: Box>, - config: Arc, assets: Arc, pub(crate) default_window_icon: Option, @@ -339,7 +333,6 @@ impl WindowManager { #[allow(unused_mut)] mut context: Context, plugins: PluginStore, invoke_handler: Box>, - on_page_load: Box>, uri_scheme_protocols: HashMap>>, state: StateManager, window_event_listeners: Vec>, @@ -362,7 +355,6 @@ impl WindowManager { listeners: Listeners::default(), state: Arc::new(state), invoke_handler, - on_page_load, config: Arc::new(context.config), assets: context.assets, default_window_icon: context.default_window_icon, @@ -869,16 +861,6 @@ impl WindowManager { (self.inner.invoke_handler)(invoke) } - pub fn run_on_page_load(&self, window: Window, payload: PageLoadPayload) { - (self.inner.on_page_load)(window.clone(), payload.clone()); - self - .inner - .plugins - .lock() - .expect("poisoned plugin store") - .on_page_load(window, payload); - } - pub fn extend_api(&self, plugin: &str, invoke: Invoke) -> bool { self .inner @@ -1376,7 +1358,6 @@ mod test { context, PluginStore::default(), Box::new(|_| false), - Box::new(|_, _| ()), Default::default(), StateManager::new(), Default::default(), diff --git a/core/tauri/src/plugin.rs b/core/tauri/src/plugin.rs index 9f6a14260fd1..233e0e78babb 100644 --- a/core/tauri/src/plugin.rs +++ b/core/tauri/src/plugin.rs @@ -5,7 +5,7 @@ //! The Tauri plugin extension to expand Tauri functionality. use crate::{ - app::{PageLoadPayload, UriSchemeResponder}, + app::UriSchemeResponder, error::Error, ipc::{Invoke, InvokeHandler}, manager::UriSchemeProtocol, @@ -60,10 +60,6 @@ pub trait Plugin: Send { true } - /// Callback invoked when the webview performs a navigation to a page. - #[allow(unused_variables)] - fn on_page_load(&mut self, window: Window, payload: PageLoadPayload) {} - /// Callback invoked when the event loop receives a new event. #[allow(unused_variables)] fn on_event(&mut self, app: &AppHandle, event: &RunEvent) {} @@ -80,7 +76,6 @@ type SetupHook = type OnWebviewReady = dyn FnMut(Window) + Send; type OnEvent = dyn FnMut(&AppHandle, &RunEvent) + Send; type OnNavigation = dyn Fn(&Window, &Url) -> bool + Send; -type OnPageLoad = dyn FnMut(Window, PageLoadPayload) + Send; type OnDrop = dyn FnOnce(AppHandle) + Send; /// A handle to a plugin. @@ -207,7 +202,6 @@ pub struct Builder { setup: Option>>, js_init_script: Option, on_navigation: Box>, - on_page_load: Box>, on_webview_ready: Box>, on_event: Box>, on_drop: Option>>, @@ -223,7 +217,6 @@ impl Builder { js_init_script: None, invoke_handler: Box::new(|_| false), on_navigation: Box::new(|_, _| true), - on_page_load: Box::new(|_, _| ()), on_webview_ready: Box::new(|_| ()), on_event: Box::new(|_, _| ()), on_drop: None, @@ -358,30 +351,6 @@ impl Builder { self } - /// Callback invoked when the webview performs a navigation to a page. - /// - /// # Examples - /// - /// ```rust - /// use tauri::{plugin::{Builder, TauriPlugin}, Runtime}; - /// - /// fn init() -> TauriPlugin { - /// Builder::new("example") - /// .on_page_load(|window, payload| { - /// println!("Loaded URL {} in window {}", payload.url(), window.label()); - /// }) - /// .build() - /// } - /// ``` - #[must_use] - pub fn on_page_load(mut self, on_page_load: F) -> Self - where - F: FnMut(Window, PageLoadPayload) + Send + 'static, - { - self.on_page_load = Box::new(on_page_load); - self - } - /// Callback invoked when the webview is created. /// /// # Examples @@ -577,7 +546,6 @@ impl Builder { setup: self.setup, js_init_script: self.js_init_script, on_navigation: self.on_navigation, - on_page_load: self.on_page_load, on_webview_ready: self.on_webview_ready, on_event: self.on_event, on_drop: self.on_drop, @@ -594,7 +562,6 @@ pub struct TauriPlugin { setup: Option>>, js_init_script: Option, on_navigation: Box>, - on_page_load: Box>, on_webview_ready: Box>, on_event: Box>, on_drop: Option>>, @@ -652,10 +619,6 @@ impl Plugin for TauriPlugin { (self.on_navigation)(window, url) } - fn on_page_load(&mut self, window: Window, payload: PageLoadPayload) { - (self.on_page_load)(window, payload) - } - fn on_event(&mut self, app: &AppHandle, event: &RunEvent) { (self.on_event)(app, event) } @@ -749,14 +712,6 @@ impl PluginStore { true } - /// Runs the on_page_load hook for all plugins in the store. - pub(crate) fn on_page_load(&mut self, window: Window, payload: PageLoadPayload) { - self - .store - .iter_mut() - .for_each(|plugin| plugin.on_page_load(window.clone(), payload.clone())) - } - /// Runs the on_event hook for all plugins in the store. pub(crate) fn on_event(&mut self, app: &AppHandle, event: &RunEvent) { self diff --git a/core/tauri/src/window/mod.rs b/core/tauri/src/window/mod.rs index aa3122338265..dd7fce47b440 100644 --- a/core/tauri/src/window/mod.rs +++ b/core/tauri/src/window/mod.rs @@ -2155,110 +2155,99 @@ impl Window { request.error, ); - match request.cmd.as_str() { - "__initialized" => match request.body.deserialize() { - Ok(payload) => { - manager.run_on_page_load(self, payload); - resolver.resolve(()); - } - Err(e) => resolver.reject(e.to_string()), - }, - _ => { - #[cfg(mobile)] - let app_handle = self.app_handle.clone(); - - let message = InvokeMessage::new( - self, - manager.state(), - request.cmd.to_string(), - request.body, - request.headers, - ); - - let mut invoke = Invoke { - message, - resolver: resolver.clone(), - }; - - if !is_local && scope.is_none() { - invoke.resolver.reject(scope_not_found_error_message); - } else if request.cmd.starts_with("plugin:") { - let command = invoke.message.command.replace("plugin:", ""); - let mut tokens = command.split('|'); - // safe to unwrap: split always has a least one item - let plugin = tokens.next().unwrap(); - invoke.message.command = tokens - .next() - .map(|c| c.to_string()) - .unwrap_or_else(String::new); - - if !(is_local - || plugin == crate::ipc::channel::CHANNEL_PLUGIN_NAME - || scope - .map(|s| s.plugins().contains(&plugin.into())) - .unwrap_or(true)) - { - invoke.resolver.reject(IPC_SCOPE_DOES_NOT_ALLOW); - return; - } + #[cfg(mobile)] + let app_handle = self.app_handle.clone(); + + let message = InvokeMessage::new( + self, + manager.state(), + request.cmd.to_string(), + request.body, + request.headers, + ); - let command = invoke.message.command.clone(); + let mut invoke = Invoke { + message, + resolver: resolver.clone(), + }; - #[cfg(mobile)] - let message = invoke.message.clone(); + if !is_local && scope.is_none() { + invoke.resolver.reject(scope_not_found_error_message); + } else if request.cmd.starts_with("plugin:") { + let command = invoke.message.command.replace("plugin:", ""); + let mut tokens = command.split('|'); + // safe to unwrap: split always has a least one item + let plugin = tokens.next().unwrap(); + invoke.message.command = tokens + .next() + .map(|c| c.to_string()) + .unwrap_or_else(String::new); + + if !(is_local + || plugin == crate::ipc::channel::CHANNEL_PLUGIN_NAME + || scope + .map(|s| s.plugins().contains(&plugin.into())) + .unwrap_or(true)) + { + invoke.resolver.reject(IPC_SCOPE_DOES_NOT_ALLOW); + return; + } + + let command = invoke.message.command.clone(); - #[allow(unused_mut)] - let mut handled = manager.extend_api(plugin, invoke); + #[cfg(mobile)] + let message = invoke.message.clone(); - #[cfg(mobile)] - { - if !handled { - handled = true; + #[allow(unused_mut)] + let mut handled = manager.extend_api(plugin, invoke); - fn load_channels(payload: &serde_json::Value, window: &Window) { - if let serde_json::Value::Object(map) = payload { - for v in map.values() { - if let serde_json::Value::String(s) = v { - if s.starts_with(crate::ipc::channel::IPC_PAYLOAD_PREFIX) { - crate::ipc::Channel::load_from_ipc(window.clone(), s); - } - } + #[cfg(mobile)] + { + if !handled { + handled = true; + + fn load_channels(payload: &serde_json::Value, window: &Window) { + if let serde_json::Value::Object(map) = payload { + for v in map.values() { + if let serde_json::Value::String(s) = v { + if s.starts_with(crate::ipc::channel::IPC_PAYLOAD_PREFIX) { + crate::ipc::Channel::load_from_ipc(window.clone(), s); } } } - - let payload = message.payload.into_json(); - // initialize channels - load_channels(&payload, &message.window); - - let resolver_ = resolver.clone(); - if let Err(e) = crate::plugin::mobile::run_command( - plugin, - &app_handle, - message.command, - payload, - move |response| match response { - Ok(r) => resolver_.resolve(r), - Err(e) => resolver_.reject(e), - }, - ) { - resolver.reject(e.to_string()); - return; - } } } - if !handled { - resolver.reject(format!("Command {command} not found")); - } - } else { - let command = invoke.message.command.clone(); - let handled = manager.run_invoke_handler(invoke); - if !handled { - resolver.reject(format!("Command {command} not found")); + let payload = message.payload.into_json(); + // initialize channels + load_channels(&payload, &message.window); + + let resolver_ = resolver.clone(); + if let Err(e) = crate::plugin::mobile::run_command( + plugin, + &app_handle, + message.command, + payload, + move |response| match response { + Ok(r) => resolver_.resolve(r), + Err(e) => resolver_.reject(e), + }, + ) { + resolver.reject(e.to_string()); + return; } } } + + if !handled { + resolver.reject(format!("Command {command} not found")); + } + } else { + let command = invoke.message.command.clone(); + let handled = manager.run_invoke_handler(invoke); + if !handled { + resolver.reject(format!("Command {command} not found")); + } } } diff --git a/examples/api/src-tauri/Cargo.lock b/examples/api/src-tauri/Cargo.lock index 5f37c4b02315..b66a9dd6bbec 100644 --- a/examples/api/src-tauri/Cargo.lock +++ b/examples/api/src-tauri/Cargo.lock @@ -3367,7 +3367,7 @@ dependencies = [ "url", "uuid", "windows 0.51.1", - "windows-implement 0.51.1", + "windows-implement", "x11-dl", "zbus", ] @@ -3435,9 +3435,9 @@ dependencies = [ "url", "uuid", "webkit2gtk", - "webview2-com 0.25.0", + "webview2-com", "window-vibrancy", - "windows 0.48.0", + "windows 0.51.1", ] [[package]] @@ -3534,7 +3534,7 @@ dependencies = [ "tauri-utils", "thiserror", "url", - "windows 0.48.0", + "windows 0.51.1", ] [[package]] @@ -3550,8 +3550,8 @@ dependencies = [ "tauri-runtime", "tauri-utils", "webkit2gtk", - "webview2-com 0.25.0", - "windows 0.48.0", + "webview2-com", + "windows 0.51.1", "wry", ] @@ -4197,19 +4197,6 @@ dependencies = [ "system-deps", ] -[[package]] -name = "webview2-com" -version = "0.25.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79e563ffe8e84d42e43ffacbace8780c0244fc8910346f334613559d92e203ad" -dependencies = [ - "webview2-com-macros", - "webview2-com-sys 0.25.0", - "windows 0.48.0", - "windows-implement 0.48.0", - "windows-interface 0.48.0", -] - [[package]] name = "webview2-com" version = "0.27.0" @@ -4217,11 +4204,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd15556ff1d1d6bc850dbb362762bae86069773dd30177c90d3bfa917080dc73" dependencies = [ "webview2-com-macros", - "webview2-com-sys 0.27.0", + "webview2-com-sys", "windows 0.51.1", "windows-core", - "windows-implement 0.51.1", - "windows-interface 0.51.1", + "windows-implement", + "windows-interface", ] [[package]] @@ -4235,21 +4222,6 @@ dependencies = [ "syn 2.0.38", ] -[[package]] -name = "webview2-com-sys" -version = "0.25.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19d39576804304cf9ead192467ef47f7859a1a12fec3bd459d5ba34b8cd65ed5" -dependencies = [ - "regex", - "serde", - "serde_json", - "thiserror", - "windows 0.48.0", - "windows-bindgen", - "windows-metadata", -] - [[package]] name = "webview2-com-sys" version = "0.27.0" @@ -4322,8 +4294,6 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" dependencies = [ - "windows-implement 0.48.0", - "windows-interface 0.48.0", "windows-targets 0.48.5", ] @@ -4334,21 +4304,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca229916c5ee38c2f2bc1e9d8f04df975b4bd93f9955dc69fabb5d91270045c9" dependencies = [ "windows-core", - "windows-implement 0.51.1", - "windows-interface 0.51.1", + "windows-implement", + "windows-interface", "windows-targets 0.48.5", ] -[[package]] -name = "windows-bindgen" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fe21a77bc54b7312dbd66f041605e098990c98be48cd52967b85b5e60e75ae6" -dependencies = [ - "windows-metadata", - "windows-tokens", -] - [[package]] name = "windows-core" version = "0.51.1" @@ -4358,17 +4318,6 @@ dependencies = [ "windows-targets 0.48.5", ] -[[package]] -name = "windows-implement" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e2ee588991b9e7e6c8338edf3333fbe4da35dc72092643958ebb43f0ab2c49c" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "windows-implement" version = "0.51.1" @@ -4380,17 +4329,6 @@ dependencies = [ "syn 2.0.38", ] -[[package]] -name = "windows-interface" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6fb8df20c9bcaa8ad6ab513f7b40104840c8867d5751126e4df3b08388d0cc7" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "windows-interface" version = "0.51.1" @@ -4402,12 +4340,6 @@ dependencies = [ "syn 2.0.38", ] -[[package]] -name = "windows-metadata" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "422ee0e5f0e2cc372bb6addbfff9a8add712155cd743df9c15f6ab000f31432d" - [[package]] name = "windows-sys" version = "0.45.0" @@ -4456,12 +4388,6 @@ dependencies = [ "windows_x86_64_msvc 0.48.5", ] -[[package]] -name = "windows-tokens" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b34c9a3b28cb41db7385546f7f9a8179348dffc89923dde66857b1ba5312f6b4" - [[package]] name = "windows_aarch64_gnullvm" version = "0.42.2" @@ -4606,9 +4532,9 @@ dependencies = [ "url", "webkit2gtk", "webkit2gtk-sys", - "webview2-com 0.27.0", + "webview2-com", "windows 0.51.1", - "windows-implement 0.51.1", + "windows-implement", ] [[package]] diff --git a/examples/api/src-tauri/src/lib.rs b/examples/api/src-tauri/src/lib.rs index 9c08b55ec587..e79df83985e6 100644 --- a/examples/api/src-tauri/src/lib.rs +++ b/examples/api/src-tauri/src/lib.rs @@ -77,6 +77,18 @@ pub fn run_app) + Send + 'static>( let window = window_builder.build().unwrap(); + let window_ = window.clone(); + window.listen("js-event", move |event| { + println!("got js-event with message '{:?}'", event.payload()); + let reply = Reply { + data: "something else".to_string(), + }; + + window_ + .emit("rust-event", Some(reply)) + .expect("failed to emit"); + }); + #[cfg(debug_assertions)] window.open_devtools(); @@ -121,19 +133,6 @@ pub fn run_app) + Send + 'static>( setup(app); Ok(()) - }) - .on_page_load(|window, _| { - let window_ = window.clone(); - window.listen("js-event", move |event| { - println!("got js-event with message '{:?}'", event.payload()); - let reply = Reply { - data: "something else".to_string(), - }; - - window_ - .emit("rust-event", Some(reply)) - .expect("failed to emit"); - }); }); #[allow(unused_mut)] diff --git a/examples/multiwindow/index.html b/examples/multiwindow/index.html index d415b1ae3322..28b094ca9ef3 100644 --- a/examples/multiwindow/index.html +++ b/examples/multiwindow/index.html @@ -14,8 +14,8 @@
- - + windowNumber += 1 + createWindowButton.innerHTML = 'Create child window ' + windowNumber + }) + container.appendChild(createWindowButton) + + + + \ No newline at end of file diff --git a/examples/parent-window/main.rs b/examples/parent-window/main.rs index a0bf7e00beb2..6bd81ad2b974 100644 --- a/examples/parent-window/main.rs +++ b/examples/parent-window/main.rs @@ -22,18 +22,16 @@ async fn create_child_window(id: String, window: Window) { fn main() { tauri::Builder::default() - .on_page_load(|window, _payload| { - let label = window.label().to_string(); - window.listen("clicked".to_string(), move |_payload| { - println!("got 'clicked' event on window '{label}'"); - }); - }) .invoke_handler(tauri::generate_handler![create_child_window]) .setup(|app| { - WindowBuilder::new(app, "main".to_string(), WindowUrl::default()) + let window = WindowBuilder::new(app, "main".to_string(), WindowUrl::default()) .title("Main") .inner_size(600.0, 400.0) .build()?; + + window.listen("clicked".to_string(), move |_payload| { + println!("got 'clicked' event on window 'main'"); + }); Ok(()) }) .run(tauri::generate_context!( diff --git a/tooling/cli/node/test/jest/fixtures/app/src-tauri/src/main.rs b/tooling/cli/node/test/jest/fixtures/app/src-tauri/src/main.rs index 482229eb1fa1..4dd26b66fb37 100644 --- a/tooling/cli/node/test/jest/fixtures/app/src-tauri/src/main.rs +++ b/tooling/cli/node/test/jest/fixtures/app/src-tauri/src/main.rs @@ -2,6 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT +use tauri::Manager; + #[tauri::command(with_window)] fn exit(window: tauri::Window) { window.close().unwrap(); @@ -9,14 +11,15 @@ fn exit(window: tauri::Window) { fn main() { tauri::Builder::default() - .on_page_load(|window, _| { - let window_ = window.clone(); + .setup(|app| { + let window = app.get_window("main").unwrap(); window.listen("hello".into(), move |_| { window_ .emit(&"reply".to_string(), Some("{ msg: 'TEST' }".to_string())) .unwrap(); }); window.eval("window.onTauriInit()").unwrap(); + Ok(()) }) .invoke_handler(tauri::generate_handler![exit]) .run(tauri::generate_context!())