From 3193e69f06189321dcdc975519ca6620d99e27a6 Mon Sep 17 00:00:00 2001 From: Jeff Hutchins Date: Sat, 17 Jun 2023 07:11:01 -0600 Subject: [PATCH] feat: Add navigate method --- .changes/core-navigate-method.md | 5 +++++ .changes/runtime-navigate-method.md | 5 +++++ .changes/wry-navigate-method.md | 5 +++++ core/tauri-runtime-wry/src/lib.rs | 13 +++++++++++++ core/tauri-runtime/src/lib.rs | 3 +++ core/tauri/src/test/mock_runtime.rs | 21 ++++++++++++++++----- core/tauri/src/window.rs | 19 +++---------------- 7 files changed, 50 insertions(+), 21 deletions(-) create mode 100644 .changes/core-navigate-method.md create mode 100644 .changes/runtime-navigate-method.md create mode 100644 .changes/wry-navigate-method.md diff --git a/.changes/core-navigate-method.md b/.changes/core-navigate-method.md new file mode 100644 index 000000000000..4767c1a1bf36 --- /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 000000000000..0f907aa7dd4f --- /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 000000000000..ca7aa7bb2504 --- /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 dfb672577503..ccad3b472495 100644 --- a/core/tauri-runtime-wry/src/lib.rs +++ b/core/tauri-runtime-wry/src/lib.rs @@ -1101,6 +1101,7 @@ pub enum WindowMessage { SetMinimizable(bool), SetClosable(bool), SetTitle(String), + Navigate(Url), Maximize, Unmaximize, Minimize, @@ -1449,6 +1450,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, @@ -2422,6 +2430,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 5cd05cac3047..75c9626852a4 100644 --- a/core/tauri-runtime/src/lib.rs +++ b/core/tauri-runtime/src/lib.rs @@ -670,6 +670,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 f007373256df..91ec2139c2f8 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 788cdba8d6da..3addc0938edd 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 {