chore: remove native_mate (Part 9) (#20645)

* refactor: remove a few uses of native_mate/gfx_converter.h

* refactor: deprecate mate::EventEmitter

* refactor: add gin_helper::EventEmitter

* refactor: convert a few classes to use gin_helper::EventEmitter

* refactor: get rid of native_mate_converters/gfx_converter.h

* fix: follow native_mate on reporting errors

* fix: gin is weak at guessing parameter types

* fix: incorrect full class name

* fix: gin::Handle does not accept null
This commit is contained in:
Cheng Zhao 2019-10-21 16:05:40 +09:00 committed by GitHub
parent b155ebeeb3
commit eb0e55c514
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
38 changed files with 532 additions and 326 deletions

View file

@ -7,18 +7,17 @@
#include <string>
#include "base/threading/thread_task_runner_handle.h"
#include "native_mate/constructor.h"
#include "native_mate/dictionary.h"
#include "shell/browser/api/atom_api_menu.h"
#include "shell/browser/browser.h"
#include "shell/common/api/atom_api_native_image.h"
#include "shell/common/native_mate_converters/gfx_converter.h"
#include "shell/common/native_mate_converters/image_converter.h"
#include "shell/common/native_mate_converters/string16_converter.h"
#include "shell/common/gin_converters/gfx_converter.h"
#include "shell/common/gin_converters/image_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/object_template_builder.h"
#include "shell/common/node_includes.h"
#include "ui/gfx/image/image.h"
namespace mate {
namespace gin {
template <>
struct Converter<electron::TrayIcon::IconType> {
@ -49,13 +48,13 @@ struct Converter<electron::TrayIcon::IconType> {
}
};
} // namespace mate
} // namespace gin
namespace electron {
namespace api {
Tray::Tray(mate::Handle<NativeImage> image, gin::Arguments* args)
Tray::Tray(gin::Handle<NativeImage> image, gin_helper::Arguments* args)
: tray_icon_(TrayIcon::Create()) {
SetImage(args->isolate(), image);
tray_icon_->AddObserver(this);
@ -67,8 +66,8 @@ Tray::~Tray() = default;
// static
mate::WrappableBase* Tray::New(gin_helper::ErrorThrower thrower,
mate::Handle<NativeImage> image,
gin::Arguments* args) {
gin::Handle<NativeImage> image,
gin_helper::Arguments* args) {
if (!Browser::Get()->is_ready()) {
thrower.ThrowError("Cannot create Tray before app is ready");
return nullptr;
@ -138,7 +137,7 @@ void Tray::OnDragEnded() {
Emit("drag-end");
}
void Tray::SetImage(v8::Isolate* isolate, mate::Handle<NativeImage> image) {
void Tray::SetImage(v8::Isolate* isolate, gin::Handle<NativeImage> image) {
#if defined(OS_WIN)
tray_icon_->SetImage(image->GetHICON(GetSystemMetrics(SM_CXSMICON)));
#else
@ -147,7 +146,7 @@ void Tray::SetImage(v8::Isolate* isolate, mate::Handle<NativeImage> image) {
}
void Tray::SetPressedImage(v8::Isolate* isolate,
mate::Handle<NativeImage> image) {
gin::Handle<NativeImage> image) {
#if defined(OS_WIN)
tray_icon_->SetPressedImage(image->GetHICON(GetSystemMetrics(SM_CXSMICON)));
#else
@ -187,17 +186,17 @@ bool Tray::GetIgnoreDoubleClickEvents() {
#endif
}
void Tray::DisplayBalloon(mate::Arguments* args,
const mate::Dictionary& options) {
void Tray::DisplayBalloon(gin_helper::ErrorThrower thrower,
const gin_helper::Dictionary& options) {
TrayIcon::BalloonOptions balloon_options;
if (!options.Get("title", &balloon_options.title) ||
!options.Get("content", &balloon_options.content)) {
args->ThrowError("'title' and 'content' must be defined");
thrower.ThrowError("'title' and 'content' must be defined");
return;
}
mate::Handle<NativeImage> icon;
gin::Handle<NativeImage> icon;
options.Get("icon", &icon);
options.Get("iconType", &balloon_options.icon_type);
options.Get("largeIcon", &balloon_options.large_icon);
@ -224,17 +223,26 @@ void Tray::Focus() {
tray_icon_->Focus();
}
void Tray::PopUpContextMenu(mate::Arguments* args) {
mate::Handle<Menu> menu;
void Tray::PopUpContextMenu(gin_helper::Arguments* args) {
gin::Handle<Menu> menu;
args->GetNext(&menu);
gfx::Point pos;
args->GetNext(&pos);
tray_icon_->PopUpContextMenu(pos, menu.IsEmpty() ? nullptr : menu->model());
}
void Tray::SetContextMenu(v8::Isolate* isolate, mate::Handle<Menu> menu) {
menu_.Reset(isolate, menu.ToV8());
tray_icon_->SetContextMenu(menu.IsEmpty() ? nullptr : menu->model());
void Tray::SetContextMenu(gin_helper::ErrorThrower thrower,
v8::Local<v8::Value> arg) {
gin::Handle<Menu> menu;
if (arg->IsNull()) {
menu_.Reset();
tray_icon_->SetContextMenu(nullptr);
} else if (gin::ConvertFromV8(thrower.isolate(), arg, &menu)) {
menu_.Reset(thrower.isolate(), menu.ToV8());
tray_icon_->SetContextMenu(menu->model());
} else {
thrower.ThrowTypeError("Must pass Menu or null");
}
}
gfx::Rect Tray::GetBounds() {
@ -244,9 +252,9 @@ gfx::Rect Tray::GetBounds() {
// static
void Tray::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "Tray"));
prototype->SetClassName(gin::StringToV8(isolate, "Tray"));
gin_helper::Destroyable::MakeDestroyable(isolate, prototype);
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("setImage", &Tray::SetImage)
.SetMethod("setPressedImage", &Tray::SetPressedImage)
.SetMethod("setToolTip", &Tray::SetToolTip)
@ -279,7 +287,7 @@ void Initialize(v8::Local<v8::Object> exports,
v8::Isolate* isolate = context->GetIsolate();
Tray::SetConstructor(isolate, base::BindRepeating(&Tray::New));
mate::Dictionary dict(isolate, exports);
gin_helper::Dictionary dict(isolate, exports);
dict.Set(
"Tray",
Tray::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());