Load HICON directly in NotifyIcon

This commit is contained in:
Cheng Zhao 2016-05-20 16:55:22 +09:00
parent 67d9ae27c3
commit 9e26e5c121
7 changed files with 62 additions and 36 deletions

View file

@ -12,7 +12,7 @@ TrayIcon::TrayIcon() {
TrayIcon::~TrayIcon() {
}
void TrayIcon::SetPressedImage(const gfx::Image& image) {
void TrayIcon::SetPressedImage(ImageType image) {
}
void TrayIcon::SetTitle(const std::string& title) {
@ -21,7 +21,7 @@ void TrayIcon::SetTitle(const std::string& title) {
void TrayIcon::SetHighlightMode(bool highlight) {
}
void TrayIcon::DisplayBalloon(const gfx::Image& icon,
void TrayIcon::DisplayBalloon(ImageType icon,
const base::string16& title,
const base::string16& contents) {
}

View file

@ -19,13 +19,19 @@ class TrayIcon {
public:
static TrayIcon* Create();
#if defined(OS_WIN)
using ImageType = HICON;
#else
using ImageType = const gfx::Image&;
#endif
virtual ~TrayIcon();
// Sets the image associated with this status icon.
virtual void SetImage(const gfx::Image& image) = 0;
virtual void SetImage(ImageType image) = 0;
// Sets the image associated with this status icon when pressed.
virtual void SetPressedImage(const gfx::Image& image);
virtual void SetPressedImage(ImageType image);
// Sets the hover text for this status icon. This is also used as the label
// for the menu item which is created as a replacement for the status icon
@ -43,7 +49,7 @@ class TrayIcon {
// Displays a notification balloon with the specified contents.
// Depending on the platform it might not appear by the icon tray.
virtual void DisplayBalloon(const gfx::Image& icon,
virtual void DisplayBalloon(ImageType icon,
const base::string16& title,
const base::string16& contents);

View file

@ -9,7 +9,6 @@
#include "base/strings/utf_string_conversions.h"
#include "base/win/windows_version.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/icon_util.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
@ -26,6 +25,7 @@ NotifyIcon::NotifyIcon(NotifyIconHost* host,
icon_id_(id),
window_(window),
message_id_(message),
icon_(NULL),
menu_model_(NULL) {
NOTIFYICONDATA icon_data;
InitIconData(&icon_data);
@ -80,7 +80,7 @@ void NotifyIcon::ResetIcon() {
InitIconData(&icon_data);
icon_data.uFlags |= NIF_MESSAGE;
icon_data.uCallbackMessage = message_id_;
icon_data.hIcon = icon_.get();
icon_data.hIcon = icon_;
// If we have an image, then set the NIF_ICON flag, which tells
// Shell_NotifyIcon() to set the image for the status icon it creates.
if (icon_data.hIcon)
@ -91,19 +91,19 @@ void NotifyIcon::ResetIcon() {
LOG(WARNING) << "Unable to re-create status tray icon.";
}
void NotifyIcon::SetImage(const gfx::Image& image) {
void NotifyIcon::SetImage(HICON image) {
// Create the icon.
icon_ = image;
NOTIFYICONDATA icon_data;
InitIconData(&icon_data);
icon_data.uFlags |= NIF_ICON;
icon_ = IconUtil::CreateHICONFromSkBitmap(image.AsBitmap());
icon_data.hIcon = icon_.get();
icon_data.hIcon = image;
BOOL result = Shell_NotifyIcon(NIM_MODIFY, &icon_data);
if (!result)
LOG(WARNING) << "Error setting status tray icon image";
}
void NotifyIcon::SetPressedImage(const gfx::Image& image) {
void NotifyIcon::SetPressedImage(HICON image) {
// Ignore pressed images, since the standard on Windows is to not highlight
// pressed status icons.
}
@ -119,7 +119,7 @@ void NotifyIcon::SetToolTip(const std::string& tool_tip) {
LOG(WARNING) << "Unable to set tooltip for status tray icon";
}
void NotifyIcon::DisplayBalloon(const gfx::Image& icon,
void NotifyIcon::DisplayBalloon(HICON icon,
const base::string16& title,
const base::string16& contents) {
NOTIFYICONDATA icon_data;
@ -129,13 +129,8 @@ void NotifyIcon::DisplayBalloon(const gfx::Image& icon,
wcsncpy_s(icon_data.szInfoTitle, title.c_str(), _TRUNCATE);
wcsncpy_s(icon_data.szInfo, contents.c_str(), _TRUNCATE);
icon_data.uTimeout = 0;
base::win::Version win_version = base::win::GetVersion();
if (!icon.IsEmpty() && win_version != base::win::VERSION_PRE_XP) {
balloon_icon_ = IconUtil::CreateHICONFromSkBitmap(icon.AsBitmap());
icon_data.hBalloonIcon = balloon_icon_.get();
icon_data.dwInfoFlags = NIIF_USER | NIIF_LARGE_ICON;
}
icon_data.hBalloonIcon = icon;
icon_data.dwInfoFlags = NIIF_USER | NIIF_LARGE_ICON;
BOOL result = Shell_NotifyIcon(NIM_MODIFY, &icon_data);
if (!result)

View file

@ -14,7 +14,6 @@
#include "base/macros.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/win/scoped_gdi_object.h"
namespace gfx {
class Point;
@ -45,10 +44,10 @@ class NotifyIcon : public TrayIcon {
UINT message_id() const { return message_id_; }
// Overridden from TrayIcon:
void SetImage(const gfx::Image& image) override;
void SetPressedImage(const gfx::Image& image) override;
void SetImage(HICON image) override;
void SetPressedImage(HICON image) override;
void SetToolTip(const std::string& tool_tip) override;
void DisplayBalloon(const gfx::Image& icon,
void DisplayBalloon(HICON icon,
const base::string16& title,
const base::string16& contents) override;
void PopUpContextMenu(const gfx::Point& pos,
@ -71,10 +70,7 @@ class NotifyIcon : public TrayIcon {
UINT message_id_;
// The currently-displayed icon for the window.
base::win::ScopedHICON icon_;
// The currently-displayed icon for the notification balloon.
base::win::ScopedHICON balloon_icon_;
HICON icon_;
// The context menu.
ui::SimpleMenuModel* menu_model_;