2014-10-31 18:17:05 +00:00
|
|
|
// 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"
|
|
|
|
|
2014-06-01 02:20:06 +00:00
|
|
|
#include <string>
|
|
|
|
|
2014-05-30 15:57:54 +00:00
|
|
|
#include "atom/browser/api/atom_api_menu.h"
|
2015-03-25 14:40:01 +00:00
|
|
|
#include "atom/browser/browser.h"
|
2014-05-30 15:57:54 +00:00
|
|
|
#include "atom/browser/ui/tray_icon.h"
|
2015-05-01 09:54:22 +00:00
|
|
|
#include "atom/common/native_mate_converters/gfx_converter.h"
|
2014-05-30 15:57:54 +00:00
|
|
|
#include "atom/common/native_mate_converters/image_converter.h"
|
2014-11-28 10:39:30 +00:00
|
|
|
#include "atom/common/native_mate_converters/string16_converter.h"
|
2015-09-07 08:29:54 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
2014-05-30 15:57:54 +00:00
|
|
|
#include "native_mate/constructor.h"
|
|
|
|
#include "native_mate/dictionary.h"
|
2015-07-29 06:25:12 +00:00
|
|
|
#include "ui/events/event_constants.h"
|
2015-01-03 02:43:56 +00:00
|
|
|
#include "ui/gfx/image/image.h"
|
2014-05-30 15:57:54 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2015-01-03 02:43:56 +00:00
|
|
|
Tray::Tray(const gfx::Image& image)
|
2014-05-30 15:57:54 +00:00
|
|
|
: tray_icon_(TrayIcon::Create()) {
|
|
|
|
tray_icon_->SetImage(image);
|
2014-06-02 03:28:23 +00:00
|
|
|
tray_icon_->AddObserver(this);
|
2014-05-30 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Tray::~Tray() {
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2015-06-10 06:21:09 +00:00
|
|
|
mate::Wrappable* Tray::New(v8::Isolate* isolate, const gfx::Image& image) {
|
2015-03-25 14:40:01 +00:00
|
|
|
if (!Browser::Get()->is_ready()) {
|
2015-09-07 08:12:31 +00:00
|
|
|
isolate->ThrowException(v8::Exception::Error(mate::StringToV8(
|
|
|
|
isolate, "Cannot create Tray before app is ready")));
|
2015-03-25 14:40:01 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2014-05-30 15:57:54 +00:00
|
|
|
return new Tray(image);
|
|
|
|
}
|
|
|
|
|
2015-07-27 10:15:51 +00:00
|
|
|
void Tray::OnClicked(const gfx::Rect& bounds, int modifiers) {
|
2015-07-29 06:25:12 +00:00
|
|
|
v8::Locker locker(isolate());
|
|
|
|
v8::HandleScope handle_scope(isolate());
|
2015-11-13 08:41:33 +00:00
|
|
|
EmitCustomEvent("click",
|
2015-07-29 06:25:12 +00:00
|
|
|
ModifiersToObject(isolate(), modifiers), bounds);
|
2014-06-02 03:08:29 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 10:15:51 +00:00
|
|
|
void Tray::OnDoubleClicked(const gfx::Rect& bounds, int modifiers) {
|
2015-07-29 06:25:12 +00:00
|
|
|
v8::Locker locker(isolate());
|
|
|
|
v8::HandleScope handle_scope(isolate());
|
2015-11-13 08:41:33 +00:00
|
|
|
EmitCustomEvent("double-click",
|
2015-07-29 06:25:12 +00:00
|
|
|
ModifiersToObject(isolate(), modifiers), bounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tray::OnRightClicked(const gfx::Rect& bounds, int modifiers) {
|
|
|
|
v8::Locker locker(isolate());
|
|
|
|
v8::HandleScope handle_scope(isolate());
|
2015-11-13 08:41:33 +00:00
|
|
|
EmitCustomEvent("right-click",
|
2015-07-29 06:25:12 +00:00
|
|
|
ModifiersToObject(isolate(), modifiers), bounds);
|
2014-09-09 11:45:21 +00:00
|
|
|
}
|
|
|
|
|
2014-11-28 11:42:57 +00:00
|
|
|
void Tray::OnBalloonShow() {
|
|
|
|
Emit("balloon-show");
|
|
|
|
}
|
|
|
|
|
2014-11-28 10:50:31 +00:00
|
|
|
void Tray::OnBalloonClicked() {
|
2015-11-13 08:41:33 +00:00
|
|
|
Emit("balloon-click");
|
2014-11-28 10:50:31 +00:00
|
|
|
}
|
|
|
|
|
2014-11-28 11:42:57 +00:00
|
|
|
void Tray::OnBalloonClosed() {
|
|
|
|
Emit("balloon-closed");
|
|
|
|
}
|
|
|
|
|
2015-11-10 16:02:50 +00:00
|
|
|
void Tray::OnDrop() {
|
|
|
|
Emit("drop");
|
|
|
|
}
|
|
|
|
|
2015-07-19 04:12:28 +00:00
|
|
|
void Tray::OnDropFiles(const std::vector<std::string>& files) {
|
|
|
|
Emit("drop-files", files);
|
|
|
|
}
|
|
|
|
|
2015-11-06 00:45:43 +00:00
|
|
|
void Tray::OnDragEntered() {
|
2015-11-10 15:27:39 +00:00
|
|
|
Emit("drag-enter");
|
2015-11-06 00:45:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Tray::OnDragExited() {
|
2015-11-10 15:27:39 +00:00
|
|
|
Emit("drag-leave");
|
2015-11-06 00:45:43 +00:00
|
|
|
}
|
|
|
|
|
2015-11-10 16:02:50 +00:00
|
|
|
void Tray::OnDragEnded() {
|
|
|
|
Emit("drag-end");
|
|
|
|
}
|
|
|
|
|
2015-07-06 10:24:40 +00:00
|
|
|
bool Tray::IsDestroyed() const {
|
|
|
|
return !tray_icon_;
|
|
|
|
}
|
|
|
|
|
2014-11-28 10:06:51 +00:00
|
|
|
void Tray::Destroy() {
|
|
|
|
tray_icon_.reset();
|
|
|
|
}
|
|
|
|
|
2015-01-03 02:43:56 +00:00
|
|
|
void Tray::SetImage(mate::Arguments* args, const gfx::Image& image) {
|
2014-05-30 15:57:54 +00:00
|
|
|
tray_icon_->SetImage(image);
|
|
|
|
}
|
|
|
|
|
2015-01-03 02:43:56 +00:00
|
|
|
void Tray::SetPressedImage(mate::Arguments* args, const gfx::Image& image) {
|
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) {
|
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) {
|
2014-09-09 11:33:58 +00:00
|
|
|
tray_icon_->SetTitle(title);
|
|
|
|
}
|
|
|
|
|
2014-11-28 10:06:51 +00:00
|
|
|
void Tray::SetHighlightMode(mate::Arguments* args, bool highlight) {
|
2014-09-09 11:39:39 +00:00
|
|
|
tray_icon_->SetHighlightMode(highlight);
|
|
|
|
}
|
|
|
|
|
2014-11-28 10:39:30 +00:00
|
|
|
void Tray::DisplayBalloon(mate::Arguments* args,
|
|
|
|
const mate::Dictionary& options) {
|
2015-01-03 02:43:56 +00:00
|
|
|
gfx::Image icon;
|
2014-11-28 10:39:30 +00:00
|
|
|
options.Get("icon", &icon);
|
|
|
|
base::string16 title, content;
|
|
|
|
if (!options.Get("title", &title) ||
|
|
|
|
!options.Get("content", &content)) {
|
|
|
|
args->ThrowError("'title' and 'content' must be defined");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tray_icon_->DisplayBalloon(icon, title, content);
|
|
|
|
}
|
|
|
|
|
2015-08-10 05:00:15 +00:00
|
|
|
void Tray::PopUpContextMenu(mate::Arguments* args) {
|
2015-12-02 10:43:11 +00:00
|
|
|
Menu* menu = nullptr;
|
|
|
|
args->GetNext(&menu);
|
2015-07-16 03:39:49 +00:00
|
|
|
gfx::Point pos;
|
|
|
|
args->GetNext(&pos);
|
2015-12-02 10:43:11 +00:00
|
|
|
tray_icon_->PopUpContextMenu(pos, menu ? menu->model() : nullptr);
|
2015-07-16 02:50:53 +00:00
|
|
|
}
|
|
|
|
|
2014-11-28 10:06:51 +00:00
|
|
|
void Tray::SetContextMenu(mate::Arguments* args, Menu* menu) {
|
2014-05-30 15:57:54 +00:00
|
|
|
tray_icon_->SetContextMenu(menu->model());
|
|
|
|
}
|
|
|
|
|
2015-07-29 06:25:12 +00:00
|
|
|
v8::Local<v8::Object> Tray::ModifiersToObject(v8::Isolate* isolate,
|
|
|
|
int modifiers) {
|
|
|
|
mate::Dictionary obj(isolate, v8::Object::New(isolate));
|
|
|
|
obj.Set("shiftKey", static_cast<bool>(modifiers & ui::EF_SHIFT_DOWN));
|
|
|
|
obj.Set("ctrlKey", static_cast<bool>(modifiers & ui::EF_CONTROL_DOWN));
|
|
|
|
obj.Set("altKey", static_cast<bool>(modifiers & ui::EF_ALT_DOWN));
|
2015-07-29 06:44:08 +00:00
|
|
|
obj.Set("metaKey", static_cast<bool>(modifiers & ui::EF_COMMAND_DOWN));
|
2015-07-29 06:25:12 +00:00
|
|
|
return obj.GetHandle();
|
|
|
|
}
|
|
|
|
|
2014-05-30 15:57:54 +00:00
|
|
|
// static
|
|
|
|
void Tray::BuildPrototype(v8::Isolate* isolate,
|
2015-05-22 11:11:22 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> prototype) {
|
2014-05-30 15:57:54 +00:00
|
|
|
mate::ObjectTemplateBuilder(isolate, prototype)
|
2015-07-06 13:26:50 +00:00
|
|
|
.SetMethod("destroy", &Tray::Destroy, true)
|
2015-11-19 09:08:16 +00:00
|
|
|
.SetMethod("isDestroyed", &Tray::IsDestroyed, true)
|
2014-05-30 15:57:54 +00:00
|
|
|
.SetMethod("setImage", &Tray::SetImage)
|
|
|
|
.SetMethod("setPressedImage", &Tray::SetPressedImage)
|
|
|
|
.SetMethod("setToolTip", &Tray::SetToolTip)
|
2014-09-09 11:33:58 +00:00
|
|
|
.SetMethod("setTitle", &Tray::SetTitle)
|
2014-09-09 11:39:39 +00:00
|
|
|
.SetMethod("setHighlightMode", &Tray::SetHighlightMode)
|
2014-11-28 10:39:30 +00:00
|
|
|
.SetMethod("displayBalloon", &Tray::DisplayBalloon)
|
2015-08-10 05:00:15 +00:00
|
|
|
.SetMethod("popUpContextMenu", &Tray::PopUpContextMenu)
|
2014-05-30 15:57:54 +00:00
|
|
|
.SetMethod("_setContextMenu", &Tray::SetContextMenu);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-05-22 11:11:22 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context, void* priv) {
|
2014-05-30 15:57:54 +00:00
|
|
|
using atom::api::Tray;
|
2014-06-29 12:48:44 +00:00
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2015-05-22 11:11:22 +00:00
|
|
|
v8::Local<v8::Function> constructor = mate::CreateConstructor<Tray>(
|
2014-05-30 15:57:54 +00:00
|
|
|
isolate, "Tray", base::Bind(&Tray::New));
|
|
|
|
mate::Dictionary dict(isolate, exports);
|
2015-05-22 11:11:22 +00:00
|
|
|
dict.Set("Tray", static_cast<v8::Local<v8::Value>>(constructor));
|
2014-05-30 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-06-29 12:48:44 +00:00
|
|
|
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_tray, Initialize)
|