Skip to content

Commit

Permalink
feat: Add navigate method
Browse files Browse the repository at this point in the history
  • Loading branch information
jhutchins committed Jun 17, 2023
1 parent c7534e7 commit 4b99b2f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 1 deletion.
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
---

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
---

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
---

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 @@ -1101,6 +1101,7 @@ pub enum WindowMessage {
SetMinimizable(bool),
SetClosable(bool),
SetTitle(String),
Navigate(Url),
Maximize,
Unmaximize,
Minimize,
Expand Down Expand Up @@ -1449,6 +1450,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 @@ -2422,6 +2430,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 @@ -670,6 +670,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
8 changes: 7 additions & 1 deletion core/tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1648,8 +1648,14 @@ impl<R: Runtime> Window<R> {
self.current_url.clone()
}

/// Navigates the webview to the defined url.
#[cfg(not(test))]
pub fn navigate(&mut self, url: Url) {
self.window.dispatcher.navigate(url).unwrap();
}

#[cfg(test)]
pub(crate) fn navigate(&mut self, url: Url) {
pub fn navigate(&mut self, url: Url) {
self.current_url = url;
}

Expand Down

0 comments on commit 4b99b2f

Please sign in to comment.