refactor: ginify NativeTheme (#24673)

This commit is contained in:
Jeremy Rose 2020-07-22 11:01:30 -07:00 committed by GitHub
parent 10bf50e1fd
commit 734753dd7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 24 deletions

View file

@ -1,8 +1,3 @@
import { EventEmitter } from 'events'; const { nativeTheme } = process._linkedBinding('electron_common_native_theme');
const { NativeTheme, nativeTheme } = process._linkedBinding('electron_common_native_theme');
Object.setPrototypeOf(NativeTheme.prototype, EventEmitter.prototype);
EventEmitter.call(nativeTheme as any);
module.exports = nativeTheme; module.exports = nativeTheme;

View file

@ -23,12 +23,13 @@ namespace electron {
namespace api { namespace api {
gin::WrapperInfo NativeTheme::kWrapperInfo = {gin::kEmbedderNativeGin};
NativeTheme::NativeTheme(v8::Isolate* isolate, NativeTheme::NativeTheme(v8::Isolate* isolate,
ui::NativeTheme* ui_theme, ui::NativeTheme* ui_theme,
ui::NativeTheme* web_theme) ui::NativeTheme* web_theme)
: ui_theme_(ui_theme), web_theme_(web_theme) { : ui_theme_(ui_theme), web_theme_(web_theme) {
ui_theme_->AddObserver(this); ui_theme_->AddObserver(this);
Init(isolate);
} }
NativeTheme::~NativeTheme() { NativeTheme::~NativeTheme() {
@ -95,19 +96,17 @@ bool NativeTheme::ShouldUseInvertedColorScheme() {
} }
// static // 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* ui_theme = ui::NativeTheme::GetInstanceForNativeUi();
ui::NativeTheme* web_theme = ui::NativeTheme::GetInstanceForWeb(); ui::NativeTheme* web_theme = ui::NativeTheme::GetInstanceForWeb();
return gin::CreateHandle(isolate, return gin::CreateHandle(isolate,
new NativeTheme(isolate, ui_theme, web_theme)) new NativeTheme(isolate, ui_theme, web_theme));
.ToV8();
} }
// static gin::ObjectTemplateBuilder NativeTheme::GetObjectTemplateBuilder(
void NativeTheme::BuildPrototype(v8::Isolate* isolate, v8::Isolate* isolate) {
v8::Local<v8::FunctionTemplate> prototype) { return gin_helper::EventEmitterMixin<NativeTheme>::GetObjectTemplateBuilder(
prototype->SetClassName(gin::StringToV8(isolate, "NativeTheme")); isolate)
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetProperty("shouldUseDarkColors", &NativeTheme::ShouldUseDarkColors) .SetProperty("shouldUseDarkColors", &NativeTheme::ShouldUseDarkColors)
.SetProperty("themeSource", &NativeTheme::GetThemeSource, .SetProperty("themeSource", &NativeTheme::GetThemeSource,
&NativeTheme::SetThemeSource) &NativeTheme::SetThemeSource)
@ -117,22 +116,25 @@ void NativeTheme::BuildPrototype(v8::Isolate* isolate,
&NativeTheme::ShouldUseInvertedColorScheme); &NativeTheme::ShouldUseInvertedColorScheme);
} }
const char* NativeTheme::GetTypeName() {
return "NativeTheme";
}
} // namespace api } // namespace api
} // namespace electron } // namespace electron
namespace { namespace {
using electron::api::NativeTheme;
void Initialize(v8::Local<v8::Object> exports, void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, v8::Local<v8::Context> context,
void* priv) { void* priv) {
v8::Isolate* isolate = context->GetIsolate(); v8::Isolate* isolate = context->GetIsolate();
gin::Dictionary dict(isolate, exports); gin::Dictionary dict(isolate, exports);
dict.Set("nativeTheme", electron::api::NativeTheme::Create(isolate)); dict.Set("nativeTheme", NativeTheme::Create(isolate));
dict.Set("NativeTheme", electron::api::NativeTheme::GetConstructor(isolate)
->GetFunction(context)
.ToLocalChecked());
} }
} // namespace } // namespace

View file

@ -5,7 +5,9 @@
#ifndef SHELL_BROWSER_API_ELECTRON_API_NATIVE_THEME_H_ #ifndef SHELL_BROWSER_API_ELECTRON_API_NATIVE_THEME_H_
#define 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.h"
#include "ui/native_theme/native_theme_observer.h" #include "ui/native_theme/native_theme_observer.h"
@ -13,13 +15,17 @@ namespace electron {
namespace api { namespace api {
class NativeTheme : public gin_helper::EventEmitter<NativeTheme>, class NativeTheme : public gin::Wrappable<NativeTheme>,
public gin_helper::EventEmitterMixin<NativeTheme>,
public ui::NativeThemeObserver { public ui::NativeThemeObserver {
public: public:
static v8::Local<v8::Value> Create(v8::Isolate* isolate); static gin::Handle<NativeTheme> Create(v8::Isolate* isolate);
static void BuildPrototype(v8::Isolate* isolate, // gin::Wrappable
v8::Local<v8::FunctionTemplate> prototype); static gin::WrapperInfo kWrapperInfo;
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
const char* GetTypeName() override;
protected: protected:
NativeTheme(v8::Isolate* isolate, NativeTheme(v8::Isolate* isolate,