Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: refactor windows and linux backends for better maintainability #1182

Merged
merged 10 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .changes/refactor-api-results.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
"wry": "minor"
---

**Breaking changes**: Changed a few methods on `WebView` type to return a `Result`:

- `Webview::url`
- `Webview::zoom`
- `Webview::load_url`
- `Webview::load_url_with_headers`
- `Webview::bounds`
- `Webview::set_bounds`
- `Webview::set_visible`
- `WebviewExtWindows::set_theme`
- `WebviewExtWindows::set_memory_usage_level`
- `WebviewExtWindows::reparent`
- `WebviewExtUnix::reparent`
- `WebviewExtMacOS::reparent`
56 changes: 32 additions & 24 deletions examples/gtk_multiwebview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,30 +99,38 @@ fn main() -> wry::Result<()> {
..
} => {
let size = size.to_logical::<u32>(window.scale_factor());
webview.set_bounds(Rect {
x: 0,
y: 0,
width: size.width / 2,
height: size.height / 2,
});
webview2.set_bounds(Rect {
x: (size.width / 2) as i32,
y: 0,
width: size.width / 2,
height: size.height / 2,
});
webview3.set_bounds(Rect {
x: 0,
y: (size.height / 2) as i32,
width: size.width / 2,
height: size.height / 2,
});
webview4.set_bounds(Rect {
x: (size.width / 2) as i32,
y: (size.height / 2) as i32,
width: size.width / 2,
height: size.height / 2,
});
webview
.set_bounds(Rect {
x: 0,
y: 0,
width: size.width / 2,
height: size.height / 2,
})
.unwrap();
webview2
.set_bounds(Rect {
x: (size.width / 2) as i32,
y: 0,
width: size.width / 2,
height: size.height / 2,
})
.unwrap();
webview3
.set_bounds(Rect {
x: 0,
y: (size.height / 2) as i32,
width: size.width / 2,
height: size.height / 2,
})
.unwrap();
webview4
.set_bounds(Rect {
x: (size.width / 2) as i32,
y: (size.height / 2) as i32,
width: size.width / 2,
height: size.height / 2,
})
.unwrap();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
Expand Down
56 changes: 32 additions & 24 deletions examples/multiwebview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,30 +100,38 @@ fn main() -> wry::Result<()> {
..
} => {
let size = size.to_logical::<u32>(window.scale_factor());
webview.set_bounds(Rect {
x: 0,
y: 0,
width: size.width / 2,
height: size.height / 2,
});
webview2.set_bounds(Rect {
x: (size.width / 2) as i32,
y: 0,
width: size.width / 2,
height: size.height / 2,
});
webview3.set_bounds(Rect {
x: 0,
y: (size.height / 2) as i32,
width: size.width / 2,
height: size.height / 2,
});
webview4.set_bounds(Rect {
x: (size.width / 2) as i32,
y: (size.height / 2) as i32,
width: size.width / 2,
height: size.height / 2,
});
webview
.set_bounds(Rect {
x: 0,
y: 0,
width: size.width / 2,
height: size.height / 2,
})
.unwrap();
webview2
.set_bounds(Rect {
x: (size.width / 2) as i32,
y: 0,
width: size.width / 2,
height: size.height / 2,
})
.unwrap();
webview3
.set_bounds(Rect {
x: 0,
y: (size.height / 2) as i32,
width: size.width / 2,
height: size.height / 2,
})
.unwrap();
webview4
.set_bounds(Rect {
x: (size.width / 2) as i32,
y: (size.height / 2) as i32,
width: size.width / 2,
height: size.height / 2,
})
.unwrap();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
Expand Down
10 changes: 7 additions & 3 deletions examples/reparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,20 @@ fn main() -> wry::Result<()> {
webview_container = new_parent.id();

#[cfg(target_os = "macos")]
webview.reparent(new_parent.ns_window() as cocoa::base::id);
webview
.reparent(new_parent.ns_window() as cocoa::base::id)
.unwrap();
#[cfg(not(any(
target_os = "windows",
target_os = "macos",
target_os = "ios",
target_os = "android"
)))]
webview.reparent(new_parent.default_vbox().unwrap());
webview
.reparent(new_parent.default_vbox().unwrap())
.unwrap();
#[cfg(target_os = "windows")]
webview.reparent(new_parent.hwnd());
webview.reparent(new_parent.hwnd()).unwrap();
}
_ => {}
}
Expand Down
14 changes: 8 additions & 6 deletions examples/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ fn main() -> wry::Result<()> {
event: WindowEvent::Resized(size),
..
} => {
_webview.set_bounds(wry::Rect {
x: 0,
y: 0,
width: size.width,
height: size.height,
});
_webview
.set_bounds(wry::Rect {
x: 0,
y: 0,
width: size.width,
height: size.height,
})
.unwrap();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
Expand Down
33 changes: 21 additions & 12 deletions src/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,14 @@ impl InnerWebView {
Ok(Self)
}

pub fn print(&self) {}
pub fn print(&self) -> crate::Result<()> {
Ok(())
}

pub fn url(&self) -> String {
pub fn url(&self) -> crate::Result<String> {
let (tx, rx) = bounded(1);
MainPipe::send(WebViewMessage::GetUrl(tx));
rx.recv().unwrap()
rx.recv().map_err(Into::into)
}

pub fn eval(&self, js: &str, callback: Option<impl Fn(String) + Send + 'static>) -> Result<()> {
Expand All @@ -326,45 +328,52 @@ impl InnerWebView {
false
}

pub fn zoom(&self, _scale_factor: f64) {}
pub fn zoom(&self, _scale_factor: f64) -> Result<()> {
Ok(())
}

pub fn set_background_color(&self, background_color: RGBA) -> Result<()> {
MainPipe::send(WebViewMessage::SetBackgroundColor(background_color));
Ok(())
}

pub fn load_url(&self, url: &str) {
pub fn load_url(&self, url: &str) -> Result<()> {
MainPipe::send(WebViewMessage::LoadUrl(url.to_string(), None));
Ok(())
}

pub fn load_url_with_headers(&self, url: &str, headers: http::HeaderMap) {
pub fn load_url_with_headers(&self, url: &str, headers: http::HeaderMap) -> Result<()> {
MainPipe::send(WebViewMessage::LoadUrl(url.to_string(), Some(headers)));
Ok(())
}

pub fn clear_all_browsing_data(&self) -> Result<()> {
MainPipe::send(WebViewMessage::ClearAllBrowsingData);
Ok(())
}

pub fn bounds(&self) -> crate::Rect {
crate::Rect {
pub fn bounds(&self) -> Result<crate::Rect> {
Ok(crate::Rect {
x: 0,
y: 0,
width: 0,
height: 0,
}
})
}

pub fn set_bounds(&self, _bounds: crate::Rect) {
pub fn set_bounds(&self, _bounds: crate::Rect) -> Result<()> {
// Unsupported
Ok(())
}

pub fn set_visible(&self, _visible: bool) {
pub fn set_visible(&self, _visible: bool) -> Result<()> {
// Unsupported
Ok(())
}

pub fn focus(&self) {
pub fn focus(&self) -> Result<()> {
// Unsupported
Ok(())
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ pub enum Error {
UnsupportedWindowHandle,
#[error(transparent)]
Utf8Error(#[from] std::str::Utf8Error),
#[cfg(target_os = "android")]
#[error(transparent)]
CrossBeamRecvError(#[from] crossbeam_channel::RecvError),
}
Loading
Loading