Reland "Set forced colors based on NativeTheme"
This is a reland of d1b4afb89b58ab8d606cae268d511a03549ebda2
Original change :
https://chromium-review.googlesource.com/c/chromium/src/+/1653408/18
Revert of original change :
https://chromium-review.googlesource.com/c/chromium/src/+/1682277
---
The original change was reverted due to a compilation error in the
following files:
- local_ntp_backgrounds_browsertest.cc
- local_ntp_browsertest.cc
The reason being that a recent change to these files had added calls
to NativeTheme::SetDarkModeParent(), which was a function that had been
removed by this change.
Since the two changes were submitted around the same time, this
compilation error was not picked up until after the change was merged.
---
In order to fix the compilation errors,
NativeTheme::SetDarkModeParent() was re-added to NativeTheme. A follow-
up change will be made to remove SetDarkModeParent() and
DarkModeObserver.
As per additional feedback, NativeTheme was updated to have-an
observer rather than is-an observer. This involved removing
the implementation of NativeThemeObserver directly by NativeTheme
and adding an inner-class (ColorSchemeNativeThemeObserver) to
implement NativeThemeObserver. This new inner-class now handles
both dark mode and high contrast changes.
---
Original change's description:
> Set forced colors based on NativeTheme
>
> Pass the state of high contrast from NativeTheme to the renderer and
> update the forced colors enum based on its value dynamically. This
> is used to evaluate the forced-colors media query.
>
> This change also removes the use of DarkModeObserver in WebContentsImpl
> and NativeTheme in preference to NativeThemeObserver. WebContentsImpl
> and NativeTheme now implement NativeThemeObserver to track changes
> to both dark mode and high contrast.
>
> Bug: 970285
> Change-Id: Id83abb902358916f514cf972876e1798e56b72ad
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1653408
> Commit-Queue: Alison Maher <[email protected]>
> Reviewed-by: Kinuko Yasuda <[email protected]>
> Reviewed-by: Peter Kasting <[email protected]>
> Reviewed-by: Kevin Babbitt <[email protected]>
> Reviewed-by: Rune Lillesveen <[email protected]>
> Cr-Commit-Position: refs/heads/master@{#673531}
[email protected],[email protected]
Bug: 970285
Change-Id: I816cb440cae33baa5492a91c2ed20c50f0c10a66
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1684132
Reviewed-by: Kevin Babbitt <[email protected]>
Reviewed-by: Kinuko Yasuda <[email protected]>
Reviewed-by: Elly Fong-Jones <[email protected]>
Commit-Queue: Alison Maher <[email protected]>
Cr-Commit-Position: refs/heads/master@{#675295}
diff --git a/chrome/browser/chrome_content_browser_client.cc b/chrome/browser/chrome_content_browser_client.cc
index b9a6f50..ee9702c 100644
--- a/chrome/browser/chrome_content_browser_client.cc
+++ b/chrome/browser/chrome_content_browser_client.cc
@@ -3463,6 +3463,9 @@
? content::AutoplayPolicy::kDocumentUserActivationRequired
: content::AutoplayPolicy::kNoUserGestureRequired;
}
+ web_prefs->forced_colors = native_theme->UsesHighContrastColors()
+ ? blink::ForcedColors::kActive
+ : blink::ForcedColors::kNone;
web_prefs->preferred_color_scheme = native_theme->SystemDarkModeEnabled()
? blink::PreferredColorScheme::kDark
: blink::PreferredColorScheme::kLight;
diff --git a/chrome/browser/chrome_content_browser_client_browsertest.cc b/chrome/browser/chrome_content_browser_client_browsertest.cc
index 389323c..f1da002c 100644
--- a/chrome/browser/chrome_content_browser_client_browsertest.cc
+++ b/chrome/browser/chrome_content_browser_client_browsertest.cc
@@ -666,6 +666,71 @@
INSTANTIATE_TEST_SUITE_P(All, PrefersColorSchemeTest, testing::Bool());
+class ForcedColorsTest : public testing::WithParamInterface<bool>,
+ public InProcessBrowserTest {
+ protected:
+ ForcedColorsTest() : theme_client_(&test_theme_) {}
+
+ ~ForcedColorsTest() {
+ CHECK_EQ(&theme_client_, SetBrowserClientForTesting(original_client_));
+ }
+
+ const char* ExpectedForcedColors() const {
+ return GetParam() ? "active" : "none";
+ }
+
+ void SetUpCommandLine(base::CommandLine* command_line) override {
+ InProcessBrowserTest::SetUpCommandLine(command_line);
+ command_line->AppendSwitchASCII(switches::kEnableBlinkFeatures,
+ "ForcedColors");
+ }
+
+ void SetUpOnMainThread() override {
+ InProcessBrowserTest::SetUpOnMainThread();
+ original_client_ = SetBrowserClientForTesting(&theme_client_);
+ }
+
+ protected:
+ ui::TestNativeTheme test_theme_;
+
+ private:
+ content::ContentBrowserClient* original_client_ = nullptr;
+
+ class ChromeContentBrowserClientWithWebTheme
+ : public ChromeContentBrowserClient {
+ public:
+ explicit ChromeContentBrowserClientWithWebTheme(
+ const ui::NativeTheme* theme)
+ : theme_(theme) {}
+
+ protected:
+ const ui::NativeTheme* GetWebTheme() const override { return theme_; }
+
+ private:
+ const ui::NativeTheme* const theme_;
+ };
+
+ ChromeContentBrowserClientWithWebTheme theme_client_;
+};
+
+IN_PROC_BROWSER_TEST_P(ForcedColorsTest, ForcedColors) {
+ test_theme_.SetUsesHighContrastColors(GetParam());
+ browser()
+ ->tab_strip_model()
+ ->GetActiveWebContents()
+ ->GetRenderViewHost()
+ ->OnWebkitPreferencesChanged();
+ ui_test_utils::NavigateToURL(
+ browser(), ui_test_utils::GetTestUrl(
+ base::FilePath(base::FilePath::kCurrentDirectory),
+ base::FilePath(FILE_PATH_LITERAL("forced-colors.html"))));
+ base::string16 tab_title;
+ ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), &tab_title));
+ EXPECT_EQ(base::ASCIIToUTF16(ExpectedForcedColors()), tab_title);
+}
+
+INSTANTIATE_TEST_SUITE_P(All, ForcedColorsTest, testing::Bool());
+
class ProtocolHandlerTest : public InProcessBrowserTest {
public:
ProtocolHandlerTest() = default;
diff --git a/chrome/test/data/forced-colors.html b/chrome/test/data/forced-colors.html
new file mode 100644
index 0000000..5923392
--- /dev/null
+++ b/chrome/test/data/forced-colors.html
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<title>FAIL</title>
+<script>
+ if (window.matchMedia("(forced-colors: none)").matches) {
+ document.title = "none";
+ } else if (window.matchMedia("(forced-colors: active)").matches) {
+ document.title = "active";
+ }
+</script>
diff --git a/content/browser/web_contents/web_contents_impl.cc b/content/browser/web_contents/web_contents_impl.cc
index 37e9397c..2cc8cae9 100644
--- a/content/browser/web_contents/web_contents_impl.cc
+++ b/content/browser/web_contents/web_contents_impl.cc
@@ -591,6 +591,7 @@
is_overlay_content_(false),
showing_context_menu_(false),
text_autosizer_page_info_({0, 0, 1.f}),
+ native_theme_observer_(this),
had_inner_webcontents_(false),
loading_weak_factory_(this),
weak_factory_(this) {
@@ -613,7 +614,10 @@
registry_.AddInterface(base::BindRepeating(
&WebContentsImpl::OnColorChooserFactoryRequest, base::Unretained(this)));
- dark_mode_observer_.Start();
+ ui::NativeTheme* native_theme = ui::NativeTheme::GetInstanceForWeb();
+ native_theme_observer_.Add(native_theme);
+ in_high_contrast_ = native_theme->UsesHighContrastColors();
+ in_dark_mode_ = native_theme->SystemDarkModeEnabled();
}
WebContentsImpl::~WebContentsImpl() {
@@ -7226,8 +7230,24 @@
GetMainFrame()->SetVisibilityForChildViews(visible);
}
-void WebContentsImpl::OnDarkModeChanged(bool dark_mode) {
- NotifyPreferencesChanged();
+void WebContentsImpl::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
+ DCHECK(native_theme_observer_.IsObserving(observed_theme));
+
+ bool in_dark_mode = observed_theme->SystemDarkModeEnabled();
+ bool in_high_contrast = observed_theme->UsesHighContrastColors();
+ bool preferences_changed = false;
+
+ if (in_dark_mode_ != in_dark_mode) {
+ in_dark_mode_ = in_dark_mode;
+ preferences_changed = true;
+ }
+ if (in_high_contrast_ != in_high_contrast) {
+ in_high_contrast_ = in_high_contrast;
+ preferences_changed = true;
+ }
+
+ if (preferences_changed)
+ NotifyPreferencesChanged();
}
} // namespace content
diff --git a/content/browser/web_contents/web_contents_impl.h b/content/browser/web_contents/web_contents_impl.h
index 210b672..bf85ec4 100644
--- a/content/browser/web_contents/web_contents_impl.h
+++ b/content/browser/web_contents/web_contents_impl.h
@@ -22,6 +22,7 @@
#include "base/observer_list.h"
#include "base/optional.h"
#include "base/process/process.h"
+#include "base/scoped_observer.h"
#include "base/time/time.h"
#include "base/values.h"
#include "build/build_config.h"
@@ -69,6 +70,7 @@
#include "ui/gfx/geometry/size.h"
#include "ui/native_theme/dark_mode_observer.h"
#include "ui/native_theme/native_theme.h"
+#include "ui/native_theme/native_theme_observer.h"
#if defined(OS_ANDROID)
#include "content/browser/android/nfc_host.h"
@@ -146,7 +148,8 @@
public blink::mojom::ColorChooserFactory,
public NotificationObserver,
public NavigationControllerDelegate,
- public NavigatorDelegate {
+ public NavigatorDelegate,
+ public ui::NativeThemeObserver {
public:
class FriendWrapper;
@@ -1495,9 +1498,8 @@
// |current_fullscreen_frame_| and notify observers whenever it changes.
void FullscreenFrameSetUpdated();
- // Called by DarkModeObserver when the dark mode state changes; triggers a
- // preference update.
- void OnDarkModeChanged(bool dark_mode);
+ // Overridden from ui::NativeThemeObserver:
+ void OnNativeThemeUpdated(ui::NativeTheme* observed_theme) override;
// Data for core operation ---------------------------------------------------
@@ -1900,12 +1902,13 @@
// with OOPIF renderers.
blink::WebTextAutosizerPageInfo text_autosizer_page_info_;
- // Observe dark mode native theme changes to notify the renderer about
- // preferred color scheme changes.
- ui::DarkModeObserver dark_mode_observer_{
- ui::NativeTheme::GetInstanceForWeb(),
- base::BindRepeating(&WebContentsImpl::OnDarkModeChanged,
- base::Unretained(this))};
+ // Observe native theme for changes to dark mode and high contrast. Used to
+ // notify the renderer of preferred color scheme and forced colors changes.
+ ScopedObserver<ui::NativeTheme, ui::NativeThemeObserver>
+ native_theme_observer_;
+
+ bool in_high_contrast_ = false;
+ bool in_dark_mode_ = false;
// TODO(crbug.com/934637): Remove this field when pdf/any inner web contents
// user gesture is properly propagated. This is a temporary fix for history
diff --git a/content/public/common/common_param_traits_macros.h b/content/public/common/common_param_traits_macros.h
index 9ad8ad0..8ef20bc 100644
--- a/content/public/common/common_param_traits_macros.h
+++ b/content/public/common/common_param_traits_macros.h
@@ -81,6 +81,9 @@
IPC_ENUM_TRAITS_MIN_MAX_VALUE(blink::PreferredColorScheme,
blink::PreferredColorScheme::kNoPreference,
blink::PreferredColorScheme::kLight)
+IPC_ENUM_TRAITS_MIN_MAX_VALUE(blink::ForcedColors,
+ blink::ForcedColors::kNone,
+ blink::ForcedColors::kMaxValue)
IPC_STRUCT_TRAITS_BEGIN(blink::WebPoint)
IPC_STRUCT_TRAITS_MEMBER(x)
@@ -242,6 +245,7 @@
IPC_STRUCT_TRAITS_MEMBER(do_not_update_selection_on_mutating_selection_range)
IPC_STRUCT_TRAITS_MEMBER(autoplay_policy)
IPC_STRUCT_TRAITS_MEMBER(preferred_color_scheme)
+ IPC_STRUCT_TRAITS_MEMBER(forced_colors)
IPC_STRUCT_TRAITS_MEMBER(low_priority_iframes_threshold)
IPC_STRUCT_TRAITS_MEMBER(picture_in_picture_enabled)
IPC_STRUCT_TRAITS_MEMBER(translate_service_available)
diff --git a/content/public/common/web_preferences.cc b/content/public/common/web_preferences.cc
index e2f3d5b..6d7b8905 100644
--- a/content/public/common/web_preferences.cc
+++ b/content/public/common/web_preferences.cc
@@ -225,6 +225,7 @@
do_not_update_selection_on_mutating_selection_range(false),
autoplay_policy(AutoplayPolicy::kDocumentUserActivationRequired),
preferred_color_scheme(blink::PreferredColorScheme::kNoPreference),
+ forced_colors(blink::ForcedColors::kNone),
low_priority_iframes_threshold(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN),
picture_in_picture_enabled(true),
translate_service_available(false),
diff --git a/content/public/common/web_preferences.h b/content/public/common/web_preferences.h
index f9a99647..6677d8e 100644
--- a/content/public/common/web_preferences.h
+++ b/content/public/common/web_preferences.h
@@ -14,6 +14,7 @@
#include "build/build_config.h"
#include "content/common/content_export.h"
#include "net/nqe/effective_connection_type.h"
+#include "third_party/blink/public/common/css/forced_colors.h"
#include "third_party/blink/public/common/css/preferred_color_scheme.h"
#include "third_party/blink/public/mojom/v8_cache_options.mojom.h"
#include "ui/base/pointer/pointer_device.h"
@@ -305,6 +306,12 @@
blink::PreferredColorScheme preferred_color_scheme =
blink::PreferredColorScheme::kNoPreference;
+ // Forced colors indicates whether forced color mode is active or not. Forced
+ // colors is used to evaluate the forced-colors and prefers-color-scheme
+ // media queries and is used to resolve the default color scheme as indicated
+ // by the preferred_color_scheme.
+ blink::ForcedColors forced_colors = blink::ForcedColors::kNone;
+
// Network quality threshold below which resources from iframes are assigned
// either kVeryLow or kVeryLow Blink priority.
net::EffectiveConnectionType low_priority_iframes_threshold;
diff --git a/content/renderer/render_view_impl.cc b/content/renderer/render_view_impl.cc
index dae2f5a0..e136b0a 100644
--- a/content/renderer/render_view_impl.cc
+++ b/content/renderer/render_view_impl.cc
@@ -935,6 +935,7 @@
settings->SetLazyLoadEnabled(prefs.lazy_load_enabled);
settings->SetPreferredColorScheme(prefs.preferred_color_scheme);
+ settings->SetForcedColors(prefs.forced_colors);
for (const auto& ect_distance_pair :
prefs.lazy_frame_loading_distance_thresholds_px) {
diff --git a/third_party/blink/public/common/css/forced_colors.h b/third_party/blink/public/common/css/forced_colors.h
index fac2270..6edbf9b 100644
--- a/third_party/blink/public/common/css/forced_colors.h
+++ b/third_party/blink/public/common/css/forced_colors.h
@@ -11,6 +11,7 @@
enum class ForcedColors {
kNone,
kActive,
+ kMaxValue = kActive,
};
} // namespace blink
diff --git a/third_party/blink/renderer/core/css/media_values.cc b/third_party/blink/renderer/core/css/media_values.cc
index 260f7c6..c31ccf3 100644
--- a/third_party/blink/renderer/core/css/media_values.cc
+++ b/third_party/blink/renderer/core/css/media_values.cc
@@ -193,7 +193,8 @@
ForcedColors MediaValues::CalculateForcedColors(LocalFrame* frame) {
DCHECK(frame);
DCHECK(frame->GetSettings());
- return frame->GetSettings()->GetForcedColors();
+ DCHECK(frame->GetDocument());
+ return frame->GetDocument()->GetStyleEngine().GetForcedColors();
}
bool MediaValues::ComputeLengthImpl(double value,
diff --git a/third_party/blink/renderer/core/css/style_engine.cc b/third_party/blink/renderer/core/css/style_engine.cc
index 12011a64..5f88201 100644
--- a/third_party/blink/renderer/core/css/style_engine.cc
+++ b/third_party/blink/renderer/core/css/style_engine.cc
@@ -95,8 +95,10 @@
global_rule_set_ = MakeGarbageCollected<CSSGlobalRuleSet>();
// Document is initially style dirty.
style_recalc_root_.Update(nullptr, &document);
- if (auto* settings = GetDocument().GetSettings())
+ if (auto* settings = GetDocument().GetSettings()) {
preferred_color_scheme_ = settings->GetPreferredColorScheme();
+ forced_colors_ = settings->GetForcedColors();
+ }
}
StyleEngine::~StyleEngine() = default;
@@ -1863,6 +1865,10 @@
void StyleEngine::UpdateColorScheme() {
auto* settings = GetDocument().GetSettings();
DCHECK(settings);
+
+ ForcedColors old_forced_colors = forced_colors_;
+ forced_colors_ = settings->GetForcedColors();
+
PreferredColorScheme old_preferred_color_scheme = preferred_color_scheme_;
preferred_color_scheme_ = settings->GetPreferredColorScheme();
bool use_dark_scheme =
@@ -1873,7 +1879,9 @@
// darkening is enabled.
preferred_color_scheme_ = PreferredColorScheme::kNoPreference;
}
- if (preferred_color_scheme_ != old_preferred_color_scheme)
+
+ if (forced_colors_ != old_forced_colors ||
+ preferred_color_scheme_ != old_preferred_color_scheme)
PlatformColorsChanged();
UpdateColorSchemeBackground();
}
@@ -1898,7 +1906,8 @@
bool use_dark_background = false;
- if (preferred_color_scheme_ == PreferredColorScheme::kDark) {
+ if (preferred_color_scheme_ == PreferredColorScheme::kDark &&
+ forced_colors_ != ForcedColors::kActive) {
const ComputedStyle* style = nullptr;
if (auto* root_element = GetDocument().documentElement())
style = root_element->GetComputedStyle();
diff --git a/third_party/blink/renderer/core/css/style_engine.h b/third_party/blink/renderer/core/css/style_engine.h
index 17ee2cf..9f1410d 100644
--- a/third_party/blink/renderer/core/css/style_engine.h
+++ b/third_party/blink/renderer/core/css/style_engine.h
@@ -33,6 +33,7 @@
#include <memory>
#include <utility>
#include "base/auto_reset.h"
+#include "third_party/blink/public/common/css/forced_colors.h"
#include "third_party/blink/public/common/css/preferred_color_scheme.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/renderer/core/core_export.h"
@@ -347,6 +348,7 @@
PreferredColorScheme GetPreferredColorScheme() const {
return preferred_color_scheme_;
}
+ ForcedColors GetForcedColors() const { return forced_colors_; }
void UpdateColorSchemeBackground();
void Trace(blink::Visitor*) override;
@@ -551,11 +553,14 @@
Member<const CSSValue> meta_color_scheme_;
// The preferred color scheme is set in settings, but may be overridden by the
- // ForceDarkMode setting where the preferred_color_scheme_ will be set no
+ // ForceDarkMode setting where the preferred_color_scheme_ will be set to
// kNoPreference to avoid dark styling to be applied before auto darkening.
PreferredColorScheme preferred_color_scheme_ =
PreferredColorScheme::kNoPreference;
+ // Forced colors is set in settings.
+ ForcedColors forced_colors_ = ForcedColors::kNone;
+
friend class NodeTest;
friend class StyleEngineTest;
friend class WhitespaceAttacherTest;
diff --git a/third_party/blink/renderer/core/frame/settings.json5 b/third_party/blink/renderer/core/frame/settings.json5
index 3806cba0..c92364f 100644
--- a/third_party/blink/renderer/core/frame/settings.json5
+++ b/third_party/blink/renderer/core/frame/settings.json5
@@ -1041,7 +1041,7 @@
{
name: "forcedColors",
initial: "ForcedColors::kNone",
- invalidate: "MediaQuery",
+ invalidate: "ColorScheme",
type: "ForcedColors",
},
],
diff --git a/ui/native_theme/native_theme.cc b/ui/native_theme/native_theme.cc
index 3d01013..104b7d3 100644
--- a/ui/native_theme/native_theme.cc
+++ b/ui/native_theme/native_theme.cc
@@ -10,7 +10,6 @@
#include "base/command_line.h"
#include "ui/base/ui_base_switches.h"
#include "ui/native_theme/dark_mode_observer.h"
-#include "ui/native_theme/native_theme_observer.h"
namespace ui {
@@ -86,4 +85,31 @@
set_dark_mode(is_dark_mode);
NotifyObservers();
}
+
+NativeTheme::ColorSchemeNativeThemeObserver::ColorSchemeNativeThemeObserver(
+ NativeTheme* theme_to_update)
+ : theme_to_update_(theme_to_update) {}
+
+NativeTheme::ColorSchemeNativeThemeObserver::~ColorSchemeNativeThemeObserver() =
+ default;
+
+void NativeTheme::ColorSchemeNativeThemeObserver::OnNativeThemeUpdated(
+ ui::NativeTheme* observed_theme) {
+ bool is_dark_mode = observed_theme->SystemDarkModeEnabled();
+ bool is_high_contrast = observed_theme->UsesHighContrastColors();
+ bool notify_observers = false;
+
+ if (theme_to_update_->SystemDarkModeEnabled() != is_dark_mode) {
+ theme_to_update_->set_dark_mode(is_dark_mode);
+ notify_observers = true;
+ }
+ if (theme_to_update_->UsesHighContrastColors() != is_high_contrast) {
+ theme_to_update_->set_high_contrast(is_high_contrast);
+ notify_observers = true;
+ }
+
+ if (notify_observers)
+ theme_to_update_->NotifyObservers();
+}
+
} // namespace ui
diff --git a/ui/native_theme/native_theme.h b/ui/native_theme/native_theme.h
index 68d9f2a..6f0ee21 100644
--- a/ui/native_theme/native_theme.h
+++ b/ui/native_theme/native_theme.h
@@ -16,6 +16,7 @@
#include "ui/gfx/native_widget_types.h"
#include "ui/native_theme/caption_style.h"
#include "ui/native_theme/native_theme_export.h"
+#include "ui/native_theme/native_theme_observer.h"
namespace gfx {
class Rect;
@@ -24,7 +25,6 @@
namespace ui {
class DarkModeObserver;
-class NativeThemeObserver;
// This class supports drawing UI controls (like buttons, text fields, lists,
// comboboxes, etc) that look like the native UI controls of the underlying
@@ -434,6 +434,25 @@
is_high_contrast_ = is_high_contrast;
}
+ // Allows one native theme to observe changes in another. For example, the
+ // web native theme for Windows observes the corresponding ui native theme in
+ // order to receive changes regarding the state of dark mode/high contrast.
+ class NATIVE_THEME_EXPORT ColorSchemeNativeThemeObserver
+ : public NativeThemeObserver {
+ public:
+ ColorSchemeNativeThemeObserver(NativeTheme* theme_to_update);
+ ~ColorSchemeNativeThemeObserver() override;
+
+ private:
+ // ui::NativeThemeObserver:
+ void OnNativeThemeUpdated(ui::NativeTheme* observed_theme) override;
+
+ // The theme that gets updated when OnNativeThemeUpdated() is called.
+ NativeTheme* const theme_to_update_;
+
+ DISALLOW_COPY_AND_ASSIGN(ColorSchemeNativeThemeObserver);
+ };
+
private:
// DarkModeObserver callback.
void OnParentDarkModeChanged(bool is_dark_mode);
diff --git a/ui/native_theme/native_theme_aura.h b/ui/native_theme/native_theme_aura.h
index 6ce6a8a4..5c08f42 100644
--- a/ui/native_theme/native_theme_aura.h
+++ b/ui/native_theme/native_theme_aura.h
@@ -24,7 +24,7 @@
static NativeThemeAura* instance();
static NativeThemeAura* web_instance();
- // Overridden from NativeThemeBase:
+ // NativeThemeBase:
SkColor GetSystemColor(ColorId color_id) const override;
void PaintMenuPopupBackground(
cc::PaintCanvas* canvas,
diff --git a/ui/native_theme/native_theme_win.cc b/ui/native_theme/native_theme_win.cc
index e74d07035..8e17360 100644
--- a/ui/native_theme/native_theme_win.cc
+++ b/ui/native_theme/native_theme_win.cc
@@ -254,11 +254,17 @@
GetProcAddress(theme_dll_, "CloseThemeData"));
}
+ // If there's no sequenced task runner handle, we can't be called back for
+ // dark mode changes. This generally happens in tests. As a result, ignore
+ // dark mode in this case.
if (!IsForcedDarkMode() && !IsForcedHighContrast() &&
base::SequencedTaskRunnerHandle::IsSet()) {
- // If there's no sequenced task runner handle, we can't be called back for
- // dark mode changes. This generally happens in tests. As a result, ignore
- // dark mode in this case.
+ // Add the web native theme as an observer to stay in sync with dark mode/
+ // high contrast changes.
+ color_scheme_observer_ =
+ std::make_unique<NativeTheme::ColorSchemeNativeThemeObserver>(
+ NativeTheme::GetInstanceForWeb());
+ AddObserver(color_scheme_observer_.get());
// Dark Mode currently targets UWP apps, which means Win32 apps need to use
// alternate, less reliable means of detecting the state. The following
@@ -270,7 +276,6 @@
L"Themes\\Personalize",
KEY_READ | KEY_NOTIFY) == ERROR_SUCCESS;
if (key_open_succeeded) {
- NativeTheme::GetInstanceForWeb()->SetDarkModeParent(this);
UpdateDarkModeStatus();
RegisterThemeRegkeyObserver();
}
diff --git a/ui/native_theme/native_theme_win.h b/ui/native_theme/native_theme_win.h
index 6794de1d..9a17b16 100644
--- a/ui/native_theme/native_theme_win.h
+++ b/ui/native_theme/native_theme_win.h
@@ -329,6 +329,11 @@
gfx::ScopedSysColorChangeListener color_change_listener_;
mutable std::map<int, SkColor> system_colors_;
+ // Used to notify the web native theme of changes to dark mode and high
+ // contrast.
+ std::unique_ptr<NativeTheme::ColorSchemeNativeThemeObserver>
+ color_scheme_observer_;
+
DISALLOW_COPY_AND_ASSIGN(NativeThemeWin);
};
diff --git a/ui/native_theme/test_native_theme.cc b/ui/native_theme/test_native_theme.cc
index 200eb212..dcc5f85 100644
--- a/ui/native_theme/test_native_theme.cc
+++ b/ui/native_theme/test_native_theme.cc
@@ -38,7 +38,7 @@
}
bool TestNativeTheme::UsesHighContrastColors() const {
- return false;
+ return high_contrast_;
}
bool TestNativeTheme::SystemDarkModeEnabled() const {
diff --git a/ui/native_theme/test_native_theme.h b/ui/native_theme/test_native_theme.h
index bdfc278..73b8a45 100644
--- a/ui/native_theme/test_native_theme.h
+++ b/ui/native_theme/test_native_theme.h
@@ -32,9 +32,13 @@
bool SystemDarkModeEnabled() const override;
void SetDarkMode(bool dark_mode) { dark_mode_ = dark_mode; }
+ void SetUsesHighContrastColors(bool high_contrast) {
+ high_contrast_ = high_contrast;
+ }
private:
bool dark_mode_ = false;
+ bool high_contrast_ = false;
DISALLOW_COPY_AND_ASSIGN(TestNativeTheme);
};