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

feat: add inner_size for webview struct #394

Merged
merged 5 commits into from
Sep 16, 2021
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
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