feat: add navigate method (#7235)

This commit is contained in:
Jeffrey Hutchins 2023-06-20 13:56:17 -06:00 committed by GitHub
parent b04444928c
commit 2a000e150d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 50 additions and 21 deletions

View File

@ -0,0 +1,5 @@
---
"tauri": 'minor:feat'
---
Added `Window::navigate`.

View File

@ -0,0 +1,5 @@
---
"tauri-runtime": 'minor:feat'
---
Added `navigate` function to `Dispatch` trait.

View File

@ -0,0 +1,5 @@
---
"tauri-runtime-wry": 'minor:feat'
---
Implement navigate method

View File

@ -1108,6 +1108,7 @@ pub enum WindowMessage {
SetMinimizable(bool),
SetClosable(bool),
SetTitle(String),
Navigate(Url),
Maximize,
Unmaximize,
Minimize,
@ -1456,6 +1457,13 @@ impl<T: UserEvent> Dispatch<T> for WryDispatcher<T> {
)
}
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<T: UserEvent>(
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),

View File

@ -676,6 +676,9 @@ pub trait Dispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 'static
/// Updates the window title.
fn set_title<S: Into<String>>(&self, title: S) -> Result<()>;
/// Naviagte to the given URL.
fn navigate(&self, url: Url) -> Result<()>;
/// Maximizes the window.
fn maximize(&self) -> Result<()>;

View File

@ -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<T: UserEvent> RuntimeHandle<T> 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<T: UserEvent> RuntimeHandle<T> for MockRuntimeHandle {
pub struct MockDispatcher {
id: WindowId,
context: RuntimeContext,
url: String,
url: Arc<Mutex<String>>,
last_evaluated_script: Arc<Mutex<Option<String>>>,
}
@ -383,7 +384,12 @@ impl<T: UserEvent> Dispatch<T> for MockDispatcher {
}
fn url(&self) -> Result<url::Url> {
self.url.parse().map_err(|_| Error::FailedToReceiveMessage)
self
.url
.lock()
.unwrap()
.parse()
.map_err(|_| Error::FailedToReceiveMessage)
}
fn scale_factor(&self) -> Result<f64> {
@ -528,7 +534,7 @@ impl<T: UserEvent> Dispatch<T> 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<T: UserEvent> Dispatch<T> 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<T: UserEvent> Runtime<T> 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(),
})

View File

@ -777,9 +777,6 @@ pub struct Window<R: Runtime> {
manager: WindowManager<R>,
pub(crate) app_handle: AppHandle<R>,
js_event_listeners: Arc<Mutex<HashMap<JsEventListenerKey, HashSet<usize>>>>,
#[cfg(test)]
pub(crate) current_url: url::Url,
}
unsafe impl<R: Runtime> raw_window_handle::HasRawWindowHandle for Window<R> {
@ -795,8 +792,6 @@ impl<R: Runtime> Clone for Window<R> {
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<R: Runtime> Window<R> {
manager,
app_handle,
js_event_listeners: Default::default(),
#[cfg(test)]
current_url: "http://tauri.app".parse().unwrap(),
}
}
@ -1638,19 +1631,13 @@ impl<R: Runtime> Window<R> {
impl<R: Runtime> Window<R> {
/// 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 {