win: Enable setting window icon in creation options.

This commit is contained in:
Cheng Zhao 2013-11-11 19:23:35 +08:00
parent d5ffa4dc77
commit baa6d9730c
4 changed files with 47 additions and 14 deletions

View file

@ -6,6 +6,7 @@
#include <string> #include <string>
#include "base/file_util.h"
#include "base/message_loop.h" #include "base/message_loop.h"
#include "base/utf_string_conversions.h" #include "base/utf_string_conversions.h"
#include "base/values.h" #include "base/values.h"
@ -31,6 +32,7 @@
#include "ui/gfx/point.h" #include "ui/gfx/point.h"
#include "ui/gfx/rect.h" #include "ui/gfx/rect.h"
#include "ui/gfx/size.h" #include "ui/gfx/size.h"
#include "webkit/glue/image_decoder.h"
using content::NavigationEntry; using content::NavigationEntry;
@ -46,6 +48,12 @@ NativeWindow::NativeWindow(content::WebContents* web_contents,
brightray::InspectableWebContents::Create(web_contents)) { brightray::InspectableWebContents::Create(web_contents)) {
options->GetBoolean(switches::kFrame, &has_frame_); options->GetBoolean(switches::kFrame, &has_frame_);
std::string icon;
if (options->GetString(switches::kIcon, &icon)) {
if (!SetIcon(icon))
LOG(ERROR) << "Failed to set icon to " << icon;
}
web_contents->SetDelegate(this); web_contents->SetDelegate(this);
WindowList::AddWindow(this); WindowList::AddWindow(this);
@ -169,6 +177,27 @@ void NativeWindow::RestartHangMonitorTimeout() {
GetWebContents()->GetRenderViewHost()->RestartHangMonitorTimeout(); GetWebContents()->GetRenderViewHost()->RestartHangMonitorTimeout();
} }
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() || !file_util::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());
webkit_glue::ImageDecoder decoder;
scoped_ptr<SkBitmap> decoded(new SkBitmap());
*decoded = decoder.Decode(data, file_contents.length());
if (decoded->empty())
return false; // Unable to decode.
icon_ = gfx::Image::CreateFrom1xBitmap(*decoded.release());
return true;
}
void NativeWindow::CloseWebContents() { void NativeWindow::CloseWebContents() {
bool prevent_default = false; bool prevent_default = false;
FOR_EACH_OBSERVER(NativeWindowObserver, FOR_EACH_OBSERVER(NativeWindowObserver,

View file

@ -13,6 +13,7 @@
#include "content/public/browser/notification_registrar.h" #include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_observer.h" #include "content/public/browser/notification_observer.h"
#include "content/public/browser/web_contents_observer.h" #include "content/public/browser/web_contents_observer.h"
#include "ui/gfx/image/image.h"
#include "vendor/brightray/browser/default_web_contents_delegate.h" #include "vendor/brightray/browser/default_web_contents_delegate.h"
namespace base { namespace base {
@ -105,6 +106,7 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
virtual void BlurWebView(); virtual void BlurWebView();
virtual bool IsWebViewFocused(); virtual bool IsWebViewFocused();
virtual void RestartHangMonitorTimeout(); virtual void RestartHangMonitorTimeout();
virtual bool SetIcon(const std::string& path);
// The same with closing a tab in a real browser. // The same with closing a tab in a real browser.
// //
@ -176,6 +178,9 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
// Whether window has standard frame. // Whether window has standard frame.
bool has_frame_; bool has_frame_;
// Window icon.
gfx::Image icon_;
private: private:
void RendererUnresponsiveDelayed(); void RendererUnresponsiveDelayed();

View file

@ -4,7 +4,6 @@
#include "browser/native_window_win.h" #include "browser/native_window_win.h"
#include "app/win/resource.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/values.h" #include "base/values.h"
@ -33,8 +32,6 @@ namespace {
const int kResizeInsideBoundsSize = 5; const int kResizeInsideBoundsSize = 5;
const int kResizeAreaCornerSize = 16; const int kResizeAreaCornerSize = 16;
HANDLE g_exe_icon = NULL;
// Wrapper of NativeWidgetWin to handle WM_MENUCOMMAND messages, which are // Wrapper of NativeWidgetWin to handle WM_MENUCOMMAND messages, which are
// triggered by window menus. // triggered by window menus.
class MenuCommandNativeWidget : public views::NativeWidgetWin { class MenuCommandNativeWidget : public views::NativeWidgetWin {
@ -220,16 +217,10 @@ NativeWindowWin::NativeWindowWin(content::WebContents* web_contents,
gfx::Size size(width, height); gfx::Size size(width, height);
window_->CenterWindow(size); window_->CenterWindow(size);
window_->UpdateWindowIcon();
web_view_->SetWebContents(web_contents); web_view_->SetWebContents(web_contents);
OnViewWasResized(); OnViewWasResized();
if (g_exe_icon == NULL)
g_exe_icon = ::LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(1),
IMAGE_ICON, 0, 0, 0);
::SendMessage(window_->GetNativeWindow(),
WM_SETICON,
static_cast<WPARAM>(ICON_BIG),
reinterpret_cast<LPARAM>(g_exe_icon));
} }
NativeWindowWin::~NativeWindowWin() { NativeWindowWin::~NativeWindowWin() {
@ -474,8 +465,15 @@ bool NativeWindowWin::ShouldHandleSystemCommands() const {
return true; return true;
} }
bool NativeWindowWin::ShouldShowWindowIcon() const { gfx::ImageSkia NativeWindowWin::GetWindowAppIcon() {
return true; if (icon_.IsEmpty())
return gfx::ImageSkia();
else
return *icon_.ToImageSkia();
}
gfx::ImageSkia NativeWindowWin::GetWindowIcon() {
return GetWindowAppIcon();
} }
views::Widget* NativeWindowWin::GetWidget() { views::Widget* NativeWindowWin::GetWidget() {

View file

@ -98,7 +98,8 @@ class NativeWindowWin : public NativeWindow,
virtual bool CanMaximize() const OVERRIDE; virtual bool CanMaximize() const OVERRIDE;
virtual string16 GetWindowTitle() const OVERRIDE; virtual string16 GetWindowTitle() const OVERRIDE;
virtual bool ShouldHandleSystemCommands() const OVERRIDE; virtual bool ShouldHandleSystemCommands() const OVERRIDE;
virtual bool ShouldShowWindowIcon() const OVERRIDE; virtual gfx::ImageSkia GetWindowAppIcon() OVERRIDE;
virtual gfx::ImageSkia GetWindowIcon() OVERRIDE;
virtual views::Widget* GetWidget() OVERRIDE; virtual views::Widget* GetWidget() OVERRIDE;
virtual const views::Widget* GetWidget() const OVERRIDE; virtual const views::Widget* GetWidget() const OVERRIDE;
virtual views::ClientView* CreateClientView(views::Widget* widget) OVERRIDE; virtual views::ClientView* CreateClientView(views::Widget* widget) OVERRIDE;