-
Notifications
You must be signed in to change notification settings - Fork 15k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add nativeTheme.themeSource to allow apps to override Chromiums…
… theme choice (#19960) * feat: add nativeTheme.shouldUseDarkColorsOverride to allow apps to override Chromiums theme choice * spec: add tests for shouldUseDarkColorsOverride * chore: add missing forward declarations * refactor: rename overrideShouldUseDarkColors to themeSource * chore: only run appLevelAppearance specs on Mojave and up * chore: update patch with more info and no define * Update spec-main/api-native-theme-spec.ts Co-Authored-By: Jeremy Apthorp <jeremya@chromium.org> * Update api-native-theme-spec.ts * Update api-native-theme-spec.ts * Update api-native-theme-spec.ts
- Loading branch information
1 parent
1376229
commit 0d16be9
Showing
10 changed files
with
294 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
patches/chromium/feat_add_set_theme_source_to_allow_apps_to.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 | ||
From: Samuel Attard <sattard@slack-corp.com> | ||
Date: Mon, 26 Aug 2019 14:32:41 -0700 | ||
Subject: feat: add set_theme_source to allow apps to override chromiums | ||
internal theme choice | ||
|
||
This patch is required as Chromium doesn't currently let folks using | ||
//ui override the theme choice in NativeTheme. It defaults to | ||
respecting the OS theme choice and some apps don't always want to do | ||
that. With this patch we can override the theme value that Chromium | ||
uses internally for things like menus and devtools. | ||
|
||
We can remove this patch once it has in some shape been upstreamed. | ||
|
||
diff --git a/ui/native_theme/native_theme.cc b/ui/native_theme/native_theme.cc | ||
index 2370d15332c8c6c7dc7e3403b38891c885704d9f..171214379437f319d3feccc289a5d91e74b77f9e 100644 | ||
--- a/ui/native_theme/native_theme.cc | ||
+++ b/ui/native_theme/native_theme.cc | ||
@@ -40,6 +40,8 @@ NativeTheme::NativeTheme() | ||
NativeTheme::~NativeTheme() = default; | ||
|
||
bool NativeTheme::ShouldUseDarkColors() const { | ||
+ if (theme_source() == ThemeSource::kForcedLight) return false; | ||
+ if (theme_source() == ThemeSource::kForcedDark) return true; | ||
return should_use_dark_colors_; | ||
} | ||
|
||
diff --git a/ui/native_theme/native_theme.h b/ui/native_theme/native_theme.h | ||
index 70389e0245993faa2c17e9deefeb000280ef2368..cef1c0d4706e7e720a4004ca54765a39fc29c5e8 100644 | ||
--- a/ui/native_theme/native_theme.h | ||
+++ b/ui/native_theme/native_theme.h | ||
@@ -429,6 +429,22 @@ class NATIVE_THEME_EXPORT NativeTheme { | ||
ColorId color_id, | ||
ColorScheme color_scheme = ColorScheme::kDefault) const = 0; | ||
|
||
+ enum ThemeSource { | ||
+ kSystem, | ||
+ kForcedDark, | ||
+ kForcedLight, | ||
+ }; | ||
+ | ||
+ ThemeSource theme_source() const { | ||
+ return theme_source_; | ||
+ } | ||
+ | ||
+ void set_theme_source(ThemeSource theme_source) { | ||
+ bool original = ShouldUseDarkColors(); | ||
+ theme_source_ = theme_source; | ||
+ if (ShouldUseDarkColors() != original) NotifyObservers(); | ||
+ } | ||
+ | ||
// Returns a shared instance of the native theme that should be used for web | ||
// rendering. Do not use it in a normal application context (i.e. browser). | ||
// The returned object should not be deleted by the caller. This function is | ||
@@ -547,6 +563,8 @@ class NATIVE_THEME_EXPORT NativeTheme { | ||
PreferredColorScheme preferred_color_scheme_ = | ||
PreferredColorScheme::kNoPreference; | ||
|
||
+ ThemeSource theme_source_ = ThemeSource::kSystem; | ||
+ | ||
DISALLOW_COPY_AND_ASSIGN(NativeTheme); | ||
}; | ||
|
||
diff --git a/ui/native_theme/native_theme_dark_aura.cc b/ui/native_theme/native_theme_dark_aura.cc | ||
index a8fbfee3b13672902aac05fd5a65fa8ee81f9f7e..1be6369acf0b7c02a6f862636c2b2de1fbf8cb5a 100644 | ||
--- a/ui/native_theme/native_theme_dark_aura.cc | ||
+++ b/ui/native_theme/native_theme_dark_aura.cc | ||
@@ -20,6 +20,8 @@ SkColor NativeThemeDarkAura::GetSystemColor(ColorId color_id, | ||
} | ||
|
||
bool NativeThemeDarkAura::ShouldUseDarkColors() const { | ||
+ if (theme_source() == ThemeSource::kForcedLight) return false; | ||
+ if (theme_source() == ThemeSource::kForcedDark) return true; | ||
return true; | ||
} | ||
|
||
diff --git a/ui/native_theme/native_theme_win.cc b/ui/native_theme/native_theme_win.cc | ||
index 3003643bfb78cec2f5e84fc9e1471e1ef54aae41..06f2cbc84401958d49445f4ce6acb1b2fef0aa04 100644 | ||
--- a/ui/native_theme/native_theme_win.cc | ||
+++ b/ui/native_theme/native_theme_win.cc | ||
@@ -611,6 +611,8 @@ bool NativeThemeWin::ShouldUseDarkColors() const { | ||
// ...unless --force-dark-mode was specified in which case caveat emptor. | ||
if (UsesHighContrastColors() && !IsForcedDarkMode()) | ||
return false; | ||
+ if (theme_source() == ThemeSource::kForcedLight) return false; | ||
+ if (theme_source() == ThemeSource::kForcedDark) return true; | ||
return NativeTheme::ShouldUseDarkColors(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) 2019 Slack Technologies, Inc. | ||
// Use of this source code is governed by the MIT license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "shell/common/api/atom_api_native_theme.h" | ||
|
||
#include "base/mac/sdk_forward_declarations.h" | ||
#include "shell/browser/mac/atom_application.h" | ||
|
||
namespace electron { | ||
|
||
namespace api { | ||
|
||
void NativeTheme::UpdateMacOSAppearanceForOverrideValue( | ||
ui::NativeTheme::ThemeSource override) { | ||
if (@available(macOS 10.14, *)) { | ||
NSAppearance* new_appearance; | ||
switch (override) { | ||
case ui::NativeTheme::ThemeSource::kForcedDark: | ||
new_appearance = | ||
[NSAppearance appearanceNamed:NSAppearanceNameDarkAqua]; | ||
break; | ||
case ui::NativeTheme::ThemeSource::kForcedLight: | ||
new_appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua]; | ||
break; | ||
case ui::NativeTheme::ThemeSource::kSystem: | ||
default: | ||
new_appearance = nil; | ||
break; | ||
} | ||
[[NSApplication sharedApplication] setAppearance:new_appearance]; | ||
} | ||
} | ||
|
||
} // namespace api | ||
|
||
} // namespace electron |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters