fix: propagate preferred color scheme to the renderer (#22900)

* fix: do not crash if the window is closed syncronously with a nativeTheme change

* fix: propogate preferred color scheme to the renderer and keep it up to date

Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com>
This commit is contained in:
trop[bot]
2020-03-31 14:46:15 -07:00
committed by GitHub
parent bfc069e5c0
commit 3f6227c61a
7 changed files with 56 additions and 17 deletions

View File

@@ -21,14 +21,16 @@ namespace electron {
namespace api {
NativeTheme::NativeTheme(v8::Isolate* isolate, ui::NativeTheme* theme)
: theme_(theme) {
theme_->AddObserver(this);
NativeTheme::NativeTheme(v8::Isolate* isolate,
ui::NativeTheme* ui_theme,
ui::NativeTheme* web_theme)
: ui_theme_(ui_theme), web_theme_(web_theme) {
ui_theme_->AddObserver(this);
Init(isolate);
}
NativeTheme::~NativeTheme() {
theme_->RemoveObserver(this);
ui_theme_->RemoveObserver(this);
}
void NativeTheme::OnNativeThemeUpdatedOnUI() {
@@ -42,7 +44,8 @@ void NativeTheme::OnNativeThemeUpdated(ui::NativeTheme* theme) {
}
void NativeTheme::SetThemeSource(ui::NativeTheme::ThemeSource override) {
theme_->set_theme_source(override);
ui_theme_->set_theme_source(override);
web_theme_->set_theme_source(override);
#if defined(OS_MACOSX)
// Update the macOS appearance setting for this new override value
UpdateMacOSAppearanceForOverrideValue(override);
@@ -52,15 +55,15 @@ void NativeTheme::SetThemeSource(ui::NativeTheme::ThemeSource override) {
}
ui::NativeTheme::ThemeSource NativeTheme::GetThemeSource() const {
return theme_->theme_source();
return ui_theme_->theme_source();
}
bool NativeTheme::ShouldUseDarkColors() {
return theme_->ShouldUseDarkColors();
return ui_theme_->ShouldUseDarkColors();
}
bool NativeTheme::ShouldUseHighContrastColors() {
return theme_->UsesHighContrastColors();
return ui_theme_->UsesHighContrastColors();
}
#if defined(OS_MACOSX)
@@ -85,8 +88,11 @@ bool NativeTheme::ShouldUseInvertedColorScheme() {
// static
v8::Local<v8::Value> NativeTheme::Create(v8::Isolate* isolate) {
ui::NativeTheme* theme = ui::NativeTheme::GetInstanceForNativeUi();
return gin::CreateHandle(isolate, new NativeTheme(isolate, theme)).ToV8();
ui::NativeTheme* ui_theme = ui::NativeTheme::GetInstanceForNativeUi();
ui::NativeTheme* web_theme = ui::NativeTheme::GetInstanceForWeb();
return gin::CreateHandle(isolate,
new NativeTheme(isolate, ui_theme, web_theme))
.ToV8();
}
// static

View File

@@ -22,7 +22,9 @@ class NativeTheme : public gin_helper::EventEmitter<NativeTheme>,
v8::Local<v8::FunctionTemplate> prototype);
protected:
NativeTheme(v8::Isolate* isolate, ui::NativeTheme* theme);
NativeTheme(v8::Isolate* isolate,
ui::NativeTheme* ui_theme,
ui::NativeTheme* web_theme);
~NativeTheme() override;
void SetThemeSource(ui::NativeTheme::ThemeSource override);
@@ -40,7 +42,8 @@ class NativeTheme : public gin_helper::EventEmitter<NativeTheme>,
void OnNativeThemeUpdatedOnUI();
private:
ui::NativeTheme* theme_;
ui::NativeTheme* ui_theme_;
ui::NativeTheme* web_theme_;
DISALLOW_COPY_AND_ASSIGN(NativeTheme);
};