Skip to content

Commit

Permalink
fix(core/wry): implement resizing natively on Windows (#9862)
Browse files Browse the repository at this point in the history
closes #7388
closes #9510
closes #9464

ref #9268
ref #9053
ref #8770
ref #8750
ref #4012
  • Loading branch information
amrbashir authored Jun 5, 2024
1 parent fafc238 commit f29b788
Show file tree
Hide file tree
Showing 3 changed files with 313 additions and 154 deletions.
10 changes: 10 additions & 0 deletions .changes/undecorated-resizing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"tauri": "patch:bug"
"tauri-runtime-wry": "patch:bug"
---

On Windows, handle resizing undecorated windows natively which improves performance and fixes a couple of annoyances with previous JS implementation:
- No more cursor flickering when moving the cursor across an edge.
- Can resize from top even when `data-tauri-drag-region` element exists there.
- Upon starting rezing, clicks don't go through elements behind it so no longer accidental clicks.

54 changes: 20 additions & 34 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1988,8 +1988,6 @@ pub struct WindowWrapper {
webviews: Vec<WebviewWrapper>,
window_event_listeners: WindowEventListeners,
#[cfg(windows)]
is_window_fullscreen: bool,
#[cfg(windows)]
is_window_transparent: bool,
#[cfg(windows)]
surface: Option<softbuffer::Surface<Arc<Window>, Arc<Window>>>,
Expand Down Expand Up @@ -2773,7 +2771,15 @@ fn handle_user_message<T: UserEvent>(
WindowMessage::Destroy => {
panic!("cannot handle `WindowMessage::Destroy` on the main thread")
}
WindowMessage::SetDecorations(decorations) => window.set_decorations(decorations),
WindowMessage::SetDecorations(decorations) => {
window.set_decorations(decorations);
#[cfg(windows)]
if decorations {
undecorated_resizing::detach_resize_handler(window.hwnd());
} else {
undecorated_resizing::attach_resize_handler(window.hwnd());
}
}
WindowMessage::SetShadow(_enable) => {
#[cfg(windows)]
window.set_undecorated_shadow(_enable);
Expand Down Expand Up @@ -2806,10 +2812,6 @@ fn handle_user_message<T: UserEvent>(
} else {
window.set_fullscreen(None)
}
#[cfg(windows)]
if let Some(w) = windows.0.borrow_mut().get_mut(&id) {
w.is_window_fullscreen = fullscreen;
}
}
WindowMessage::SetFocus => {
window.set_focus();
Expand Down Expand Up @@ -3197,8 +3199,6 @@ fn handle_user_message<T: UserEvent>(
Message::CreateRawWindow(window_id, handler, sender) => {
let (label, builder) = handler();

#[cfg(windows)]
let is_window_fullscreen = builder.window.fullscreen.is_some();
#[cfg(windows)]
let is_window_transparent = builder.window.transparent;

Expand Down Expand Up @@ -3232,8 +3232,6 @@ fn handle_user_message<T: UserEvent>(
window_event_listeners: Default::default(),
webviews: Vec::new(),
#[cfg(windows)]
is_window_fullscreen,
#[cfg(windows)]
is_window_transparent,
#[cfg(windows)]
surface,
Expand Down Expand Up @@ -3577,8 +3575,6 @@ fn create_window<T: UserEvent, F: Fn(RawWindow) + Send + 'static>(

#[cfg(windows)]
let is_window_transparent = window_builder.inner.window.transparent;
#[cfg(windows)]
let is_window_fullscreen = window_builder.inner.window.fullscreen.is_some();

#[cfg(target_os = "macos")]
{
Expand Down Expand Up @@ -3727,8 +3723,6 @@ fn create_window<T: UserEvent, F: Fn(RawWindow) + Send + 'static>(
webviews,
window_event_listeners,
#[cfg(windows)]
is_window_fullscreen,
#[cfg(windows)]
is_window_transparent,
#[cfg(windows)]
surface,
Expand Down Expand Up @@ -3818,11 +3812,6 @@ fn create_webview<T: UserEvent>(
.with_accept_first_mouse(webview_attributes.accept_first_mouse)
.with_hotkeys_zoom(webview_attributes.zoom_hotkeys_enabled);

#[cfg(windows)]
if kind == WebviewKind::WindowContent {
webview_builder = webview_builder.with_initialization_script(undecorated_resizing::SCRIPT);
}

if webview_attributes.drag_drop_handler_enabled {
let proxy = context.proxy.clone();
let window_id_ = window_id.clone();
Expand Down Expand Up @@ -4054,15 +4043,19 @@ fn create_webview<T: UserEvent>(
.build()
.map_err(|e| Error::CreateWebview(Box::new(e)))?;

#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
if kind == WebviewKind::WindowContent {
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
undecorated_resizing::attach_resize_handler(&webview);
#[cfg(windows)]
if window.is_resizable() && !window.is_decorated() {
undecorated_resizing::attach_resize_handler(window.hwnd());
}
}

#[cfg(windows)]
Expand Down Expand Up @@ -4127,13 +4120,6 @@ fn create_ipc_handler<T: UserEvent>(
ipc_handler: Option<WebviewIpcHandler<T, Wry<T>>>,
) -> Box<IpcHandler> {
Box::new(move |request| {
#[cfg(windows)]
if _kind == WebviewKind::WindowContent
&& undecorated_resizing::handle_request(context.clone(), *window_id.lock().unwrap(), &request)
{
return;
}

if let Some(handler) = &ipc_handler {
handler(
DetachedWebview {
Expand Down
Loading

0 comments on commit f29b788

Please sign in to comment.