Bind TrayIcon to JS.

This commit is contained in:
Cheng Zhao 2014-05-30 23:57:54 +08:00
parent 52d8d6fdb3
commit 6c7fe80ec5
13 changed files with 169 additions and 11 deletions

View file

@ -34,6 +34,8 @@ class Menu : public mate::Wrappable,
static void SendActionToFirstResponder(const std::string& action);
#endif
ui::SimpleMenuModel* model() const { return model_.get(); }
protected:
Menu();
virtual ~Menu();

View file

@ -0,0 +1,76 @@
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
// 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 "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::~Tray() {
}
// static
mate::Wrappable* Tray::New(const gfx::ImageSkia& image) {
return new Tray(image);
}
void Tray::SetImage(const gfx::ImageSkia& image) {
tray_icon_->SetImage(image);
}
void Tray::SetPressedImage(const gfx::ImageSkia& image) {
tray_icon_->SetPressedImage(image);
}
void Tray::SetToolTip(const std::string& tool_tip) {
tray_icon_->SetToolTip(tool_tip);
}
void Tray::SetContextMenu(Menu* menu) {
tray_icon_->SetContextMenu(menu->model());
}
// static
void Tray::BuildPrototype(v8::Isolate* isolate,
v8::Handle<v8::ObjectTemplate> prototype) {
mate::ObjectTemplateBuilder(isolate, prototype)
.SetMethod("setImage", &Tray::SetImage)
.SetMethod("setPressedImage", &Tray::SetPressedImage)
.SetMethod("setToolTip", &Tray::SetToolTip)
.SetMethod("_setContextMenu", &Tray::SetContextMenu);
}
} // namespace api
} // namespace atom
namespace {
void Initialize(v8::Handle<v8::Object> exports) {
using atom::api::Tray;
v8::Isolate* isolate = v8::Isolate::GetCurrent();
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(atom_browser_tray, Initialize)

View file

@ -0,0 +1,49 @@
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_API_ATOM_API_TRAY_H_
#define ATOM_BROWSER_API_ATOM_API_TRAY_H_
#include "atom/browser/api/event_emitter.h"
#include "base/memory/scoped_ptr.h"
namespace gfx {
class ImageSkia;
}
namespace atom {
class TrayIcon;
namespace api {
class Menu;
class Tray : public mate::EventEmitter {
public:
static mate::Wrappable* New(const gfx::ImageSkia& image);
static void BuildPrototype(v8::Isolate* isolate,
v8::Handle<v8::ObjectTemplate> prototype);
protected:
Tray(const gfx::ImageSkia& image);
virtual ~Tray();
void SetImage(const gfx::ImageSkia& image);
void SetPressedImage(const gfx::ImageSkia& image);
void SetToolTip(const std::string& tool_tip);
void SetContextMenu(Menu* menu);
private:
scoped_ptr<TrayIcon> tray_icon_;
DISALLOW_COPY_AND_ASSIGN(Tray);
};
} // namespace api
} // namespace atom
#endif // ATOM_BROWSER_API_ATOM_API_TRAY_H_

View file

@ -0,0 +1,10 @@
EventEmitter = require('events').EventEmitter
bindings = process.atomBinding 'tray'
Tray = bindings.Tray
Tray::__proto__ = EventEmitter.prototype
Tray::setContextMenu = (menu) ->
@_setContextMenu menu
@menu = menu # Keep a strong reference of menu.
module.exports = Tray