Skip to content

Commit

Permalink
fix(windows): resize webview to parent after initialization (#1233)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored Apr 25, 2024
1 parent d7a89ec commit 203604c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changes/webview-size-windows.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": "patch"
---

On Windows, fix the webview occasianlly not filling up the whole window if the parent window was resized during the webview initialization.
37 changes: 23 additions & 14 deletions src/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl InnerWebView {
) -> Result<Self> {
let _ = unsafe { CoInitializeEx(None, COINIT_APARTMENTTHREADED) };

let (hwnd, size) = Self::create_container_hwnd(parent, &attributes, is_child)?;
let hwnd = Self::create_container_hwnd(parent, &attributes, is_child)?;

let drop_handler = attributes.drag_drop_handler.take();

Expand All @@ -134,7 +134,6 @@ impl InnerWebView {
let webview = Self::init_webview(
parent,
hwnd,
size,
attributes,
&env,
&controller,
Expand All @@ -144,23 +143,29 @@ impl InnerWebView {

let drag_drop_controller = drop_handler.map(|handler| DragDropController::new(hwnd, handler));

Ok(Self {
let w = Self {
parent: RefCell::new(parent),
hwnd,
controller,
is_child,
webview,
env,
drag_drop_controller,
})
};

if !is_child {
w.resize_to_parent()?;
}

Ok(w)
}

#[inline]
fn create_container_hwnd(
parent: HWND,
attributes: &WebViewAttributes,
is_child: bool,
) -> Result<(HWND, (i32, i32))> {
) -> Result<HWND> {
unsafe extern "system" fn default_window_proc(
hwnd: HWND,
msg: u32,
Expand Down Expand Up @@ -235,7 +240,7 @@ impl InnerWebView {
)
};

Ok((hwnd, (width, height)))
Ok(hwnd)
}

#[inline]
Expand Down Expand Up @@ -355,7 +360,6 @@ impl InnerWebView {
fn init_webview(
parent: HWND,
hwnd: HWND,
bounds: (i32, i32),
mut attributes: WebViewAttributes,
env: &ICoreWebView2Environment,
controller: &ICoreWebView2Controller,
Expand Down Expand Up @@ -469,13 +473,6 @@ impl InnerWebView {
}

unsafe {
controller.SetBounds(RECT {
left: 0,
top: 0,
right: bounds.0,
bottom: bounds.1,
})?;

controller.SetIsVisible(attributes.visible)?;

if attributes.focused {
Expand Down Expand Up @@ -1265,6 +1262,18 @@ impl InnerWebView {
Ok(())
}

fn resize_to_parent(&self) -> crate::Result<()> {
let mut rect = RECT::default();
unsafe { win32wm::GetClientRect(*self.parent.borrow(), &mut rect)? };
let width = rect.right - rect.left;
let height = rect.bottom - rect.top;

self.set_bounds(Rect {
size: dpi::Size::Logical((width, height).into()),
..Default::default()
})
}

pub fn set_visible(&self, visible: bool) -> Result<()> {
unsafe {
let _ = ShowWindow(
Expand Down

0 comments on commit 203604c

Please sign in to comment.