Skip to content

Commit d17027e

Browse files
lucasfernogFabianLarsJonasKruckenberg
authored
feat: expose url method (#5914)
Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de> Co-authored-by: Jonas Kruckenberg <iterpre@protonmail.com>
1 parent 7262189 commit d17027e

File tree

7 files changed

+34
-2
lines changed

7 files changed

+34
-2
lines changed

.changes/url-getter.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri-runtime": minor
3+
"tauri-runtime-wry": minor
4+
"tauri": minor
5+
---
6+
7+
Added window's `url()` getter.

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use wry::{
6464
},
6565
},
6666
http::{Request as WryRequest, Response as WryResponse},
67-
webview::{FileDropEvent as WryFileDropEvent, WebContext, WebView, WebViewBuilder},
67+
webview::{FileDropEvent as WryFileDropEvent, Url, WebContext, WebView, WebViewBuilder},
6868
};
6969

7070
pub use wry::application::window::{Window, WindowBuilder as WryWindowBuilder, WindowId};
@@ -1037,6 +1037,7 @@ pub enum WindowMessage {
10371037
#[cfg(any(debug_assertions, feature = "devtools"))]
10381038
IsDevToolsOpen(Sender<bool>),
10391039
// Getters
1040+
Url(Sender<Url>),
10401041
ScaleFactor(Sender<f64>),
10411042
InnerPosition(Sender<Result<PhysicalPosition<i32>>>),
10421043
OuterPosition(Sender<Result<PhysicalPosition<i32>>>),
@@ -1238,6 +1239,10 @@ impl<T: UserEvent> Dispatch<T> for WryDispatcher<T> {
12381239

12391240
// Getters
12401241

1242+
fn url(&self) -> Result<Url> {
1243+
window_getter!(self, WindowMessage::Url)
1244+
}
1245+
12411246
fn scale_factor(&self) -> Result<f64> {
12421247
window_getter!(self, WindowMessage::ScaleFactor)
12431248
}
@@ -2362,6 +2367,11 @@ fn handle_user_message<T: UserEvent>(
23622367
}
23632368
}
23642369
// Getters
2370+
WindowMessage::Url(tx) => {
2371+
if let WindowHandle::Webview { inner: w, .. } = &window {
2372+
tx.send(w.url()).unwrap();
2373+
}
2374+
}
23652375
WindowMessage::ScaleFactor(tx) => tx.send(window.scale_factor()).unwrap(),
23662376
WindowMessage::InnerPosition(tx) => tx
23672377
.send(

core/tauri-runtime/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ http = "0.2.4"
3232
http-range = "0.1.4"
3333
raw-window-handle = "0.5"
3434
rand = "0.8"
35+
url = { version = "2" }
3536

3637
[target."cfg(windows)".dependencies]
3738
webview2-com = "0.19.1"

core/tauri-runtime/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use raw_window_handle::RawDisplayHandle;
1010
use serde::Deserialize;
1111
use std::{fmt::Debug, sync::mpsc::Sender};
1212
use tauri_utils::Theme;
13+
use url::Url;
1314
use uuid::Uuid;
1415

1516
pub mod http;
@@ -530,6 +531,9 @@ pub trait Dispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 'static
530531

531532
// GETTERS
532533

534+
/// Returns the webview's current URL.
535+
fn url(&self) -> Result<Url>;
536+
533537
/// Returns the scale factor that can be used to map logical pixels to physical pixels, and vice versa.
534538
fn scale_factor(&self) -> Result<f64>;
535539

core/tauri/src/test/mock_runtime.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@ impl<T: UserEvent> Dispatch<T> for MockDispatcher {
333333
Ok(false)
334334
}
335335

336+
fn url(&self) -> Result<url::Url> {
337+
todo!()
338+
}
339+
336340
fn scale_factor(&self) -> Result<f64> {
337341
Ok(1.0)
338342
}

core/tauri/src/updater/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ fn copy_files_and_run<R: Read + Seek>(archive_buffer: R, extract_path: &Path) ->
895895
})?;
896896

897897
let _ = std::process::Command::new("touch")
898-
.arg(&extract_path)
898+
.arg(extract_path)
899899
.status();
900900

901901
Ok(())

core/tauri/src/window.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
pub(crate) mod menu;
88

99
pub use menu::{MenuEvent, MenuHandle};
10+
use url::Url;
1011

1112
#[cfg(target_os = "macos")]
1213
use crate::TitleBarStyle;
@@ -1298,6 +1299,11 @@ impl<R: Runtime> Window<R> {
12981299

12991300
/// Webview APIs.
13001301
impl<R: Runtime> Window<R> {
1302+
/// Returns the current url of the webview.
1303+
pub fn url(&self) -> crate::Result<Url> {
1304+
self.window.dispatcher.url().map_err(Into::into)
1305+
}
1306+
13011307
/// Handles this window receiving an [`InvokeMessage`].
13021308
pub fn on_message(self, payload: InvokePayload) -> crate::Result<()> {
13031309
let manager = self.manager.clone();

0 commit comments

Comments
 (0)