Correctly set window icon from ICO
authorCheng Zhao <zcbenz@gmail.com>
Fri, 20 May 2016 10:46:05 +0000 (19:46 +0900)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 20 May 2016 10:46:05 +0000 (19:46 +0900)
atom/browser/api/atom_api_window.cc
atom/browser/native_window.cc
atom/browser/native_window.h
atom/browser/native_window_views.cc
atom/browser/native_window_views.h
atom/browser/native_window_views_win.cc

index 399b4d3..caefd27 100644 (file)
@@ -9,6 +9,7 @@
 #include "atom/browser/api/atom_api_web_contents.h"
 #include "atom/browser/browser.h"
 #include "atom/browser/native_window.h"
+#include "atom/common/api/atom_api_native_image.h"
 #include "atom/common/native_mate_converters/callback.h"
 #include "atom/common/native_mate_converters/gfx_converter.h"
 #include "atom/common/native_mate_converters/gurl_converter.h"
@@ -99,6 +100,15 @@ Window::Window(v8::Isolate* isolate, const mate::Dictionary& options) {
   window_->InitFromOptions(options);
   window_->AddObserver(this);
   AttachAsUserData(window_.get());
+
+#if defined(OS_WIN)
+  // Sets the window icon.
+  mate::Handle<NativeImage> icon;
+  if (options.Get(options::kIcon, &icon) && !icon.IsEmpty()) {
+    static_cast<NativeWindowViews*>(window_.get())->SetIcon(
+        icon->GetHICON(GetSystemMetrics(SM_CXSMICON)), icon->GetHICON(256));
+  }
+#endif
 }
 
 Window::~Window() {
index 0d5c26c..f0c0864 100644 (file)
@@ -65,9 +65,6 @@ NativeWindow::NativeWindow(
   // mode.
   ui::GpuSwitchingManager::SetTransparent(transparent_);
 
-  // Read icon before window is created.
-  options.Get(options::kIcon, &icon_);
-
   WindowList::AddWindow(this);
 }
 
index 7317a7b..6a96c01 100644 (file)
@@ -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;
@@ -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_;
 
index a04a5a0..d83e69b 100644 (file)
@@ -221,6 +221,10 @@ NativeWindowViews::NativeWindowViews(
   std::string window_type;
   if (options.Get(options::kType, &window_type))
     SetWindowType(GetAcceleratedWidget(), window_type);
+
+  // Set window icon.
+  options.Get(options::kIcon, &icon_);
+  window_->UpdateWindowIcon();
 #endif
 
   // Add web view.
@@ -273,7 +277,6 @@ NativeWindowViews::NativeWindowViews(
       use_content_size_)
     size = ContentSizeToWindowSize(size);
 
-  window_->UpdateWindowIcon();
   window_->CenterWindow(size);
   Layout();
 }
@@ -842,13 +845,15 @@ bool NativeWindowViews::ShouldHandleSystemCommands() const {
   return true;
 }
 
+#if defined(USE_X11)
 gfx::ImageSkia NativeWindowViews::GetWindowAppIcon() {
-  return icon();
+  return icon_;
 }
 
 gfx::ImageSkia NativeWindowViews::GetWindowIcon() {
   return GetWindowAppIcon();
 }
+#endif
 
 views::Widget* NativeWindowViews::GetWidget() {
   return window_.get();
index bcf3ca8..799c02e 100644 (file)
@@ -104,12 +104,14 @@ class NativeWindowViews : public NativeWindow,
 
   gfx::AcceleratedWidget GetAcceleratedWidget() override;
 
-  views::Widget* widget() const { return window_.get(); }
-
 #if defined(OS_WIN)
+  void SetIcon(HICON small_icon, HICON app_icon);
+
   TaskbarHost& taskbar_host() { return taskbar_host_; }
 #endif
 
+  views::Widget* widget() const { return window_.get(); }
+
  private:
   // views::WidgetObserver:
   void OnWidgetActivationChanged(
@@ -125,8 +127,10 @@ class NativeWindowViews : public NativeWindow,
   bool CanMinimize() const override;
   base::string16 GetWindowTitle() const override;
   bool ShouldHandleSystemCommands() const override;
+#if defined(USE_X11)
   gfx::ImageSkia GetWindowAppIcon() override;
   gfx::ImageSkia GetWindowIcon() override;
+#endif
   views::Widget* GetWidget() override;
   const views::Widget* GetWidget() const override;
   views::View* GetContentsView() override;
@@ -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
 
@@ -185,6 +188,9 @@ class NativeWindowViews : public NativeWindow,
   // we need to make sure size constraints are restored when window becomes
   // resizable again.
   extensions::SizeConstraints old_size_constraints_;
+
+  // Window icon.
+  gfx::ImageSkia icon_;
 #elif defined(OS_WIN)
   // Weak ref.
   AtomDesktopWindowTreeHostWin* atom_desktop_window_tree_host_win_;
@@ -202,7 +208,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.
index 038ab10..0cb6859 100644 (file)
@@ -73,6 +73,14 @@ const char* AppCommandToString(int command_id) {
 
 }  // namespace
 
+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));
+}
+
 bool NativeWindowViews::ExecuteWindowsCommand(int command_id) {
   std::string command = AppCommandToString(command_id);
   NotifyWindowExecuteWindowsCommand(command);