diff --git a/lib/browser/api/native-theme.ts b/lib/browser/api/native-theme.ts index 732b3775ad3e..08fd92b209dc 100644 --- a/lib/browser/api/native-theme.ts +++ b/lib/browser/api/native-theme.ts @@ -1,8 +1,3 @@ -import { EventEmitter } from 'events'; - -const { NativeTheme, nativeTheme } = process._linkedBinding('electron_common_native_theme'); - -Object.setPrototypeOf(NativeTheme.prototype, EventEmitter.prototype); -EventEmitter.call(nativeTheme as any); +const { nativeTheme } = process._linkedBinding('electron_common_native_theme'); module.exports = nativeTheme; diff --git a/shell/browser/api/electron_api_native_theme.cc b/shell/browser/api/electron_api_native_theme.cc index 1792b040cf27..9a07118e4fe7 100644 --- a/shell/browser/api/electron_api_native_theme.cc +++ b/shell/browser/api/electron_api_native_theme.cc @@ -23,12 +23,13 @@ namespace electron { namespace api { +gin::WrapperInfo NativeTheme::kWrapperInfo = {gin::kEmbedderNativeGin}; + 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() { @@ -95,19 +96,17 @@ bool NativeTheme::ShouldUseInvertedColorScheme() { } // static -v8::Local NativeTheme::Create(v8::Isolate* isolate) { +gin::Handle NativeTheme::Create(v8::Isolate* isolate) { 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(); + new NativeTheme(isolate, ui_theme, web_theme)); } -// static -void NativeTheme::BuildPrototype(v8::Isolate* isolate, - v8::Local prototype) { - prototype->SetClassName(gin::StringToV8(isolate, "NativeTheme")); - gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) +gin::ObjectTemplateBuilder NativeTheme::GetObjectTemplateBuilder( + v8::Isolate* isolate) { + return gin_helper::EventEmitterMixin::GetObjectTemplateBuilder( + isolate) .SetProperty("shouldUseDarkColors", &NativeTheme::ShouldUseDarkColors) .SetProperty("themeSource", &NativeTheme::GetThemeSource, &NativeTheme::SetThemeSource) @@ -117,22 +116,25 @@ void NativeTheme::BuildPrototype(v8::Isolate* isolate, &NativeTheme::ShouldUseInvertedColorScheme); } +const char* NativeTheme::GetTypeName() { + return "NativeTheme"; +} + } // namespace api } // namespace electron namespace { +using electron::api::NativeTheme; + void Initialize(v8::Local exports, v8::Local unused, v8::Local context, void* priv) { v8::Isolate* isolate = context->GetIsolate(); gin::Dictionary dict(isolate, exports); - dict.Set("nativeTheme", electron::api::NativeTheme::Create(isolate)); - dict.Set("NativeTheme", electron::api::NativeTheme::GetConstructor(isolate) - ->GetFunction(context) - .ToLocalChecked()); + dict.Set("nativeTheme", NativeTheme::Create(isolate)); } } // namespace diff --git a/shell/browser/api/electron_api_native_theme.h b/shell/browser/api/electron_api_native_theme.h index ca9d30e2487a..30f5fa31a581 100644 --- a/shell/browser/api/electron_api_native_theme.h +++ b/shell/browser/api/electron_api_native_theme.h @@ -5,7 +5,9 @@ #ifndef SHELL_BROWSER_API_ELECTRON_API_NATIVE_THEME_H_ #define SHELL_BROWSER_API_ELECTRON_API_NATIVE_THEME_H_ -#include "shell/common/gin_helper/event_emitter.h" +#include "gin/handle.h" +#include "gin/wrappable.h" +#include "shell/browser/event_emitter_mixin.h" #include "ui/native_theme/native_theme.h" #include "ui/native_theme/native_theme_observer.h" @@ -13,13 +15,17 @@ namespace electron { namespace api { -class NativeTheme : public gin_helper::EventEmitter, +class NativeTheme : public gin::Wrappable, + public gin_helper::EventEmitterMixin, public ui::NativeThemeObserver { public: - static v8::Local Create(v8::Isolate* isolate); + static gin::Handle Create(v8::Isolate* isolate); - static void BuildPrototype(v8::Isolate* isolate, - v8::Local prototype); + // gin::Wrappable + static gin::WrapperInfo kWrapperInfo; + gin::ObjectTemplateBuilder GetObjectTemplateBuilder( + v8::Isolate* isolate) override; + const char* GetTypeName() override; protected: NativeTheme(v8::Isolate* isolate,