From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Cezary Kulakowski <cezary@openfin.co>
Date: Mon, 22 Jul 2024 16:23:13 +0200
Subject: fix: adjust headless mode handling in native widget

We need this method as we create window in headless mode and we
switch it back to normal mode only after inital paint is done in
order to get some events like WebContents.beginFrameSubscription.
If we don't set `is_headless_` to false then some child windows
e.g. autofill popups will be created in headless mode leading to
ui problems (like dissapearing popup during typing in html's
input list).

We also need to ensure that an initial paint is scheduled when
the compositor is unsuspended in headles mode.

diff --git a/ui/views/cocoa/native_widget_mac_ns_window_host.mm b/ui/views/cocoa/native_widget_mac_ns_window_host.mm
index 48dfca966a594f54523c129fa49d2a561cb41dec..049cf4f1adcf4f64cd8692f6e6eda7418008e799 100644
--- a/ui/views/cocoa/native_widget_mac_ns_window_host.mm
+++ b/ui/views/cocoa/native_widget_mac_ns_window_host.mm
@@ -664,9 +664,10 @@ void HandleAccelerator(const ui::Accelerator& accelerator,
   // case it will never become visible but we want its compositor to produce
   // frames for screenshooting and screencasting.
   UpdateCompositorProperties();
-  layer()->SetVisible(is_visible_);
+  layer()->SetVisible(is_visible_ || is_headless_mode_window_);
   if (is_visible_ || is_headless_mode_window_) {
     compositor_->Unsuspend();
+    layer()->SchedulePaint(layer()->bounds());
   }
 
   // Register the CGWindowID (used to identify this window for video capture)
diff --git a/ui/views/widget/widget.h b/ui/views/widget/widget.h
index 2306354eaf3441c875da54e82103b6c57bce02f6..75575ab4998f676b4dd4f23a368c7661a32906f7 100644
--- a/ui/views/widget/widget.h
+++ b/ui/views/widget/widget.h
@@ -1284,6 +1284,8 @@ class VIEWS_EXPORT Widget : public internal::NativeWidgetDelegate,
   // True if widget was created in headless mode.
   bool is_headless() const { return is_headless_; }
 
+  void DisableHeadlessMode() { is_headless_ = false; }
+
   // True if the window size will follow the content preferred size.
   bool is_autosized() const { return is_autosized_; }
 
