Skip to content

Commit 2a000e1

Browse files
authored
feat: add navigate method (#7235)
1 parent b044449 commit 2a000e1

File tree

7 files changed

+50
-21
lines changed

7 files changed

+50
-21
lines changed

.changes/core-navigate-method.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": 'minor:feat'
3+
---
4+
5+
Added `Window::navigate`.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-runtime": 'minor:feat'
3+
---
4+
5+
Added `navigate` function to `Dispatch` trait.

.changes/wry-navigate-method.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-runtime-wry": 'minor:feat'
3+
---
4+
5+
Implement navigate method

core/tauri-runtime-wry/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,7 @@ pub enum WindowMessage {
11081108
SetMinimizable(bool),
11091109
SetClosable(bool),
11101110
SetTitle(String),
1111+
Navigate(Url),
11111112
Maximize,
11121113
Unmaximize,
11131114
Minimize,
@@ -1456,6 +1457,13 @@ impl<T: UserEvent> Dispatch<T> for WryDispatcher<T> {
14561457
)
14571458
}
14581459

1460+
fn navigate(&self, url: Url) -> Result<()> {
1461+
send_user_message(
1462+
&self.context,
1463+
Message::Window(self.window_id, WindowMessage::Navigate(url)),
1464+
)
1465+
}
1466+
14591467
fn maximize(&self) -> Result<()> {
14601468
send_user_message(
14611469
&self.context,
@@ -2429,6 +2437,11 @@ fn handle_user_message<T: UserEvent>(
24292437
WindowMessage::SetMinimizable(minimizable) => window.set_minimizable(minimizable),
24302438
WindowMessage::SetClosable(closable) => window.set_closable(closable),
24312439
WindowMessage::SetTitle(title) => window.set_title(&title),
2440+
WindowMessage::Navigate(url) => {
2441+
if let WindowHandle::Webview { inner: w, .. } = &window {
2442+
w.load_url(url.as_str())
2443+
}
2444+
}
24322445
WindowMessage::Maximize => window.set_maximized(true),
24332446
WindowMessage::Unmaximize => window.set_maximized(false),
24342447
WindowMessage::Minimize => window.set_minimized(true),

core/tauri-runtime/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,9 @@ pub trait Dispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 'static
676676
/// Updates the window title.
677677
fn set_title<S: Into<String>>(&self, title: S) -> Result<()>;
678678

679+
/// Naviagte to the given URL.
680+
fn navigate(&self, url: Url) -> Result<()>;
681+
679682
/// Maximizes the window.
680683
fn maximize(&self) -> Result<()>;
681684

core/tauri/src/test/mock_runtime.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use tauri_runtime::{
2424
#[cfg(target_os = "macos")]
2525
use tauri_utils::TitleBarStyle;
2626
use tauri_utils::{config::WindowConfig, Theme};
27+
use url::Url;
2728
use uuid::Uuid;
2829

2930
#[cfg(windows)]
@@ -116,7 +117,7 @@ impl<T: UserEvent> RuntimeHandle<T> for MockRuntimeHandle {
116117
id,
117118
context: self.context.clone(),
118119
last_evaluated_script: Default::default(),
119-
url: pending.url,
120+
url: Arc::new(Mutex::new(pending.url)),
120121
},
121122
menu_ids: Default::default(),
122123
})
@@ -190,7 +191,7 @@ impl<T: UserEvent> RuntimeHandle<T> for MockRuntimeHandle {
190191
pub struct MockDispatcher {
191192
id: WindowId,
192193
context: RuntimeContext,
193-
url: String,
194+
url: Arc<Mutex<String>>,
194195
last_evaluated_script: Arc<Mutex<Option<String>>>,
195196
}
196197

@@ -383,7 +384,12 @@ impl<T: UserEvent> Dispatch<T> for MockDispatcher {
383384
}
384385

385386
fn url(&self) -> Result<url::Url> {
386-
self.url.parse().map_err(|_| Error::FailedToReceiveMessage)
387+
self
388+
.url
389+
.lock()
390+
.unwrap()
391+
.parse()
392+
.map_err(|_| Error::FailedToReceiveMessage)
387393
}
388394

389395
fn scale_factor(&self) -> Result<f64> {
@@ -528,7 +534,7 @@ impl<T: UserEvent> Dispatch<T> for MockDispatcher {
528534
id,
529535
context: self.context.clone(),
530536
last_evaluated_script: Default::default(),
531-
url: pending.url,
537+
url: Arc::new(Mutex::new(pending.url)),
532538
},
533539
menu_ids: Default::default(),
534540
})
@@ -554,6 +560,11 @@ impl<T: UserEvent> Dispatch<T> for MockDispatcher {
554560
Ok(())
555561
}
556562

563+
fn navigate(&self, url: Url) -> Result<()> {
564+
*self.url.lock().unwrap() = url.to_string();
565+
Ok(())
566+
}
567+
557568
fn maximize(&self) -> Result<()> {
558569
Ok(())
559570
}
@@ -788,7 +799,7 @@ impl<T: UserEvent> Runtime<T> for MockRuntime {
788799
id,
789800
context: self.context.clone(),
790801
last_evaluated_script: Default::default(),
791-
url: pending.url,
802+
url: Arc::new(Mutex::new(pending.url)),
792803
},
793804
menu_ids: Default::default(),
794805
})

core/tauri/src/window.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -777,9 +777,6 @@ pub struct Window<R: Runtime> {
777777
manager: WindowManager<R>,
778778
pub(crate) app_handle: AppHandle<R>,
779779
js_event_listeners: Arc<Mutex<HashMap<JsEventListenerKey, HashSet<usize>>>>,
780-
781-
#[cfg(test)]
782-
pub(crate) current_url: url::Url,
783780
}
784781

785782
unsafe impl<R: Runtime> raw_window_handle::HasRawWindowHandle for Window<R> {
@@ -795,8 +792,6 @@ impl<R: Runtime> Clone for Window<R> {
795792
manager: self.manager.clone(),
796793
app_handle: self.app_handle.clone(),
797794
js_event_listeners: self.js_event_listeners.clone(),
798-
#[cfg(test)]
799-
current_url: self.current_url.clone(),
800795
}
801796
}
802797
}
@@ -949,8 +944,6 @@ impl<R: Runtime> Window<R> {
949944
manager,
950945
app_handle,
951946
js_event_listeners: Default::default(),
952-
#[cfg(test)]
953-
current_url: "http://tauri.app".parse().unwrap(),
954947
}
955948
}
956949

@@ -1638,19 +1631,13 @@ impl<R: Runtime> Window<R> {
16381631
impl<R: Runtime> Window<R> {
16391632
/// Returns the current url of the webview.
16401633
// TODO: in v2, change this type to Result
1641-
#[cfg(not(test))]
16421634
pub fn url(&self) -> Url {
16431635
self.window.dispatcher.url().unwrap()
16441636
}
16451637

1646-
#[cfg(test)]
1647-
pub fn url(&self) -> Url {
1648-
self.current_url.clone()
1649-
}
1650-
1651-
#[cfg(test)]
1652-
pub(crate) fn navigate(&mut self, url: Url) {
1653-
self.current_url = url;
1638+
/// Navigates the webview to the defined url.
1639+
pub fn navigate(&mut self, url: Url) {
1640+
self.window.dispatcher.navigate(url).unwrap();
16541641
}
16551642

16561643
fn is_local_url(&self, current_url: &Url) -> bool {

0 commit comments

Comments
 (0)