Skip to content

Commit

Permalink
feat: add navigate method (#7235)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhutchins authored Jun 20, 2023
1 parent b044449 commit 2a000e1
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changes/core-navigate-method.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": 'minor:feat'
---

Added `Window::navigate`.
5 changes: 5 additions & 0 deletions .changes/runtime-navigate-method.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-runtime": 'minor:feat'
---

Added `navigate` function to `Dispatch` trait.
5 changes: 5 additions & 0 deletions .changes/wry-navigate-method.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-runtime-wry": 'minor:feat'
---

Implement navigate method
13 changes: 13 additions & 0 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,7 @@ pub enum WindowMessage {
SetMinimizable(bool),
SetClosable(bool),
SetTitle(String),
Navigate(Url),
Maximize,
Unmaximize,
Minimize,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
3 changes: 3 additions & 0 deletions core/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()>;

Expand Down
21 changes: 16 additions & 5 deletions core/tauri/src/test/mock_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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(),
})
Expand Down Expand Up @@ -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>>>,
}

Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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(),
})
Expand All @@ -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(())
}
Expand Down Expand Up @@ -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(),
})
Expand Down
19 changes: 3 additions & 16 deletions core/tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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(),
}
}
}
Expand Down Expand Up @@ -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(),
}
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 2a000e1

Please sign in to comment.