Support high dpi icon as window icon.

This commit is contained in:
Cheng Zhao 2014-06-23 22:26:01 +08:00
parent b92e6e97ea
commit ca1d2a32b0
4 changed files with 15 additions and 32 deletions

View file

@ -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<const unsigned char*>(file_contents.data());
scoped_ptr<SkBitmap> 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();
}

View file

@ -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<gfx::Image> icon_;
private:
// Schedule a notification unresponsive event.

View file

@ -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);

View file

@ -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() {