Skip to content

Commit

Permalink
refactor: migrate to dpi crate (#1202)
Browse files Browse the repository at this point in the history
* refactor: migrate to `dpi` crate

closes #1172

* macOS

* linux

* fix doctests

* imports

* more doctests

* fix android and ios

* Update examples/winit.rs

Co-authored-by: Jason Tsai <[email protected]>

* Update src/webview2/mod.rs

---------

Co-authored-by: Jason Tsai <[email protected]>
  • Loading branch information
amrbashir and pewsheen authored Mar 28, 2024
1 parent fdbd3d3 commit 34ae1ca
Show file tree
Hide file tree
Showing 13 changed files with 346 additions and 236 deletions.
5 changes: 5 additions & 0 deletions .changes/dpi-crate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": "minor"
---

Add `dpi` module which is a re-export of `dpi` crate.
5 changes: 5 additions & 0 deletions .changes/rect-dpi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": "minor"
---

**Breaking Change**: Removed `x`, `y`, `with` and `height` fields from `Rect` struct and replaced it with `size` and `position` fields.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ rustdoc-args = [ "--cfg", "docsrs" ]

[features]
default = [ "drag-drop", "objc-exception", "protocol", "os-webview" ]
serde = [ "dpi/serde" ]
objc-exception = [ "objc/exception" ]
drag-drop = [ ]
protocol = [ ]
Expand Down Expand Up @@ -54,6 +55,7 @@ once_cell = "1"
thiserror = "1.0"
http = "1.1"
raw-window-handle = { version = "0.6", features = [ "std" ] }
dpi = "0.1"

[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
javascriptcore-rs = { version = "=1.1", features = [ "v2_28" ], optional = true }
Expand Down
55 changes: 20 additions & 35 deletions examples/gtk_multiwebview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use tao::{
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
use wry::dpi::{LogicalPosition, LogicalSize};
use wry::{Rect, WebViewBuilder};

fn main() -> wry::Result<()> {
Expand Down Expand Up @@ -55,37 +56,29 @@ fn main() -> wry::Result<()> {

let webview = create_webview_builder()
.with_bounds(Rect {
x: 0,
y: 0,
width: size.width / 2,
height: size.height / 2,
position: LogicalPosition::new(0, 0).into(),
size: LogicalSize::new(size.width / 2, size.height / 2).into(),
})
.with_url("https://tauri.app")
.build()?;
let webview2 = create_webview_builder()
let webview2 = WebViewBuilder::new_as_child(&window)
.with_bounds(Rect {
x: (size.width / 2) as i32,
y: 0,
width: size.width / 2,
height: size.height / 2,
position: LogicalPosition::new(size.width / 2, 0).into(),
size: LogicalSize::new(size.width / 2, size.height / 2).into(),
})
.with_url("https://github.com/tauri-apps/wry")
.build()?;
let webview3 = create_webview_builder()
let webview3 = WebViewBuilder::new_as_child(&window)
.with_bounds(Rect {
x: 0,
y: (size.height / 2) as i32,
width: size.width / 2,
height: size.height / 2,
position: LogicalPosition::new(0, size.height / 2).into(),
size: LogicalSize::new(size.width / 2, size.height / 2).into(),
})
.with_url("https://twitter.com/TauriApps")
.build()?;
let webview4 = create_webview_builder()
let webview4 = WebViewBuilder::new_as_child(&window)
.with_bounds(Rect {
x: (size.width / 2) as i32,
y: (size.height / 2) as i32,
width: size.width / 2,
height: size.height / 2,
position: LogicalPosition::new(size.width / 2, size.height / 2).into(),
size: LogicalSize::new(size.width / 2, size.height / 2).into(),
})
.with_url("https://google.com")
.build()?;
Expand All @@ -101,34 +94,26 @@ 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,
position: LogicalPosition::new(0, 0).into(),
size: LogicalSize::new(size.width / 2, size.height / 2).into(),
})
.unwrap();
webview2
.set_bounds(Rect {
x: (size.width / 2) as i32,
y: 0,
width: size.width / 2,
height: size.height / 2,
position: LogicalPosition::new(size.width / 2, 0).into(),
size: LogicalSize::new(size.width / 2, size.height / 2).into(),
})
.unwrap();
webview3
.set_bounds(Rect {
x: 0,
y: (size.height / 2) as i32,
width: size.width / 2,
height: size.height / 2,
position: LogicalPosition::new(0, size.height / 2).into(),
size: LogicalSize::new(size.width / 2, size.height / 2).into(),
})
.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,
position: LogicalPosition::new(size.width / 2, size.height / 2).into(),
size: LogicalSize::new(size.width / 2, size.height / 2).into(),
})
.unwrap();
}
Expand Down
52 changes: 18 additions & 34 deletions examples/multiwebview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
// SPDX-License-Identifier: MIT

