atom_api_tray.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Copyright (c) 2014 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/api/atom_api_tray.h"
  5. #include <string>
  6. #include "atom/browser/api/atom_api_menu.h"
  7. #include "atom/browser/browser.h"
  8. #include "atom/common/api/atom_api_native_image.h"
  9. #include "atom/common/native_mate_converters/gfx_converter.h"
  10. #include "atom/common/native_mate_converters/image_converter.h"
  11. #include "atom/common/native_mate_converters/string16_converter.h"
  12. #include "atom/common/node_includes.h"
  13. #include "base/threading/thread_task_runner_handle.h"
  14. #include "native_mate/constructor.h"
  15. #include "native_mate/dictionary.h"
  16. #include "ui/gfx/image/image.h"
  17. namespace mate {
  18. template <>
  19. struct Converter<atom::TrayIcon::HighlightMode> {
  20. static bool FromV8(v8::Isolate* isolate,
  21. v8::Local<v8::Value> val,
  22. atom::TrayIcon::HighlightMode* out) {
  23. std::string mode;
  24. if (ConvertFromV8(isolate, val, &mode)) {
  25. if (mode == "always") {
  26. *out = atom::TrayIcon::HighlightMode::ALWAYS;
  27. return true;
  28. }
  29. if (mode == "selection") {
  30. *out = atom::TrayIcon::HighlightMode::SELECTION;
  31. return true;
  32. }
  33. if (mode == "never") {
  34. *out = atom::TrayIcon::HighlightMode::NEVER;
  35. return true;
  36. }
  37. }
  38. return false;
  39. }
  40. };
  41. } // namespace mate
  42. namespace atom {
  43. namespace api {
  44. Tray::Tray(v8::Isolate* isolate,
  45. v8::Local<v8::Object> wrapper,
  46. mate::Handle<NativeImage> image)
  47. : tray_icon_(TrayIcon::Create()) {
  48. SetImage(isolate, image);
  49. tray_icon_->AddObserver(this);
  50. InitWith(isolate, wrapper);
  51. }
  52. Tray::~Tray() = default;
  53. // static
  54. mate::WrappableBase* Tray::New(mate::Handle<NativeImage> image,
  55. mate::Arguments* args) {
  56. if (!Browser::Get()->is_ready()) {
  57. args->ThrowError("Cannot create Tray before app is ready");
  58. return nullptr;
  59. }
  60. return new Tray(args->isolate(), args->GetThis(), image);
  61. }
  62. void Tray::OnClicked(const gfx::Rect& bounds,
  63. const gfx::Point& location,
  64. int modifiers) {
  65. EmitWithFlags("click", modifiers, bounds, location);
  66. }
  67. void Tray::OnDoubleClicked(const gfx::Rect& bounds, int modifiers) {
  68. EmitWithFlags("double-click", modifiers, bounds);
  69. }
  70. void Tray::OnRightClicked(const gfx::Rect& bounds, int modifiers) {
  71. EmitWithFlags("right-click", modifiers, bounds);
  72. }
  73. void Tray::OnBalloonShow() {
  74. Emit("balloon-show");
  75. }
  76. void Tray::OnBalloonClicked() {
  77. Emit("balloon-click");
  78. }
  79. void Tray::OnBalloonClosed() {
  80. Emit("balloon-closed");
  81. }
  82. void Tray::OnDrop() {
  83. Emit("drop");
  84. }
  85. void Tray::OnDropFiles(const std::vector<std::string>& files) {
  86. Emit("drop-files", files);
  87. }
  88. void Tray::OnDropText(const std::string& text) {
  89. Emit("drop-text", text);
  90. }
  91. void Tray::OnMouseEntered(const gfx::Point& location, int modifiers) {
  92. EmitWithFlags("mouse-enter", modifiers, location);
  93. }
  94. void Tray::OnMouseExited(const gfx::Point& location, int modifiers) {
  95. EmitWithFlags("mouse-leave", modifiers, location);
  96. }
  97. void Tray::OnMouseMoved(const gfx::Point& location, int modifiers) {
  98. EmitWithFlags("mouse-move", modifiers, location);
  99. }
  100. void Tray::OnDragEntered() {
  101. Emit("drag-enter");
  102. }
  103. void Tray::OnDragExited() {
  104. Emit("drag-leave");
  105. }
  106. void Tray::OnDragEnded() {
  107. Emit("drag-end");
  108. }
  109. void Tray::SetImage(v8::Isolate* isolate, mate::Handle<NativeImage> image) {
  110. #if defined(OS_WIN)
  111. tray_icon_->SetImage(image->GetHICON(GetSystemMetrics(SM_CXSMICON)));
  112. #else
  113. tray_icon_->SetImage(image->image());
  114. #endif
  115. }
  116. void Tray::SetPressedImage(v8::Isolate* isolate,
  117. mate::Handle<NativeImage> image) {
  118. #if defined(OS_WIN)
  119. tray_icon_->SetPressedImage(image->GetHICON(GetSystemMetrics(SM_CXSMICON)));
  120. #else
  121. tray_icon_->SetPressedImage(image->image());
  122. #endif
  123. }
  124. void Tray::SetToolTip(const std::string& tool_tip) {
  125. tray_icon_->SetToolTip(tool_tip);
  126. }
  127. void Tray::SetTitle(const std::string& title) {
  128. tray_icon_->SetTitle(title);
  129. }
  130. void Tray::SetHighlightMode(TrayIcon::HighlightMode mode) {
  131. tray_icon_->SetHighlightMode(mode);
  132. }
  133. void Tray::SetIgnoreDoubleClickEvents(bool ignore) {
  134. #if defined(OS_MACOSX)
  135. tray_icon_->SetIgnoreDoubleClickEvents(ignore);
  136. #endif
  137. }
  138. bool Tray::GetIgnoreDoubleClickEvents() {
  139. #if defined(OS_MACOSX)
  140. return tray_icon_->GetIgnoreDoubleClickEvents();
  141. #else
  142. return false;
  143. #endif
  144. }
  145. void Tray::DisplayBalloon(mate::Arguments* args,
  146. const mate::Dictionary& options) {
  147. mate::Handle<NativeImage> icon;
  148. options.Get("icon", &icon);
  149. base::string16 title, content;
  150. if (!options.Get("title", &title) || !options.Get("content", &content)) {
  151. args->ThrowError("'title' and 'content' must be defined");
  152. return;
  153. }
  154. #if defined(OS_WIN)
  155. tray_icon_->DisplayBalloon(
  156. icon.IsEmpty() ? NULL : icon->GetHICON(GetSystemMetrics(SM_CXICON)),
  157. title, content);
  158. #else
  159. tray_icon_->DisplayBalloon(icon.IsEmpty() ? gfx::Image() : icon->image(),
  160. title, content);
  161. #endif
  162. }
  163. void Tray::PopUpContextMenu(mate::Arguments* args) {
  164. mate::Handle<Menu> menu;
  165. args->GetNext(&menu);
  166. gfx::Point pos;
  167. args->GetNext(&pos);
  168. tray_icon_->PopUpContextMenu(pos, menu.IsEmpty() ? nullptr : menu->model());
  169. }
  170. void Tray::SetContextMenu(v8::Isolate* isolate, mate::Handle<Menu> menu) {
  171. menu_.Reset(isolate, menu.ToV8());
  172. tray_icon_->SetContextMenu(menu.IsEmpty() ? nullptr : menu->model());
  173. }
  174. gfx::Rect Tray::GetBounds() {
  175. return tray_icon_->GetBounds();
  176. }
  177. // static
  178. void Tray::BuildPrototype(v8::Isolate* isolate,
  179. v8::Local<v8::FunctionTemplate> prototype) {
  180. prototype->SetClassName(mate::StringToV8(isolate, "Tray"));
  181. mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
  182. .MakeDestroyable()
  183. .SetMethod("setImage", &Tray::SetImage)
  184. .SetMethod("setPressedImage", &Tray::SetPressedImage)
  185. .SetMethod("setToolTip", &Tray::SetToolTip)
  186. .SetMethod("setTitle", &Tray::SetTitle)
  187. .SetMethod("setHighlightMode", &Tray::SetHighlightMode)
  188. .SetMethod("setIgnoreDoubleClickEvents",
  189. &Tray::SetIgnoreDoubleClickEvents)
  190. .SetMethod("getIgnoreDoubleClickEvents",
  191. &Tray::GetIgnoreDoubleClickEvents)
  192. .SetMethod("displayBalloon", &Tray::DisplayBalloon)
  193. .SetMethod("popUpContextMenu", &Tray::PopUpContextMenu)
  194. .SetMethod("setContextMenu", &Tray::SetContextMenu)
  195. .SetMethod("getBounds", &Tray::GetBounds);
  196. }
  197. } // namespace api
  198. } // namespace atom
  199. namespace {
  200. using atom::api::Tray;
  201. void Initialize(v8::Local<v8::Object> exports,
  202. v8::Local<v8::Value> unused,
  203. v8::Local<v8::Context> context,
  204. void* priv) {
  205. v8::Isolate* isolate = context->GetIsolate();
  206. Tray::SetConstructor(isolate, base::Bind(&Tray::New));
  207. mate::Dictionary dict(isolate, exports);
  208. dict.Set(
  209. "Tray",
  210. Tray::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
  211. }
  212. } // namespace
  213. NODE_LINKED_MODULE_CONTEXT_AWARE(atom_browser_tray, Initialize)