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

Reverting the kDisableWindows10CustomTitlebar switch removal #188

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion chrome/browser/themes/theme_helper_win.cc
Expand Up @@ -19,5 +19,6 @@ int ThemeHelperWin::GetDefaultDisplayProperty(int id) const {

bool ThemeHelperWin::ShouldUseNativeFrame(
const CustomThemeSupplier* theme_supplier) const {
return true;
return !ShouldAlwaysUseSystemTitlebar() ||
!HasCustomImage(IDR_THEME_FRAME, theme_supplier);
}
170 changes: 93 additions & 77 deletions chrome/browser/ui/color/win/native_chrome_color_mixer_win.cc
Expand Up @@ -8,6 +8,7 @@
#include "base/functional/bind.h"
#include "base/no_destructor.h"
#include "base/win/windows_version.h"
#include "chrome/browser/themes/browser_theme_pack.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/win/titlebar_config.h"
Expand All @@ -16,7 +17,7 @@
#include "ui/color/color_id.h"
#include "ui/color/color_mixer.h"
#include "ui/color/color_provider.h"
#include "ui/color/color_provider_key.h"
#include "ui/color/color_provider_manager.h"
#include "ui/color/color_provider_utils.h"
#include "ui/color/color_recipe.h"
#include "ui/color/color_transform.h"
Expand All @@ -38,15 +39,23 @@ class FrameColorHelper {
~FrameColorHelper() = default;

void AddNativeChromeColors(ui::ColorMixer& mixer,
const ui::ColorProviderKey& key) const;
const ui::ColorProviderManager::Key& key) const;
void AddBorderAccentColors(ui::ColorMixer& mixer) const;

static FrameColorHelper* Get();

private:
// Returns whether there is a custom image provided for the given id.
bool HasCustomImage(int id, const ui::ColorProviderManager::Key& key) const;

// Returns true if colors from DWM can be used, i.e. this is a native frame
// on Windows 8+.
bool DwmColorsAllowed(const ui::ColorProviderManager::Key& key) const;

// Returns the Tint for the given |id|. If there is no tint, the identity tint
// {-1, -1, -1} is returned and won't tint the color on which it is used.
color_utils::HSL GetTint(int id, const ui::ColorProviderKey& key) const;
color_utils::HSL GetTint(int id,
const ui::ColorProviderManager::Key& key) const;

// Callback executed when the accent color is updated. This re-reads the
// accent color and updates |dwm_frame_color_| and
Expand Down Expand Up @@ -77,9 +86,9 @@ FrameColorHelper::FrameColorHelper() {

void FrameColorHelper::AddNativeChromeColors(
ui::ColorMixer& mixer,
const ui::ColorProviderKey& key) const {
const ui::ColorProviderManager::Key& key) const {
using TP = ThemeProperties;
using ColorMode = ui::ColorProviderKey::ColorMode;
using ColorMode = ui::ColorProviderManager::ColorMode;

auto get_theme_color = [key](int id) -> absl::optional<SkColor> {
SkColor theme_color;
Expand All @@ -88,72 +97,66 @@ void FrameColorHelper::AddNativeChromeColors(
return absl::nullopt;
};

// When we're custom-drawing the titlebar we want to use either the colors
// we calculated in OnDwmKeyUpdated() or the default colors. When we're not
// custom-drawing the titlebar we want to match the color Windows actually
// uses because some things (like the incognito icon) use this color to
// decide whether they should draw in light or dark mode. Incognito colors
// should be the same as non-incognito in all cases here.

constexpr SkColor kSystemMicaLightFrameColor =
SkColorSetRGB(0xE8, 0xE8, 0xE8);
constexpr SkColor kSystemMicaDarkFrameColor = SkColorSetRGB(0x20, 0x20, 0x20);

absl::optional<ui::ColorTransform> active_frame_transform;
if (auto color = get_theme_color(TP::COLOR_FRAME_ACTIVE)) {
active_frame_transform = {color.value()};
} else if (dwm_frame_color_) {
active_frame_transform = {dwm_frame_color_.value()};
} else if (ShouldDefaultThemeUseMicaTitlebar()) {
active_frame_transform = {key.color_mode == ColorMode::kDark
? kSystemMicaDarkFrameColor
: kSystemMicaLightFrameColor};
}

absl::optional<ui::ColorTransform> inactive_frame_transform;
if (auto color = get_theme_color(TP::COLOR_FRAME_INACTIVE)) {
inactive_frame_transform = {color.value()};
} else if (dwm_inactive_frame_color_) {
inactive_frame_transform = {dwm_inactive_frame_color_.value()};
} else if (dwm_frame_color_) {
inactive_frame_transform =
ui::HSLShift({dwm_frame_color_.value()},
GetTint(ThemeProperties::TINT_FRAME_INACTIVE, key));
} else if (ShouldDefaultThemeUseMicaTitlebar()) {
inactive_frame_transform = {key.color_mode == ColorMode::kDark
? kSystemMicaDarkFrameColor
: kSystemMicaLightFrameColor};
}

// If setting custom window frame colors ensure we also update the
// corresponding sys header colors. Although this diverges from chrome's
// material spec these overrides are necessary to ensure UI assigned to these
// color roles can continue to work as expected while respecting platform
// frame overrides.
if (active_frame_transform) {
mixer[ui::kColorFrameActive] = active_frame_transform.value();
mixer[ui::kColorSysHeader] = active_frame_transform.value();
mixer[ui::kColorSysOnHeaderDivider] =
GetColorWithMaxContrast(ui::kColorSysHeader);
mixer[ui::kColorSysOnHeaderPrimary] =
GetColorWithMaxContrast(ui::kColorSysHeader);
}
if (inactive_frame_transform) {
mixer[ui::kColorFrameInactive] = inactive_frame_transform.value();
mixer[ui::kColorSysHeaderInactive] = inactive_frame_transform.value();
mixer[ui::kColorSysOnHeaderDividerInactive] =
GetColorWithMaxContrast(ui::kColorSysHeaderInactive);
mixer[ui::kColorSysOnHeaderPrimaryInactive] =
GetColorWithMaxContrast(ui::kColorSysHeaderInactive);
}

if (ShouldDefaultThemeUseMicaTitlebar() && !key.app_controller) {
mixer[kColorNewTabButtonBackgroundFrameActive] = {SK_ColorTRANSPARENT};
mixer[kColorNewTabButtonBackgroundFrameInactive] = {SK_ColorTRANSPARENT};
mixer[kColorNewTabButtonInkDropFrameActive] =
ui::GetColorWithMaxContrast(ui::kColorFrameActive);
mixer[kColorNewTabButtonInkDropFrameInactive] =
ui::GetColorWithMaxContrast(ui::kColorFrameInactive);
if (DwmColorsAllowed(key)) {
// When we're custom-drawing the titlebar we want to use either the colors
// we calculated in OnDwmKeyUpdated() or the default colors. When we're not
// custom-drawing the titlebar we want to match the color Windows actually
// uses because some things (like the incognito icon) use this color to
// decide whether they should draw in light or dark mode. Incognito colors
// should be the same as non-incognito in all cases here.

constexpr SkColor kSystemSolidLightFrameColor = SK_ColorWHITE;
constexpr SkColor kSystemSolidDarkActiveFrameColor = SK_ColorBLACK;
constexpr SkColor kSystemSolidDarkInactiveFrameColor =
SkColorSetRGB(0x2B, 0x2B, 0x2B);

constexpr SkColor kSystemMicaLightFrameColor =
SkColorSetRGB(0xE8, 0xE8, 0xE8);
constexpr SkColor kSystemMicaDarkFrameColor =
SkColorSetRGB(0x20, 0x20, 0x20);

if (auto color = get_theme_color(TP::COLOR_FRAME_ACTIVE)) {
mixer[ui::kColorFrameActive] = {color.value()};
} else if (dwm_frame_color_) {
mixer[ui::kColorFrameActive] = {dwm_frame_color_.value()};
} else if (ShouldDefaultThemeUseMicaTitlebar() && !key.app_controller) {
mixer[ui::kColorFrameActive] = {key.color_mode == ColorMode::kDark
? kSystemMicaDarkFrameColor
: kSystemMicaLightFrameColor};
} else if (ShouldAlwaysUseSystemTitlebar()) {
mixer[ui::kColorFrameActive] = {key.color_mode == ColorMode::kDark
? kSystemSolidDarkActiveFrameColor
: kSystemSolidLightFrameColor};
}

if (auto color = get_theme_color(TP::COLOR_FRAME_INACTIVE)) {
mixer[ui::kColorFrameInactive] = {color.value()};
} else if (dwm_inactive_frame_color_) {
mixer[ui::kColorFrameInactive] = {dwm_inactive_frame_color_.value()};
} else if (ShouldDefaultThemeUseMicaTitlebar() && !key.app_controller) {
mixer[ui::kColorFrameInactive] = {key.color_mode == ColorMode::kDark
? kSystemMicaDarkFrameColor
: kSystemMicaLightFrameColor};
} else if (ShouldAlwaysUseSystemTitlebar()) {
mixer[ui::kColorFrameInactive] = {key.color_mode == ColorMode::kDark
? kSystemSolidDarkInactiveFrameColor
: kSystemSolidLightFrameColor};
} else if (dwm_frame_color_) {
mixer[ui::kColorFrameInactive] =
ui::HSLShift({dwm_frame_color_.value()},
GetTint(ThemeProperties::TINT_FRAME_INACTIVE, key));
}

if (ShouldDefaultThemeUseMicaTitlebar() && !key.app_controller) {
mixer[kColorNewTabButtonBackgroundFrameActive] = {SK_ColorTRANSPARENT};
mixer[kColorNewTabButtonInkDropFrameActive] =
ui::GetColorWithMaxContrast(ui::kColorFrameActive);
}
} else {
if (auto color = get_theme_color(TP::COLOR_FRAME_ACTIVE))
mixer[ui::kColorFrameActive] = {color.value()};
if (auto color = get_theme_color(TP::COLOR_FRAME_INACTIVE))
mixer[ui::kColorFrameInactive] = {color.value()};
}
}

Expand All @@ -176,17 +179,30 @@ FrameColorHelper* FrameColorHelper::Get() {
return g_frame_color_helper.get();
}

bool FrameColorHelper::HasCustomImage(
int id,
const ui::ColorProviderManager::Key& key) const {
return BrowserThemePack::IsPersistentImageID(id) && key.custom_theme &&
key.custom_theme->HasCustomImage(id);
}

bool FrameColorHelper::DwmColorsAllowed(
const ui::ColorProviderManager::Key& key) const {
return !ShouldAlwaysUseSystemTitlebar() ||
!HasCustomImage(IDR_THEME_FRAME, key);
}

color_utils::HSL FrameColorHelper::GetTint(
int id,
const ui::ColorProviderKey& key) const {
const ui::ColorProviderManager::Key& key) const {
color_utils::HSL hsl;
if (key.custom_theme && key.custom_theme->GetTint(id, &hsl))
return hsl;
// Always pass false for |incognito| here since the ColorProvider is treating
// incognito mode as dark mode. If this needs to change, that information will
// need to propagate into the ColorProviderKey.
// need to propagate into the ColorProviderManager::Key.
return ThemeProperties::GetDefaultTint(
id, false, key.color_mode == ui::ColorProviderKey::ColorMode::kDark);
id, false, key.color_mode == ui::ColorProviderManager::ColorMode::kDark);
}

void FrameColorHelper::OnAccentColorUpdated() {
Expand Down Expand Up @@ -225,7 +241,7 @@ ui::ColorTransform GetCaptionForegroundColor(
} // namespace

void AddNativeChromeColorMixer(ui::ColorProvider* provider,
const ui::ColorProviderKey& key) {
const ui::ColorProviderManager::Key& key) {
ui::ColorMixer& mixer = provider->AddMixer();

// NOTE: These cases are always handled, even on Win7, in order to ensure the
Expand Down Expand Up @@ -257,13 +273,13 @@ void AddNativeChromeColorMixer(ui::ColorProvider* provider,
mixer[kColorTryChromeForeground] = {SkColorSetA(SK_ColorWHITE, 0xAD)};
mixer[kColorTryChromeHeaderForeground] = {SK_ColorWHITE};

if (key.color_mode == ui::ColorProviderKey::ColorMode::kLight) {
if (key.color_mode == ui::ColorProviderManager::ColorMode::kLight) {
mixer[kColorNewTabPageBackground] = {ui::kColorNativeWindow};
mixer[kColorNewTabPageLink] = {ui::kColorNativeHotlight};
mixer[kColorNewTabPageText] = {ui::kColorNativeWindowText};
}

if (key.contrast_mode != ui::ColorProviderKey::ContrastMode::kHigh) {
if (key.contrast_mode != ui::ColorProviderManager::ContrastMode::kHigh) {
FrameColorHelper::Get()->AddNativeChromeColors(mixer, key);
return;
}
Expand Down
41 changes: 18 additions & 23 deletions chrome/browser/ui/views/frame/browser_frame_view_win.cc
Expand Up @@ -33,7 +33,6 @@
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/resource/resource_bundle_win.h"
#include "ui/base/theme_provider.h"
#include "ui/base/ui_base_features.h"
#include "ui/base/win/hwnd_metrics.h"
#include "ui/display/win/dpi.h"
#include "ui/display/win/screen_win.h"
Expand Down Expand Up @@ -72,8 +71,6 @@ constexpr int kMaximizedLeftMargin = 2;

constexpr int kIconTitleSpacing = 5;

constexpr int kCR23TopAreaHeight = 6;

} // namespace

///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -409,7 +406,7 @@ void BrowserFrameViewWin::ResetWindowControls() {
void BrowserFrameViewWin::OnThemeChanged() {
BrowserNonClientFrameView::OnThemeChanged();
if (!ShouldBrowserCustomDrawTitlebar(browser_view())) {
SetSystemMicaTitlebarAttributes();
SetSystemTitlebarAttributes();
}
}

Expand Down Expand Up @@ -520,7 +517,7 @@ int BrowserFrameViewWin::FrameTopBorderThicknessPx(bool restored) const {

int BrowserFrameViewWin::TopAreaHeight(bool restored) const {
if (frame()->IsFullscreen() && !restored) {
return features::IsChromeRefresh2023() ? kCR23TopAreaHeight : 0;
return 0;
}

const bool maximized = IsMaximized() && !restored;
Expand All @@ -534,10 +531,6 @@ int BrowserFrameViewWin::TopAreaHeight(bool restored) const {
return top;
}

if (features::IsChromeRefresh2023()) {
return top + kCR23TopAreaHeight;
}

// In maximized mode, we do not add any additional thickness to the grab
// handle above the tabs; just return the frame thickness.
if (maximized) {
Expand Down Expand Up @@ -639,24 +632,26 @@ bool BrowserFrameViewWin::ShouldShowWindowTitle(TitlebarType type) const {

void BrowserFrameViewWin::TabletModeChanged() {
if (!ShouldBrowserCustomDrawTitlebar(browser_view())) {
SetSystemMicaTitlebarAttributes();
SetSystemTitlebarAttributes();
}
}

void BrowserFrameViewWin::SetSystemMicaTitlebarAttributes() {
CHECK(SystemTitlebarCanUseMicaMaterial());

const BOOL dark_titlebar_enabled = GetNativeTheme()->ShouldUseDarkColors();
DwmSetWindowAttribute(views::HWNDForWidget(frame()),
DWMWA_USE_IMMERSIVE_DARK_MODE, &dark_titlebar_enabled,
sizeof(dark_titlebar_enabled));
void BrowserFrameViewWin::SetSystemTitlebarAttributes() {
if (SystemTitlebarSupportsDarkMode()) {
const BOOL dark_titlebar_enabled = GetNativeTheme()->ShouldUseDarkColors();
DwmSetWindowAttribute(views::HWNDForWidget(frame()),
DWMWA_USE_IMMERSIVE_DARK_MODE, &dark_titlebar_enabled,
sizeof(dark_titlebar_enabled));
}

const DWM_SYSTEMBACKDROP_TYPE dwm_backdrop_type =
browser_view()->GetTabStripVisible() ? DWMSBT_TABBEDWINDOW
: DWMSBT_MAINWINDOW;
DwmSetWindowAttribute(views::HWNDForWidget(frame()),
DWMWA_SYSTEMBACKDROP_TYPE, &dwm_backdrop_type,
sizeof(dwm_backdrop_type));
if (ShouldBrowserUseMicaTitlebar(browser_view())) {
const DWM_SYSTEMBACKDROP_TYPE dwm_backdrop_type =
browser_view()->GetTabStripVisible() ? DWMSBT_TABBEDWINDOW
: DWMSBT_MAINWINDOW;
DwmSetWindowAttribute(views::HWNDForWidget(frame()),
DWMWA_SYSTEMBACKDROP_TYPE, &dwm_backdrop_type,
sizeof(dwm_backdrop_type));
}
}

SkColor BrowserFrameViewWin::GetTitlebarColor() const {
Expand Down
14 changes: 7 additions & 7 deletions chrome/browser/ui/views/frame/browser_frame_view_win.h
Expand Up @@ -69,11 +69,6 @@ class BrowserFrameViewWin : public BrowserNonClientFrameView,
bool IsMaximized() const;
bool IsWebUITabStrip() const;

// Returns the y coordinate for the top of the frame, which in maximized mode
// is the top of the screen and in restored mode is 1 pixel below the top of
// the window to leave room for the visual border that Windows draws.
int WindowTopY() const;

// Visual height of the titlebar when the window is maximized (i.e. excluding
// the area above the top of the screen).
int TitlebarMaximizedVisualHeight() const;
Expand Down Expand Up @@ -130,6 +125,11 @@ class BrowserFrameViewWin : public BrowserNonClientFrameView,
// don't have tabs.
int TitlebarHeight(bool restored) const;

// Returns the y coordinate for the top of the frame, which in maximized mode
// is the top of the screen and in restored mode is 1 pixel below the top of
// the window to leave room for the visual border that Windows draws.
int WindowTopY() const;

// Returns the width of the caption buttons region, including visible
// system-drawn and custom-drawn caption buttons.
int CaptionButtonsRegionWidth() const;
Expand All @@ -145,8 +145,8 @@ class BrowserFrameViewWin : public BrowserNonClientFrameView,
// Called when the device enters or exits tablet mode.
void TabletModeChanged();

// Sets DWM attributes for rendering the system-drawn Mica titlebar.
void SetSystemMicaTitlebarAttributes();
// Sets DWM attributes for rendering the system-drawn titlebar.
void SetSystemTitlebarAttributes();

// Paint various sub-components of this view.
void PaintTitlebar(gfx::Canvas* canvas) const;
Expand Down