File: theme_service_aura_linux.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (179 lines) | stat: -rw-r--r-- 5,897 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/themes/theme_service_aura_linux.h"

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/themes/custom_theme_supplier.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "ui/color/system_theme.h"
#include "ui/gfx/image/image.h"
#include "ui/linux/linux_ui.h"
#include "ui/linux/linux_ui_factory.h"
#include "ui/native_theme/native_theme_aura.h"

namespace {

ui::SystemTheme ValidateSystemTheme(ui::SystemTheme system_theme) {
  switch (system_theme) {
    case ui::SystemTheme::kDefault:
#if BUILDFLAG(IS_LINUX)
    case ui::SystemTheme::kGtk:
    case ui::SystemTheme::kQt:
#endif
      return system_theme;
    default:
      return ui::SystemTheme::kDefault;
  }
}

class SystemThemeLinux : public CustomThemeSupplier {
 public:
  SystemThemeLinux(PrefService* pref_service, ui::LinuxUiTheme* linux_ui_theme);

  SystemThemeLinux(const SystemThemeLinux&) = delete;
  SystemThemeLinux& operator=(const SystemThemeLinux&) = delete;

  // Overridden from CustomThemeSupplier:
  void StartUsingTheme() override;
  void StopUsingTheme() override;
  bool GetColor(int id, SkColor* color) const override;
  bool GetDisplayProperty(int id, int* result) const override;
  gfx::Image GetImageNamed(int id) const override;
  bool HasCustomImage(int id) const override;
  ui::NativeTheme* GetNativeTheme() const override;

 private:
  ~SystemThemeLinux() override;

  // These pointers are not owned by us.
  const raw_ptr<PrefService, DanglingUntriaged> pref_service_;
  const raw_ptr<ui::LinuxUiTheme> linux_ui_theme_;
};

SystemThemeLinux::SystemThemeLinux(PrefService* pref_service,
                                   ui::LinuxUiTheme* linux_ui_theme)
    : CustomThemeSupplier(ThemeType::kNativeX11),
      pref_service_(pref_service),
      linux_ui_theme_(linux_ui_theme) {}

void SystemThemeLinux::StartUsingTheme() {
  pref_service_->SetInteger(
      prefs::kSystemTheme,
      static_cast<int>(linux_ui_theme_->GetNativeTheme()->system_theme()));
  // Have the former theme notify its observers of change.
  ui::NativeTheme::GetInstanceForNativeUi()->NotifyOnNativeThemeUpdated();
}

void SystemThemeLinux::StopUsingTheme() {
  pref_service_->SetInteger(prefs::kSystemTheme,
                            static_cast<int>(ui::SystemTheme::kDefault));
  // Have the former theme notify its observers of change.
  linux_ui_theme_->GetNativeTheme()->NotifyOnNativeThemeUpdated();
}

bool SystemThemeLinux::GetColor(int id, SkColor* color) const {
  return linux_ui_theme_->GetColor(
      id, color, pref_service_->GetBoolean(prefs::kUseCustomChromeFrame));
}

bool SystemThemeLinux::GetDisplayProperty(int id, int* result) const {
  return linux_ui_theme_->GetDisplayProperty(id, result);
}

gfx::Image SystemThemeLinux::GetImageNamed(int id) const {
  return gfx::Image();
}

bool SystemThemeLinux::HasCustomImage(int id) const {
  return false;
}

ui::NativeTheme* SystemThemeLinux::GetNativeTheme() const {
  return linux_ui_theme_->GetNativeTheme();
}

SystemThemeLinux::~SystemThemeLinux() = default;

}  // namespace

ThemeServiceAuraLinux::~ThemeServiceAuraLinux() = default;

ui::SystemTheme ThemeServiceAuraLinux::GetDefaultSystemTheme() const {
  return GetSystemThemeForProfile(profile());
}

void ThemeServiceAuraLinux::UseTheme(ui::SystemTheme system_theme) {
  if (system_theme == ui::SystemTheme::kDefault) {
    UseDefaultTheme();
  } else if (auto* linux_ui_theme = ui::GetLinuxUiTheme(system_theme)) {
    SetCustomDefaultTheme(base::MakeRefCounted<SystemThemeLinux>(
        profile()->GetPrefs(), linux_ui_theme));
  } else {
    return;
  }
}

void ThemeServiceAuraLinux::UseSystemTheme() {
  if (UsingSystemTheme()) {
    return;
  }
  if (auto* linux_ui_theme = ui::GetDefaultLinuxUiTheme()) {
    if (auto* native_theme = linux_ui_theme->GetNativeTheme()) {
      UseTheme(native_theme->system_theme());
      return;
    }
  }
  ThemeService::UseSystemTheme();
}

bool ThemeServiceAuraLinux::IsSystemThemeDistinctFromDefaultTheme() const {
  return true;
}

bool ThemeServiceAuraLinux::UsingSystemTheme() const {
  return GetThemeSupplier() &&
         GetThemeSupplier()->get_theme_type() ==
             ui::ColorProviderKey::ThemeInitializerSupplier::ThemeType::
                 kNativeX11;
}

void ThemeServiceAuraLinux::FixInconsistentPreferencesIfNeeded() {
  PrefService* prefs = profile()->GetPrefs();

  // When using the system theme, the theme ID should match the default. Give
  // precedence to the non-default theme specified.
  if (GetThemeID() != ThemeHelper::kDefaultThemeID &&
      prefs->GetInteger(prefs::kSystemTheme) !=
          static_cast<int>(ui::SystemTheme::kDefault)) {
    prefs->SetInteger(prefs::kSystemTheme,
                      static_cast<int>(ui::SystemTheme::kDefault));
  }
}

ThemeService::BrowserColorScheme ThemeServiceAuraLinux::GetBrowserColorScheme()
    const {
  // If using the system theme (GTK or QT), always use the system color scheme
  // as well.  This prevents eg. setting the color scheme to light when the
  // system theme is dark, which may lead to white text on white backgrounds.
  if (UsingSystemTheme()) {
    return ThemeService::BrowserColorScheme::kSystem;
  }
  return ThemeService::GetBrowserColorScheme();
}

// static
ui::SystemTheme ThemeServiceAuraLinux::GetSystemThemeForProfile(
    const Profile* profile) {
  if (!profile || profile->IsChild()) {
    return ui::SystemTheme::kDefault;
  }
  return ValidateSystemTheme(static_cast<ui::SystemTheme>(
      profile->GetPrefs()->GetInteger(prefs::kSystemTheme)));
}