From 917148c5ce6b4801dee7ab191b6fb5144f84d0ea Mon Sep 17 00:00:00 2001 From: Barani S Date: Wed, 19 Nov 2025 04:50:32 +0530 Subject: [PATCH] gpui: Use DWM API for backdrop effects and add Mica/Mica Alt support (#41842) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR updates window background rendering to use the **official DWM backdrop API** (`DwmSetWindowAttribute`) instead of the legacy `SetWindowCompositionAttribute`. It also adds **Mica** and **Mica Alt** options to `WindowBackgroundAppearance` for native Windows 11 effects. ### Motivation Enables modern, stable, and GPU-accelerated backdrops consistent with Windows 11’s Fluent Design. Removes reliance on undocumented APIs while maintaining backward compatibility with older Windows versions. ### Changes * Added `MicaBackdrop` and `MicaAltBackdrop` variants. * Switched to DWM API for applying backdrop effects. * Verified fallback behavior on Windows 10. ### Release Notes: - Added `WindowBackgroundAppearance::MicaBackdrop` and `WindowBackgroundAppearance::MicaAltBackdrop` for Windows 11 Mica and Mica Alt window backdrops. ### Screenshots - `WindowBackgroundAppearance::Blurred` image - `WindowBackgroundAppearance::MicaBackdrop` image - `WindowBackgroundAppearance::MicaAltBackdrop` image --------- Co-authored-by: John Tur --- crates/gpui/src/platform.rs | 4 +++ crates/gpui/src/platform/windows/window.rs | 40 +++++++++++++++++++--- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rs index 7168d0179424028e7f823d39df0f6f51f45095ac..caedf0317f21b9bceb548b31543da2f33bfac254 100644 --- a/crates/gpui/src/platform.rs +++ b/crates/gpui/src/platform.rs @@ -1389,6 +1389,10 @@ pub enum WindowBackgroundAppearance { /// /// Not always supported. Blurred, + /// The Mica backdrop material, supported on Windows 11. + MicaBackdrop, + /// The Mica Alt backdrop material, supported on Windows 11. + MicaAltBackdrop, } /// The options that can be configured for a file dialog prompt diff --git a/crates/gpui/src/platform/windows/window.rs b/crates/gpui/src/platform/windows/window.rs index fe6e6ff664a6c8f9b9524501ca1e875b5023169e..334f0519f15a608a8b36b3610c88fb456a4a8f5b 100644 --- a/crates/gpui/src/platform/windows/window.rs +++ b/crates/gpui/src/platform/windows/window.rs @@ -18,6 +18,7 @@ use smallvec::SmallVec; use windows::{ Win32::{ Foundation::*, + Graphics::Dwm::*, Graphics::Gdi::*, System::{Com::*, LibraryLoader::*, Ole::*, SystemServices::*}, UI::{Controls::*, HiDpi::*, Input::KeyboardAndMouse::*, Shell::*, WindowsAndMessaging::*}, @@ -773,20 +774,26 @@ impl PlatformWindow for WindowsWindow { fn set_background_appearance(&self, background_appearance: WindowBackgroundAppearance) { let hwnd = self.0.hwnd; + // using Dwm APIs for Mica and MicaAlt backdrops. + // others follow the set_window_composition_attribute approach match background_appearance { WindowBackgroundAppearance::Opaque => { - // ACCENT_DISABLED set_window_composition_attribute(hwnd, None, 0); } WindowBackgroundAppearance::Transparent => { - // Use ACCENT_ENABLE_TRANSPARENTGRADIENT for transparent background set_window_composition_attribute(hwnd, None, 2); } WindowBackgroundAppearance::Blurred => { - // Enable acrylic blur - // ACCENT_ENABLE_ACRYLICBLURBEHIND set_window_composition_attribute(hwnd, Some((0, 0, 0, 0)), 4); } + WindowBackgroundAppearance::MicaBackdrop => { + // DWMSBT_MAINWINDOW => MicaBase + dwm_set_window_composition_attribute(hwnd, 2); + } + WindowBackgroundAppearance::MicaAltBackdrop => { + // DWMSBT_TABBEDWINDOW => MicaAlt + dwm_set_window_composition_attribute(hwnd, 4); + } } } @@ -1330,9 +1337,34 @@ fn retrieve_window_placement( Ok(placement) } +fn dwm_set_window_composition_attribute(hwnd: HWND, backdrop_type: u32) { + let mut version = unsafe { std::mem::zeroed() }; + let status = unsafe { windows::Wdk::System::SystemServices::RtlGetVersion(&mut version) }; + + // DWMWA_SYSTEMBACKDROP_TYPE is available only on version 22621 or later + // using SetWindowCompositionAttributeType as a fallback + if !status.is_ok() || version.dwBuildNumber < 22621 { + return; + } + + unsafe { + let result = DwmSetWindowAttribute( + hwnd, + DWMWA_SYSTEMBACKDROP_TYPE, + &backdrop_type as *const _ as *const _, + std::mem::size_of_val(&backdrop_type) as u32, + ); + + if !result.is_ok() { + return; + } + } +} + fn set_window_composition_attribute(hwnd: HWND, color: Option, state: u32) { let mut version = unsafe { std::mem::zeroed() }; let status = unsafe { windows::Wdk::System::SystemServices::RtlGetVersion(&mut version) }; + if !status.is_ok() || version.dwBuildNumber < 17763 { return; }