[email protected] | 990e622 | 2012-11-16 13:31:18 | [diff] [blame] | 1 | // 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 | |
varkha | ef6637f | 2016-02-26 22:12:27 | [diff] [blame] | 7 | #include <cstring> |
| 8 | |
Leonard Grey | fc562498 | 2019-05-01 16:50:14 | [diff] [blame] | 9 | #include "base/bind.h" |
Leonard Grey | e66472c | 2018-10-16 16:19:49 | [diff] [blame] | 10 | #include "base/command_line.h" |
| 11 | #include "ui/base/ui_base_switches.h" |
Leonard Grey | fc562498 | 2019-05-01 16:50:14 | [diff] [blame] | 12 | #include "ui/native_theme/dark_mode_observer.h" |
[email protected] | f58e31c5 | 2014-04-25 20:20:07 | [diff] [blame] | 13 | #include "ui/native_theme/native_theme_observer.h" |
| 14 | |
[email protected] | a673f35 | 2012-11-20 00:52:29 | [diff] [blame] | 15 | namespace ui { |
[email protected] | 990e622 | 2012-11-16 13:31:18 | [diff] [blame] | 16 | |
varkha | ef6637f | 2016-02-26 22:12:27 | [diff] [blame] | 17 | NativeTheme::ExtraParams::ExtraParams() { |
| 18 | memset(this, 0, sizeof(*this)); |
| 19 | } |
| 20 | |
vmpstr | bf0d713a | 2016-03-24 20:22:54 | [diff] [blame] | 21 | NativeTheme::ExtraParams::ExtraParams(const ExtraParams& other) { |
| 22 | memcpy(this, &other, sizeof(*this)); |
| 23 | } |
| 24 | |
[email protected] | f58e31c5 | 2014-04-25 20:20:07 | [diff] [blame] | 25 | void NativeTheme::AddObserver(NativeThemeObserver* observer) { |
| 26 | native_theme_observers_.AddObserver(observer); |
| 27 | } |
| 28 | |
| 29 | void NativeTheme::RemoveObserver(NativeThemeObserver* observer) { |
| 30 | native_theme_observers_.RemoveObserver(observer); |
| 31 | } |
| 32 | |
| 33 | void NativeTheme::NotifyObservers() { |
ericwilligers | 44bb427 | 2016-10-19 00:15:24 | [diff] [blame] | 34 | for (NativeThemeObserver& observer : native_theme_observers_) |
| 35 | observer.OnNativeThemeUpdated(this); |
[email protected] | f58e31c5 | 2014-04-25 20:20:07 | [diff] [blame] | 36 | } |
| 37 | |
[email protected] | a673f35 | 2012-11-20 00:52:29 | [diff] [blame] | 38 | NativeTheme::NativeTheme() |
Evan Stade | 89ccf1d | 2019-06-04 22:38:52 | [diff] [blame] | 39 | : is_dark_mode_(IsForcedDarkMode()), |
Leonard Grey | c79e032b0 | 2019-04-29 14:52:05 | [diff] [blame] | 40 | is_high_contrast_(IsForcedHighContrast()) {} |
[email protected] | a673f35 | 2012-11-20 00:52:29 | [diff] [blame] | 41 | |
Leonard Grey | fc562498 | 2019-05-01 16:50:14 | [diff] [blame] | 42 | NativeTheme::~NativeTheme() { |
| 43 | if (dark_mode_parent_observer_) |
| 44 | dark_mode_parent_observer_->Stop(); |
| 45 | } |
[email protected] | 7f98def | 2012-12-10 22:04:17 | [diff] [blame] | 46 | |
Leonard Grey | e66472c | 2018-10-16 16:19:49 | [diff] [blame] | 47 | bool NativeTheme::SystemDarkModeEnabled() const { |
Leonard Grey | c79e032b0 | 2019-04-29 14:52:05 | [diff] [blame] | 48 | return is_dark_mode_; |
| 49 | } |
| 50 | |
| 51 | bool NativeTheme::UsesHighContrastColors() const { |
| 52 | return is_high_contrast_; |
| 53 | } |
| 54 | |
| 55 | bool NativeTheme::IsForcedDarkMode() const { |
| 56 | static bool kIsForcedDarkMode = |
| 57 | base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 58 | switches::kForceDarkMode); |
| 59 | return kIsForcedDarkMode; |
| 60 | } |
| 61 | |
| 62 | bool NativeTheme::IsForcedHighContrast() const { |
| 63 | static bool kIsForcedHighContrast = |
| 64 | base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 65 | switches::kForceHighContrast); |
| 66 | return kIsForcedHighContrast; |
Leonard Grey | e66472c | 2018-10-16 16:19:49 | [diff] [blame] | 67 | } |
| 68 | |
Elly Fong-Jones | f9289739 | 2019-01-24 16:05:49 | [diff] [blame] | 69 | CaptionStyle NativeTheme::GetSystemCaptionStyle() const { |
Rahul Singh | 1862faa9 | 2019-03-06 22:41:43 | [diff] [blame] | 70 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 71 | ::switches::kForceCaptionStyle)) { |
| 72 | return CaptionStyle::FromSpec( |
| 73 | base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 74 | switches::kForceCaptionStyle)); |
| 75 | } |
Elly Fong-Jones | f9289739 | 2019-01-24 16:05:49 | [diff] [blame] | 76 | |
Rahul Singh | 1862faa9 | 2019-03-06 22:41:43 | [diff] [blame] | 77 | return CaptionStyle::FromSystemSettings(); |
Elly Fong-Jones | f9289739 | 2019-01-24 16:05:49 | [diff] [blame] | 78 | } |
| 79 | |
Leonard Grey | fc562498 | 2019-05-01 16:50:14 | [diff] [blame] | 80 | void 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 | |
| 88 | void NativeTheme::OnParentDarkModeChanged(bool is_dark_mode) { |
| 89 | set_dark_mode(is_dark_mode); |
| 90 | NotifyObservers(); |
| 91 | } |
[email protected] | 990e622 | 2012-11-16 13:31:18 | [diff] [blame] | 92 | } // namespace ui |