Skip to content

Commit

Permalink
Uplift of #21065 (squashed) to beta
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-builds committed Nov 23, 2023
1 parent d4d1c76 commit 3e9ea28
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 17 deletions.
4 changes: 4 additions & 0 deletions browser/themes/brave_dark_mode_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ bool SystemDarkModeEnabled() {
if (g_is_test_)
return g_system_dark_mode_enabled_in_test_;

#if BUILDFLAG(IS_LINUX)
return HasCachedSystemDarkModeType();
#else
return ui::NativeTheme::GetInstanceForNativeUi()->SystemDarkModeSupported();
#endif
}

void SetUseSystemDarkModeEnabledForTest(bool enabled) {
Expand Down
7 changes: 7 additions & 0 deletions browser/themes/brave_dark_mode_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ void SetUseSystemDarkModeEnabledForTest(bool enabled);
// By overriding, base ui components also use same brave theme type.
void SetSystemDarkMode(BraveDarkModeType type);

#if BUILDFLAG(IS_LINUX)
// Cache system preference from DarkModeManagerLinux.
// This cached value is used whenever user chooses "Same as Linux" option.
void CacheSystemDarkModePrefs(bool prefer_dark_theme);
bool HasCachedSystemDarkModeType();
#endif

} // namespace dark_mode

#endif // BRAVE_BROWSER_THEMES_BRAVE_DARK_MODE_UTILS_H_
22 changes: 18 additions & 4 deletions browser/themes/brave_dark_mode_utils_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,31 @@

#include "brave/browser/themes/brave_dark_mode_utils.h"

#include "base/notreached.h"
#include "brave/browser/themes/brave_dark_mode_utils_internal.h"
#include "ui/native_theme/native_theme.h"

namespace dark_mode {

namespace {
absl::optional<bool> g_system_dark_mode_prefs;
} // namespace

void CacheSystemDarkModePrefs(bool prefer_dark_theme) {
g_system_dark_mode_prefs = prefer_dark_theme;
}

bool HasCachedSystemDarkModeType() {
return g_system_dark_mode_prefs.has_value();
}

void SetSystemDarkMode(BraveDarkModeType type) {
if (type == BraveDarkModeType::BRAVE_DARK_MODE_TYPE_DEFAULT) {
// Linux doesn't support system dark theme so there is no chance to set
// default type. Default is used for 'Same as Windows/MacOS'.
NOTREACHED();
if (g_system_dark_mode_prefs.has_value()) {
internal::SetSystemDarkModeForNonDefaultMode(*g_system_dark_mode_prefs);
}
return;
}

internal::SetSystemDarkModeForNonDefaultMode(
type == BraveDarkModeType::BRAVE_DARK_MODE_TYPE_DARK);
}
Expand Down
8 changes: 8 additions & 0 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,14 @@ source_set("ui") {
]
}

if (is_linux && use_dbus) {
sources += [
"views/brave_dark_mode_manager_linux.cc",
"views/brave_dark_mode_manager_linux.h",
]
deps += [ "//ui/linux:linux_ui_factory" ]
}

if (enable_sparkle) {
sources += [
"views/update_recommended_message_box_mac.h",
Expand Down
38 changes: 38 additions & 0 deletions browser/ui/views/brave_dark_mode_manager_linux.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* Copyright (c) 2023 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "brave/browser/ui/views/brave_dark_mode_manager_linux.h"

#include "brave/browser/themes/brave_dark_mode_utils.h"
#include "ui/linux/linux_ui_factory.h"

namespace ui {

BraveDarkModeManagerLinux::BraveDarkModeManagerLinux()
: DarkModeManagerLinux() {
// In base class' ctor, |prefer_dark_theme_| is set by calling
// SetColorScheme() when ui::GetDefaultLinuxUiTheme()
if (ui::GetDefaultLinuxUiTheme()) {
dark_mode::CacheSystemDarkModePrefs(prefer_dark_theme_);
}
}

BraveDarkModeManagerLinux::~BraveDarkModeManagerLinux() = default;

void BraveDarkModeManagerLinux::SetColorScheme(bool prefer_dark_theme,
bool from_toolkit_theme) {
dark_mode::CacheSystemDarkModePrefs(prefer_dark_theme);
if (dark_mode::GetBraveDarkModeType() ==
dark_mode::BraveDarkModeType::BRAVE_DARK_MODE_TYPE_DEFAULT) {
DarkModeManagerLinux::SetColorScheme(prefer_dark_theme, from_toolkit_theme);
} else {
// Make |prefer_dark_theme_| stores latest system theme even brave theme(
// dark or light) is set. If not, system theme change could not be applied
// properly later.
prefer_dark_theme_ = prefer_dark_theme;
}
}

} // namespace ui
25 changes: 25 additions & 0 deletions browser/ui/views/brave_dark_mode_manager_linux.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Copyright (c) 2023 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_UI_VIEWS_BRAVE_DARK_MODE_MANAGER_LINUX_H_
#define BRAVE_BROWSER_UI_VIEWS_BRAVE_DARK_MODE_MANAGER_LINUX_H_

#include "chrome/browser/ui/views/dark_mode_manager_linux.h"

namespace ui {

class BraveDarkModeManagerLinux : public DarkModeManagerLinux {
public:
BraveDarkModeManagerLinux();
~BraveDarkModeManagerLinux() override;

private:
// DarkModeManagerLinux overrides:
void SetColorScheme(bool prefer_dark_theme, bool from_toolkit_theme) override;
};

} // namespace ui

#endif // BRAVE_BROWSER_UI_VIEWS_BRAVE_DARK_MODE_MANAGER_LINUX_H_
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,11 @@
#include "chrome/browser/ui/views/chrome_browser_main_extra_parts_views_linux.h"

#if defined(USE_DBUS)
#include "brave/browser/ui/views/brave_dark_mode_manager_linux.h"
#include "chrome/browser/ui/views/dark_mode_manager_linux.h"
#endif

#if defined(USE_DBUS)
namespace ui {

class BraveDarkModeManagerLinux {
public:
BraveDarkModeManagerLinux() = default;
~BraveDarkModeManagerLinux() = default;
BraveDarkModeManagerLinux(const BraveDarkModeManagerLinux&) = delete;
BraveDarkModeManagerLinux& operator=(const BraveDarkModeManagerLinux&) =
delete;
};

} // namespace ui

#define DarkModeManagerLinux BraveDarkModeManagerLinux
#endif

Expand Down
18 changes: 18 additions & 0 deletions chromium_src/chrome/browser/ui/views/dark_mode_manager_linux.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Copyright (c) 2023 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_VIEWS_DARK_MODE_MANAGER_LINUX_H_
#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_VIEWS_DARK_MODE_MANAGER_LINUX_H_

#define SetColorScheme \
SetColorScheme_UnUsed() {} \
friend class BraveDarkModeManagerLinux; \
virtual void SetColorScheme

#include "src/chrome/browser/ui/views/dark_mode_manager_linux.h" // IWYU pragma: export

#undef SetColorScheme

#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_VIEWS_DARK_MODE_MANAGER_LINUX_H_

0 comments on commit 3e9ea28

Please sign in to comment.