Browse Source

refactor: ginify NativeTheme (#24673)

Jeremy Rose 4 years ago
parent
commit
734753dd7a

+ 1 - 6
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;

+ 15 - 13
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<v8::Value> NativeTheme::Create(v8::Isolate* isolate) {
+gin::Handle<NativeTheme> 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<v8::FunctionTemplate> prototype) {
-  prototype->SetClassName(gin::StringToV8(isolate, "NativeTheme"));
-  gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
+gin::ObjectTemplateBuilder NativeTheme::GetObjectTemplateBuilder(
+    v8::Isolate* isolate) {
+  return gin_helper::EventEmitterMixin<NativeTheme>::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<v8::Object> exports,
                 v8::Local<v8::Value> unused,
                 v8::Local<v8::Context> 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

+ 11 - 5
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<NativeTheme>,
+class NativeTheme : public gin::Wrappable<NativeTheme>,
+                    public gin_helper::EventEmitterMixin<NativeTheme>,
                     public ui::NativeThemeObserver {
  public:
-  static v8::Local<v8::Value> Create(v8::Isolate* isolate);
+  static gin::Handle<NativeTheme> Create(v8::Isolate* isolate);
 
-  static void BuildPrototype(v8::Isolate* isolate,
-                             v8::Local<v8::FunctionTemplate> prototype);
+  // gin::Wrappable
+  static gin::WrapperInfo kWrapperInfo;
+  gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
+      v8::Isolate* isolate) override;
+  const char* GetTypeName() override;
 
  protected:
   NativeTheme(v8::Isolate* isolate,