From d17027e1a0db3e8c5ae81fc4f472c5918fbce611 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Mon, 26 Dec 2022 10:31:40 -0800 Subject: [PATCH] feat: expose url method (#5914) Co-authored-by: Fabian-Lars Co-authored-by: Jonas Kruckenberg --- .changes/url-getter.md | 7 +++++++ core/tauri-runtime-wry/src/lib.rs | 12 +++++++++++- core/tauri-runtime/Cargo.toml | 1 + core/tauri-runtime/src/lib.rs | 4 ++++ core/tauri/src/test/mock_runtime.rs | 4 ++++ core/tauri/src/updater/core.rs | 2 +- core/tauri/src/window.rs | 6 ++++++ 7 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 .changes/url-getter.md diff --git a/.changes/url-getter.md b/.changes/url-getter.md new file mode 100644 index 000000000000..b40863feff99 --- /dev/null +++ b/.changes/url-getter.md @@ -0,0 +1,7 @@ +--- +"tauri-runtime": minor +"tauri-runtime-wry": minor +"tauri": minor +--- + +Added window's `url()` getter. diff --git a/core/tauri-runtime-wry/src/lib.rs b/core/tauri-runtime-wry/src/lib.rs index d8eb0ea27156..3764e42cd54a 100644 --- a/core/tauri-runtime-wry/src/lib.rs +++ b/core/tauri-runtime-wry/src/lib.rs @@ -64,7 +64,7 @@ use wry::{ }, }, http::{Request as WryRequest, Response as WryResponse}, - webview::{FileDropEvent as WryFileDropEvent, WebContext, WebView, WebViewBuilder}, + webview::{FileDropEvent as WryFileDropEvent, Url, WebContext, WebView, WebViewBuilder}, }; pub use wry::application::window::{Window, WindowBuilder as WryWindowBuilder, WindowId}; @@ -1037,6 +1037,7 @@ pub enum WindowMessage { #[cfg(any(debug_assertions, feature = "devtools"))] IsDevToolsOpen(Sender), // Getters + Url(Sender), ScaleFactor(Sender), InnerPosition(Sender>>), OuterPosition(Sender>>), @@ -1238,6 +1239,10 @@ impl Dispatch for WryDispatcher { // Getters + fn url(&self) -> Result { + window_getter!(self, WindowMessage::Url) + } + fn scale_factor(&self) -> Result { window_getter!(self, WindowMessage::ScaleFactor) } @@ -2362,6 +2367,11 @@ fn handle_user_message( } } // Getters + WindowMessage::Url(tx) => { + if let WindowHandle::Webview { inner: w, .. } = &window { + tx.send(w.url()).unwrap(); + } + } WindowMessage::ScaleFactor(tx) => tx.send(window.scale_factor()).unwrap(), WindowMessage::InnerPosition(tx) => tx .send( diff --git a/core/tauri-runtime/Cargo.toml b/core/tauri-runtime/Cargo.toml index b8a090f90937..7fdc1afb2cae 100644 --- a/core/tauri-runtime/Cargo.toml +++ b/core/tauri-runtime/Cargo.toml @@ -32,6 +32,7 @@ http = "0.2.4" http-range = "0.1.4" raw-window-handle = "0.5" rand = "0.8" +url = { version = "2" } [target."cfg(windows)".dependencies] webview2-com = "0.19.1" diff --git a/core/tauri-runtime/src/lib.rs b/core/tauri-runtime/src/lib.rs index a0a70f266fa0..9d8f65151489 100644 --- a/core/tauri-runtime/src/lib.rs +++ b/core/tauri-runtime/src/lib.rs @@ -10,6 +10,7 @@ use raw_window_handle::RawDisplayHandle; use serde::Deserialize; use std::{fmt::Debug, sync::mpsc::Sender}; use tauri_utils::Theme; +use url::Url; use uuid::Uuid; pub mod http; @@ -530,6 +531,9 @@ pub trait Dispatch: Debug + Clone + Send + Sync + Sized + 'static // GETTERS + /// Returns the webview's current URL. + fn url(&self) -> Result; + /// Returns the scale factor that can be used to map logical pixels to physical pixels, and vice versa. fn scale_factor(&self) -> Result; diff --git a/core/tauri/src/test/mock_runtime.rs b/core/tauri/src/test/mock_runtime.rs index 41c892b88a87..d3e28790543b 100644 --- a/core/tauri/src/test/mock_runtime.rs +++ b/core/tauri/src/test/mock_runtime.rs @@ -333,6 +333,10 @@ impl Dispatch for MockDispatcher { Ok(false) } + fn url(&self) -> Result { + todo!() + } + fn scale_factor(&self) -> Result { Ok(1.0) } diff --git a/core/tauri/src/updater/core.rs b/core/tauri/src/updater/core.rs index e263ccd7c636..c212bf91e427 100644 --- a/core/tauri/src/updater/core.rs +++ b/core/tauri/src/updater/core.rs @@ -895,7 +895,7 @@ fn copy_files_and_run(archive_buffer: R, extract_path: &Path) -> })?; let _ = std::process::Command::new("touch") - .arg(&extract_path) + .arg(extract_path) .status(); Ok(()) diff --git a/core/tauri/src/window.rs b/core/tauri/src/window.rs index ec7111b8c756..7e5a1a285560 100644 --- a/core/tauri/src/window.rs +++ b/core/tauri/src/window.rs @@ -7,6 +7,7 @@ pub(crate) mod menu; pub use menu::{MenuEvent, MenuHandle}; +use url::Url; #[cfg(target_os = "macos")] use crate::TitleBarStyle; @@ -1298,6 +1299,11 @@ impl Window { /// Webview APIs. impl Window { + /// Returns the current url of the webview. + pub fn url(&self) -> crate::Result { + self.window.dispatcher.url().map_err(Into::into) + } + /// Handles this window receiving an [`InvokeMessage`]. pub fn on_message(self, payload: InvokePayload) -> crate::Result<()> { let manager = self.manager.clone();