fix: eliminate white window flash on startup with hidden init and dark Win32 brush

This commit is contained in:
marwy 2026-11-27 14:36:18 +03:00
parent e9aa253f72
commit 5f4838a89d

View file

@ -135,7 +135,8 @@ fn build_window_attrs(init_w: f64, init_h: f64) -> WindowAttributes {
.with_inner_size(LogicalSize::new(init_w, init_h))
.with_min_inner_size(LogicalSize::new(768.0, 650.0))
.with_decorations(false) // Custom frameless 32px Yandex TitleBar!
.with_resizable(true);
.with_resizable(true)
.with_visible(false); // Stay hidden until DWM + first pure-black/splash frame is presented!
#[cfg(target_os = "linux")]
{
use winit::platform::wayland::WindowAttributesExtWayland;
@ -628,6 +629,17 @@ impl ApplicationHandler for AppRunner {
self.window_focused = true;
self.window_occluded = false;
self.last_frame_hash = None;
// Present the first theme-matched + splash frame into the swapchain BEFORE making the
// window visible so there is zero white OS/DWM flash on startup!
// (On Linux/Wayland where an unmapped window may report 0x0, skip pre-render and reveal.)
let init_size = window.inner_size();
if init_size.width > 0 && init_size.height > 0 {
self.render(event_loop);
self.last_frame_hash = None;
}
window.set_visible(true);
window.focus_window();
window.request_redraw();
}
@ -880,6 +892,76 @@ fn apply_win11_dwm_style(window: &Window, is_dark: bool) {
&round as *const _ as *const _,
std::mem::size_of::<u32>() as u32,
);
// DWMWA_CAPTION_COLOR (35): pure black in dark mode, pure white in light mode
const DWMWA_CAPTION_COLOR: u32 = 35;
let caption_color: u32 = if is_dark { 0x0000_0000 } else { 0x00FF_FFFF };
DwmSetWindowAttribute(
hwnd,
DWMWA_CAPTION_COLOR,
&caption_color as *const _ as *const _,
std::mem::size_of::<u32>() as u32,
);
// Replace Win32 class background brush (BLACK_BRUSH=4 / WHITE_BRUSH=0) & prime
// the client backing surface so Win32 never flashes white before or during resize.
#[repr(C)]
struct WinRect {
left: i32,
top: i32,
right: i32,
bottom: i32,
}
#[link(name = "gdi32")]
extern "system" {
fn GetStockObject(i: i32) -> *mut std::ffi::c_void;
}
#[link(name = "user32")]
extern "system" {
#[cfg(target_pointer_width = "64")]
fn SetClassLongPtrW(
hwnd: *mut std::ffi::c_void,
index: i32,
new_long: isize,
) -> isize;
#[cfg(target_pointer_width = "32")]
fn SetClassLongW(
hwnd: *mut std::ffi::c_void,
index: i32,
new_long: i32,
) -> i32;
fn GetClientRect(hwnd: *mut std::ffi::c_void, rect: *mut WinRect) -> i32;
fn GetDC(hwnd: *mut std::ffi::c_void) -> *mut std::ffi::c_void;
fn FillRect(
hdc: *mut std::ffi::c_void,
rect: *const WinRect,
hbr: *mut std::ffi::c_void,
) -> i32;
fn ReleaseDC(hwnd: *mut std::ffi::c_void, hdc: *mut std::ffi::c_void) -> i32;
}
const WHITE_BRUSH: i32 = 0;
const BLACK_BRUSH: i32 = 4;
const GCLP_HBRBACKGROUND: i32 = -10;
let brush = GetStockObject(if is_dark { BLACK_BRUSH } else { WHITE_BRUSH });
if !brush.is_null() {
#[cfg(target_pointer_width = "64")]
SetClassLongPtrW(hwnd, GCLP_HBRBACKGROUND, brush as isize);
#[cfg(target_pointer_width = "32")]
SetClassLongW(hwnd, GCLP_HBRBACKGROUND, brush as i32);
let mut rc = WinRect {
left: 0,
top: 0,
right: 0,
bottom: 0,
};
if GetClientRect(hwnd, &mut rc) != 0 {
let hdc = GetDC(hwnd);
if !hdc.is_null() {
FillRect(hdc, &rc, brush);
ReleaseDC(hwnd, hdc);
}
}
}
}
}
}