Skip to content

Commit

Permalink
refactor: optimize undecorated resizing handling (#8790)
Browse files Browse the repository at this point in the history
* refactor: optimize undecorated resizing handling

* license

* separate modules

* fix windows

* rename export

* fix export

* same for gtk

* shared hit_test function

* change cursor on drag

* only set cursor on windows

* Revert "only set cursor on windows"

This reverts commit 03294a8.

* fix flickering on Windows, change cursor on motion on Linux

* remove changing cursor on move on linux

* fix linux implementation

* clippy

* Windows, on left click only

* prevent on fullscreen, use OS border size on Windows

* fix build

* clippy

---------

Co-authored-by: Lucas Nogueira <[email protected]>
  • Loading branch information
amrbashir and lucasfernog authored Feb 18, 2024
1 parent dbd525c commit 77f49ad
Show file tree
Hide file tree
Showing 9 changed files with 517 additions and 294 deletions.
97 changes: 76 additions & 21 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ use std::{
pub type WebviewId = u32;
type IpcHandler = dyn Fn(String) + 'static;

#[cfg(any(
windows,
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
mod undecorated_resizing;

mod webview;
pub use webview::Webview;

Expand Down Expand Up @@ -1858,6 +1868,9 @@ pub struct WindowWrapper {
has_children: AtomicBool,
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 All @@ -1868,7 +1881,6 @@ impl fmt::Debug for WindowWrapper {
f.debug_struct("WindowWrapper")
.field("label", &self.label)
.field("inner", &self.inner)
.field("is_window_transparent", &self.is_window_transparent)
.finish()
}
}
Expand Down Expand Up @@ -2603,6 +2615,10 @@ fn handle_user_message<T: UserEvent>(
} else {
window.set_fullscreen(None)
}
#[cfg(windows)]
if let Some(w) = windows.borrow_mut().get_mut(&id) {
w.is_window_fullscreen = fullscreen;
}
}
WindowMessage::SetFocus => {
window.set_focus();
Expand Down Expand Up @@ -2838,7 +2854,12 @@ 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;

if let Ok(window) = builder.build(event_loop) {
window_id_map.insert(window.id(), window_id);

Expand Down Expand Up @@ -2868,6 +2889,9 @@ fn handle_user_message<T: UserEvent>(
inner: Some(window.clone()),
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 @@ -3192,7 +3216,10 @@ fn create_window<T: UserEvent, F: Fn(RawWindow) + Send + 'static>(

let window_event_listeners = WindowEventListeners::default();

#[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 @@ -3324,6 +3351,9 @@ fn create_window<T: UserEvent, F: Fn(RawWindow) + Send + 'static>(
inner: Some(window),
webviews,
window_event_listeners,
#[cfg(windows)]
is_window_fullscreen,
#[cfg(windows)]
is_window_transparent,
#[cfg(windows)]
surface,
Expand Down Expand Up @@ -3413,6 +3443,11 @@ fn create_webview<T: UserEvent>(
.with_transparent(webview_attributes.transparent)
.with_accept_first_mouse(webview_attributes.accept_first_mouse);

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

if webview_attributes.file_drop_handler_enabled {
let proxy = context.proxy.clone();
webview_builder = webview_builder.with_file_drop_handler(move |event| {
Expand Down Expand Up @@ -3541,15 +3576,14 @@ fn create_webview<T: UserEvent>(
webview_builder = webview_builder.with_https_scheme(false);
}

if let Some(handler) = ipc_handler {
webview_builder = webview_builder.with_ipc_handler(create_ipc_handler(
window_id,
id,
context.clone(),
label.clone(),
handler,
));
}
webview_builder = webview_builder.with_ipc_handler(create_ipc_handler(
kind,
window_id,
id,
context.clone(),
label.clone(),
ipc_handler,
));

for (scheme, protocol) in uri_scheme_protocols {
webview_builder =
Expand Down Expand Up @@ -3626,6 +3660,17 @@ 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 {
undecorated_resizing::attach_resize_handler(&webview);
}

#[cfg(windows)]
if kind == WebviewKind::WindowContent {
let controller = webview.controller();
Expand Down Expand Up @@ -3679,24 +3724,34 @@ fn create_webview<T: UserEvent>(

/// Create a wry ipc handler from a tauri ipc handler.
fn create_ipc_handler<T: UserEvent>(
_kind: WebviewKind,
window_id: WindowId,
webview_id: WebviewId,
context: Context<T>,
label: String,
handler: WebviewIpcHandler<T, Wry<T>>,
ipc_handler: Option<WebviewIpcHandler<T, Wry<T>>>,
) -> Box<IpcHandler> {
Box::new(move |request| {
handler(
DetachedWebview {
label: label.clone(),
dispatcher: WryWebviewDispatcher {
window_id,
webview_id,
context: context.clone(),
#[cfg(windows)]
if _kind == WebviewKind::WindowContent
&& undecorated_resizing::handle_request(context.clone(), window_id, &request)
{
return;
}

if let Some(handler) = &ipc_handler {
handler(
DetachedWebview {
label: label.clone(),
dispatcher: WryWebviewDispatcher {
window_id,
webview_id,
context: context.clone(),
},
},
},
request,
);
request,
);
}
})
}

Expand Down
Loading

0 comments on commit 77f49ad

Please sign in to comment.