From f6c66a737498223933d0a34c3113515cc6284f74 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 28 Nov 2014 18:06:51 +0800 Subject: [PATCH] Add Tray.destroy() API, fixes #871 --- atom/browser/api/atom_api_tray.cc | 38 ++++++++++++++++++++++++++----- atom/browser/api/atom_api_tray.h | 19 +++++++++++----- docs/api/tray.md | 4 ++++ 3 files changed, 49 insertions(+), 12 deletions(-) diff --git a/atom/browser/api/atom_api_tray.cc b/atom/browser/api/atom_api_tray.cc index 451221a82c9d..35d89de744d4 100644 --- a/atom/browser/api/atom_api_tray.cc +++ b/atom/browser/api/atom_api_tray.cc @@ -40,34 +40,60 @@ void Tray::OnDoubleClicked() { Emit("double-clicked"); } -void Tray::SetImage(const gfx::ImageSkia& image) { +void Tray::Destroy() { + tray_icon_.reset(); +} + +void Tray::SetImage(mate::Arguments* args, const gfx::ImageSkia& image) { + if (!CheckTrayLife(args)) + return; tray_icon_->SetImage(image); } -void Tray::SetPressedImage(const gfx::ImageSkia& image) { +void Tray::SetPressedImage(mate::Arguments* args, const gfx::ImageSkia& image) { + if (!CheckTrayLife(args)) + return; tray_icon_->SetPressedImage(image); } -void Tray::SetToolTip(const std::string& tool_tip) { +void Tray::SetToolTip(mate::Arguments* args, const std::string& tool_tip) { + if (!CheckTrayLife(args)) + return; tray_icon_->SetToolTip(tool_tip); } -void Tray::SetTitle(const std::string& title) { +void Tray::SetTitle(mate::Arguments* args, const std::string& title) { + if (!CheckTrayLife(args)) + return; tray_icon_->SetTitle(title); } -void Tray::SetHighlightMode(bool highlight) { +void Tray::SetHighlightMode(mate::Arguments* args, bool highlight) { + if (!CheckTrayLife(args)) + return; tray_icon_->SetHighlightMode(highlight); } -void Tray::SetContextMenu(Menu* menu) { +void Tray::SetContextMenu(mate::Arguments* args, Menu* menu) { + if (!CheckTrayLife(args)) + return; tray_icon_->SetContextMenu(menu->model()); } +bool Tray::CheckTrayLife(mate::Arguments* args) { + if (!tray_icon_) { + args->ThrowError("Tray is already destroyed"); + return false; + } else { + return true; + } +} + // static void Tray::BuildPrototype(v8::Isolate* isolate, v8::Handle prototype) { mate::ObjectTemplateBuilder(isolate, prototype) + .SetMethod("destroy", &Tray::Destroy) .SetMethod("setImage", &Tray::SetImage) .SetMethod("setPressedImage", &Tray::SetPressedImage) .SetMethod("setToolTip", &Tray::SetToolTip) diff --git a/atom/browser/api/atom_api_tray.h b/atom/browser/api/atom_api_tray.h index a90202f3bcd4..13b0e0da316a 100644 --- a/atom/browser/api/atom_api_tray.h +++ b/atom/browser/api/atom_api_tray.h @@ -15,6 +15,10 @@ namespace gfx { class ImageSkia; } +namespace mate { +class Arguments; +} + namespace atom { class TrayIcon; @@ -39,14 +43,17 @@ class Tray : public mate::EventEmitter, void OnClicked() override; void OnDoubleClicked() override; - void SetImage(const gfx::ImageSkia& image); - void SetPressedImage(const gfx::ImageSkia& image); - void SetToolTip(const std::string& tool_tip); - void SetTitle(const std::string& title); - void SetHighlightMode(bool highlight); - void SetContextMenu(Menu* menu); + void Destroy(); + void SetImage(mate::Arguments* args, const gfx::ImageSkia& image); + void SetPressedImage(mate::Arguments* args, const gfx::ImageSkia& image); + void SetToolTip(mate::Arguments* args, const std::string& tool_tip); + void SetTitle(mate::Arguments* args, const std::string& title); + void SetHighlightMode(mate::Arguments* args, bool highlight); + void SetContextMenu(mate::Arguments* args, Menu* menu); private: + bool CheckTrayLife(mate::Arguments* args); + scoped_ptr tray_icon_; DISALLOW_COPY_AND_ASSIGN(Tray); diff --git a/docs/api/tray.md b/docs/api/tray.md index a25fd6cbbfe9..5175a16823e3 100644 --- a/docs/api/tray.md +++ b/docs/api/tray.md @@ -54,6 +54,10 @@ Emitted when the tray icon is double clicked. This is only implmented on OS X. +### Tray.destroy() + +Destroys the tray icon immediately. + ### Tray.setImage(image) * `image` [Image](image.md)