diff --git a/shell/browser/ui/win/electron_desktop_window_tree_host_win.cc b/shell/browser/ui/win/electron_desktop_window_tree_host_win.cc index 82b7628dba27..485c04bb29d1 100644 --- a/shell/browser/ui/win/electron_desktop_window_tree_host_win.cc +++ b/shell/browser/ui/win/electron_desktop_window_tree_host_win.cc @@ -143,11 +143,33 @@ bool ElectronDesktopWindowTreeHostWin::HandleMouseEvent(ui::MouseEvent* event) { void ElectronDesktopWindowTreeHostWin::HandleVisibilityChanged(bool visible) { if (native_window_view_->widget()) native_window_view_->widget()->OnNativeWidgetVisibilityChanged(visible); + + if (visible) + UpdateAllowScreenshots(); } void ElectronDesktopWindowTreeHostWin::SetAllowScreenshots(bool allow) { - ::SetWindowDisplayAffinity(GetAcceleratedWidget(), - allow ? WDA_NONE : WDA_EXCLUDEFROMCAPTURE); + if (allow_screenshots_ == allow) + return; + + allow_screenshots_ = allow; + + // If the window is not visible, do not set the window display affinity + // because `SetWindowDisplayAffinity` will attempt to compose the window, + if (!IsVisible()) + return; + + UpdateAllowScreenshots(); +} + +void ElectronDesktopWindowTreeHostWin::UpdateAllowScreenshots() { + bool allowed = views::DesktopWindowTreeHostWin::AreScreenshotsAllowed(); + if (allowed == allow_screenshots_) + return; + + ::SetWindowDisplayAffinity( + GetAcceleratedWidget(), + allow_screenshots_ ? WDA_NONE : WDA_EXCLUDEFROMCAPTURE); } void ElectronDesktopWindowTreeHostWin::OnNativeThemeUpdated( diff --git a/shell/browser/ui/win/electron_desktop_window_tree_host_win.h b/shell/browser/ui/win/electron_desktop_window_tree_host_win.h index e151b1384e6e..366dd0827052 100644 --- a/shell/browser/ui/win/electron_desktop_window_tree_host_win.h +++ b/shell/browser/ui/win/electron_desktop_window_tree_host_win.h @@ -52,8 +52,11 @@ class ElectronDesktopWindowTreeHostWin : public views::DesktopWindowTreeHostWin, bool ShouldWindowContentsBeTransparent() const override; private: + void UpdateAllowScreenshots(); + raw_ptr native_window_view_; // weak ref std::optional force_should_paint_as_active_; + bool allow_screenshots_ = true; bool widget_init_done_ = false; };