blob: 44e4aca403fad58c67b31272d32bc4a0fa8126f1 [file] [log] [blame]
[email protected]990e6222012-11-16 13:31:181// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ui/native_theme/native_theme.h"
6
varkhaef6637f2016-02-26 22:12:277#include <cstring>
8
Leonard Greyfc5624982019-05-01 16:50:149#include "base/bind.h"
Leonard Greye66472c2018-10-16 16:19:4910#include "base/command_line.h"
11#include "ui/base/ui_base_switches.h"
Leonard Greyfc5624982019-05-01 16:50:1412#include "ui/native_theme/dark_mode_observer.h"
[email protected]f58e31c52014-04-25 20:20:0713#include "ui/native_theme/native_theme_observer.h"
14
[email protected]a673f352012-11-20 00:52:2915namespace ui {
[email protected]990e6222012-11-16 13:31:1816
varkhaef6637f2016-02-26 22:12:2717NativeTheme::ExtraParams::ExtraParams() {
18 memset(this, 0, sizeof(*this));
19}
20
vmpstrbf0d713a2016-03-24 20:22:5421NativeTheme::ExtraParams::ExtraParams(const ExtraParams& other) {
22 memcpy(this, &other, sizeof(*this));
23}
24
[email protected]f58e31c52014-04-25 20:20:0725void NativeTheme::AddObserver(NativeThemeObserver* observer) {
26 native_theme_observers_.AddObserver(observer);
27}
28
29void NativeTheme::RemoveObserver(NativeThemeObserver* observer) {
30 native_theme_observers_.RemoveObserver(observer);
31}
32
33void NativeTheme::NotifyObservers() {
ericwilligers44bb4272016-10-19 00:15:2434 for (NativeThemeObserver& observer : native_theme_observers_)
35 observer.OnNativeThemeUpdated(this);
[email protected]f58e31c52014-04-25 20:20:0736}
37
[email protected]a673f352012-11-20 00:52:2938NativeTheme::NativeTheme()
Evan Stade89ccf1d2019-06-04 22:38:5239 : is_dark_mode_(IsForcedDarkMode()),
Leonard Greyc79e032b02019-04-29 14:52:0540 is_high_contrast_(IsForcedHighContrast()) {}
[email protected]a673f352012-11-20 00:52:2941
Leonard Greyfc5624982019-05-01 16:50:1442NativeTheme::~NativeTheme() {
43 if (dark_mode_parent_observer_)
44 dark_mode_parent_observer_->Stop();
45}
[email protected]7f98def2012-12-10 22:04:1746
Leonard Greye66472c2018-10-16 16:19:4947bool NativeTheme::SystemDarkModeEnabled() const {
Leonard Greyc79e032b02019-04-29 14:52:0548 return is_dark_mode_;
49}
50
51bool NativeTheme::UsesHighContrastColors() const {
52 return is_high_contrast_;
53}
54
55bool NativeTheme::IsForcedDarkMode() const {
56 static bool kIsForcedDarkMode =
57 base::CommandLine::ForCurrentProcess()->HasSwitch(
58 switches::kForceDarkMode);
59 return kIsForcedDarkMode;
60}
61
62bool NativeTheme::IsForcedHighContrast() const {
63 static bool kIsForcedHighContrast =
64 base::CommandLine::ForCurrentProcess()->HasSwitch(
65 switches::kForceHighContrast);
66 return kIsForcedHighContrast;
Leonard Greye66472c2018-10-16 16:19:4967}
68
Elly Fong-Jonesf92897392019-01-24 16:05:4969CaptionStyle NativeTheme::GetSystemCaptionStyle() const {
Rahul Singh1862faa92019-03-06 22:41:4370 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
71 ::switches::kForceCaptionStyle)) {
72 return CaptionStyle::FromSpec(
73 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
74 switches::kForceCaptionStyle));
75 }
Elly Fong-Jonesf92897392019-01-24 16:05:4976
Rahul Singh1862faa92019-03-06 22:41:4377 return CaptionStyle::FromSystemSettings();
Elly Fong-Jonesf92897392019-01-24 16:05:4978}
79
Leonard Greyfc5624982019-05-01 16:50:1480void NativeTheme::SetDarkModeParent(NativeTheme* dark_mode_parent) {
81 dark_mode_parent_observer_ = std::make_unique<DarkModeObserver>(
82 dark_mode_parent,
83 base::BindRepeating(&NativeTheme::OnParentDarkModeChanged,
84 base::Unretained(this)));
85 dark_mode_parent_observer_->Start();
86}
87
88void NativeTheme::OnParentDarkModeChanged(bool is_dark_mode) {
89 set_dark_mode(is_dark_mode);
90 NotifyObservers();
91}
[email protected]990e6222012-11-16 13:31:1892} // namespace ui