From ca1d2a32b0426ee3b133ea63a8729d1c1643bcf3 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 23 Jun 2014 22:26:01 +0800 Subject: [PATCH] Support high dpi icon as window icon. --- atom/browser/native_window.cc | 29 ++++++----------------------- atom/browser/native_window.h | 6 ++---- atom/browser/native_window_gtk.cc | 5 +++-- atom/browser/native_window_win.cc | 7 ++++--- 4 files changed, 15 insertions(+), 32 deletions(-) diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index b7331ba3..fc907af6 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -16,6 +16,7 @@ #include "atom/browser/window_list.h" #include "atom/common/api/api_messages.h" #include "atom/common/atom_version.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/command_line.h" @@ -41,6 +42,8 @@ #include "ipc/ipc_message_macros.h" #include "native_mate/dictionary.h" #include "ui/gfx/codec/png_codec.h" +#include "ui/gfx/image/image.h" +#include "ui/gfx/image/image_skia.h" #include "ui/gfx/point.h" #include "ui/gfx/rect.h" #include "ui/gfx/size.h" @@ -73,9 +76,9 @@ NativeWindow::NativeWindow(content::WebContents* web_contents, #endif // Read icon before window is created. - std::string icon; - if (options.Get(switches::kIcon, &icon) && !SetIcon(icon)) - LOG(ERROR) << "Failed to set icon to " << icon; + gfx::ImageSkia icon; + if (options.Get(switches::kIcon, &icon)) + icon_.reset(new gfx::Image(icon)); // Read iframe security before any navigation. options.Get(switches::kNodeIntegration, &node_integration_); @@ -257,26 +260,6 @@ bool NativeWindow::IsWebViewFocused() { return host_view && host_view->HasFocus(); } -bool NativeWindow::SetIcon(const std::string& str_path) { - base::FilePath path = base::FilePath::FromUTF8Unsafe(str_path); - - // Read the file from disk. - std::string file_contents; - if (path.empty() || !base::ReadFileToString(path, &file_contents)) - return false; - - // Decode the bitmap using WebKit's image decoder. - const unsigned char* data = - reinterpret_cast(file_contents.data()); - scoped_ptr decoded(new SkBitmap()); - gfx::PNGCodec::Decode(data, file_contents.length(), decoded.get()); - if (decoded->empty()) - return false; // Unable to decode. - - icon_ = gfx::Image::CreateFrom1xBitmap(*decoded.release()); - return true; -} - base::ProcessHandle NativeWindow::GetRenderProcessHandle() { return GetWebContents()->GetRenderProcessHost()->GetHandle(); } diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index 3f9f24e1..667df974 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -18,7 +18,6 @@ #include "atom/browser/native_window_observer.h" #include "content/public/browser/notification_registrar.h" #include "content/public/browser/notification_observer.h" -#include "ui/gfx/image/image.h" #include "vendor/brightray/browser/default_web_contents_delegate.h" #include "vendor/brightray/browser/inspectable_web_contents_delegate.h" #include "vendor/brightray/browser/inspectable_web_contents_impl.h" @@ -32,6 +31,7 @@ class WebContents; } namespace gfx { +class Image; class Point; class Rect; class Size; @@ -144,8 +144,6 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, virtual void BlurWebView(); virtual bool IsWebViewFocused(); - virtual bool SetIcon(const std::string& path); - // Returns the process handle of render process, useful for killing the // render process manually virtual base::ProcessHandle GetRenderProcessHandle(); @@ -251,7 +249,7 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, bool has_frame_; // Window icon. - gfx::Image icon_; + scoped_ptr icon_; private: // Schedule a notification unresponsive event. diff --git a/atom/browser/native_window_gtk.cc b/atom/browser/native_window_gtk.cc index 96a58ca8..f0f6b599 100644 --- a/atom/browser/native_window_gtk.cc +++ b/atom/browser/native_window_gtk.cc @@ -23,6 +23,7 @@ #include "ui/base/x/x11_util.h" #include "ui/gfx/font_render_params_linux.h" #include "ui/gfx/gtk_util.h" +#include "ui/gfx/image.h" #include "ui/gfx/rect.h" #include "ui/gfx/skia_utils_gtk.h" @@ -129,8 +130,8 @@ NativeWindowGtk::NativeWindowGtk(content::WebContents* web_contents, // Create the underlying gdk window. gtk_widget_realize(GTK_WIDGET(window_)); - if (!icon_.IsEmpty()) - gtk_window_set_icon(window_, icon_.ToGdkPixbuf()); + if (icon_) + gtk_window_set_icon(window_, icon_->ToGdkPixbuf()); ui::ActiveWindowWatcherX::AddObserver(this); diff --git a/atom/browser/native_window_win.cc b/atom/browser/native_window_win.cc index 4a0493c5..15f723ed 100644 --- a/atom/browser/native_window_win.cc +++ b/atom/browser/native_window_win.cc @@ -22,6 +22,7 @@ #include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents_view.h" #include "native_mate/dictionary.h" +#include "ui/gfx/image.h" #include "ui/gfx/path.h" #include "ui/base/models/simple_menu_model.h" #include "ui/views/widget/widget.h" @@ -527,10 +528,10 @@ bool NativeWindowWin::ShouldHandleSystemCommands() const { } gfx::ImageSkia NativeWindowWin::GetWindowAppIcon() { - if (icon_.IsEmpty()) - return gfx::ImageSkia(); + if (icon_) + return *(icon_->ToImageSkia()); else - return *icon_.ToImageSkia(); + return gfx::ImageSkia(); } gfx::ImageSkia NativeWindowWin::GetWindowIcon() { -- 2.43.0