electron/atom/browser/api/atom_api_tray.cc

125 lines
3.1 KiB
C++
Raw Normal View History

// Copyright (c) 2014 GitHub, Inc.
2014-05-30 15:57:54 +00:00
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/api/atom_api_tray.h"
#include <string>
2014-05-30 15:57:54 +00:00
#include "atom/browser/api/atom_api_menu.h"
#include "atom/browser/ui/tray_icon.h"
#include "atom/common/native_mate_converters/image_converter.h"
#include "native_mate/constructor.h"
#include "native_mate/dictionary.h"
#include "atom/common/node_includes.h"
namespace atom {
namespace api {
Tray::Tray(const gfx::ImageSkia& image)
: tray_icon_(TrayIcon::Create()) {
tray_icon_->SetImage(image);
tray_icon_->AddObserver(this);
2014-05-30 15:57:54 +00:00
}
Tray::~Tray() {
}
// static
mate::Wrappable* Tray::New(const gfx::ImageSkia& image) {
return new Tray(image);
}
2014-06-02 03:08:29 +00:00
void Tray::OnClicked() {
Emit("clicked");
}
void Tray::OnDoubleClicked() {
Emit("double-clicked");
}
2014-11-28 10:06:51 +00:00
void Tray::Destroy() {
tray_icon_.reset();
}
void Tray::SetImage(mate::Arguments* args, const gfx::ImageSkia& image) {
if (!CheckTrayLife(args))
return;
2014-05-30 15:57:54 +00:00
tray_icon_->SetImage(image);
}
2014-11-28 10:06:51 +00:00
void Tray::SetPressedImage(mate::Arguments* args, const gfx::ImageSkia& image) {
if (!CheckTrayLife(args))
return;
2014-05-30 15:57:54 +00:00
tray_icon_->SetPressedImage(image);
}
2014-11-28 10:06:51 +00:00
void Tray::SetToolTip(mate::Arguments* args, const std::string& tool_tip) {
if (!CheckTrayLife(args))
return;
2014-05-30 15:57:54 +00:00
tray_icon_->SetToolTip(tool_tip);
}
2014-11-28 10:06:51 +00:00
void Tray::SetTitle(mate::Arguments* args, const std::string& title) {
if (!CheckTrayLife(args))
return;
tray_icon_->SetTitle(title);
}
2014-11-28 10:06:51 +00:00
void Tray::SetHighlightMode(mate::Arguments* args, bool highlight) {
if (!CheckTrayLife(args))
return;
tray_icon_->SetHighlightMode(highlight);
}
2014-11-28 10:06:51 +00:00
void Tray::SetContextMenu(mate::Arguments* args, Menu* menu) {
if (!CheckTrayLife(args))
return;
2014-05-30 15:57:54 +00:00
tray_icon_->SetContextMenu(menu->model());
}
2014-11-28 10:06:51 +00:00
bool Tray::CheckTrayLife(mate::Arguments* args) {
if (!tray_icon_) {
args->ThrowError("Tray is already destroyed");
return false;
} else {
return true;
}
}
2014-05-30 15:57:54 +00:00
// static
void Tray::BuildPrototype(v8::Isolate* isolate,
v8::Handle<v8::ObjectTemplate> prototype) {
mate::ObjectTemplateBuilder(isolate, prototype)
2014-11-28 10:06:51 +00:00
.SetMethod("destroy", &Tray::Destroy)
2014-05-30 15:57:54 +00:00
.SetMethod("setImage", &Tray::SetImage)
.SetMethod("setPressedImage", &Tray::SetPressedImage)
.SetMethod("setToolTip", &Tray::SetToolTip)
.SetMethod("setTitle", &Tray::SetTitle)
.SetMethod("setHighlightMode", &Tray::SetHighlightMode)
2014-05-30 15:57:54 +00:00
.SetMethod("_setContextMenu", &Tray::SetContextMenu);
}
} // namespace api
} // namespace atom
namespace {
void Initialize(v8::Handle<v8::Object> exports, v8::Handle<v8::Value> unused,
v8::Handle<v8::Context> context, void* priv) {
2014-05-30 15:57:54 +00:00
using atom::api::Tray;
v8::Isolate* isolate = context->GetIsolate();
2014-05-30 15:57:54 +00:00
v8::Handle<v8::Function> constructor = mate::CreateConstructor<Tray>(
isolate, "Tray", base::Bind(&Tray::New));
mate::Dictionary dict(isolate, exports);
dict.Set("Tray", static_cast<v8::Handle<v8::Value>>(constructor));
}
} // namespace
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_tray, Initialize)