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"
|
2016-05-20 07:14:40 +00:00
|
|
|
#include "atom/common/api/atom_api_native_image.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"
|
2016-11-30 07:30:03 +00:00
|
|
|
#include "base/threading/thread_task_runner_handle.h"
|
2014-05-30 15:57:54 +00:00
|
|
|
#include "native_mate/constructor.h"
|
|
|
|
#include "native_mate/dictionary.h"
|
2015-01-03 02:43:56 +00:00
|
|
|
#include "ui/gfx/image/image.h"
|
2014-05-30 15:57:54 +00:00
|
|
|
|
2016-07-26 20:51:43 +00:00
|
|
|
namespace mate {
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
template <>
|
2016-07-26 20:51:43 +00:00
|
|
|
struct Converter<atom::TrayIcon::HighlightMode> {
|
2018-04-18 01:55:30 +00:00
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
2016-07-26 20:51:43 +00:00
|
|
|
atom::TrayIcon::HighlightMode* out) {
|
|
|
|
std::string mode;
|
|
|
|
if (ConvertFromV8(isolate, val, &mode)) {
|
|
|
|
if (mode == "always") {
|
|
|
|
*out = atom::TrayIcon::HighlightMode::ALWAYS;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (mode == "selection") {
|
|
|
|
*out = atom::TrayIcon::HighlightMode::SELECTION;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (mode == "never") {
|
|
|
|
*out = atom::TrayIcon::HighlightMode::NEVER;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-02-21 04:30:32 +00:00
|
|
|
|
|
|
|
bool highlight;
|
|
|
|
if (ConvertFromV8(isolate, val, &highlight)) {
|
|
|
|
if (highlight)
|
|
|
|
*out = atom::TrayIcon::HighlightMode::SELECTION;
|
|
|
|
else
|
|
|
|
*out = atom::TrayIcon::HighlightMode::NEVER;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-26 20:51:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace mate
|
|
|
|
|
2014-05-30 15:57:54 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
Tray::Tray(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> wrapper,
|
2016-08-02 06:15:40 +00:00
|
|
|
mate::Handle<NativeImage> image)
|
2016-05-20 08:51:05 +00:00
|
|
|
: tray_icon_(TrayIcon::Create()) {
|
|
|
|
SetImage(isolate, image);
|
2014-06-02 03:28:23 +00:00
|
|
|
tray_icon_->AddObserver(this);
|
2016-08-02 06:15:40 +00:00
|
|
|
|
|
|
|
InitWith(isolate, wrapper);
|
2014-05-30 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Tray::~Tray() {
|
2016-07-12 05:23:38 +00:00
|
|
|
// Destroy the native tray in next tick.
|
2018-04-18 01:55:30 +00:00
|
|
|
base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE,
|
|
|
|
tray_icon_.release());
|
2014-05-30 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2016-08-02 06:15:40 +00:00
|
|
|
mate::WrappableBase* Tray::New(mate::Handle<NativeImage> image,
|
|
|
|
mate::Arguments* args) {
|
2015-03-25 14:40:01 +00:00
|
|
|
if (!Browser::Get()->is_ready()) {
|
2016-08-02 06:15:40 +00:00
|
|
|
args->ThrowError("Cannot create Tray before app is ready");
|
2015-03-25 14:40:01 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-08-02 06:15:40 +00:00
|
|
|
return new Tray(args->isolate(), args->GetThis(), image);
|
2014-05-30 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 02:49:26 +00:00
|
|
|
void Tray::OnClicked(const gfx::Rect& bounds,
|
|
|
|
const gfx::Point& location,
|
|
|
|
int modifiers) {
|
2017-08-26 18:04:58 +00:00
|
|
|
EmitWithFlags("click", modifiers, bounds, location);
|
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) {
|
2016-06-22 02:00:45 +00:00
|
|
|
EmitWithFlags("double-click", modifiers, bounds);
|
2015-07-29 06:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Tray::OnRightClicked(const gfx::Rect& bounds, int modifiers) {
|
2016-06-22 02:00:45 +00:00
|
|
|
EmitWithFlags("right-click", 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);
|
|
|
|
}
|
|
|
|
|
2016-07-14 17:21:39 +00:00
|
|
|
void Tray::OnDropText(const std::string& text) {
|
|
|
|
Emit("drop-text", text);
|
|
|
|
}
|
|
|
|
|
2017-06-28 19:09:12 +00:00
|
|
|
void Tray::OnMouseEntered(const gfx::Point& location, int modifiers) {
|
|
|
|
EmitWithFlags("mouse-enter", modifiers, location);
|
2017-06-14 22:00:29 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 19:09:12 +00:00
|
|
|
void Tray::OnMouseExited(const gfx::Point& location, int modifiers) {
|
|
|
|
EmitWithFlags("mouse-leave", modifiers, location);
|
2017-06-14 22:00:29 +00:00
|
|
|
}
|
|
|
|
|
2017-08-26 18:04:58 +00:00
|
|
|
void Tray::OnMouseMoved(const gfx::Point& location, int modifiers) {
|
|
|
|
EmitWithFlags("mouse-move", modifiers, location);
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2016-05-20 07:14:40 +00:00
|
|
|
void Tray::SetImage(v8::Isolate* isolate, mate::Handle<NativeImage> image) {
|
2016-05-20 07:55:22 +00:00
|
|
|
#if defined(OS_WIN)
|
2016-05-20 08:51:05 +00:00
|
|
|
tray_icon_->SetImage(image->GetHICON(GetSystemMetrics(SM_CXSMICON)));
|
2016-05-20 07:55:22 +00:00
|
|
|
#else
|
2016-05-20 07:14:40 +00:00
|
|
|
tray_icon_->SetImage(image->image());
|
2016-05-20 07:55:22 +00:00
|
|
|
#endif
|
2014-05-30 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 07:14:40 +00:00
|
|
|
void Tray::SetPressedImage(v8::Isolate* isolate,
|
|
|
|
mate::Handle<NativeImage> image) {
|
2016-05-20 07:55:22 +00:00
|
|
|
#if defined(OS_WIN)
|
2016-05-20 08:51:05 +00:00
|
|
|
tray_icon_->SetPressedImage(image->GetHICON(GetSystemMetrics(SM_CXSMICON)));
|
2016-05-20 07:55:22 +00:00
|
|
|
#else
|
2016-05-20 07:14:40 +00:00
|
|
|
tray_icon_->SetPressedImage(image->image());
|
2016-05-20 07:55:22 +00:00
|
|
|
#endif
|
2014-05-30 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 07:14:40 +00:00
|
|
|
void Tray::SetToolTip(const std::string& tool_tip) {
|
2014-05-30 15:57:54 +00:00
|
|
|
tray_icon_->SetToolTip(tool_tip);
|
|
|
|
}
|
|
|
|
|
2016-05-20 07:14:40 +00:00
|
|
|
void Tray::SetTitle(const std::string& title) {
|
2014-09-09 11:33:58 +00:00
|
|
|
tray_icon_->SetTitle(title);
|
|
|
|
}
|
|
|
|
|
2016-07-26 20:51:43 +00:00
|
|
|
void Tray::SetHighlightMode(TrayIcon::HighlightMode mode) {
|
|
|
|
tray_icon_->SetHighlightMode(mode);
|
2014-09-09 11:39:39 +00:00
|
|
|
}
|
|
|
|
|
2018-04-02 01:07:26 +00:00
|
|
|
void Tray::SetIgnoreDoubleClickEvents(bool ignore) {
|
2018-04-29 03:07:32 +00:00
|
|
|
#if defined(OS_MACOSX)
|
2018-04-02 01:07:26 +00:00
|
|
|
tray_icon_->SetIgnoreDoubleClickEvents(ignore);
|
2018-04-29 03:07:32 +00:00
|
|
|
#endif
|
2018-04-02 01:07:26 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 07:43:46 +00:00
|
|
|
bool Tray::GetIgnoreDoubleClickEvents() {
|
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
return tray_icon_->GetIgnoreDoubleClickEvents();
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-11-28 10:39:30 +00:00
|
|
|
void Tray::DisplayBalloon(mate::Arguments* args,
|
|
|
|
const mate::Dictionary& options) {
|
2016-05-20 07:55:22 +00:00
|
|
|
mate::Handle<NativeImage> icon;
|
2014-11-28 10:39:30 +00:00
|
|
|
options.Get("icon", &icon);
|
|
|
|
base::string16 title, content;
|
2018-04-18 01:55:30 +00:00
|
|
|
if (!options.Get("title", &title) || !options.Get("content", &content)) {
|
2014-11-28 10:39:30 +00:00
|
|
|
args->ThrowError("'title' and 'content' must be defined");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-20 07:55:22 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
tray_icon_->DisplayBalloon(
|
2016-05-20 08:51:05 +00:00
|
|
|
icon.IsEmpty() ? NULL : icon->GetHICON(GetSystemMetrics(SM_CXSMICON)),
|
|
|
|
title, content);
|
2016-05-20 07:55:22 +00:00
|
|
|
#else
|
2018-04-18 01:55:30 +00:00
|
|
|
tray_icon_->DisplayBalloon(icon.IsEmpty() ? gfx::Image() : icon->image(),
|
|
|
|
title, content);
|
2016-05-20 07:55:22 +00:00
|
|
|
#endif
|
2014-11-28 10:39:30 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 05:00:15 +00:00
|
|
|
void Tray::PopUpContextMenu(mate::Arguments* args) {
|
2015-12-02 11:05:22 +00:00
|
|
|
mate::Handle<Menu> menu;
|
2015-12-02 10:43:11 +00:00
|
|
|
args->GetNext(&menu);
|
2015-07-16 03:39:49 +00:00
|
|
|
gfx::Point pos;
|
|
|
|
args->GetNext(&pos);
|
2015-12-02 11:05:22 +00:00
|
|
|
tray_icon_->PopUpContextMenu(pos, menu.IsEmpty() ? nullptr : menu->model());
|
2015-07-16 02:50:53 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 07:18:38 +00:00
|
|
|
void Tray::SetContextMenu(v8::Isolate* isolate, mate::Handle<Menu> menu) {
|
|
|
|
menu_.Reset(isolate, menu.ToV8());
|
2014-05-30 15:57:54 +00:00
|
|
|
tray_icon_->SetContextMenu(menu->model());
|
|
|
|
}
|
|
|
|
|
2016-06-21 06:40:30 +00:00
|
|
|
gfx::Rect Tray::GetBounds() {
|
|
|
|
return tray_icon_->GetBounds();
|
|
|
|
}
|
|
|
|
|
2014-05-30 15:57:54 +00:00
|
|
|
// static
|
|
|
|
void Tray::BuildPrototype(v8::Isolate* isolate,
|
2016-08-02 09:08:12 +00:00
|
|
|
v8::Local<v8::FunctionTemplate> prototype) {
|
2016-08-02 10:28:12 +00:00
|
|
|
prototype->SetClassName(mate::StringToV8(isolate, "Tray"));
|
2016-08-02 09:08:12 +00:00
|
|
|
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
2015-12-03 07:38:43 +00:00
|
|
|
.MakeDestroyable()
|
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)
|
2018-04-02 01:07:26 +00:00
|
|
|
.SetMethod("setIgnoreDoubleClickEvents",
|
|
|
|
&Tray::SetIgnoreDoubleClickEvents)
|
2018-05-03 07:43:46 +00:00
|
|
|
.SetMethod("getIgnoreDoubleClickEvents",
|
|
|
|
&Tray::GetIgnoreDoubleClickEvents)
|
2014-11-28 10:39:30 +00:00
|
|
|
.SetMethod("displayBalloon", &Tray::DisplayBalloon)
|
2015-08-10 05:00:15 +00:00
|
|
|
.SetMethod("popUpContextMenu", &Tray::PopUpContextMenu)
|
2016-06-21 06:40:30 +00:00
|
|
|
.SetMethod("setContextMenu", &Tray::SetContextMenu)
|
|
|
|
.SetMethod("getBounds", &Tray::GetBounds);
|
2014-05-30 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-08-02 08:02:04 +00:00
|
|
|
using atom::api::Tray;
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
void* priv) {
|
2014-06-29 12:48:44 +00:00
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2016-08-02 10:28:12 +00:00
|
|
|
Tray::SetConstructor(isolate, base::Bind(&Tray::New));
|
2016-08-02 08:02:04 +00:00
|
|
|
|
2014-05-30 15:57:54 +00:00
|
|
|
mate::Dictionary dict(isolate, exports);
|
2016-08-02 08:20:58 +00:00
|
|
|
dict.Set("Tray", Tray::GetConstructor(isolate)->GetFunction());
|
2014-05-30 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-01-06 15:58:24 +00:00
|
|
|
NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_browser_tray, Initialize)
|