diff --git a/.changes/core-navigate-method.md b/.changes/core-navigate-method.md new file mode 100644 index 000000000..4767c1a1b --- /dev/null +++ b/.changes/core-navigate-method.md @@ -0,0 +1,5 @@ +--- +"tauri": 'minor:feat' +--- + +Added `Window::navigate`. diff --git a/.changes/runtime-navigate-method.md b/.changes/runtime-navigate-method.md new file mode 100644 index 000000000..0f907aa7d --- /dev/null +++ b/.changes/runtime-navigate-method.md @@ -0,0 +1,5 @@ +--- +"tauri-runtime": 'minor:feat' +--- + +Added `navigate` function to `Dispatch` trait. diff --git a/.changes/wry-navigate-method.md b/.changes/wry-navigate-method.md new file mode 100644 index 000000000..ca7aa7bb2 --- /dev/null +++ b/.changes/wry-navigate-method.md @@ -0,0 +1,5 @@ +--- +"tauri-runtime-wry": 'minor:feat' +--- + +Implement navigate method diff --git a/core/tauri-runtime-wry/src/lib.rs b/core/tauri-runtime-wry/src/lib.rs index 4f1c37513..558dabcc4 100644 --- a/core/tauri-runtime-wry/src/lib.rs +++ b/core/tauri-runtime-wry/src/lib.rs @@ -1108,6 +1108,7 @@ pub enum WindowMessage { SetMinimizable(bool), SetClosable(bool), SetTitle(String), + Navigate(Url), Maximize, Unmaximize, Minimize, @@ -1456,6 +1457,13 @@ impl Dispatch for WryDispatcher { ) } + fn navigate(&self, url: Url) -> Result<()> { + send_user_message( + &self.context, + Message::Window(self.window_id, WindowMessage::Navigate(url)), + ) + } + fn maximize(&self) -> Result<()> { send_user_message( &self.context, @@ -2429,6 +2437,11 @@ fn handle_user_message( WindowMessage::SetMinimizable(minimizable) => window.set_minimizable(minimizable), WindowMessage::SetClosable(closable) => window.set_closable(closable), WindowMessage::SetTitle(title) => window.set_title(&title), + WindowMessage::Navigate(url) => { + if let WindowHandle::Webview { inner: w, .. } = &window { + w.load_url(url.as_str()) + } + } WindowMessage::Maximize => window.set_maximized(true), WindowMessage::Unmaximize => window.set_maximized(false), WindowMessage::Minimize => window.set_minimized(true), diff --git a/core/tauri-runtime/src/lib.rs b/core/tauri-runtime/src/lib.rs index c6f719a03..efd409231 100644 --- a/core/tauri-runtime/src/lib.rs +++ b/core/tauri-runtime/src/lib.rs @@ -676,6 +676,9 @@ pub trait Dispatch: Debug + Clone + Send + Sync + Sized + 'static /// Updates the window title. fn set_title>(&self, title: S) -> Result<()>; + /// Naviagte to the given URL. + fn navigate(&self, url: Url) -> Result<()>; + /// Maximizes the window. fn maximize(&self) -> Result<()>; diff --git a/core/tauri/src/test/mock_runtime.rs b/core/tauri/src/test/mock_runtime.rs index f00737325..91ec2139c 100644 --- a/core/tauri/src/test/mock_runtime.rs +++ b/core/tauri/src/test/mock_runtime.rs @@ -24,6 +24,7 @@ use tauri_runtime::{ #[cfg(target_os = "macos")] use tauri_utils::TitleBarStyle; use tauri_utils::{config::WindowConfig, Theme}; +use url::Url; use uuid::Uuid; #[cfg(windows)] @@ -116,7 +117,7 @@ impl RuntimeHandle for MockRuntimeHandle { id, context: self.context.clone(), last_evaluated_script: Default::default(), - url: pending.url, + url: Arc::new(Mutex::new(pending.url)), }, menu_ids: Default::default(), }) @@ -190,7 +191,7 @@ impl RuntimeHandle for MockRuntimeHandle { pub struct MockDispatcher { id: WindowId, context: RuntimeContext, - url: String, + url: Arc>, last_evaluated_script: Arc>>, } @@ -383,7 +384,12 @@ impl Dispatch for MockDispatcher { } fn url(&self) -> Result { - self.url.parse().map_err(|_| Error::FailedToReceiveMessage) + self + .url + .lock() + .unwrap() + .parse() + .map_err(|_| Error::FailedToReceiveMessage) } fn scale_factor(&self) -> Result { @@ -528,7 +534,7 @@ impl Dispatch for MockDispatcher { id, context: self.context.clone(), last_evaluated_script: Default::default(), - url: pending.url, + url: Arc::new(Mutex::new(pending.url)), }, menu_ids: Default::default(), }) @@ -554,6 +560,11 @@ impl Dispatch for MockDispatcher { Ok(()) } + fn navigate(&self, url: Url) -> Result<()> { + *self.url.lock().unwrap() = url.to_string(); + Ok(()) + } + fn maximize(&self) -> Result<()> { Ok(()) } @@ -788,7 +799,7 @@ impl Runtime for MockRuntime { id, context: self.context.clone(), last_evaluated_script: Default::default(), - url: pending.url, + url: Arc::new(Mutex::new(pending.url)), }, menu_ids: Default::default(), }) diff --git a/core/tauri/src/window.rs b/core/tauri/src/window.rs index 4609604b6..4b3fc28b5 100644 --- a/core/tauri/src/window.rs +++ b/core/tauri/src/window.rs @@ -777,9 +777,6 @@ pub struct Window { manager: WindowManager, pub(crate) app_handle: AppHandle, js_event_listeners: Arc>>>, - - #[cfg(test)] - pub(crate) current_url: url::Url, } unsafe impl raw_window_handle::HasRawWindowHandle for Window { @@ -795,8 +792,6 @@ impl Clone for Window { manager: self.manager.clone(), app_handle: self.app_handle.clone(), js_event_listeners: self.js_event_listeners.clone(), - #[cfg(test)] - current_url: self.current_url.clone(), } } } @@ -949,8 +944,6 @@ impl Window { manager, app_handle, js_event_listeners: Default::default(), - #[cfg(test)] - current_url: "http://tauri.app".parse().unwrap(), } } @@ -1638,19 +1631,13 @@ impl Window { impl Window { /// Returns the current url of the webview. // TODO: in v2, change this type to Result - #[cfg(not(test))] pub fn url(&self) -> Url { self.window.dispatcher.url().unwrap() } - #[cfg(test)] - pub fn url(&self) -> Url { - self.current_url.clone() - } - - #[cfg(test)] - pub(crate) fn navigate(&mut self, url: Url) { - self.current_url = url; + /// Navigates the webview to the defined url. + pub fn navigate(&mut self, url: Url) { + self.window.dispatcher.navigate(url).unwrap(); } fn is_local_url(&self, current_url: &Url) -> bool {