refactor!(core): return result in Webview/WebviewWindow::url getter (#9647)

* refactor!(core): return result in `Webview/WebviewWindow::url` getter

* clippy

* Apply suggestions from code review
This commit is contained in:
Amr Bashir 2024-05-07 17:16:17 +03:00 committed by GitHub
parent d2fc48f0e6
commit 783ef0f2d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 45 additions and 15 deletions

View File

@ -0,0 +1,6 @@
---
"tauri-runtime": "patch"
"tauri-runtime-wry": "patch"
---
Changed `WebviewDispatch::url` getter to return a result.

5
.changes/url-result.md Normal file
View File

@ -0,0 +1,5 @@
---
"tauri": "patch:breaking"
---
Changed `WebviewWindow::url` and `Webview::url` getter to return a result.

View File

@ -1190,7 +1190,7 @@ pub enum WebviewMessage {
SetAutoResize(bool), SetAutoResize(bool),
SetZoom(f64), SetZoom(f64),
// Getters // Getters
Url(Sender<Result<Url>>), Url(Sender<Result<String>>),
Bounds(Sender<Result<tauri_runtime::Rect>>), Bounds(Sender<Result<tauri_runtime::Rect>>),
Position(Sender<Result<PhysicalPosition<i32>>>), Position(Sender<Result<PhysicalPosition<i32>>>),
Size(Sender<Result<PhysicalSize<u32>>>), Size(Sender<Result<PhysicalSize<u32>>>),
@ -1305,7 +1305,7 @@ impl<T: UserEvent> WebviewDispatch<T> for WryWebviewDispatcher<T> {
// Getters // Getters
fn url(&self) -> Result<Url> { fn url(&self) -> Result<String> {
webview_getter!(self, WebviewMessage::Url)? webview_getter!(self, WebviewMessage::Url)?
} }

View File

@ -445,7 +445,7 @@ pub trait WebviewDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + '
// GETTERS // GETTERS
/// Returns the webview's current URL. /// Returns the webview's current URL.
fn url(&self) -> Result<Url>; fn url(&self) -> Result<String>;
/// Returns the webview's bounds. /// Returns the webview's bounds.
fn bounds(&self) -> Result<Rect>; fn bounds(&self) -> Result<Rect>;

View File

@ -502,13 +502,8 @@ impl<T: UserEvent> WebviewDispatch<T> for MockWebviewDispatcher {
Ok(()) Ok(())
} }
fn url(&self) -> Result<url::Url> { fn url(&self) -> Result<String> {
self Ok(self.url.lock().unwrap().clone())
.url
.lock()
.unwrap()
.parse()
.map_err(|_| Error::FailedToReceiveMessage)
} }
fn bounds(&self) -> Result<tauri_runtime::Rect> { fn bounds(&self) -> Result<tauri_runtime::Rect> {

View File

@ -1073,9 +1073,12 @@ fn main() {
} }
/// Returns the current url of the webview. /// Returns the current url of the webview.
// TODO: in v2, change this type to Result pub fn url(&self) -> crate::Result<Url> {
pub fn url(&self) -> Url { self
self.webview.dispatcher.url().unwrap() .webview
.dispatcher
.url()
.map(|url| url.parse().map_err(crate::Error::InvalidUrl))?
} }
/// Navigates the webview to the defined url. /// Navigates the webview to the defined url.

View File

@ -1605,8 +1605,7 @@ impl<R: Runtime> WebviewWindow<R> {
} }
/// Returns the current url of the webview. /// Returns the current url of the webview.
// TODO: in v2, change this type to Result pub fn url(&self) -> crate::Result<Url> {
pub fn url(&self) -> Url {
self.webview.url() self.webview.url()
} }

View File

@ -4002,6 +4002,17 @@ dependencies = [
"syn 2.0.61", "syn 2.0.61",
] ]
[[package]]
name = "windows-implement"
version = "0.56.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6fc35f58ecd95a9b71c4f2329b911016e6bec66b3f2e6a4aad86bd2e99e2f9b"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.52",
]
[[package]] [[package]]
name = "windows-interface" name = "windows-interface"
version = "0.56.0" version = "0.56.0"
@ -4013,6 +4024,17 @@ dependencies = [
"syn 2.0.61", "syn 2.0.61",
] ]
[[package]]
name = "windows-interface"
version = "0.56.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08990546bf4edef8f431fa6326e032865f27138718c587dc21bc0265bbcb57cc"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.52",
]
[[package]] [[package]]
name = "windows-result" name = "windows-result"
version = "0.1.1" version = "0.1.1"