From: Cheng Zhao Date: Sat, 31 May 2014 02:27:07 +0000 (+0800) Subject: gtk: Add TrayIcon implementation for GtkStatusIcon. X-Git-Tag: v0.13.0~7^2~11 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=065185baead13c81921c713be4e97386fe283254;p=electron.git gtk: Add TrayIcon implementation for GtkStatusIcon. --- diff --git a/atom.gyp b/atom.gyp index 3e011363..dd41d5c4 100644 --- a/atom.gyp +++ b/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', diff --git a/atom/browser/ui/gtk/status_icon.cc b/atom/browser/ui/gtk/status_icon.cc new file mode 100644 index 00000000..3de2a8f6 --- /dev/null +++ b/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 diff --git a/atom/browser/ui/gtk/status_icon.h b/atom/browser/ui/gtk/status_icon.h new file mode 100644 index 00000000..2e357443 --- /dev/null +++ b/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 + +#include + +#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 menu_; + + DISALLOW_COPY_AND_ASSIGN(StatusIcon); +}; + +} // namespace atom + +#endif // ATOM_BROWSER_UI_GTK_STATUS_ICON_H_ diff --git a/atom/browser/ui/tray_icon_gtk.cc b/atom/browser/ui/tray_icon_gtk.cc index 9069c074..ada6cef3 100644 --- a/atom/browser/ui/tray_icon_gtk.cc +++ b/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 diff --git a/atom/browser/ui/tray_icon_gtk.h b/atom/browser/ui/tray_icon_gtk.h deleted file mode 100644 index cc66819c..00000000 --- a/atom/browser/ui/tray_icon_gtk.h +++ /dev/null @@ -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 - -#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_