|
| 1 | +// Run with scripts/test-windows-window-material.mjs. This creates no product |
| 2 | +// state and never shows the test windows. Hit testing is performed by the real |
| 3 | +// production Tao/Tauri window procedures, not a fixture copy of their algorithm. |
| 4 | +#[cfg(target_os = "windows")] |
| 5 | +mod native { |
| 6 | + use std::error::Error; |
| 7 | + use std::mem::size_of; |
| 8 | + use std::sync::mpsc; |
| 9 | + use std::time::Duration; |
| 10 | + |
| 11 | + use tauri::{WebviewUrl, WebviewWindow, WebviewWindowBuilder}; |
| 12 | + use webview2_com::Microsoft::Web::WebView2::Win32::{ |
| 13 | + ICoreWebView2Controller2, COREWEBVIEW2_COLOR, |
| 14 | + }; |
| 15 | + use windows::core::Interface; |
| 16 | + use windows::Win32::Foundation::{HWND, LPARAM, LRESULT, POINT, RECT, WPARAM}; |
| 17 | + use windows::Win32::Graphics::Dwm::{DwmGetWindowAttribute, DWMWA_SYSTEMBACKDROP_TYPE}; |
| 18 | + use windows::Win32::Graphics::Gdi::ClientToScreen; |
| 19 | + use windows::Win32::UI::Shell::{DefSubclassProc, RemoveWindowSubclass, SetWindowSubclass}; |
| 20 | + use windows::Win32::UI::WindowsAndMessaging::{ |
| 21 | + GetClientRect, GetWindowLongPtrW, GetWindowRect, IsWindowVisible, SendMessageW, |
| 22 | + SetWindowPos, GWL_STYLE, HTBOTTOM, HTBOTTOMLEFT, HTBOTTOMRIGHT, HTLEFT, HTRIGHT, HTTOP, |
| 23 | + HTTOPLEFT, HTTOPRIGHT, SWP_FRAMECHANGED, SWP_NOACTIVATE, SWP_NOMOVE, SWP_NOSIZE, |
| 24 | + SWP_NOZORDER, WM_NCCALCSIZE, WM_NCHITTEST, WS_SIZEBOX, |
| 25 | + }; |
| 26 | + |
| 27 | + type Result<T = ()> = std::result::Result<T, Box<dyn Error>>; |
| 28 | + |
| 29 | + fn window_rect(hwnd: HWND) -> Result<RECT> { |
| 30 | + let mut rect = RECT::default(); |
| 31 | + unsafe { GetWindowRect(hwnd, &mut rect)? }; |
| 32 | + Ok(rect) |
| 33 | + } |
| 34 | + |
| 35 | + fn verify_frame(hwnd: HWND) -> Result { |
| 36 | + let outer = window_rect(hwnd)?; |
| 37 | + let mut client = RECT::default(); |
| 38 | + unsafe { GetClientRect(hwnd, &mut client)? }; |
| 39 | + let mut origin = POINT::default(); |
| 40 | + assert!(unsafe { ClientToScreen(hwnd, &mut origin) }.as_bool()); |
| 41 | + let insets = [ |
| 42 | + origin.x - outer.left, |
| 43 | + origin.y - outer.top, |
| 44 | + outer.right - origin.x - client.right, |
| 45 | + outer.bottom - origin.y - client.bottom, |
| 46 | + ]; |
| 47 | + assert!( |
| 48 | + insets[0] > 0 && insets[2] > 0 && insets[3] > 0, |
| 49 | + "Native side/bottom frame must remain outside the client area: {insets:?}" |
| 50 | + ); |
| 51 | + assert_ne!( |
| 52 | + unsafe { GetWindowLongPtrW(hwnd, GWL_STYLE) } as u32 & WS_SIZEBOX.0, |
| 53 | + 0, |
| 54 | + "Native window must retain its resizable frame style" |
| 55 | + ); |
| 56 | + println!("PASS native resize frame insets: {insets:?}"); |
| 57 | + Ok(()) |
| 58 | + } |
| 59 | + |
| 60 | + fn verify_hit_testing(hwnd: HWND) -> Result { |
| 61 | + let rect = window_rect(hwnd)?; |
| 62 | + let middle_x = rect.left + (rect.right - rect.left) / 2; |
| 63 | + let middle_y = rect.top + (rect.bottom - rect.top) / 2; |
| 64 | + let left = rect.left + 1; |
| 65 | + let right = rect.right - 2; |
| 66 | + let top = rect.top + 1; |
| 67 | + let bottom = rect.bottom - 2; |
| 68 | + for (name, x, y, expected) in [ |
| 69 | + ("left", left, middle_y, HTLEFT), |
| 70 | + ("right", right, middle_y, HTRIGHT), |
| 71 | + ("top", middle_x, top, HTTOP), |
| 72 | + ("bottom", middle_x, bottom, HTBOTTOM), |
| 73 | + ("top-left", left, top, HTTOPLEFT), |
| 74 | + ("top-right", right, top, HTTOPRIGHT), |
| 75 | + ("bottom-left", left, bottom, HTBOTTOMLEFT), |
| 76 | + ("bottom-right", right, bottom, HTBOTTOMRIGHT), |
| 77 | + ] { |
| 78 | + // WM_NCHITTEST packs signed screen coordinates into two 16-bit words. |
| 79 | + let coordinates = LPARAM(((x as u16 as u32) | ((y as u16 as u32) << 16)) as isize); |
| 80 | + let actual = unsafe { SendMessageW(hwnd, WM_NCHITTEST, None, Some(coordinates)) }.0; |
| 81 | + assert_eq!(actual, expected as isize, "Native resize hit test: {name}"); |
| 82 | + } |
| 83 | + println!("PASS native hit testing on all four edges and all four corners"); |
| 84 | + Ok(()) |
| 85 | + } |
| 86 | + |
| 87 | + fn verify_size_change(hwnd: HWND) -> Result { |
| 88 | + let original = window_rect(hwnd)?; |
| 89 | + let width = original.right - original.left; |
| 90 | + let height = original.bottom - original.top; |
| 91 | + unsafe { |
| 92 | + SetWindowPos( |
| 93 | + hwnd, |
| 94 | + None, |
| 95 | + 0, |
| 96 | + 0, |
| 97 | + width + 73, |
| 98 | + height + 41, |
| 99 | + SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE, |
| 100 | + )?; |
| 101 | + } |
| 102 | + let resized = window_rect(hwnd)?; |
| 103 | + assert_eq!(resized.right - resized.left, width + 73); |
| 104 | + assert_eq!(resized.bottom - resized.top, height + 41); |
| 105 | + verify_frame(hwnd)?; |
| 106 | + verify_hit_testing(hwnd)?; |
| 107 | + unsafe { |
| 108 | + SetWindowPos( |
| 109 | + hwnd, |
| 110 | + None, |
| 111 | + 0, |
| 112 | + 0, |
| 113 | + width, |
| 114 | + height, |
| 115 | + SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE, |
| 116 | + )?; |
| 117 | + } |
| 118 | + let restored = window_rect(hwnd)?; |
| 119 | + assert_eq!(restored.right - restored.left, width); |
| 120 | + assert_eq!(restored.bottom - restored.top, height); |
| 121 | + assert!(!unsafe { IsWindowVisible(hwnd) }.as_bool()); |
| 122 | + println!("PASS physical native resize and size restoration while hidden"); |
| 123 | + Ok(()) |
| 124 | + } |
| 125 | + |
| 126 | + fn verify_backdrop(hwnd: HWND, native_material: bool) -> Result { |
| 127 | + let mut backdrop: i32 = -1; |
| 128 | + let result = unsafe { |
| 129 | + DwmGetWindowAttribute( |
| 130 | + hwnd, |
| 131 | + DWMWA_SYSTEMBACKDROP_TYPE, |
| 132 | + &mut backdrop as *mut _ as _, |
| 133 | + size_of::<i32>() as u32, |
| 134 | + ) |
| 135 | + }; |
| 136 | + match result { |
| 137 | + Ok(()) if native_material => { |
| 138 | + assert_eq!(backdrop, 3, "Native Acrylic uses DWMSBT_TRANSIENTWINDOW"); |
| 139 | + println!("PASS compositor-owned Acrylic backdrop"); |
| 140 | + } |
| 141 | + Ok(()) => { |
| 142 | + assert!( |
| 143 | + backdrop == 0 || backdrop == 1, |
| 144 | + "Opaque fallback must not request a system material: {backdrop}" |
| 145 | + ); |
| 146 | + println!("PASS opaque fallback has no explicit system backdrop"); |
| 147 | + } |
| 148 | + Err(_) if !crate::window_material::native_sidebar_material_available() => { |
| 149 | + println!("PASS fallback on Windows without system backdrop support"); |
| 150 | + } |
| 151 | + Err(error) => return Err(error.into()), |
| 152 | + } |
| 153 | + Ok(()) |
| 154 | + } |
| 155 | + |
| 156 | + fn verify_webview_background(window: &WebviewWindow, native_material: bool) -> Result { |
| 157 | + let (sender, receiver) = mpsc::channel(); |
| 158 | + window.with_webview(move |webview| { |
| 159 | + let color = (|| -> windows::core::Result<_> { |
| 160 | + let controller: ICoreWebView2Controller2 = webview.controller().cast()?; |
| 161 | + let mut color = COREWEBVIEW2_COLOR::default(); |
| 162 | + unsafe { controller.DefaultBackgroundColor(&mut color)? }; |
| 163 | + Ok((color.A, color.R, color.G, color.B)) |
| 164 | + })(); |
| 165 | + let _ = sender.send(color); |
| 166 | + })?; |
| 167 | + let actual = receiver.recv_timeout(Duration::from_secs(5))??; |
| 168 | + let expected = if native_material { |
| 169 | + (0, 0, 0, 0) |
| 170 | + } else { |
| 171 | + (255, 24, 26, 28) |
| 172 | + }; |
| 173 | + assert_eq!( |
| 174 | + actual, expected, |
| 175 | + "Production WebView background opacity and color" |
| 176 | + ); |
| 177 | + println!("PASS actual WebView2 background: {actual:?}"); |
| 178 | + Ok(()) |
| 179 | + } |
| 180 | + |
| 181 | + // Negative control reproduces PR #3217's frame collapse, demonstrating that |
| 182 | + // the actual HWND assertion detects the regression. It is removed immediately. |
| 183 | + unsafe extern "system" fn collapse_frame( |
| 184 | + hwnd: HWND, |
| 185 | + message: u32, |
| 186 | + wparam: WPARAM, |
| 187 | + lparam: LPARAM, |
| 188 | + _subclass_id: usize, |
| 189 | + _data: usize, |
| 190 | + ) -> LRESULT { |
| 191 | + if message == WM_NCCALCSIZE && wparam.0 != 0 { |
| 192 | + LRESULT(0) |
| 193 | + } else { |
| 194 | + unsafe { DefSubclassProc(hwnd, message, wparam, lparam) } |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + fn verify_regression_control(hwnd: HWND) -> Result { |
| 199 | + const SUBCLASS_ID: usize = 0x4f42_4654; |
| 200 | + unsafe { |
| 201 | + assert!(SetWindowSubclass(hwnd, Some(collapse_frame), SUBCLASS_ID, 0).as_bool()); |
| 202 | + SetWindowPos( |
| 203 | + hwnd, |
| 204 | + None, |
| 205 | + 0, |
| 206 | + 0, |
| 207 | + 0, |
| 208 | + 0, |
| 209 | + SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE, |
| 210 | + )?; |
| 211 | + } |
| 212 | + let outer = window_rect(hwnd)?; |
| 213 | + let mut client = RECT::default(); |
| 214 | + unsafe { GetClientRect(hwnd, &mut client)? }; |
| 215 | + assert_eq!( |
| 216 | + client.right, |
| 217 | + outer.right - outer.left, |
| 218 | + "Negative control must expose the collapsed horizontal frame" |
| 219 | + ); |
| 220 | + assert_eq!( |
| 221 | + client.bottom, |
| 222 | + outer.bottom - outer.top, |
| 223 | + "Negative control must expose the collapsed vertical frame" |
| 224 | + ); |
| 225 | + unsafe { |
| 226 | + assert!(RemoveWindowSubclass(hwnd, Some(collapse_frame), SUBCLASS_ID).as_bool()); |
| 227 | + SetWindowPos( |
| 228 | + hwnd, |
| 229 | + None, |
| 230 | + 0, |
| 231 | + 0, |
| 232 | + 0, |
| 233 | + 0, |
| 234 | + SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE, |
| 235 | + )?; |
| 236 | + } |
| 237 | + verify_frame(hwnd)?; |
| 238 | + verify_hit_testing(hwnd)?; |
| 239 | + println!("PASS regression control distinguishes collapsed and native frames"); |
| 240 | + Ok(()) |
| 241 | + } |
| 242 | + |
| 243 | + pub fn run() -> Result { |
| 244 | + let storage = tempfile::tempdir()?; |
| 245 | + let app = tauri::Builder::default().build(tauri::generate_context!())?; |
| 246 | + let native_available = crate::window_material::native_sidebar_material_available(); |
| 247 | + println!( |
| 248 | + "Windows build {}: native material available={native_available}", |
| 249 | + windows_version::OsVersion::current().build |
| 250 | + ); |
| 251 | + let mut windows: Vec<WebviewWindow> = Vec::new(); |
| 252 | + for native_material in [false, true] { |
| 253 | + if native_material && !native_available { |
| 254 | + println!("SKIP native material: this Windows build uses the opaque fallback"); |
| 255 | + continue; |
| 256 | + } |
| 257 | + let label = if native_material { |
| 258 | + "native-material" |
| 259 | + } else { |
| 260 | + "opaque-fallback" |
| 261 | + }; |
| 262 | + let builder = WebviewWindowBuilder::new( |
| 263 | + &app, |
| 264 | + label, |
| 265 | + WebviewUrl::External("about:blank".parse()?), |
| 266 | + ) |
| 267 | + .title("OpenBitFun hidden window material fixture") |
| 268 | + .inner_size(640.0, 480.0) |
| 269 | + .position(80.0, 80.0) |
| 270 | + .resizable(true) |
| 271 | + .visible(false) |
| 272 | + .skip_taskbar(true) |
| 273 | + .incognito(true) |
| 274 | + .data_directory(storage.path().join(label)); |
| 275 | + let window = crate::window_material::configure_main_window( |
| 276 | + builder, |
| 277 | + native_material, |
| 278 | + tauri::window::Color(24, 26, 28, 255), |
| 279 | + ) |
| 280 | + .build()?; |
| 281 | + let hwnd = window.hwnd()?; |
| 282 | + assert!(!unsafe { IsWindowVisible(hwnd) }.as_bool()); |
| 283 | + verify_frame(hwnd)?; |
| 284 | + verify_hit_testing(hwnd)?; |
| 285 | + verify_backdrop(hwnd, native_material)?; |
| 286 | + verify_webview_background(&window, native_material)?; |
| 287 | + verify_size_change(hwnd)?; |
| 288 | + verify_regression_control(hwnd)?; |
| 289 | + println!("PASS production main-window material mode: {label}"); |
| 290 | + windows.push(window); |
| 291 | + } |
| 292 | + for window in windows { |
| 293 | + window.destroy()?; |
| 294 | + } |
| 295 | + // Destroy is dispatched through the real event loop, even on the main |
| 296 | + // thread. Let all native Destroyed events release their WebViews before |
| 297 | + // the last-window exit completes and temporary storage is removed. |
| 298 | + assert_eq!(app.run_return(|_, _| {}), 0); |
| 299 | + println!("Native checks do not measure drag frame rate or rendered shadow appearance."); |
| 300 | + Ok(()) |
| 301 | + } |
| 302 | +} |
| 303 | + |
| 304 | +#[cfg(target_os = "windows")] |
| 305 | +fn main() -> std::result::Result<(), Box<dyn std::error::Error>> { |
| 306 | + native::run() |
| 307 | +} |
| 308 | + |
| 309 | +#[cfg(not(target_os = "windows"))] |
| 310 | +fn main() { |
| 311 | + panic!("This native fixture must be built for and executed on Windows"); |
| 312 | +} |
0 commit comments