From 3abe0bc30e377b7055ee50e05bbdab80365fedc5 Mon Sep 17 00:00:00 2001 From: "Ngo Iok Ui (Wu Yu Wei)" Date: Thu, 16 Sep 2021 13:41:21 +0800 Subject: [PATCH] feat: add inner_size for webview struct (#394) * Add inner_size method * Add inner_size for webview * Fix feature flag placement * Add feature flag on inner_size * Cargo fmt --- .changes/mac-inner-size.md | 5 +++++ examples/hello_world.rs | 6 ++++-- src/webview/mod.rs | 12 +++++++++++- src/webview/wkwebview/mod.rs | 22 +++++++++++++++++++--- 4 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 .changes/mac-inner-size.md diff --git a/.changes/mac-inner-size.md b/.changes/mac-inner-size.md new file mode 100644 index 000000000..a22e11127 --- /dev/null +++ b/.changes/mac-inner-size.md @@ -0,0 +1,5 @@ +--- +"wry": patch +--- + +Add inner size method for webview. This can reflect correct size of webview on macOS. diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 195f5376b..007e263b7 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -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()?; @@ -29,7 +29,9 @@ fn main() -> wry::Result<()> { event: WindowEvent::CloseRequested, .. } => *control_flow = ControlFlow::Exit, - _ => (), + _ => { + dbg!(webview.window().inner_size()); + } } }); } diff --git a/src/webview/mod.rs b/src/webview/mod.rs index 1aa72c477..a5d1d17cf 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -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}; @@ -421,6 +421,16 @@ impl WebView { pub fn focus(&self) { self.webview.focus(); } + + pub fn inner_size(&self) -> PhysicalSize { + #[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. diff --git a/src/webview/wkwebview/mod.rs b/src/webview/wkwebview/mod.rs index efcced1c9..96d643543 100644 --- a/src/webview/wkwebview/mod.rs +++ b/src/webview/wkwebview/mod.rs @@ -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, }; @@ -48,7 +51,7 @@ use crate::http::{ mod file_drop; pub struct InnerWebView { - webview: Id, + webview: id, #[cfg(target_os = "macos")] ns_window: id, manager: id, @@ -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, @@ -423,6 +426,14 @@ impl InnerWebView { } pub fn focus(&self) {} + + #[cfg(target_os = "macos")] + pub fn inner_size(&self, scale_factor: f64) -> PhysicalSize { + let view_frame = unsafe { NSView::frame(self.webview) }; + let logical: LogicalSize = + (view_frame.size.width as f64, view_frame.size.height as f64).into(); + logical.to_physical(scale_factor) + } } pub fn platform_webview_version() -> Result { @@ -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); } } }