| 
									
										
										
										
											2014-10-31 11:17:05 -07:00
										 |  |  | // Copyright (c) 2014 GitHub, Inc.
 | 
					
						
							| 
									
										
										
										
											2014-05-30 23:57:54 +08: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 10:20:06 +08:00
										 |  |  | #include <string>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-30 23:57:54 +08:00
										 |  |  | #include "atom/browser/api/atom_api_menu.h"
 | 
					
						
							| 
									
										
										
										
											2015-03-25 20:10:01 +05:30
										 |  |  | #include "atom/browser/browser.h"
 | 
					
						
							| 
									
										
										
										
											2014-05-30 23:57:54 +08:00
										 |  |  | #include "atom/browser/ui/tray_icon.h"
 | 
					
						
							| 
									
										
										
										
											2016-05-20 16:14:40 +09:00
										 |  |  | #include "atom/common/api/atom_api_native_image.h"
 | 
					
						
							| 
									
										
										
										
											2015-05-01 15:24:22 +05:30
										 |  |  | #include "atom/common/native_mate_converters/gfx_converter.h"
 | 
					
						
							| 
									
										
										
										
											2014-05-30 23:57:54 +08:00
										 |  |  | #include "atom/common/native_mate_converters/image_converter.h"
 | 
					
						
							| 
									
										
										
										
											2014-11-28 18:39:30 +08:00
										 |  |  | #include "atom/common/native_mate_converters/string16_converter.h"
 | 
					
						
							| 
									
										
										
										
											2015-09-07 16:29:54 +08:00
										 |  |  | #include "atom/common/node_includes.h"
 | 
					
						
							| 
									
										
										
										
											2014-05-30 23:57:54 +08:00
										 |  |  | #include "native_mate/constructor.h"
 | 
					
						
							|  |  |  | #include "native_mate/dictionary.h"
 | 
					
						
							| 
									
										
										
										
											2015-01-02 18:43:56 -08:00
										 |  |  | #include "ui/gfx/image/image.h"
 | 
					
						
							| 
									
										
										
										
											2014-05-30 23:57:54 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace atom { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace api { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 16:14:40 +09:00
										 |  |  | Tray::Tray(v8::Isolate* isolate, mate::Handle<NativeImage> image) | 
					
						
							| 
									
										
										
										
											2016-05-20 17:51:05 +09:00
										 |  |  |     : tray_icon_(TrayIcon::Create()) { | 
					
						
							|  |  |  |   SetImage(isolate, image); | 
					
						
							| 
									
										
										
										
											2014-06-02 11:28:23 +08:00
										 |  |  |   tray_icon_->AddObserver(this); | 
					
						
							| 
									
										
										
										
											2014-05-30 23:57:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Tray::~Tray() { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // static
 | 
					
						
							| 
									
										
										
										
											2016-05-20 16:14:40 +09:00
										 |  |  | mate::WrappableBase* Tray::New(v8::Isolate* isolate, | 
					
						
							|  |  |  |                                mate::Handle<NativeImage> image) { | 
					
						
							| 
									
										
										
										
											2015-03-25 20:10:01 +05:30
										 |  |  |   if (!Browser::Get()->is_ready()) { | 
					
						
							| 
									
										
										
										
											2015-09-07 16:12:31 +08:00
										 |  |  |     isolate->ThrowException(v8::Exception::Error(mate::StringToV8( | 
					
						
							|  |  |  |         isolate, "Cannot create Tray before app is ready"))); | 
					
						
							| 
									
										
										
										
											2015-03-25 20:10:01 +05:30
										 |  |  |     return nullptr; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-04-25 10:17:54 +09:00
										 |  |  |   return new Tray(isolate, image); | 
					
						
							| 
									
										
										
										
											2014-05-30 23:57:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-27 03:15:51 -07:00
										 |  |  | void Tray::OnClicked(const gfx::Rect& bounds, int modifiers) { | 
					
						
							| 
									
										
										
										
											2016-06-22 11:00:45 +09:00
										 |  |  |   EmitWithFlags("click", modifiers, bounds); | 
					
						
							| 
									
										
										
										
											2014-06-02 11:08:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-27 03:15:51 -07:00
										 |  |  | void Tray::OnDoubleClicked(const gfx::Rect& bounds, int modifiers) { | 
					
						
							| 
									
										
										
										
											2016-06-22 11:00:45 +09:00
										 |  |  |   EmitWithFlags("double-click", modifiers, bounds); | 
					
						
							| 
									
										
										
										
											2015-07-29 14:25:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Tray::OnRightClicked(const gfx::Rect& bounds, int modifiers) { | 
					
						
							| 
									
										
										
										
											2016-06-22 11:00:45 +09:00
										 |  |  |   EmitWithFlags("right-click", modifiers, bounds); | 
					
						
							| 
									
										
										
										
											2014-09-09 19:45:21 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:42:57 +08:00
										 |  |  | void Tray::OnBalloonShow() { | 
					
						
							|  |  |  |   Emit("balloon-show"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 18:50:31 +08:00
										 |  |  | void Tray::OnBalloonClicked() { | 
					
						
							| 
									
										
										
										
											2015-11-13 16:41:33 +08:00
										 |  |  |   Emit("balloon-click"); | 
					
						
							| 
									
										
										
										
											2014-11-28 18:50:31 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:42:57 +08:00
										 |  |  | void Tray::OnBalloonClosed() { | 
					
						
							|  |  |  |   Emit("balloon-closed"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-10 10:02:50 -06:00
										 |  |  | void Tray::OnDrop() { | 
					
						
							|  |  |  |   Emit("drop"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-19 12:12:28 +08:00
										 |  |  | void Tray::OnDropFiles(const std::vector<std::string>& files) { | 
					
						
							|  |  |  |   Emit("drop-files", files); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-05 18:45:43 -06:00
										 |  |  | void Tray::OnDragEntered() { | 
					
						
							| 
									
										
										
										
											2015-11-10 09:27:39 -06:00
										 |  |  |   Emit("drag-enter"); | 
					
						
							| 
									
										
										
										
											2015-11-05 18:45:43 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Tray::OnDragExited() { | 
					
						
							| 
									
										
										
										
											2015-11-10 09:27:39 -06:00
										 |  |  |   Emit("drag-leave"); | 
					
						
							| 
									
										
										
										
											2015-11-05 18:45:43 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-10 10:02:50 -06:00
										 |  |  | void Tray::OnDragEnded() { | 
					
						
							|  |  |  |   Emit("drag-end"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 16:14:40 +09:00
										 |  |  | void Tray::SetImage(v8::Isolate* isolate, mate::Handle<NativeImage> image) { | 
					
						
							| 
									
										
										
										
											2016-05-20 16:55:22 +09:00
										 |  |  | #if defined(OS_WIN)
 | 
					
						
							| 
									
										
										
										
											2016-05-20 17:51:05 +09:00
										 |  |  |   tray_icon_->SetImage(image->GetHICON(GetSystemMetrics(SM_CXSMICON))); | 
					
						
							| 
									
										
										
										
											2016-05-20 16:55:22 +09:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2016-05-20 16:14:40 +09:00
										 |  |  |   tray_icon_->SetImage(image->image()); | 
					
						
							| 
									
										
										
										
											2016-05-20 16:55:22 +09:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2014-05-30 23:57:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 16:14:40 +09:00
										 |  |  | void Tray::SetPressedImage(v8::Isolate* isolate, | 
					
						
							|  |  |  |                            mate::Handle<NativeImage> image) { | 
					
						
							| 
									
										
										
										
											2016-05-20 16:55:22 +09:00
										 |  |  | #if defined(OS_WIN)
 | 
					
						
							| 
									
										
										
										
											2016-05-20 17:51:05 +09:00
										 |  |  |   tray_icon_->SetPressedImage(image->GetHICON(GetSystemMetrics(SM_CXSMICON))); | 
					
						
							| 
									
										
										
										
											2016-05-20 16:55:22 +09:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2016-05-20 16:14:40 +09:00
										 |  |  |   tray_icon_->SetPressedImage(image->image()); | 
					
						
							| 
									
										
										
										
											2016-05-20 16:55:22 +09:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2014-05-30 23:57:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 16:14:40 +09:00
										 |  |  | void Tray::SetToolTip(const std::string& tool_tip) { | 
					
						
							| 
									
										
										
										
											2014-05-30 23:57:54 +08:00
										 |  |  |   tray_icon_->SetToolTip(tool_tip); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 16:14:40 +09:00
										 |  |  | void Tray::SetTitle(const std::string& title) { | 
					
						
							| 
									
										
										
										
											2014-09-09 19:33:58 +08:00
										 |  |  |   tray_icon_->SetTitle(title); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 16:14:40 +09:00
										 |  |  | void Tray::SetHighlightMode(bool highlight) { | 
					
						
							| 
									
										
										
										
											2014-09-09 19:39:39 +08:00
										 |  |  |   tray_icon_->SetHighlightMode(highlight); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 18:39:30 +08:00
										 |  |  | void Tray::DisplayBalloon(mate::Arguments* args, | 
					
						
							|  |  |  |                           const mate::Dictionary& options) { | 
					
						
							| 
									
										
										
										
											2016-05-20 16:55:22 +09:00
										 |  |  |   mate::Handle<NativeImage> icon; | 
					
						
							| 
									
										
										
										
											2014-11-28 18:39:30 +08: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 16:55:22 +09:00
										 |  |  | #if defined(OS_WIN)
 | 
					
						
							|  |  |  |   tray_icon_->DisplayBalloon( | 
					
						
							| 
									
										
										
										
											2016-05-20 17:51:05 +09:00
										 |  |  |       icon.IsEmpty() ? NULL : icon->GetHICON(GetSystemMetrics(SM_CXSMICON)), | 
					
						
							|  |  |  |       title, content); | 
					
						
							| 
									
										
										
										
											2016-05-20 16:55:22 +09:00
										 |  |  | #else
 | 
					
						
							|  |  |  |   tray_icon_->DisplayBalloon( | 
					
						
							|  |  |  |       icon.IsEmpty() ? gfx::Image() : icon->image(), title, content); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2014-11-28 18:39:30 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-10 13:00:15 +08:00
										 |  |  | void Tray::PopUpContextMenu(mate::Arguments* args) { | 
					
						
							| 
									
										
										
										
											2015-12-02 19:05:22 +08:00
										 |  |  |   mate::Handle<Menu> menu; | 
					
						
							| 
									
										
										
										
											2015-12-02 18:43:11 +08:00
										 |  |  |   args->GetNext(&menu); | 
					
						
							| 
									
										
										
										
											2015-07-16 11:39:49 +08:00
										 |  |  |   gfx::Point pos; | 
					
						
							|  |  |  |   args->GetNext(&pos); | 
					
						
							| 
									
										
										
										
											2015-12-02 19:05:22 +08:00
										 |  |  |   tray_icon_->PopUpContextMenu(pos, menu.IsEmpty() ? nullptr : menu->model()); | 
					
						
							| 
									
										
										
										
											2015-07-16 10:50:53 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 16:18:38 +09:00
										 |  |  | void Tray::SetContextMenu(v8::Isolate* isolate, mate::Handle<Menu> menu) { | 
					
						
							|  |  |  |   menu_.Reset(isolate, menu.ToV8()); | 
					
						
							| 
									
										
										
										
											2014-05-30 23:57:54 +08:00
										 |  |  |   tray_icon_->SetContextMenu(menu->model()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-21 15:40:30 +09:00
										 |  |  | gfx::Rect Tray::GetBounds() { | 
					
						
							|  |  |  |   return tray_icon_->GetBounds(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-30 23:57:54 +08:00
										 |  |  | // static
 | 
					
						
							|  |  |  | void Tray::BuildPrototype(v8::Isolate* isolate, | 
					
						
							| 
									
										
										
										
											2015-05-22 19:11:22 +08:00
										 |  |  |                           v8::Local<v8::ObjectTemplate> prototype) { | 
					
						
							| 
									
										
										
										
											2014-05-30 23:57:54 +08:00
										 |  |  |   mate::ObjectTemplateBuilder(isolate, prototype) | 
					
						
							| 
									
										
										
										
											2015-12-03 15:38:43 +08:00
										 |  |  |       .MakeDestroyable() | 
					
						
							| 
									
										
										
										
											2014-05-30 23:57:54 +08:00
										 |  |  |       .SetMethod("setImage", &Tray::SetImage) | 
					
						
							|  |  |  |       .SetMethod("setPressedImage", &Tray::SetPressedImage) | 
					
						
							|  |  |  |       .SetMethod("setToolTip", &Tray::SetToolTip) | 
					
						
							| 
									
										
										
										
											2014-09-09 19:33:58 +08:00
										 |  |  |       .SetMethod("setTitle", &Tray::SetTitle) | 
					
						
							| 
									
										
										
										
											2014-09-09 19:39:39 +08:00
										 |  |  |       .SetMethod("setHighlightMode", &Tray::SetHighlightMode) | 
					
						
							| 
									
										
										
										
											2014-11-28 18:39:30 +08:00
										 |  |  |       .SetMethod("displayBalloon", &Tray::DisplayBalloon) | 
					
						
							| 
									
										
										
										
											2015-08-10 13:00:15 +08:00
										 |  |  |       .SetMethod("popUpContextMenu", &Tray::PopUpContextMenu) | 
					
						
							| 
									
										
										
										
											2016-06-21 15:40:30 +09:00
										 |  |  |       .SetMethod("setContextMenu", &Tray::SetContextMenu) | 
					
						
							|  |  |  |       .SetMethod("getBounds", &Tray::GetBounds); | 
					
						
							| 
									
										
										
										
											2014-05-30 23:57:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace api
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace atom
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-22 19:11:22 +08:00
										 |  |  | void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused, | 
					
						
							|  |  |  |                 v8::Local<v8::Context> context, void* priv) { | 
					
						
							| 
									
										
										
										
											2014-05-30 23:57:54 +08:00
										 |  |  |   using atom::api::Tray; | 
					
						
							| 
									
										
										
										
											2014-06-29 20:48:44 +08:00
										 |  |  |   v8::Isolate* isolate = context->GetIsolate(); | 
					
						
							| 
									
										
										
										
											2015-05-22 19:11:22 +08:00
										 |  |  |   v8::Local<v8::Function> constructor = mate::CreateConstructor<Tray>( | 
					
						
							| 
									
										
										
										
											2014-05-30 23:57:54 +08:00
										 |  |  |       isolate, "Tray", base::Bind(&Tray::New)); | 
					
						
							|  |  |  |   mate::Dictionary dict(isolate, exports); | 
					
						
							| 
									
										
										
										
											2015-05-22 19:11:22 +08:00
										 |  |  |   dict.Set("Tray", static_cast<v8::Local<v8::Value>>(constructor)); | 
					
						
							| 
									
										
										
										
											2014-05-30 23:57:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-29 20:48:44 +08:00
										 |  |  | NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_tray, Initialize) |