blob: 929e339487b7d2b4407cff069944f7a2cc1266a4 [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/native_theme/native_theme.h"
#include <cstring>
#include "base/command_line.h"
#include "ui/base/ui_base_switches.h"
namespace ui {
NativeTheme::ExtraParams::ExtraParams() {
memset(this, 0, sizeof(*this));
}
NativeTheme::ExtraParams::ExtraParams(const ExtraParams& other) {
memcpy(this, &other, sizeof(*this));
}
void NativeTheme::AddObserver(NativeThemeObserver* observer) {
native_theme_observers_.AddObserver(observer);
}
void NativeTheme::RemoveObserver(NativeThemeObserver* observer) {
native_theme_observers_.RemoveObserver(observer);
}
void NativeTheme::NotifyObservers() {
for (NativeThemeObserver& observer : native_theme_observers_)
observer.OnNativeThemeUpdated(this);
}
NativeTheme::NativeTheme()
: should_use_dark_colors_(IsForcedDarkMode()),
is_high_contrast_(IsForcedHighContrast()),
preferred_color_scheme_(CalculatePreferredColorScheme()) {}
NativeTheme::~NativeTheme() = default;
bool NativeTheme::ShouldUseDarkColors() const {
return should_use_dark_colors_;
}
bool NativeTheme::SystemDarkModeSupported() const {
return false;
}
bool NativeTheme::UsesHighContrastColors() const {
return is_high_contrast_;
}
NativeTheme::PreferredColorScheme NativeTheme::GetPreferredColorScheme() const {
return preferred_color_scheme_;
}
bool NativeTheme::IsForcedDarkMode() const {
static bool kIsForcedDarkMode =
base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kForceDarkMode);
return kIsForcedDarkMode;
}
bool NativeTheme::IsForcedHighContrast() const {
static bool kIsForcedHighContrast =
base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kForceHighContrast);
return kIsForcedHighContrast;
}
NativeTheme::PreferredColorScheme NativeTheme::CalculatePreferredColorScheme()
const {
return ShouldUseDarkColors() ? NativeTheme::PreferredColorScheme::kDark
: NativeTheme::PreferredColorScheme::kLight;
}
base::Optional<CaptionStyle> NativeTheme::GetSystemCaptionStyle() const {
return CaptionStyle::FromSystemSettings();
}
const std::map<NativeTheme::SystemThemeColor, SkColor>&
NativeTheme::GetSystemColors() const {
return system_colors_;
}
bool NativeTheme::HasDifferentSystemColors(
const std::map<NativeTheme::SystemThemeColor, SkColor>& colors) const {
return system_colors_ != colors;
}
void NativeTheme::set_system_colors(
const std::map<NativeTheme::SystemThemeColor, SkColor>& colors) {
system_colors_ = colors;
}
void NativeTheme::UpdateSystemColorInfo(
bool is_dark_mode,
bool is_high_contrast,
PreferredColorScheme preferred_color_scheme,
const base::flat_map<SystemThemeColor, uint32_t>& colors) {
set_use_dark_colors(is_dark_mode);
set_high_contrast(is_high_contrast);
set_preferred_color_scheme(preferred_color_scheme);
for (const auto& color : colors) {
system_colors_[color.first] = color.second;
}
}
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 should_use_dark_colors = observed_theme->ShouldUseDarkColors();
bool is_high_contrast = observed_theme->UsesHighContrastColors();
PreferredColorScheme preferred_color_scheme =
observed_theme->GetPreferredColorScheme();
bool notify_observers = false;
if (theme_to_update_->ShouldUseDarkColors() != should_use_dark_colors) {
theme_to_update_->set_use_dark_colors(should_use_dark_colors);
notify_observers = true;
}
if (theme_to_update_->UsesHighContrastColors() != is_high_contrast) {
theme_to_update_->set_high_contrast(is_high_contrast);
notify_observers = true;
}
if (theme_to_update_->GetPreferredColorScheme() != preferred_color_scheme) {
theme_to_update_->set_preferred_color_scheme(preferred_color_scheme);
notify_observers = true;
}
const auto& system_colors = observed_theme->GetSystemColors();
if (theme_to_update_->HasDifferentSystemColors(system_colors)) {
theme_to_update_->set_system_colors(system_colors);
notify_observers = true;
}
if (notify_observers)
theme_to_update_->NotifyObservers();
}
NativeTheme::ColorScheme NativeTheme::GetSystemColorScheme() const {
return ShouldUseDarkColors() ? ColorScheme::kDark : ColorScheme::kLight;
}
} // namespace ui