Skip to content

Commit

Permalink
refactor: combine position and size into bounds (#1079)
Browse files Browse the repository at this point in the history
* refactor: combine position and size into bounds

* update change file

* fix macOS build

* again

* Update src/wkwebview/mod.rs
  • Loading branch information
amrbashir committed Nov 14, 2023
1 parent f41a8f0 commit fee99b6
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 152 deletions.
30 changes: 16 additions & 14 deletions .changes/rwh.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
---

Refactor new method to take raw window handle instead. Following are APIs got affected:
- `application` module is removed, and `webivew` module is moved to root module.
- `WebViewBuilder::new`, `WebView::new` now take `RawWindowHandle` instead.
- Attributes `ipc_handler`, `file_drop_handler`, `document_change_handler` don't have window parameter anymore.
Users should use closure to capture the types they want to use.
- Position field in `FileDrop` event is now `Position` instead of `PhysicalPosition`. Users need to handle scale factor
depend on the situation they have.
- `Webview::inner_size` is removed.
- Add `WebViewBuilderExtUnix` trait to extend `WebViewBuilder` on Unix platforms.
- Add `new_gtk` functions to `WebViewBuilderExtUnix` and `WebviewExtUnix`.
- [raw-window-handle](https://docs.rs/raw-window-handle/latest/raw_window_handle/) crate is re-exported as `wry::raw_window_handle`.

This also means that we removed `tao` as a dependency completely which required some changes to the Android backend:
- We exposed the `android_setup` function that needs to be called once to setup necessary logic.
- Previously the `android_binding!` had internal call to `tao::android_binding` but now that `tao` has been removed,
the macro signature has changed and you now need to call `tao::android_binding` yourself, checkout the crate documentation for more information.
- `application` module is removed, and `webivew` module is moved to root module.
- `WebViewBuilder::new`, `WebView::new` now take `RawWindowHandle` instead.
- Add `WebViewBuilder::new_as_child`, `WebView::new_as_child` to crate a webview as a child inside a parent window.
- `Webview::inner_size` is removed.
- Add `WebViewBuilderExtUnix` trait to extend `WebViewBuilder` on Unix platforms.
- Add `new_gtk` functions to `WebViewBuilderExtUnix` and `WebviewExtUnix`.
- [raw-window-handle](https://docs.rs/raw-window-handle/latest/raw_window_handle/) crate is re-exported as `wry::raw_window_handle`.

This also means that we removed `tao` as a dependency completely which required some changes to the public APIs and to the Android backend:

- Webview attributes `ipc_handler`, `file_drop_handler`, `document_change_handler` don't take the `Window` as first parameter anymore.
Users should use closure to capture the types they want to use.
- Position field in `FileDrop` event is now a tuple of `(x, y)` physical position instead of `PhysicalPosition`. Users need to handle scale factor
- We exposed the `android_setup` function that needs to be called once to setup necessary logic.
- Previously the `android_binding!` had internal call to `tao::android_binding` but now that `tao` has been removed,
the macro signature has changed and you now need to call `tao::android_binding` yourself, checkout the crate documentation for more information.
66 changes: 49 additions & 17 deletions examples/multiwebview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use winit::{
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
use wry::WebViewBuilder;
use wry::{Rect, WebViewBuilder};

fn main() -> wry::Result<()> {
#[cfg(any(
Expand Down Expand Up @@ -43,23 +43,39 @@ fn main() -> wry::Result<()> {
let size = window.inner_size().to_logical::<u32>(window.scale_factor());

let webview = WebViewBuilder::new_as_child(&window)
.with_position((0, 0))
.with_size((size.width / 2, size.height / 2))
.with_bounds(Rect {
x: 0,
y: 0,
width: size.width / 2,
height: size.height / 2,
})
.with_url("https://tauri.app")?
.build()?;
let webview2 = WebViewBuilder::new_as_child(&window)
.with_position(((size.width / 2) as i32, 0))
.with_size((size.width / 2, size.height / 2))
.with_bounds(Rect {
x: (size.width / 2) as i32,
y: 0,
width: size.width / 2,
height: size.height / 2,
})
.with_url("https://github.com/tauri-apps/wry")?
.build()?;
let webview3 = WebViewBuilder::new_as_child(&window)
.with_position((0, (size.height / 2) as i32))
.with_size((size.width / 2, size.height / 2))
.with_bounds(Rect {
x: 0,
y: (size.height / 2) as i32,
width: size.width / 2,
height: size.height / 2,
})
.with_url("https://twitter.com/TauriApps")?
.build()?;
let webview4 = WebViewBuilder::new_as_child(&window)
.with_position(((size.width / 2) as i32, (size.height / 2) as i32))
.with_size((size.width / 2, size.height / 2))
.with_bounds(Rect {
x: (size.width / 2) as i32,
y: (size.height / 2) as i32,
width: size.width / 2,
height: size.height / 2,
})
.with_url("https://google.com")?
.build()?;

Expand All @@ -84,14 +100,30 @@ fn main() -> wry::Result<()> {
..
} => {
let size = size.to_logical::<u32>(window.scale_factor());
webview.set_size((size.width / 2, size.height / 2));
webview.set_position((0, 0));
webview2.set_position(((size.width / 2) as i32, 0));
webview2.set_size((size.width / 2, size.height / 2));
webview3.set_position((0, (size.height / 2) as i32));
webview3.set_size((size.width / 2, size.height / 2));
webview4.set_position(((size.width / 2) as i32, (size.height / 2) as i32));
webview4.set_size((size.width / 2, size.height / 2));
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,
});
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
Expand Down
10 changes: 7 additions & 3 deletions examples/wgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use winit::{
event_loop::{ControlFlow, EventLoop},
window::Window,
};
use wry::WebViewBuilder;
use wry::{Rect, WebViewBuilder};

async fn run(event_loop: EventLoop<()>, window: Window) {
let size = window.inner_size();
Expand Down Expand Up @@ -97,8 +97,12 @@ fn fs_main() -> @location(0) vec4<f32> {
surface.configure(&device, &config);

let _webview = WebViewBuilder::new_as_child(&window)
.with_position((100, 100))
.with_size((400, 400))
.with_bounds(Rect {
x: 0,
y: 0,
width: 1,
height: 1,
})
.with_transparent(true)
.with_html(
r#"<html>
Expand Down
7 changes: 6 additions & 1 deletion examples/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ fn main() -> wry::Result<()> {
event: WindowEvent::Resized(size),
..
} => {
_webview.set_size(size.into());
_webview.set_bounds(wry::Rect {
x: 0,
y: 0,
width: size.width,
height: size.height,
});
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
Expand Down
8 changes: 2 additions & 6 deletions src/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,8 @@ impl InnerWebView {
Ok(())
}

pub fn set_position(&self, _position: (i32, i32)) {
// Unsupported.
}

pub fn set_size(&self, _size: (u32, u32)) {
// Unsupported.
pub fn set_bounds(&self, bounds: crate::Rect) {
// Unsupported
}

pub fn set_visible(&self, _visible: bool) {
Expand Down
60 changes: 31 additions & 29 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,19 @@ pub use proxy::{ProxyConfig, ProxyEndpoint};
pub use url::Url;
pub use web_context::WebContext;

/// A rectangular region.
#[derive(Clone, Copy, Debug)]
pub struct Rect {
/// x coordinate of top left corner
pub x: i32,
/// y coordinate of top left corner
pub y: i32,
/// width
pub width: u32,
/// height
pub height: u32,
}

/// Resolves a custom protocol [`Request`] asynchronously.
///
/// See [`WebViewBuilder::with_asynchronous_custom_protocol`] for more information.
Expand Down Expand Up @@ -450,15 +463,9 @@ pub struct WebViewAttributes {
/// - **macOS / Android / iOS:** Unsupported.
pub focused: bool,

/// The webview postion.
/// This is effective if the webview was created by [`WebView::new_as_child`].
/// If it's `None`, the position will be (0, 0).
pub position: Option<(i32, i32)>,

/// The webview size.
/// This is effective if the webview was created by [`WebView::new_as_child`].
/// If it's `None`, the size will be (0, 0).
pub size: Option<(u32, u32)>,
/// The webview bounds. Defaults to `x: 0, y: 0, width: 200, height: 200`.
/// This is only effective if the webview was created by [`WebView::new_as_child`] or [`WebViewBuilder::new_as_child`].
pub bounds: Option<Rect>,
}

impl Default for WebViewAttributes {
Expand Down Expand Up @@ -493,8 +500,12 @@ impl Default for WebViewAttributes {
on_page_load_handler: None,
proxy_config: None,
focused: true,
position: None,
size: None,
bounds: Some(Rect {
x: 0,
y: 0,
width: 200,
height: 200,
}),
}
}
}
Expand Down Expand Up @@ -951,15 +962,10 @@ impl<'a> WebViewBuilder<'a> {
self
}

/// Set the webview position relative to its parent if it was created as a child.
pub fn with_position(mut self, position: (i32, i32)) -> Self {
self.attrs.position = Some(position);
self
}

/// Set the webview size if it was created as a child.
pub fn with_size(mut self, size: (u32, u32)) -> Self {
self.attrs.size = Some(size);
/// Specify the webview position relative to its parent if it will be created as a child.
/// Defaults to `x: 0, y: 0, width: 200, height: 200`.
pub fn with_bounds(mut self, bounds: Rect) -> Self {
self.attrs.bounds.replace(bounds);
self
}

Expand Down Expand Up @@ -1354,15 +1360,11 @@ impl WebView {
self.webview.clear_all_browsing_data()
}

/// Set the webview position relative to its parent if it was created as a child.
pub fn set_position(&self, position: (i32, i32)) {
self.webview.set_position(position)
}

/// Set the webview size if it was created as a child
/// or if ot was created directly in an X11 Window.
pub fn set_size(&self, size: (u32, u32)) {
self.webview.set_size(size)
/// Set the webview bounds.
///
/// This is only effective if the webview was created as a child.
pub fn set_bounds(&self, bounds: Rect) {
self.webview.set_bounds(bounds)
}

/// Shows or hides the webview.
Expand Down
33 changes: 21 additions & 12 deletions src/webkitgtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ use web_context::WebContextExt;
pub use web_context::WebContextImpl;

use crate::{
proxy::ProxyConfig, web_context::WebContext, Error, PageLoadEvent, Result, WebViewAttributes,
RGBA,
proxy::ProxyConfig, web_context::WebContext, Error, PageLoadEvent, Rect, Result,
WebViewAttributes, RGBA,
};

mod file_drop;
Expand Down Expand Up @@ -115,10 +115,13 @@ impl InnerWebView {
(xlib.XCreateSimpleWindow)(
display as _,
window_handle,
attributes.position.map(|p| p.0).unwrap_or(0),
attributes.position.map(|p| p.1).unwrap_or(0),
attributes.size.map(|s| s.0).unwrap_or(0),
attributes.size.map(|s| s.1).unwrap_or(0),
attributes.bounds.map(|p| p.x).unwrap_or(0),
attributes.bounds.map(|p| p.y).unwrap_or(0),
// it is unlikey that bounds are not set because
// we have a default for it, but anyways we need to have a fallback
// and we need to use 1 not 0 here otherwise xlib will crash
attributes.bounds.map(|s| s.width).unwrap_or(1),
attributes.bounds.map(|s| s.height).unwrap_or(1),
0,
0,
0,
Expand Down Expand Up @@ -597,20 +600,26 @@ impl InnerWebView {
Ok(())
}

pub fn set_position(&self, position: (i32, i32)) {
pub fn set_bounds(&self, bounds: Rect) {
if self.is_child {
if let Some(window) = &self.gtk_window {
window.move_(position.0, position.1);
window.move_(bounds.x, bounds.y);
}
}
}

pub fn set_size(&self, size: (u32, u32)) {
if let Some(window) = &self.gtk_window {
if self.is_child {
window.window().unwrap().resize(size.0 as _, size.1 as _);
window
.window()
.unwrap()
.resize(bounds.width as i32, bounds.height as i32);
}
window.size_allocate(&gtk::Allocation::new(200, 200, size.0 as _, size.1 as _));
window.size_allocate(&gtk::Allocation::new(
0,
0,
bounds.width as i32,
bounds.height as i32,
));
}
}

Expand Down
Loading

0 comments on commit fee99b6

Please sign in to comment.