Skip to content

Commit

Permalink
feat: add inner_size for webview struct (#394)
Browse files Browse the repository at this point in the history
* Add inner_size method

* Add inner_size for webview

* Fix feature flag placement

* Add feature flag on inner_size

* Cargo fmt
  • Loading branch information
Ngo Iok Ui (Wu Yu Wei) authored Sep 16, 2021
1 parent 01de53f commit 3abe0bc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changes/mac-inner-size.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Add inner size method for webview. This can reflect correct size of webview on macOS.
6 changes: 4 additions & 2 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() -> wry::Result<()> {
let window = WindowBuilder::new()
.with_title("Hello World")
.build(&event_loop)?;
let _webview = WebViewBuilder::new(window)?
let webview = WebViewBuilder::new(window)?
.with_url("https://html5test.com")?
.build()?;

Expand All @@ -29,7 +29,9 @@ fn main() -> wry::Result<()> {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
_ => (),
_ => {
dbg!(webview.window().inner_size());
}
}
});
}
12 changes: 11 additions & 1 deletion src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use url::Url;

#[cfg(target_os = "windows")]
use crate::application::platform::windows::WindowExtWindows;
use crate::application::window::Window;
use crate::application::{dpi::PhysicalSize, window::Window};

use crate::http::{Request as HttpRequest, Response as HttpResponse};

Expand Down Expand Up @@ -421,6 +421,16 @@ impl WebView {
pub fn focus(&self) {
self.webview.focus();
}

pub fn inner_size(&self) -> PhysicalSize<u32> {
#[cfg(target_os = "macos")]
{
let scale_factor = self.window.scale_factor();
self.webview.inner_size(scale_factor)
}
#[cfg(not(target_os = "macos"))]
self.window.inner_size()
}
}

// Helper so all platforms handle RPC messages consistently.
Expand Down
22 changes: 19 additions & 3 deletions src/webview/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ use file_drop::{add_file_drop_methods, set_file_drop_handler};
use crate::application::platform::ios::WindowExtIOS;

use crate::{
application::window::Window,
application::{
dpi::{LogicalSize, PhysicalSize},
window::Window,
},
webview::{FileDropEvent, RpcRequest, RpcResponse, WebContext, WebViewAttributes},
Result,
};
Expand All @@ -48,7 +51,7 @@ use crate::http::{
mod file_drop;

pub struct InnerWebView {
webview: Id<Object>,
webview: id,
#[cfg(target_os = "macos")]
ns_window: id,
manager: id,
Expand Down Expand Up @@ -307,7 +310,7 @@ impl InnerWebView {
let ns_window = window.ns_window() as id;

let w = Self {
webview: Id::from_ptr(webview),
webview,
#[cfg(target_os = "macos")]
ns_window,
manager,
Expand Down Expand Up @@ -423,6 +426,14 @@ impl InnerWebView {
}

pub fn focus(&self) {}

#[cfg(target_os = "macos")]
pub fn inner_size(&self, scale_factor: f64) -> PhysicalSize<u32> {
let view_frame = unsafe { NSView::frame(self.webview) };
let logical: LogicalSize<f64> =
(view_frame.size.width as f64, view_frame.size.height as f64).into();
logical.to_physical(scale_factor)
}
}

pub fn platform_webview_version() -> Result<String> {
Expand Down Expand Up @@ -455,6 +466,11 @@ impl Drop for InnerWebView {
let _ = Box::from_raw(*ptr);
}
}

let _: Id<_> = Id::from_ptr(self.webview);
#[cfg(target_os = "macos")]
let _: Id<_> = Id::from_ptr(self.ns_window);
let _: Id<_> = Id::from_ptr(self.manager);
}
}
}
Expand Down

0 comments on commit 3abe0bc

Please sign in to comment.