Skip to content

Commit

Permalink
feat: expose url method (tauri-apps#5914)
Browse files Browse the repository at this point in the history
Co-authored-by: Fabian-Lars <[email protected]>
Co-authored-by: Jonas Kruckenberg <[email protected]>
  • Loading branch information
3 people authored Dec 26, 2022
1 parent 7262189 commit d17027e
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changes/url-getter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"tauri-runtime": minor
"tauri-runtime-wry": minor
"tauri": minor
---

Added window's `url()` getter.
12 changes: 11 additions & 1 deletion core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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>>>),
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions core/tauri-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions core/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>;

Expand Down
4 changes: 4 additions & 0 deletions core/tauri/src/test/mock_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/updater/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
6 changes: 6 additions & 0 deletions core/tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
pub(crate) mod menu;

pub use menu::{MenuEvent, MenuHandle};
use url::Url;

#[cfg(target_os = "macos")]
use crate::TitleBarStyle;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit d17027e

Please sign in to comment.