From 5973aa36ad69b2b950f89526e80e8a8bd878dd73 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 2 Feb 2017 09:34:20 -0800 Subject: [PATCH] Initialize color listener after app is ready --- atom/browser/api/atom_api_system_preferences.cc | 9 ++++----- atom/browser/api/atom_api_system_preferences.h | 8 +++++++- atom/browser/api/atom_api_system_preferences_win.cc | 13 +++++++++++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/atom/browser/api/atom_api_system_preferences.cc b/atom/browser/api/atom_api_system_preferences.cc index ad35d548d9e..8426c4dbbdf 100644 --- a/atom/browser/api/atom_api_system_preferences.cc +++ b/atom/browser/api/atom_api_system_preferences.cc @@ -14,11 +14,7 @@ namespace atom { namespace api { -SystemPreferences::SystemPreferences(v8::Isolate* isolate) -#if defined(OS_WIN) - : color_change_listener_(this) -#endif - { +SystemPreferences::SystemPreferences(v8::Isolate* isolate) { Init(isolate); #if defined(OS_WIN) InitializeWindow(); @@ -26,6 +22,9 @@ SystemPreferences::SystemPreferences(v8::Isolate* isolate) } SystemPreferences::~SystemPreferences() { +#if defined(OS_WIN) + Browser::Get()->RemoveObserver(this); +#endif } #if !defined(OS_MACOSX) diff --git a/atom/browser/api/atom_api_system_preferences.h b/atom/browser/api/atom_api_system_preferences.h index aa7f946eb81..895974e9aef 100644 --- a/atom/browser/api/atom_api_system_preferences.h +++ b/atom/browser/api/atom_api_system_preferences.h @@ -13,6 +13,8 @@ #include "native_mate/handle.h" #if defined(OS_WIN) +#include "atom/browser/browser.h" +#include "atom/browser/browser_observer.h" #include "ui/gfx/sys_color_change_listener.h" #endif @@ -26,6 +28,7 @@ namespace api { class SystemPreferences : public mate::EventEmitter #if defined(OS_WIN) + , public BrowserObserver , public gfx::SysColorChangeListener #endif { @@ -51,6 +54,9 @@ class SystemPreferences : public mate::EventEmitter // gfx::SysColorChangeListener: void OnSysColorChange() override; + // BrowserObserver: + void OnFinishLaunching(const base::DictionaryValue& launch_info) override; + #elif defined(OS_MACOSX) using NotificationCallback = base::Callback< void(const std::string&, const base::DictionaryValue&)>; @@ -111,7 +117,7 @@ class SystemPreferences : public mate::EventEmitter bool invertered_color_scheme_; - gfx::ScopedSysColorChangeListener color_change_listener_; + std::unique_ptr color_change_listener_; #endif DISALLOW_COPY_AND_ASSIGN(SystemPreferences); }; diff --git a/atom/browser/api/atom_api_system_preferences_win.cc b/atom/browser/api/atom_api_system_preferences_win.cc index 6156104665f..43a2f2fc944 100644 --- a/atom/browser/api/atom_api_system_preferences_win.cc +++ b/atom/browser/api/atom_api_system_preferences_win.cc @@ -117,6 +117,14 @@ std::string SystemPreferences::GetColor(const std::string& color, void SystemPreferences::InitializeWindow() { invertered_color_scheme_ = IsInvertedColorScheme(); + // Wait until app is ready before creating sys color listener + // Creating this listener before the app is ready causes global shortcuts + // to not fire + if (Browser::Get()->is_ready()) + color_change_listener_.reset(new gfx::ScopedSysColorChangeListener(this)); + else + Browser::Get()->AddObserver(this); + WNDCLASSEX window_class; base::win::InitializeWindowClass( kSystemPreferencesWindowClass, @@ -172,6 +180,11 @@ void SystemPreferences::OnSysColorChange() { Emit("color-changed"); } +void SystemPreferences::OnFinishLaunching( + const base::DictionaryValue& launch_info) { + color_change_listener_.reset(new gfx::ScopedSysColorChangeListener(this)); +} + } // namespace api } // namespace atom