Skip to content

Commit

Permalink
refactor!(core): return result in Webview/WebviewWindow::url getter (
Browse files Browse the repository at this point in the history
…#9647)

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

* clippy

* Apply suggestions from code review
  • Loading branch information
amrbashir authored May 7, 2024
1 parent d2fc48f commit 783ef0f
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .changes/url-result-runtime.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-runtime": "patch"
"tauri-runtime-wry": "patch"
---

Changed `WebviewDispatch::url` getter to return a result.
5 changes: 5 additions & 0 deletions .changes/url-result.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": "patch:breaking"
---

Changed `WebviewWindow::url` and `Webview::url` getter to return a result.
4 changes: 2 additions & 2 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ pub enum WebviewMessage {
SetAutoResize(bool),
SetZoom(f64),
// Getters
Url(Sender<Result<Url>>),
Url(Sender<Result<String>>),
Bounds(Sender<Result<tauri_runtime::Rect>>),
Position(Sender<Result<PhysicalPosition<i32>>>),
Size(Sender<Result<PhysicalSize<u32>>>),
Expand Down Expand Up @@ -1305,7 +1305,7 @@ impl<T: UserEvent> WebviewDispatch<T> for WryWebviewDispatcher<T> {

// Getters

fn url(&self) -> Result<Url> {
fn url(&self) -> Result<String> {
webview_getter!(self, WebviewMessage::Url)?
}

Expand Down
2 changes: 1 addition & 1 deletion core/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ pub trait WebviewDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + '
// GETTERS

/// Returns the webview's current URL.
fn url(&self) -> Result<Url>;
fn url(&self) -> Result<String>;

/// Returns the webview's bounds.
fn bounds(&self) -> Result<Rect>;
Expand Down
9 changes: 2 additions & 7 deletions core/tauri/src/test/mock_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,13 +502,8 @@ impl<T: UserEvent> WebviewDispatch<T> for MockWebviewDispatcher {
Ok(())
}

fn url(&self) -> Result<url::Url> {
self
.url
.lock()
.unwrap()
.parse()
.map_err(|_| Error::FailedToReceiveMessage)
fn url(&self) -> Result<String> {
Ok(self.url.lock().unwrap().clone())
}

fn bounds(&self) -> Result<tauri_runtime::Rect> {
Expand Down
9 changes: 6 additions & 3 deletions core/tauri/src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,9 +1073,12 @@ fn main() {
}

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

/// Navigates the webview to the defined url.
Expand Down
3 changes: 1 addition & 2 deletions core/tauri/src/webview/webview_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1605,8 +1605,7 @@ impl<R: Runtime> WebviewWindow<R> {
}

/// Returns the current url of the webview.
// TODO: in v2, change this type to Result
pub fn url(&self) -> Url {
pub fn url(&self) -> crate::Result<Url> {
self.webview.url()
}

Expand Down
22 changes: 22 additions & 0 deletions examples/api/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 783ef0f

Please sign in to comment.