Browse Source

gtk: Add TrayIcon implementation for GtkStatusIcon.

Cheng Zhao 11 years ago
parent
commit
065185baea

+ 2 - 1
atom.gyp

@@ -130,6 +130,8 @@
       'atom/browser/ui/file_dialog_gtk.cc',
       'atom/browser/ui/file_dialog_mac.mm',
       'atom/browser/ui/file_dialog_win.cc',
+      'atom/browser/ui/gtk/status_icon.cc',
+      'atom/browser/ui/gtk/status_icon.h',
       'atom/browser/ui/message_box.h',
       'atom/browser/ui/message_box_gtk.cc',
       'atom/browser/ui/message_box_mac.mm',
@@ -137,7 +139,6 @@
       'atom/browser/ui/tray_icon.cc',
       'atom/browser/ui/tray_icon.h',
       'atom/browser/ui/tray_icon_gtk.cc',
-      'atom/browser/ui/tray_icon_gtk.h',
       'atom/browser/ui/tray_icon_cocoa.h',
       'atom/browser/ui/tray_icon_cocoa.mm',
       'atom/browser/ui/tray_icon_win.cc',

+ 51 - 0
atom/browser/ui/gtk/status_icon.cc

@@ -0,0 +1,51 @@
+// Copyright (c) 2014 GitHub, Inc. All rights reserved.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#include "atom/browser/ui/gtk/status_icon.h"
+
+#include "chrome/browser/ui/gtk/menu_gtk.h"
+#include "ui/gfx/gtk_util.h"
+
+namespace atom {
+
+StatusIcon::StatusIcon() : icon_(gtk_status_icon_new()) {
+  gtk_status_icon_set_visible(icon_, TRUE);
+
+  g_signal_connect(icon_, "popup-menu", G_CALLBACK(OnPopupMenuThunk), this);
+}
+
+StatusIcon::~StatusIcon() {
+  gtk_status_icon_set_visible(icon_, FALSE);
+  g_object_unref(icon_);
+}
+
+void StatusIcon::SetImage(const gfx::ImageSkia& image) {
+  if (image.isNull())
+    return;
+
+  GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(*image.bitmap());
+  gtk_status_icon_set_from_pixbuf(icon_, pixbuf);
+  g_object_unref(pixbuf);
+}
+
+void StatusIcon::SetPressedImage(const gfx::ImageSkia& image) {
+  // Ignore pressed images, since the standard on Linux is to not highlight
+  // pressed status icons.
+}
+
+void StatusIcon::SetToolTip(const std::string& tool_tip) {
+  gtk_status_icon_set_tooltip_text(icon_, tool_tip.c_str());
+}
+
+void StatusIcon::SetContextMenu(ui::SimpleMenuModel* menu_model) {
+  menu_.reset(new MenuGtk(NULL, menu_model));
+}
+
+void StatusIcon::OnPopupMenu(GtkWidget* widget, guint button, guint time) {
+  // If we have a menu - display it.
+  if (menu_.get())
+    menu_->PopupAsContextForStatusIcon(time, button, icon_);
+}
+
+}  // namespace atom

+ 44 - 0
atom/browser/ui/gtk/status_icon.h

@@ -0,0 +1,44 @@
+// Copyright (c) 2014 GitHub, Inc. All rights reserved.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#ifndef ATOM_BROWSER_UI_GTK_STATUS_ICON_H_
+#define ATOM_BROWSER_UI_GTK_STATUS_ICON_H_
+
+#include <string>
+
+#include <gtk/gtk.h>
+
+#include "atom/browser/ui/tray_icon.h"
+#include "ui/base/gtk/gtk_signal.h"
+
+class MenuGtk;
+
+namespace atom {
+
+class StatusIcon : public TrayIcon {
+ public:
+  StatusIcon();
+  virtual ~StatusIcon();
+
+  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:
+  // Callback invoked when user right-clicks on the status icon.
+  CHROMEGTK_CALLBACK_2(StatusIcon, void, OnPopupMenu, guint, guint);
+
+  // The currently-displayed icon for the window.
+  GtkStatusIcon* icon_;
+
+  // The context menu for this icon (if any).
+  scoped_ptr<MenuGtk> menu_;
+
+  DISALLOW_COPY_AND_ASSIGN(StatusIcon);
+};
+
+}  // namespace atom
+
+#endif  // ATOM_BROWSER_UI_GTK_STATUS_ICON_H_

+ 2 - 20
atom/browser/ui/tray_icon_gtk.cc

@@ -2,31 +2,13 @@
 // Use of this source code is governed by the MIT license that can be
 // found in the LICENSE file.
 
-#include "atom/browser/ui/tray_icon_gtk.h"
+#include "atom/browser/ui/gtk/status_icon.h"
 
 namespace atom {
 
-TrayIconGtk::TrayIconGtk() {
-}
-
-TrayIconGtk::~TrayIconGtk() {
-}
-
-void TrayIconGtk::SetImage(const gfx::ImageSkia& image) {
-}
-
-void TrayIconGtk::SetPressedImage(const gfx::ImageSkia& image) {
-}
-
-void TrayIconGtk::SetToolTip(const std::string& tool_tip) {
-}
-
-void TrayIconGtk::SetContextMenu(ui::SimpleMenuModel* menu_model) {
-}
-
 // static
 TrayIcon* TrayIcon::Create() {
-  return new TrayIconGtk;
+  return new StatusIcon;
 }
 
 }  // namespace atom

+ 0 - 30
atom/browser/ui/tray_icon_gtk.h

@@ -1,30 +0,0 @@
-// Copyright (c) 2014 GitHub, Inc. All rights reserved.
-// Use of this source code is governed by the MIT license that can be
-// found in the LICENSE file.
-
-#ifndef ATOM_BROWSER_UI_TRAY_ICON_GTK_H_
-#define ATOM_BROWSER_UI_TRAY_ICON_GTK_H_
-
-#include <string>
-
-#include "atom/browser/ui/tray_icon.h"
-
-namespace atom {
-
-class TrayIconGtk : public TrayIcon {
- public:
-  TrayIconGtk();
-  virtual ~TrayIconGtk();
-
-  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:
-  DISALLOW_COPY_AND_ASSIGN(TrayIconGtk);
-};
-
-}  // namespace atom
-
-#endif  // ATOM_BROWSER_UI_TRAY_ICON_GTK_H_