use winit::{
dpi::LogicalSize,
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
use wry::dpi::{LogicalPosition, LogicalSize};
use wry::{Rect, WebViewBuilder};

fn main() -> wry::Result<()> {
Expand Down Expand Up @@ -36,45 +36,37 @@ fn main() -> wry::Result<()> {

let event_loop = EventLoop::new().unwrap();
let window = WindowBuilder::new()
.with_inner_size(LogicalSize::new(800, 800))
.with_inner_size(winit::dpi::LogicalSize::new(800, 800))
.build(&event_loop)
.unwrap();

let size = window.inner_size().to_logical::<u32>(window.scale_factor());

let webview = WebViewBuilder::new_as_child(&window)
.with_bounds(Rect {
x: 0,
y: 0,
width: size.width / 2,
height: size.height / 2,
position: LogicalPosition::new(0, 0).into(),
size: LogicalSize::new(size.width / 2, size.height / 2).into(),
})
.with_url("https://tauri.app")
.build()?;
let webview2 = WebViewBuilder::new_as_child(&window)
.with_bounds(Rect {
x: (size.width / 2) as i32,
y: 0,
width: size.width / 2,
height: size.height / 2,
position: LogicalPosition::new(size.width / 2, 0).into(),
size: LogicalSize::new(size.width / 2, size.height / 2).into(),
})
.with_url("https://github.com/tauri-apps/wry")
.build()?;
let webview3 = WebViewBuilder::new_as_child(&window)
.with_bounds(Rect {
x: 0,
y: (size.height / 2) as i32,
width: size.width / 2,
height: size.height / 2,
position: LogicalPosition::new(0, size.height / 2).into(),
size: LogicalSize::new(size.width / 2, size.height / 2).into(),
})
.with_url("https://twitter.com/TauriApps")
.build()?;
let webview4 = WebViewBuilder::new_as_child(&window)
.with_bounds(Rect {
x: (size.width / 2) as i32,
y: (size.height / 2) as i32,
width: size.width / 2,
height: size.height / 2,
position: LogicalPosition::new(size.width / 2, size.height / 2).into(),
size: LogicalSize::new(size.width / 2, size.height / 2).into(),
})
.with_url("https://google.com")
.build()?;
Expand Down Expand Up @@ -102,34 +94,26 @@ 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,
position: LogicalPosition::new(0, 0).into(),
size: LogicalSize::new(size.width / 2, size.height / 2).into(),
})
.unwrap();
webview2
.set_bounds(Rect {
x: (size.width / 2) as i32,
y: 0,
width: size.width / 2,
height: size.height / 2,
position: LogicalPosition::new(size.width / 2, 0).into(),
size: LogicalSize::new(size.width / 2, size.height / 2).into(),
})
.unwrap();
webview3
.set_bounds(Rect {
x: 0,
y: (size.height / 2) as i32,
width: size.width / 2,
height: size.height / 2,
position: LogicalPosition::new(0, size.height / 2).into(),
size: LogicalSize::new(size.width / 2, size.height / 2).into(),
})
.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,
position: LogicalPosition::new(size.width / 2, size.height / 2).into(),
size: LogicalSize::new(size.width / 2, size.height / 2).into(),
})
.unwrap();
}
Expand Down
7 changes: 3 additions & 4 deletions examples/wgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use winit::{
event_loop::{ControlFlow, EventLoop},
window::Window,
};
use wry::dpi::{LogicalPosition, LogicalSize};
use wry::{Rect, WebViewBuilder};

async fn run(event_loop: EventLoop<()>, window: Window) {
Expand Down Expand Up @@ -100,10 +101,8 @@ fn fs_main() -> @location(0) vec4<f32> {

let _webview = WebViewBuilder::new_as_child(&window)
.with_bounds(Rect {
x: 100,
y: 100,
width: 200,
height: 200,
position: LogicalPosition::new(100, 100).into(),
size: LogicalSize::new(200, 200).into(),
})
.with_transparent(true)
.with_html(
Expand Down
11 changes: 5 additions & 6 deletions examples/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// SPDX-License-Identifier: MIT

use winit::{
dpi::LogicalSize,
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
Expand Down Expand Up @@ -36,7 +35,7 @@ fn main() -> wry::Result<()> {

let event_loop = EventLoop::new().unwrap();
let window = WindowBuilder::new()
.with_inner_size(LogicalSize::new(800, 800))
.with_inner_size(winit::dpi::LogicalSize::new(800, 800))
.build(&event_loop)
.unwrap();

Expand Down Expand Up @@ -71,12 +70,12 @@ fn main() -> wry::Result<()> {
event: WindowEvent::Resized(size),
..
} => {
use wry::dpi::{PhysicalPosition, PhysicalSize};

_webview
.set_bounds(wry::Rect {
x: 0,
y: 0,
width: size.width,
height: size.height,
position: PhysicalPosition::new(0, 0).into(),
size: PhysicalSize::new(size.width, size.height).into(),
})
.unwrap();
}
Expand Down
7 changes: 1 addition & 6 deletions src/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,7 @@ impl InnerWebView {
}

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

pub fn set_bounds(&self, _bounds: crate::Rect) -> Result<()> {
Expand Down
Loading

0 comments on commit 34ae1ca

Please sign in to comment.