atom_api_tray.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. #if defined(OS_MACOSX)
  129. tray_icon_->SetTitle(title);
  130. #endif
  131. }
  132. std::string Tray::GetTitle() {
  133. #if defined(OS_MACOSX)
  134. return tray_icon_->GetTitle();
  135. #else
  136. return "";
  137. #endif
  138. }
  139. void Tray::SetHighlightMode(TrayIcon::HighlightMode mode) {
  140. tray_icon_->SetHighlightMode(mode);
  141. }
  142. void Tray::SetIgnoreDoubleClickEvents(bool ignore) {
  143. #if defined(OS_MACOSX)
  144. tray_icon_->SetIgnoreDoubleClickEvents(ignore);
  145. #endif
  146. }
  147. bool Tray::GetIgnoreDoubleClickEvents() {
  148. #if defined(OS_MACOSX)
  149. return tray_icon_->GetIgnoreDoubleClickEvents();
  150. #else
  151. return false;
  152. #endif
  153. }
  154. void Tray::DisplayBalloon(mate::Arguments* args,
  155. const mate::Dictionary& options) {
  156. mate::Handle<NativeImage> icon;
  157. options.Get("icon", &icon);
  158. base::string16 title, content;
  159. if (!options.Get("title", &title) || !options.Get("content", &content)) {
  160. args->ThrowError("'title' and 'content' must be defined");
  161. return;
  162. }
  163. #if defined(OS_WIN)
  164. tray_icon_->DisplayBalloon(
  165. icon.IsEmpty() ? NULL : icon->GetHICON(GetSystemMetrics(SM_CXICON)),
  166. title, content);
  167. #else
  168. tray_icon_->DisplayBalloon(icon.IsEmpty() ? gfx::Image() : icon->image(),
  169. title, content);
  170. #endif
  171. }
  172. void Tray::PopUpContextMenu(mate::Arguments* args) {
  173. mate::Handle<Menu> menu;
  174. args->GetNext(&menu);
  175. gfx::Point pos;
  176. args->GetNext(&pos);
  177. tray_icon_->PopUpContextMenu(pos, menu.IsEmpty() ? nullptr : menu->model());
  178. }
  179. void Tray::SetContextMenu(v8::Isolate* isolate, mate::Handle<Menu> menu) {
  180. menu_.Reset(isolate, menu.ToV8());
  181. tray_icon_->SetContextMenu(menu.IsEmpty() ? nullptr : menu->model());
  182. }
  183. gfx::Rect Tray::GetBounds() {
  184. return tray_icon_->GetBounds();
  185. }
  186. // static
  187. void Tray::BuildPrototype(v8::Isolate* isolate,
  188. v8::Local<v8::FunctionTemplate> prototype) {
  189. prototype->SetClassName(mate::StringToV8(isolate, "Tray"));
  190. mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
  191. .MakeDestroyable()
  192. .SetMethod("setImage", &Tray::SetImage)
  193. .SetMethod("setPressedImage", &Tray::SetPressedImage)
  194. .SetMethod("setToolTip", &Tray::SetToolTip)
  195. .SetMethod("setTitle", &Tray::SetTitle)
  196. .SetMethod("getTitle", &Tray::GetTitle)
  197. .SetMethod("setHighlightMode", &Tray::SetHighlightMode)
  198. .SetMethod("setIgnoreDoubleClickEvents",
  199. &Tray::SetIgnoreDoubleClickEvents)
  200. .SetMethod("getIgnoreDoubleClickEvents",
  201. &Tray::GetIgnoreDoubleClickEvents)
  202. .SetMethod("displayBalloon", &Tray::DisplayBalloon)
  203. .SetMethod("popUpContextMenu", &Tray::PopUpContextMenu)
  204. .SetMethod("setContextMenu", &Tray::SetContextMenu)
  205. .SetMethod("getBounds", &Tray::GetBounds);
  206. }
  207. } // namespace api
  208. } // namespace atom
  209. namespace {
  210. using atom::api::Tray;
  211. void Initialize(v8::Local<v8::Object> exports,
  212. v8::Local<v8::Value> unused,
  213. v8::Local<v8::Context> context,
  214. void* priv) {
  215. v8::Isolate* isolate = context->GetIsolate();
  216. Tray::SetConstructor(isolate, base::Bind(&Tray::New));
  217. mate::Dictionary dict(isolate, exports);
  218. dict.Set(
  219. "Tray",
  220. Tray::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
  221. }
  222. } // namespace
  223. NODE_LINKED_MODULE_CONTEXT_AWARE(atom_browser_tray, Initialize)