From 6f5184f001199d40734021fabd8fb12b47f10213 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 30 May 2014 14:37:53 +0800 Subject: [PATCH] mac: Simple implementation of tray icon. --- atom/browser/ui/tray_icon.cc | 6 +----- atom/browser/ui/tray_icon.h | 4 +--- atom/browser/ui/tray_icon_cocoa.h | 11 +++++++++++ atom/browser/ui/tray_icon_cocoa.mm | 25 +++++++++++++++++++++++++ atom/browser/ui/tray_icon_gtk.cc | 3 +++ atom/browser/ui/tray_icon_gtk.h | 1 + atom/browser/ui/tray_icon_win.cc | 3 +++ atom/browser/ui/tray_icon_win.h | 1 + 8 files changed, 46 insertions(+), 8 deletions(-) diff --git a/atom/browser/ui/tray_icon.cc b/atom/browser/ui/tray_icon.cc index 5b707756d9dc..c67c9bbcf562 100644 --- a/atom/browser/ui/tray_icon.cc +++ b/atom/browser/ui/tray_icon.cc @@ -6,14 +6,10 @@ namespace atom { -TrayIcon::TrayIcon() : model_(NULL) { +TrayIcon::TrayIcon() { } TrayIcon::~TrayIcon() { } -void TrayIcon::SetContextMenu(ui::SimpleMenuModel* menu_model) { - model_ = menu_model; -} - } // namespace atom diff --git a/atom/browser/ui/tray_icon.h b/atom/browser/ui/tray_icon.h index 69cdba2ab5ad..ed96653bcdab 100644 --- a/atom/browser/ui/tray_icon.h +++ b/atom/browser/ui/tray_icon.h @@ -28,15 +28,13 @@ class TrayIcon { virtual void SetToolTip(const std::string& tool_tip) = 0; // Set the context menu for this icon. - void SetContextMenu(ui::SimpleMenuModel* menu_model); + virtual void SetContextMenu(ui::SimpleMenuModel* menu_model) = 0; protected: TrayIcon(); virtual ~TrayIcon(); private: - ui::SimpleMenuModel* model_; // Weak reference. - DISALLOW_COPY_AND_ASSIGN(TrayIcon); }; diff --git a/atom/browser/ui/tray_icon_cocoa.h b/atom/browser/ui/tray_icon_cocoa.h index 73da462351c9..eb18c15db1ee 100644 --- a/atom/browser/ui/tray_icon_cocoa.h +++ b/atom/browser/ui/tray_icon_cocoa.h @@ -5,9 +5,14 @@ #ifndef ATOM_BROWSER_UI_TRAY_ICON_COCOA_H_ #define ATOM_BROWSER_UI_TRAY_ICON_COCOA_H_ +#import + #include #include "atom/browser/ui/tray_icon.h" +#include "base/mac/scoped_nsobject.h" + +@class AtomMenuController; namespace atom { @@ -16,11 +21,17 @@ class TrayIconCocoa : public TrayIcon { virtual void SetImage(const gfx::ImageSkia& image) OVERRIDE; virtual void SetPressedImage(const gfx::ImageSkia& image) OVERRIDE; virtual void SetToolTip(const std::string& tool_tip) OVERRIDE; + virtual void SetContextMenu(ui::SimpleMenuModel* menu_model) OVERRIDE; private: TrayIconCocoa(); virtual ~TrayIconCocoa(); + base::scoped_nsobject item_; + + // Status menu shown when right-clicking the system icon. + base::scoped_nsobject menu_; + DISALLOW_COPY_AND_ASSIGN(TrayIconCocoa); }; diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index ff3aa469bfb8..ee42c69e44de 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -4,21 +4,46 @@ #include "atom/browser/ui/tray_icon_cocoa.h" +#include "atom/browser/ui/cocoa/atom_menu_controller.h" +#include "base/strings/sys_string_conversions.h" +#include "skia/ext/skia_utils_mac.h" + namespace atom { TrayIconCocoa::TrayIconCocoa() { + item_.reset([[[NSStatusBar systemStatusBar] + statusItemWithLength:NSVariableStatusItemLength] retain]); + [item_ setEnabled:YES]; + [item_ setHighlightMode:YES]; } TrayIconCocoa::~TrayIconCocoa() { } void TrayIconCocoa::SetImage(const gfx::ImageSkia& image) { + if (!image.isNull()) { + NSImage* ns_image = gfx::SkBitmapToNSImage(*image.bitmap()); + if (ns_image) + [item_ setImage:ns_image]; + } } void TrayIconCocoa::SetPressedImage(const gfx::ImageSkia& image) { + if (!image.isNull()) { + NSImage* ns_image = gfx::SkBitmapToNSImage(*image.bitmap()); + if (ns_image) + [item_ setAlternateImage:ns_image]; + } } void TrayIconCocoa::SetToolTip(const std::string& tool_tip) { + [item_ setToolTip:base::SysUTF8ToNSString(tool_tip)]; +} + +void TrayIconCocoa::SetContextMenu(ui::SimpleMenuModel* menu_model) { + TrayIcon::SetContextMenu(menu_model); + menu_.reset([[AtomMenuController alloc] initWithModel:menu_model]); + [item_ setMenu:[menu_ menu]]; } } // namespace atom diff --git a/atom/browser/ui/tray_icon_gtk.cc b/atom/browser/ui/tray_icon_gtk.cc index 1a61b2f462a1..40c5bbbb4abb 100644 --- a/atom/browser/ui/tray_icon_gtk.cc +++ b/atom/browser/ui/tray_icon_gtk.cc @@ -21,4 +21,7 @@ void TrayIconGtk::SetPressedImage(const gfx::ImageSkia& image) { void TrayIconGtk::SetToolTip(const std::string& tool_tip) { } +void TrayIconGtk::SetContextMenu(ui::SimpleMenuModel* menu_model) { +} + } // namespace atom diff --git a/atom/browser/ui/tray_icon_gtk.h b/atom/browser/ui/tray_icon_gtk.h index b4845ad78107..da398e4eee94 100644 --- a/atom/browser/ui/tray_icon_gtk.h +++ b/atom/browser/ui/tray_icon_gtk.h @@ -16,6 +16,7 @@ class TrayIconGtk : public TrayIcon { virtual void SetImage(const gfx::ImageSkia& image) OVERRIDE; virtual void SetPressedImage(const gfx::ImageSkia& image) OVERRIDE; virtual void SetToolTip(const std::string& tool_tip) OVERRIDE; + virtual void SetContextMenu(ui::SimpleMenuModel* menu_model) OVERRIDE; private: TrayIconGtk(); diff --git a/atom/browser/ui/tray_icon_win.cc b/atom/browser/ui/tray_icon_win.cc index 21bf2f6ad292..0ef5b0fdef79 100644 --- a/atom/browser/ui/tray_icon_win.cc +++ b/atom/browser/ui/tray_icon_win.cc @@ -21,4 +21,7 @@ void TrayIconWin::SetPressedImage(const gfx::ImageSkia& image) { void TrayIconWin::SetToolTip(const std::string& tool_tip) { } +void TrayIconWin::SetContextMenu(ui::SimpleMenuModel* menu_model) { +} + } // namespace atom diff --git a/atom/browser/ui/tray_icon_win.h b/atom/browser/ui/tray_icon_win.h index 4724e49b4341..b7077037d404 100644 --- a/atom/browser/ui/tray_icon_win.h +++ b/atom/browser/ui/tray_icon_win.h @@ -16,6 +16,7 @@ class TrayIconWin : public TrayIcon { virtual void SetImage(const gfx::ImageSkia& image) OVERRIDE; virtual void SetPressedImage(const gfx::ImageSkia& image) OVERRIDE; virtual void SetToolTip(const std::string& tool_tip) OVERRIDE; + virtual void SetContextMenu(ui::SimpleMenuModel* menu_model) OVERRIDE; private: TrayIconWin();