Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ginify NativeTheme #24673

Merged
merged 1 commit into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 1 addition & 6 deletions lib/browser/api/native-theme.ts
Original file line number Diff line number Diff line change
@@ -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;
28 changes: 15 additions & 13 deletions shell/browser/api/electron_api_native_theme.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
16 changes: 11 additions & 5 deletions shell/browser/api/electron_api_native_theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@
#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"

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,
Expand Down