Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix white flash on minimize/maximize on Windows #24941

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions browser/new_tab/background_color_tab_helper_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,30 @@
#include "base/test/scoped_feature_list.h"
#include "brave/browser/themes/brave_dark_mode_utils.h"
#include "brave/browser/ui/brave_ui_features.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/color/color_provider.h"
#include "ui/compositor/compositor.h"
#include "ui/gfx/native_widget_types.h"

class BackgroundColorTabHelperBrowserTest
: public InProcessBrowserTest,
public testing::WithParamInterface<bool> {
public:
BackgroundColorTabHelperBrowserTest() {
dark_mode::SetUseSystemDarkModeEnabledForTest(false);
scoped_feature_list_.InitAndEnableFeature(
features::kBraveWorkaroundNewWindowFlash);
}

void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
dark_mode::SetBraveDarkModeType(
IsDarkMode()
? dark_mode::BraveDarkModeType::BRAVE_DARK_MODE_TYPE_DARK
: dark_mode::BraveDarkModeType::BRAVE_DARK_MODE_TYPE_LIGHT);
}

content::WebContents* web_contents() {
return chrome_test_utils::GetActiveWebContents(this);
}
Expand All @@ -42,21 +40,43 @@ class BackgroundColorTabHelperBrowserTest
base::test::ScopedFeatureList scoped_feature_list_;
};

IN_PROC_BROWSER_TEST_P(BackgroundColorTabHelperBrowserTest,
PRE_BackgroundColorIsSet) {
dark_mode::SetBraveDarkModeType(
IsDarkMode() ? dark_mode::BraveDarkModeType::BRAVE_DARK_MODE_TYPE_DARK
: dark_mode::BraveDarkModeType::BRAVE_DARK_MODE_TYPE_LIGHT);
}

IN_PROC_BROWSER_TEST_P(BackgroundColorTabHelperBrowserTest,
BackgroundColorIsSet) {
ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUINewTabPageURL),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);

const auto view_background_color =
web_contents()->GetPrimaryMainFrame()->GetView()->GetBackgroundColor();
// Expected colors:
const auto expected_tab_background_color =
web_contents()->GetColorProvider().GetColor(kColorNewTabPageBackground);

if (IsDarkMode()) {
EXPECT_NE(view_background_color, SK_ColorWHITE);
} else {
EXPECT_EQ(view_background_color, SK_ColorWHITE);
}
const auto expected_view_host_background_color =
web_contents()->GetColorProvider().GetColor(kColorToolbar);

// Actual colors:
const auto tab_background_color =
web_contents()->GetTopLevelRenderWidgetHostView()->GetBackgroundColor();

const auto view_host_background_color =
BrowserView::GetBrowserViewForBrowser(browser())
->GetWidget()
->GetNativeView()
->GetHost()
->compositor()
->host_for_testing()
->background_color()
.toSkColor();

EXPECT_EQ(tab_background_color, expected_tab_background_color);
EXPECT_EQ(view_host_background_color, expected_view_host_background_color);
}

INSTANTIATE_TEST_SUITE_P(,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ bool BrowserDesktopWindowTreeHostWin::PreHandleMSG(UINT message,
break;
}
case WM_NCPAINT: {
// If the window is cloacked, fill it with the ntp background color and
// uncloak.
// If the window is cloacked, fill it with the toolbar color and uncloak.
if (is_cloacked_ && base::FeatureList::IsEnabled(
features::kBraveWorkaroundNewWindowFlash)) {
HWND hwnd = GetHWND();
Expand All @@ -54,8 +53,7 @@ bool BrowserDesktopWindowTreeHostWin::PreHandleMSG(UINT message,
window_rect.bottom - window_rect.top,
};

SkColor bg_color = GetWidget()->GetColorProvider()->GetColor(
kColorNewTabPageBackground);
SkColor bg_color = GetToolbarColor();
HBRUSH brush = ::CreateSolidBrush(skia::SkColorToCOLORREF(bg_color));
::FillRect(dc, &fill_rect, brush);
::DeleteObject(brush);
Expand All @@ -71,6 +69,21 @@ bool BrowserDesktopWindowTreeHostWin::PreHandleMSG(UINT message,
message, w_param, l_param, result);
}

SkColor BrowserDesktopWindowTreeHostWin::GetBackgroundColor(
SkColor requested_color) const {
if (requested_color == SK_ColorTRANSPARENT ||
!base::FeatureList::IsEnabled(features::kBraveWorkaroundNewWindowFlash)) {
return requested_color;
}

return GetToolbarColor();
}

SkColor BrowserDesktopWindowTreeHostWin::GetToolbarColor() const {
CHECK(base::FeatureList::IsEnabled(features::kBraveWorkaroundNewWindowFlash));
return GetWidget()->GetColorProvider()->GetColor(kColorToolbar);
}

// static
BrowserDesktopWindowTreeHost*
BrowserDesktopWindowTreeHost::CreateBrowserDesktopWindowTreeHost(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class BrowserDesktopWindowTreeHostWin
LPARAM l_param,
LRESULT* result) override;

// Returns the optionally modified background color to correctly match the
// toolbar color in dark/private browsing modes.
SkColor GetBackgroundColor(SkColor requested_color) const override;

SkColor GetToolbarColor() const;

bool is_cloacked_ = false;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* Copyright (c) 2024 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"

#include "ui/compositor/compositor.h"

#define SetBackgroundColor(color) \
SetBackgroundColor(desktop_window_tree_host_->GetBackgroundColor(color))

#include "src/ui/views/widget/desktop_aura/desktop_native_widget_aura.cc"

#undef SetBackgroundColor
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Copyright (c) 2024 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "ui/views/widget/desktop_aura/desktop_window_tree_host.h"

#include "src/ui/views/widget/desktop_aura/desktop_window_tree_host.cc"

namespace views {

SkColor DesktopWindowTreeHost::GetBackgroundColor(
SkColor requested_color) const {
return requested_color;
}

} // namespace views
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Copyright (c) 2024 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_CHROMIUM_SRC_UI_VIEWS_WIDGET_DESKTOP_AURA_DESKTOP_WINDOW_TREE_HOST_H_
#define BRAVE_CHROMIUM_SRC_UI_VIEWS_WIDGET_DESKTOP_AURA_DESKTOP_WINDOW_TREE_HOST_H_

#define GetSingletonDesktopNativeCursorManager() \
GetSingletonDesktopNativeCursorManager(); \
virtual SkColor GetBackgroundColor(SkColor requested_color) const

#include "src/ui/views/widget/desktop_aura/desktop_window_tree_host.h" // IWYU pragma: export

#undef GetSingletonDesktopNativeCursorManager

#endif // BRAVE_CHROMIUM_SRC_UI_VIEWS_WIDGET_DESKTOP_AURA_DESKTOP_WINDOW_TREE_HOST_H_
Loading