From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: zoy <zoy-l@outlook.com>
Date: Mon, 5 May 2025 23:28:53 +0800
Subject: fix: resolve dynamic background material update issue on Windows 11

This patch addresses issues with background materials on Windows 11,
such as the background turning black when maximizing the window and
dynamic background material settings not taking effect.

diff --git a/ui/views/widget/desktop_aura/desktop_window_tree_host_win.cc b/ui/views/widget/desktop_aura/desktop_window_tree_host_win.cc
index 2bf4889631ea93ab34bad6e69b301f1b09844840..d366e440dddde5627743e21ce0e184cd485bf76a 100644
--- a/ui/views/widget/desktop_aura/desktop_window_tree_host_win.cc
+++ b/ui/views/widget/desktop_aura/desktop_window_tree_host_win.cc
@@ -183,6 +183,10 @@ void DesktopWindowTreeHostWin::FinishTouchDrag(gfx::Point screen_point) {
   }
 }
 
+void DesktopWindowTreeHostWin::SetIsTranslucent(bool is_translucent) {
+  message_handler_->set_is_translucent(is_translucent);
+}
+
 // DesktopWindowTreeHostWin, DesktopWindowTreeHost implementation:
 
 void DesktopWindowTreeHostWin::Init(const Widget::InitParams& params) {
diff --git a/ui/views/widget/desktop_aura/desktop_window_tree_host_win.h b/ui/views/widget/desktop_aura/desktop_window_tree_host_win.h
index 0cd07fd5fb55dcc0d972de4c027fcb895d156592..0f4d335e1d54b5e92fc217080d86513db94d4122 100644
--- a/ui/views/widget/desktop_aura/desktop_window_tree_host_win.h
+++ b/ui/views/widget/desktop_aura/desktop_window_tree_host_win.h
@@ -93,6 +93,8 @@ class VIEWS_EXPORT DesktopWindowTreeHostWin
   // false.
   void FinishTouchDrag(gfx::Point screen_point);
 
+  void SetIsTranslucent(bool is_translucent);
+
  protected:
   // Overridden from DesktopWindowTreeHost:
   void Init(const Widget::InitParams& params) override;
diff --git a/ui/views/win/hwnd_message_handler.cc b/ui/views/win/hwnd_message_handler.cc
index bbd092d2a90765c52fd5792ee37a909d028634fe..fcd2adc9b2eff4b75b1e381351b78f11dca63d30 100644
--- a/ui/views/win/hwnd_message_handler.cc
+++ b/ui/views/win/hwnd_message_handler.cc
@@ -986,13 +986,13 @@ void HWNDMessageHandler::FrameTypeChanged() {
 
 void HWNDMessageHandler::PaintAsActiveChanged() {
   if (!delegate_->HasNonClientView() || !delegate_->CanActivate() ||
-      !delegate_->HasFrame() ||
+      (!delegate_->HasFrame() && !is_translucent_) ||
       (delegate_->GetFrameMode() == FrameMode::CUSTOM_DRAWN)) {
     return;
   }
 
   DefWindowProcWithRedrawLock(WM_NCACTIVATE, delegate_->ShouldPaintAsActive(),
-                              0);
+                              delegate_->HasFrame() ? 0 : -1);
 }
 
 void HWNDMessageHandler::SetWindowIcons(const gfx::ImageSkia& window_icon,
@@ -1077,7 +1077,14 @@ void HWNDMessageHandler::SizeConstraintsChanged() {
   //   allowing ui::GetResizableFrameThickness() to be used consistently when
   //   removing the visible system frame.
   const bool had_caption_on_init = window_style() & WS_CAPTION;
-  const bool can_resize = !is_translucent_ && delegate_->CanResize();
+
+  // In Chromium, the !is_translucent_ check was introduced for Glic-specific
+  // behavior. Since Electron does not use Glic, this restriction can be safely
+  // removed. Keeping the is_translucent_ check disables maximization for
+  // translucent framed windows. Original code: !is_translucent_ &&
+  // delegate_->CanResize() See:
+  // https://chromium-review.googlesource.com/c/chromium/src/+/6372329
+  const bool can_resize = delegate_->CanResize();
   const bool can_maximize = can_resize && delegate_->CanMaximize();
 
   auto set_style_func = [&style](LONG bit, bool should_set) {
@@ -1678,11 +1685,16 @@ void HWNDMessageHandler::ResetWindowRegion(bool force, bool redraw) {
   // through, but that isn't the case when using Direct3D to draw transparent
   // windows. So we route translucent windows throught to the delegate to
   // allow for a custom hit mask.
-  if (!is_translucent_ && !custom_window_region_.is_valid() &&
+  // patch: fix_resolve_dynamic_background_material_update_issue_on_windows_11
+  // Our translucent windows use the native frame by default, and we should not
+  // set a custom region when the window is maximized; otherwise, it will cause
+  // a white title bar to appear under Windows 11.
+  if (!custom_window_region_.is_valid() &&
       (IsFrameSystemDrawn() || !delegate_->HasNonClientView())) {
     if (force) {
       SetWindowRgn(hwnd(), nullptr, redraw);
     }
+
     return;
   }
 
@@ -2435,17 +2447,18 @@ LRESULT HWNDMessageHandler::OnNCActivate(UINT message,
     delegate_->SchedulePaint();
   }
 
-  // Calling DefWindowProc is only necessary if there's a system frame being
-  // drawn. Otherwise it can draw an incorrect title bar and cause visual
-  // corruption.
-  if (!delegate_->HasFrame() ||
+  // If the window is translucent, it may have the Mica background.
+  // In that case, it's necessary to call |DefWindowProc|, but we can
+  // pass -1 in the lParam to prevent any non-client area elements from
+  // being displayed.
+  if ((!delegate_->HasFrame() && !is_translucent_) ||
       delegate_->GetFrameMode() == FrameMode::CUSTOM_DRAWN) {
     SetMsgHandled(TRUE);
     return TRUE;
   }
 
   return DefWindowProcWithRedrawLock(WM_NCACTIVATE, paint_as_active || active,
-                                     0);
+                                     delegate_->HasFrame() ? 0 : -1);
 }
 
 LRESULT HWNDMessageHandler::OnNCCalcSize(BOOL mode, LPARAM l_param) {
