electron/atom/browser/api/atom_api_tray.cc

198 lines
5.3 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/browser.h"
2014-05-30 15:57:54 +00:00
#include "atom/browser/ui/tray_icon.h"
#include "atom/common/api/atom_api_native_image.h"
#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"
#include "atom/common/node_includes.h"
2014-05-30 15:57:54 +00:00
#include "native_mate/constructor.h"
#include "native_mate/dictionary.h"
#include "ui/gfx/image/image.h"
2014-05-30 15:57:54 +00:00
namespace atom {
namespace api {
Tray::Tray(v8::Isolate* isolate, mate::Handle<NativeImage> image)
: tray_icon_(TrayIcon::Create()) {
SetImage(isolate, image);
tray_icon_->AddObserver(this);
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.
base::MessageLoop::current()->DeleteSoon(FROM_HERE, tray_icon_.release());
2014-05-30 15:57:54 +00:00
}
// static
mate::WrappableBase* Tray::New(v8::Isolate* isolate,
mate::Handle<NativeImage> image) {
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")));
return nullptr;
}
2016-04-25 01:17:54 +00:00
return new Tray(isolate, image);
2014-05-30 15:57:54 +00:00
}
void Tray::OnClicked(const gfx::Rect& bounds, int modifiers) {
2016-06-22 02:00:45 +00:00
EmitWithFlags("click", modifiers, bounds);
2014-06-02 03:08:29 +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);
}
void Tray::OnBalloonShow() {
Emit("balloon-show");
}
void Tray::OnBalloonClicked() {
Emit("balloon-click");
}
void Tray::OnBalloonClosed() {
Emit("balloon-closed");
}
2015-11-10 16:02:50 +00:00
void Tray::OnDrop() {
Emit("drop");
}
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);
}
void Tray::OnDragEntered() {
Emit("drag-enter");
}
void Tray::OnDragExited() {
Emit("drag-leave");
}
2015-11-10 16:02:50 +00:00
void Tray::OnDragEnded() {
Emit("drag-end");
}
void Tray::SetImage(v8::Isolate* isolate, mate::Handle<NativeImage> image) {
2016-05-20 07:55:22 +00:00
#if defined(OS_WIN)
tray_icon_->SetImage(image->GetHICON(GetSystemMetrics(SM_CXSMICON)));
2016-05-20 07:55:22 +00:00
#else
tray_icon_->SetImage(image->image());
2016-05-20 07:55:22 +00:00
#endif
2014-05-30 15:57:54 +00:00
}
void Tray::SetPressedImage(v8::Isolate* isolate,
mate::Handle<NativeImage> image) {
2016-05-20 07:55:22 +00:00
#if defined(OS_WIN)
tray_icon_->SetPressedImage(image->GetHICON(GetSystemMetrics(SM_CXSMICON)));
2016-05-20 07:55:22 +00:00
#else
tray_icon_->SetPressedImage(image->image());
2016-05-20 07:55:22 +00:00
#endif
2014-05-30 15:57:54 +00:00
}
void Tray::SetToolTip(const std::string& tool_tip) {
2014-05-30 15:57:54 +00:00
tray_icon_->SetToolTip(tool_tip);
}
void Tray::SetTitle(const std::string& title) {
tray_icon_->SetTitle(title);
}
void Tray::SetHighlightMode(bool highlight) {
tray_icon_->SetHighlightMode(highlight);
}
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;
if (!options.Get("title", &title) ||
!options.Get("content", &content)) {
args->ThrowError("'title' and 'content' must be defined");
return;
}
2016-05-20 07:55:22 +00:00
#if defined(OS_WIN)
tray_icon_->DisplayBalloon(
icon.IsEmpty() ? NULL : icon->GetHICON(GetSystemMetrics(SM_CXSMICON)),
title, content);
2016-05-20 07:55:22 +00:00
#else
tray_icon_->DisplayBalloon(
icon.IsEmpty() ? gfx::Image() : icon->image(), title, content);
#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;
args->GetNext(&menu);
gfx::Point pos;
args->GetNext(&pos);
2015-12-02 11:05:22 +00:00
tray_icon_->PopUpContextMenu(pos, menu.IsEmpty() ? nullptr : menu->model());
}
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,
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)
.MakeDestroyable()
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-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 {
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;
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
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_tray, Initialize)