From 78d15e892d30fb3f8494f0c00685fb455740a181 Mon Sep 17 00:00:00 2001 From: Tony <68118705+Legend-Master@users.noreply.github.com> Date: Tue, 3 Jun 2025 19:04:14 +0800 Subject: [PATCH] fix(windows): allow web fullscreen APIs to work (#13558) * fix(windows): allow web fullscreen APIs to work * tauri-runtime-wry not tauri-wry * Remove last clone * Change file on windows --- .changes/windows-fullscreen.md | 5 +++++ crates/tauri-runtime-wry/src/lib.rs | 33 +++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 .changes/windows-fullscreen.md diff --git a/.changes/windows-fullscreen.md b/.changes/windows-fullscreen.md new file mode 100644 index 000000000..47d0634a7 --- /dev/null +++ b/.changes/windows-fullscreen.md @@ -0,0 +1,5 @@ +--- +tauri-runtime-wry: "patch:bug" +--- + +Allow web fullscreen APIs to work on Windows (e.g. `video.requestFullscreen` and `document.exitFullscreen`) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index b1222de9a..7551c7d95 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -38,7 +38,7 @@ use tao::platform::unix::{WindowBuilderExtUnix, WindowExtUnix}; #[cfg(windows)] use tao::platform::windows::{WindowBuilderExtWindows, WindowExtWindows}; #[cfg(windows)] -use webview2_com::FocusChangedEventHandler; +use webview2_com::{ContainsFullScreenElementChangedEventHandler, FocusChangedEventHandler}; #[cfg(windows)] use windows::Win32::Foundation::HWND; #[cfg(target_os = "ios")] @@ -4821,8 +4821,7 @@ You may have it installed on another user account, but it is not available for t #[cfg(windows)] { let controller = webview.controller(); - let proxy = context.proxy.clone(); - let proxy_ = proxy.clone(); + let proxy_clone = context.proxy.clone(); let window_id_ = window_id.clone(); let mut token = 0; unsafe { @@ -4837,7 +4836,7 @@ You may have it installed on another user account, but it is not available for t focused_webview.replace(label_.clone()); if !already_focused { - let _ = proxy.send_event(Message::Webview( + let _ = proxy_clone.send_event(Message::Webview( *window_id_.lock().unwrap(), id, WebviewMessage::SynthesizedWindowEvent(SynthesizedWindowEvent::Focused(true)), @@ -4851,6 +4850,8 @@ You may have it installed on another user account, but it is not available for t .unwrap(); unsafe { let label_ = label.clone(); + let window_id_ = window_id.clone(); + let proxy_clone = context.proxy.clone(); controller.add_LostFocus( &FocusChangedEventHandler::create(Box::new(move |_, _| { let mut focused_webview = focused_webview.lock().unwrap(); @@ -4866,8 +4867,8 @@ You may have it installed on another user account, but it is not available for t if lost_window_focus { // only reset when we lost window focus - otherwise some other webview is focused *focused_webview = None; - let _ = proxy_.send_event(Message::Webview( - *window_id.lock().unwrap(), + let _ = proxy_clone.send_event(Message::Webview( + *window_id_.lock().unwrap(), id, WebviewMessage::SynthesizedWindowEvent(SynthesizedWindowEvent::Focused(false)), )); @@ -4878,6 +4879,26 @@ You may have it installed on another user account, but it is not available for t ) } .unwrap(); + + if let Ok(webview) = unsafe { controller.CoreWebView2() } { + let proxy_clone = context.proxy.clone(); + unsafe { + let _ = webview.add_ContainsFullScreenElementChanged( + &ContainsFullScreenElementChangedEventHandler::create(Box::new(move |sender, _| { + let mut contains_fullscreen_element = windows::core::BOOL::default(); + sender + .ok_or_else(windows::core::Error::empty)? + .ContainsFullScreenElement(&mut contains_fullscreen_element)?; + let _ = proxy_clone.send_event(Message::Window( + *window_id.lock().unwrap(), + WindowMessage::SetFullscreen(contains_fullscreen_element.as_bool()), + )); + Ok(()) + })), + &mut token, + ); + } + } } Ok(WebviewWrapper {