Forráskód Böngészése

feat: expose url method (#5914)

Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de>
Co-authored-by: Jonas Kruckenberg <iterpre@protonmail.com>
Lucas Fernandes Nogueira 2 éve
szülő
commit
d17027e1a0

+ 7 - 0
.changes/url-getter.md

@@ -0,0 +1,7 @@
+---
+"tauri-runtime": minor
+"tauri-runtime-wry": minor
+"tauri": minor
+---
+
+Added window's `url()` getter.

+ 11 - 1
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<bool>),
   // Getters
+  Url(Sender<Url>),
   ScaleFactor(Sender<f64>),
   InnerPosition(Sender<Result<PhysicalPosition<i32>>>),
   OuterPosition(Sender<Result<PhysicalPosition<i32>>>),
@@ -1238,6 +1239,10 @@ impl<T: UserEvent> Dispatch<T> for WryDispatcher<T> {
 
   // Getters
 
+  fn url(&self) -> Result<Url> {
+    window_getter!(self, WindowMessage::Url)
+  }
+
   fn scale_factor(&self) -> Result<f64> {
     window_getter!(self, WindowMessage::ScaleFactor)
   }
@@ -2362,6 +2367,11 @@ fn handle_user_message<T: UserEvent>(
               }
             }
             // 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(

+ 1 - 0
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"

+ 4 - 0
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<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 'static
 
   // GETTERS
 
+  /// Returns the webview's current URL.
+  fn url(&self) -> Result<Url>;
+
   /// Returns the scale factor that can be used to map logical pixels to physical pixels, and vice versa.
   fn scale_factor(&self) -> Result<f64>;
 

+ 4 - 0
core/tauri/src/test/mock_runtime.rs

@@ -333,6 +333,10 @@ impl<T: UserEvent> Dispatch<T> for MockDispatcher {
     Ok(false)
   }
 
+  fn url(&self) -> Result<url::Url> {
+    todo!()
+  }
+
   fn scale_factor(&self) -> Result<f64> {
     Ok(1.0)
   }

+ 1 - 1
core/tauri/src/updater/core.rs

@@ -895,7 +895,7 @@ fn copy_files_and_run<R: Read + Seek>(archive_buffer: R, extract_path: &Path) ->
   })?;
 
   let _ = std::process::Command::new("touch")
-    .arg(&extract_path)
+    .arg(extract_path)
     .status();
 
   Ok(())

+ 6 - 0
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<R: Runtime> Window<R> {
 
 /// Webview APIs.
 impl<R: Runtime> Window<R> {
+  /// Returns the current url of the webview.
+  pub fn url(&self) -> crate::Result<Url> {
+    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();