[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 | |||||
Findit | 72e2f8a | 2019-06-29 00:07:58 | [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" | ||||
Findit | 72e2f8a | 2019-06-29 00:07:58 | [diff] [blame] | 12 | #include "ui/native_theme/dark_mode_observer.h" |
13 | #include "ui/native_theme/native_theme_observer.h" | ||||
[email protected] | f58e31c5 | 2014-04-25 20:20:07 | [diff] [blame] | 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 | |
Findit | 72e2f8a | 2019-06-29 00:07:58 | [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 | |||||
Leonard Grey | 5be670c | 2019-06-13 17:56:29 | [diff] [blame] | 51 | bool NativeTheme::SystemDarkModeSupported() const { |
52 | return false; | ||||
53 | } | ||||
54 | |||||
Leonard Grey | c79e032b0 | 2019-04-29 14:52:05 | [diff] [blame] | 55 | bool NativeTheme::UsesHighContrastColors() const { |
56 | return is_high_contrast_; | ||||
57 | } | ||||
58 | |||||
59 | bool NativeTheme::IsForcedDarkMode() const { | ||||
60 | static bool kIsForcedDarkMode = | ||||
61 | base::CommandLine::ForCurrentProcess()->HasSwitch( | ||||
62 | switches::kForceDarkMode); | ||||
63 | return kIsForcedDarkMode; | ||||
64 | } | ||||
65 | |||||
66 | bool NativeTheme::IsForcedHighContrast() const { | ||||
67 | static bool kIsForcedHighContrast = | ||||
68 | base::CommandLine::ForCurrentProcess()->HasSwitch( | ||||
69 | switches::kForceHighContrast); | ||||
70 | return kIsForcedHighContrast; | ||||
Leonard Grey | e66472c | 2018-10-16 16:19:49 | [diff] [blame] | 71 | } |
72 | |||||
Evan Liu | 7cdbf887 | 2019-06-19 16:47:21 | [diff] [blame] | 73 | base::Optional<CaptionStyle> NativeTheme::GetSystemCaptionStyle() const { |
Rahul Singh | 1862faa9 | 2019-03-06 22:41:43 | [diff] [blame] | 74 | return CaptionStyle::FromSystemSettings(); |
Elly Fong-Jones | f9289739 | 2019-01-24 16:05:49 | [diff] [blame] | 75 | } |
76 | |||||
Findit | 72e2f8a | 2019-06-29 00:07:58 | [diff] [blame] | 77 | void NativeTheme::SetDarkModeParent(NativeTheme* dark_mode_parent) { |
78 | dark_mode_parent_observer_ = std::make_unique<DarkModeObserver>( | ||||
79 | dark_mode_parent, | ||||
80 | base::BindRepeating(&NativeTheme::OnParentDarkModeChanged, | ||||
81 | base::Unretained(this))); | ||||
82 | dark_mode_parent_observer_->Start(); | ||||
83 | } | ||||
Leonard Grey | fc562498 | 2019-05-01 16:50:14 | [diff] [blame] | 84 | |
Findit | 72e2f8a | 2019-06-29 00:07:58 | [diff] [blame] | 85 | void NativeTheme::OnParentDarkModeChanged(bool is_dark_mode) { |
86 | set_dark_mode(is_dark_mode); | ||||
87 | NotifyObservers(); | ||||
88 | } | ||||
[email protected] | 990e622 | 2012-11-16 13:31:18 | [diff] [blame] | 89 | } // namespace ui |