Skip to content

Commit

Permalink
Merge pull request #5624 from electron/hicon
Browse files Browse the repository at this point in the history
Feed Windows APIs with ICO icons of appropriate size
  • Loading branch information
zcbenz committed May 20, 2016
2 parents d5f3e5d + 3182485 commit 8b9d189
Show file tree
Hide file tree
Showing 20 changed files with 248 additions and 104 deletions.
47 changes: 33 additions & 14 deletions atom/browser/api/atom_api_tray.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "atom/browser/api/atom_api_menu.h"
#include "atom/browser/browser.h"
#include "atom/browser/ui/tray_icon.h"
#include "atom/common/api/atom_api_native_image.h"
#include "atom/common/native_mate_converters/gfx_converter.h"
#include "atom/common/native_mate_converters/image_converter.h"
#include "atom/common/native_mate_converters/string16_converter.h"
Expand All @@ -22,17 +23,18 @@ namespace atom {

namespace api {

Tray::Tray(v8::Isolate* isolate, const gfx::Image& image)
Tray::Tray(v8::Isolate* isolate, mate::Handle<NativeImage> image)
: tray_icon_(TrayIcon::Create()) {
tray_icon_->SetImage(image);
SetImage(isolate, image);
tray_icon_->AddObserver(this);
}

Tray::~Tray() {
}

// static
mate::WrappableBase* Tray::New(v8::Isolate* isolate, const gfx::Image& image) {
mate::WrappableBase* Tray::New(v8::Isolate* isolate,
mate::Handle<NativeImage> image) {
if (!Browser::Get()->is_ready()) {
isolate->ThrowException(v8::Exception::Error(mate::StringToV8(
isolate, "Cannot create Tray before app is ready")));
Expand Down Expand Up @@ -94,29 +96,38 @@ void Tray::OnDragEnded() {
Emit("drag-end");
}

void Tray::SetImage(mate::Arguments* args, const gfx::Image& image) {
tray_icon_->SetImage(image);
void Tray::SetImage(v8::Isolate* isolate, mate::Handle<NativeImage> image) {
#if defined(OS_WIN)
tray_icon_->SetImage(image->GetHICON(GetSystemMetrics(SM_CXSMICON)));
#else
tray_icon_->SetImage(image->image());
#endif
}

void Tray::SetPressedImage(mate::Arguments* args, const gfx::Image& image) {
tray_icon_->SetPressedImage(image);
void Tray::SetPressedImage(v8::Isolate* isolate,
mate::Handle<NativeImage> image) {
#if defined(OS_WIN)
tray_icon_->SetPressedImage(image->GetHICON(GetSystemMetrics(SM_CXSMICON)));
#else
tray_icon_->SetPressedImage(image->image());
#endif
}

void Tray::SetToolTip(mate::Arguments* args, const std::string& tool_tip) {
void Tray::SetToolTip(const std::string& tool_tip) {
tray_icon_->SetToolTip(tool_tip);
}

void Tray::SetTitle(mate::Arguments* args, const std::string& title) {
void Tray::SetTitle(const std::string& title) {
tray_icon_->SetTitle(title);
}

void Tray::SetHighlightMode(mate::Arguments* args, bool highlight) {
void Tray::SetHighlightMode(bool highlight) {
tray_icon_->SetHighlightMode(highlight);
}

void Tray::DisplayBalloon(mate::Arguments* args,
const mate::Dictionary& options) {
gfx::Image icon;
mate::Handle<NativeImage> icon;
options.Get("icon", &icon);
base::string16 title, content;
if (!options.Get("title", &title) ||
Expand All @@ -125,7 +136,14 @@ void Tray::DisplayBalloon(mate::Arguments* args,
return;
}

tray_icon_->DisplayBalloon(icon, title, content);
#if defined(OS_WIN)
tray_icon_->DisplayBalloon(
icon.IsEmpty() ? NULL : icon->GetHICON(GetSystemMetrics(SM_CXSMICON)),
title, content);
#else
tray_icon_->DisplayBalloon(
icon.IsEmpty() ? gfx::Image() : icon->image(), title, content);
#endif
}

void Tray::PopUpContextMenu(mate::Arguments* args) {
Expand All @@ -136,7 +154,8 @@ void Tray::PopUpContextMenu(mate::Arguments* args) {
tray_icon_->PopUpContextMenu(pos, menu.IsEmpty() ? nullptr : menu->model());
}

void Tray::SetContextMenu(mate::Arguments* args, Menu* menu) {
void Tray::SetContextMenu(v8::Isolate* isolate, mate::Handle<Menu> menu) {
menu_.Reset(isolate, menu.ToV8());
tray_icon_->SetContextMenu(menu->model());
}

Expand All @@ -162,7 +181,7 @@ void Tray::BuildPrototype(v8::Isolate* isolate,
.SetMethod("setHighlightMode", &Tray::SetHighlightMode)
.SetMethod("displayBalloon", &Tray::DisplayBalloon)
.SetMethod("popUpContextMenu", &Tray::PopUpContextMenu)
.SetMethod("_setContextMenu", &Tray::SetContextMenu);
.SetMethod("setContextMenu", &Tray::SetContextMenu);
}

} // namespace api
Expand Down
19 changes: 11 additions & 8 deletions atom/browser/api/atom_api_tray.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "atom/browser/api/trackable_object.h"
#include "atom/browser/ui/tray_icon_observer.h"
#include "base/memory/scoped_ptr.h"
#include "native_mate/handle.h"

namespace gfx {
class Image;
Expand All @@ -28,18 +29,19 @@ class TrayIcon;
namespace api {

class Menu;
class NativeImage;

class Tray : public mate::TrackableObject<Tray>,
public TrayIconObserver {
public:
static mate::WrappableBase* New(
v8::Isolate* isolate, const gfx::Image& image);
v8::Isolate* isolate, mate::Handle<NativeImage> image);

static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> prototype);

protected:
Tray(v8::Isolate* isolate, const gfx::Image& image);
Tray(v8::Isolate* isolate, mate::Handle<NativeImage> image);
~Tray() override;

// TrayIconObserver:
Expand All @@ -55,18 +57,19 @@ class Tray : public mate::TrackableObject<Tray>,
void OnDragExited() override;
void OnDragEnded() override;

void SetImage(mate::Arguments* args, const gfx::Image& image);
void SetPressedImage(mate::Arguments* args, const gfx::Image& 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 SetImage(v8::Isolate* isolate, mate::Handle<NativeImage> image);
void SetPressedImage(v8::Isolate* isolate, mate::Handle<NativeImage> image);
void SetToolTip(const std::string& tool_tip);
void SetTitle(const std::string& title);
void SetHighlightMode(bool highlight);
void DisplayBalloon(mate::Arguments* args, const mate::Dictionary& options);
void PopUpContextMenu(mate::Arguments* args);
void SetContextMenu(mate::Arguments* args, Menu* menu);
void SetContextMenu(v8::Isolate* isolate, mate::Handle<Menu> menu);

private:
v8::Local<v8::Object> ModifiersToObject(v8::Isolate* isolate, int modifiers);

v8::Global<v8::Object> menu_;
scoped_ptr<TrayIcon> tray_icon_;

DISALLOW_COPY_AND_ASSIGN(Tray);
Expand Down
27 changes: 26 additions & 1 deletion atom/browser/api/atom_api_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
#include "native_mate/dictionary.h"
#include "ui/gfx/geometry/rect.h"

#if defined(OS_WIN)
#if defined(TOOLKIT_VIEWS)
#include "atom/browser/native_window_views.h"
#endif

#if defined(OS_WIN)
#include "atom/browser/ui/win/taskbar_host.h"
#endif

Expand Down Expand Up @@ -99,6 +102,13 @@ Window::Window(v8::Isolate* isolate, const mate::Dictionary& options) {
window_->InitFromOptions(options);
window_->AddObserver(this);
AttachAsUserData(window_.get());

#if defined(TOOLKIT_VIEWS)
// Sets the window icon.
mate::Handle<NativeImage> icon;
if (options.Get(options::kIcon, &icon))
SetIcon(icon);
#endif
}

Window::~Window() {
Expand Down Expand Up @@ -617,6 +627,18 @@ void Window::ShowDefinitionForSelection() {
}
#endif

#if defined(TOOLKIT_VIEWS)
void Window::SetIcon(mate::Handle<NativeImage> icon) {
#if defined(OS_WIN)
static_cast<NativeWindowViews*>(window_.get())->SetIcon(
icon->GetHICON(GetSystemMetrics(SM_CXSMICON)), icon->GetHICON(256));
#elif defined(USE_X11)
static_cast<NativeWindowViews*>(window_.get())->SetIcon(
icon->image().AsImageSkia());
#endif
}
#endif

void Window::SetAspectRatio(double aspect_ratio, mate::Arguments* args) {
gfx::Size extra_size;
args->GetNext(&extra_size);
Expand Down Expand Up @@ -738,6 +760,9 @@ void Window::BuildPrototype(v8::Isolate* isolate,
#if defined(OS_MACOSX)
.SetMethod("showDefinitionForSelection",
&Window::ShowDefinitionForSelection)
#endif
#if defined(TOOLKIT_VIEWS)
.SetMethod("setIcon", &Window::SetIcon)
#endif
.SetProperty("id", &Window::ID)
.SetProperty("webContents", &Window::WebContents);
Expand Down
5 changes: 5 additions & 0 deletions atom/browser/api/atom_api_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "atom/browser/api/trackable_object.h"
#include "atom/browser/native_window.h"
#include "atom/browser/native_window_observer.h"
#include "atom/common/api/atom_api_native_image.h"
#include "native_mate/handle.h"

class GURL;
Expand Down Expand Up @@ -172,6 +173,10 @@ class Window : public mate::TrackableObject<Window>,
void ShowDefinitionForSelection();
#endif

#if defined(TOOLKIT_VIEWS)
void SetIcon(mate::Handle<NativeImage> icon);
#endif

void SetVisibleOnAllWorkspaces(bool visible);
bool IsVisibleOnAllWorkspaces();

Expand Down
4 changes: 0 additions & 4 deletions atom/browser/native_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/window_list.h"
#include "atom/common/api/api_messages.h"
#include "atom/common/native_mate_converters/image_converter.h"
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "atom/common/options_switches.h"
#include "base/files/file_util.h"
Expand Down Expand Up @@ -65,9 +64,6 @@ NativeWindow::NativeWindow(
// mode.
ui::GpuSwitchingManager::SetTransparent(transparent_);

// Read icon before window is created.
options.Get(options::kIcon, &icon_);

WindowList::AddWindow(this);
}

Expand Down
4 changes: 0 additions & 4 deletions atom/browser/native_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ class NativeWindow : public base::SupportsUserData,
bool transparent() const { return transparent_; }
SkRegion* draggable_region() const { return draggable_region_.get(); }
bool enable_larger_than_screen() const { return enable_larger_than_screen_; }
gfx::ImageSkia icon() const { return icon_; }

void set_has_dialog_attached(bool has_dialog_attached) {
has_dialog_attached_ = has_dialog_attached;
Expand Down Expand Up @@ -307,9 +306,6 @@ class NativeWindow : public base::SupportsUserData,
// Whether window can be resized larger than screen.
bool enable_larger_than_screen_;

// Window icon.
gfx::ImageSkia icon_;

// The windows has been closed.
bool is_closed_;

Expand Down
28 changes: 19 additions & 9 deletions atom/browser/native_window_views.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "atom/browser/ui/views/menu_layout.h"
#include "atom/common/color_util.h"
#include "atom/common/draggable_region.h"
#include "atom/common/native_mate_converters/image_converter.h"
#include "atom/common/options_switches.h"
#include "base/strings/utf_string_conversions.h"
#include "brightray/browser/inspectable_web_contents.h"
Expand Down Expand Up @@ -40,6 +41,7 @@
#include "chrome/browser/ui/libgtk2ui/unity_service.h"
#include "ui/base/x/x11_util.h"
#include "ui/gfx/x/x11_types.h"
#include "ui/views/widget/desktop_aura/desktop_window_tree_host_x11.h"
#include "ui/views/window/native_frame_view.h"
#elif defined(OS_WIN)
#include "atom/browser/ui/views/win_frame_view.h"
Expand Down Expand Up @@ -273,7 +275,6 @@ NativeWindowViews::NativeWindowViews(
use_content_size_)
size = ContentSizeToWindowSize(size);

window_->UpdateWindowIcon();
window_->CenterWindow(size);
Layout();
}
Expand Down Expand Up @@ -778,6 +779,23 @@ gfx::AcceleratedWidget NativeWindowViews::GetAcceleratedWidget() {
return GetNativeWindow()->GetHost()->GetAcceleratedWidget();
}

#if defined(OS_WIN)
void NativeWindowViews::SetIcon(HICON small_icon, HICON app_icon) {
HWND hwnd = GetAcceleratedWidget();
SendMessage(hwnd, WM_SETICON, ICON_SMALL,
reinterpret_cast<LPARAM>(small_icon));
SendMessage(hwnd, WM_SETICON, ICON_BIG,
reinterpret_cast<LPARAM>(app_icon));
}
#elif defined(USE_X11)
void NativeWindowViews::SetIcon(const gfx::ImageSkia& icon) {
views::DesktopWindowTreeHostX11* tree_host =
views::DesktopWindowTreeHostX11::GetHostForXID(GetAcceleratedWidget());
static_cast<views::DesktopWindowTreeHost*>(tree_host)->SetWindowIcons(
icon, icon);
}
#endif

void NativeWindowViews::OnWidgetActivationChanged(
views::Widget* widget, bool active) {
if (widget != window_.get())
Expand Down Expand Up @@ -842,14 +860,6 @@ bool NativeWindowViews::ShouldHandleSystemCommands() const {
return true;
}

gfx::ImageSkia NativeWindowViews::GetWindowAppIcon() {
return icon();
}

gfx::ImageSkia NativeWindowViews::GetWindowIcon() {
return GetWindowAppIcon();
}

views::Widget* NativeWindowViews::GetWidget() {
return window_.get();
}
Expand Down
10 changes: 6 additions & 4 deletions atom/browser/native_window_views.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ class NativeWindowViews : public NativeWindow,

gfx::AcceleratedWidget GetAcceleratedWidget() override;

#if defined(OS_WIN)
void SetIcon(HICON small_icon, HICON app_icon);
#elif defined(USE_X11)
void SetIcon(const gfx::ImageSkia& icon);
#endif

views::Widget* widget() const { return window_.get(); }

#if defined(OS_WIN)
Expand All @@ -125,8 +131,6 @@ class NativeWindowViews : public NativeWindow,
bool CanMinimize() const override;
base::string16 GetWindowTitle() const override;
bool ShouldHandleSystemCommands() const override;
gfx::ImageSkia GetWindowAppIcon() override;
gfx::ImageSkia GetWindowIcon() override;
views::Widget* GetWidget() override;
const views::Widget* GetWidget() const override;
views::View* GetContentsView() override;
Expand All @@ -145,7 +149,6 @@ class NativeWindowViews : public NativeWindow,
// MessageHandlerDelegate:
bool PreHandleMSG(
UINT message, WPARAM w_param, LPARAM l_param, LRESULT* result) override;

void HandleSizeEvent(WPARAM w_param, LPARAM l_param);
#endif

Expand Down Expand Up @@ -202,7 +205,6 @@ class NativeWindowViews : public NativeWindow,

// If true we have enabled a11y
bool enabled_a11y_support_;

#endif

// Handles unhandled keyboard messages coming back from the renderer process.
Expand Down
4 changes: 2 additions & 2 deletions atom/browser/ui/tray_icon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
}
Expand Down
Loading

0 comments on commit 8b9d189

Please sign in to comment.