Add BrowserWindow.setIcon

This commit is contained in:
Cheng Zhao 2016-05-20 22:22:15 +09:00
parent b33c04d041
commit a93c9462ed
7 changed files with 59 additions and 43 deletions

View file

@ -9,7 +9,6 @@
#include "atom/browser/api/atom_api_web_contents.h" #include "atom/browser/api/atom_api_web_contents.h"
#include "atom/browser/browser.h" #include "atom/browser/browser.h"
#include "atom/browser/native_window.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/callback.h"
#include "atom/common/native_mate_converters/gfx_converter.h" #include "atom/common/native_mate_converters/gfx_converter.h"
#include "atom/common/native_mate_converters/gurl_converter.h" #include "atom/common/native_mate_converters/gurl_converter.h"
@ -22,8 +21,11 @@
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
#include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/rect.h"
#if defined(OS_WIN) #if defined(TOOLKIT_VIEWS)
#include "atom/browser/native_window_views.h" #include "atom/browser/native_window_views.h"
#endif
#if defined(OS_WIN)
#include "atom/browser/ui/win/taskbar_host.h" #include "atom/browser/ui/win/taskbar_host.h"
#endif #endif
@ -101,13 +103,11 @@ Window::Window(v8::Isolate* isolate, const mate::Dictionary& options) {
window_->AddObserver(this); window_->AddObserver(this);
AttachAsUserData(window_.get()); AttachAsUserData(window_.get());
#if defined(OS_WIN) #if defined(TOOLKIT_VIEWS)
// Sets the window icon. // Sets the window icon.
mate::Handle<NativeImage> icon; mate::Handle<NativeImage> icon;
if (options.Get(options::kIcon, &icon) && !icon.IsEmpty()) { if (options.Get(options::kIcon, &icon))
static_cast<NativeWindowViews*>(window_.get())->SetIcon( SetIcon(icon);
icon->GetHICON(GetSystemMetrics(SM_CXSMICON)), icon->GetHICON(256));
}
#endif #endif
} }
@ -627,6 +627,18 @@ void Window::ShowDefinitionForSelection() {
} }
#endif #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) { void Window::SetAspectRatio(double aspect_ratio, mate::Arguments* args) {
gfx::Size extra_size; gfx::Size extra_size;
args->GetNext(&extra_size); args->GetNext(&extra_size);
@ -748,6 +760,9 @@ void Window::BuildPrototype(v8::Isolate* isolate,
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
.SetMethod("showDefinitionForSelection", .SetMethod("showDefinitionForSelection",
&Window::ShowDefinitionForSelection) &Window::ShowDefinitionForSelection)
#endif
#if defined(TOOLKIT_VIEWS)
.SetMethod("setIcon", &Window::SetIcon)
#endif #endif
.SetProperty("id", &Window::ID) .SetProperty("id", &Window::ID)
.SetProperty("webContents", &Window::WebContents); .SetProperty("webContents", &Window::WebContents);

View file

@ -14,6 +14,7 @@
#include "atom/browser/api/trackable_object.h" #include "atom/browser/api/trackable_object.h"
#include "atom/browser/native_window.h" #include "atom/browser/native_window.h"
#include "atom/browser/native_window_observer.h" #include "atom/browser/native_window_observer.h"
#include "atom/common/api/atom_api_native_image.h"
#include "native_mate/handle.h" #include "native_mate/handle.h"
class GURL; class GURL;
@ -172,6 +173,10 @@ class Window : public mate::TrackableObject<Window>,
void ShowDefinitionForSelection(); void ShowDefinitionForSelection();
#endif #endif
#if defined(TOOLKIT_VIEWS)
void SetIcon(mate::Handle<NativeImage> icon);
#endif
void SetVisibleOnAllWorkspaces(bool visible); void SetVisibleOnAllWorkspaces(bool visible);
bool IsVisibleOnAllWorkspaces(); bool IsVisibleOnAllWorkspaces();

View file

@ -12,7 +12,6 @@
#include "atom/browser/atom_browser_main_parts.h" #include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/window_list.h" #include "atom/browser/window_list.h"
#include "atom/common/api/api_messages.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/native_mate_converters/file_path_converter.h"
#include "atom/common/options_switches.h" #include "atom/common/options_switches.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"

View file

@ -11,6 +11,7 @@
#include "atom/browser/ui/views/menu_layout.h" #include "atom/browser/ui/views/menu_layout.h"
#include "atom/common/color_util.h" #include "atom/common/color_util.h"
#include "atom/common/draggable_region.h" #include "atom/common/draggable_region.h"
#include "atom/common/native_mate_converters/image_converter.h"
#include "atom/common/options_switches.h" #include "atom/common/options_switches.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "brightray/browser/inspectable_web_contents.h" #include "brightray/browser/inspectable_web_contents.h"
@ -40,6 +41,7 @@
#include "chrome/browser/ui/libgtk2ui/unity_service.h" #include "chrome/browser/ui/libgtk2ui/unity_service.h"
#include "ui/base/x/x11_util.h" #include "ui/base/x/x11_util.h"
#include "ui/gfx/x/x11_types.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" #include "ui/views/window/native_frame_view.h"
#elif defined(OS_WIN) #elif defined(OS_WIN)
#include "atom/browser/ui/views/win_frame_view.h" #include "atom/browser/ui/views/win_frame_view.h"
@ -221,10 +223,6 @@ NativeWindowViews::NativeWindowViews(
std::string window_type; std::string window_type;
if (options.Get(options::kType, &window_type)) if (options.Get(options::kType, &window_type))
SetWindowType(GetAcceleratedWidget(), window_type); SetWindowType(GetAcceleratedWidget(), window_type);
// Set window icon.
options.Get(options::kIcon, &icon_);
window_->UpdateWindowIcon();
#endif #endif
// Add web view. // Add web view.
@ -781,6 +779,13 @@ gfx::AcceleratedWidget NativeWindowViews::GetAcceleratedWidget() {
return GetNativeWindow()->GetHost()->GetAcceleratedWidget(); return GetNativeWindow()->GetHost()->GetAcceleratedWidget();
} }
void NativeWindowViews::SetIcon(const gfx::ImageSkia& icon) {
views::DesktopWindowTreeHostX11* tree_host =
views::DesktopWindowTreeHostX11::GetHostForXID(GetAcceleratedWidget());
static_cast<views::DesktopWindowTreeHost*>(tree_host)->SetWindowIcons(
icon, icon);
}
void NativeWindowViews::OnWidgetActivationChanged( void NativeWindowViews::OnWidgetActivationChanged(
views::Widget* widget, bool active) { views::Widget* widget, bool active) {
if (widget != window_.get()) if (widget != window_.get())
@ -845,16 +850,6 @@ bool NativeWindowViews::ShouldHandleSystemCommands() const {
return true; return true;
} }
#if defined(USE_X11)
gfx::ImageSkia NativeWindowViews::GetWindowAppIcon() {
return icon_;
}
gfx::ImageSkia NativeWindowViews::GetWindowIcon() {
return GetWindowAppIcon();
}
#endif
views::Widget* NativeWindowViews::GetWidget() { views::Widget* NativeWindowViews::GetWidget() {
return window_.get(); return window_.get();
} }

View file

@ -106,12 +106,16 @@ class NativeWindowViews : public NativeWindow,
#if defined(OS_WIN) #if defined(OS_WIN)
void SetIcon(HICON small_icon, HICON app_icon); void SetIcon(HICON small_icon, HICON app_icon);
#elif defined(USE_X11)
TaskbarHost& taskbar_host() { return taskbar_host_; } void SetIcon(const gfx::ImageSkia& icon);
#endif #endif
views::Widget* widget() const { return window_.get(); } views::Widget* widget() const { return window_.get(); }
#if defined(OS_WIN)
TaskbarHost& taskbar_host() { return taskbar_host_; }
#endif
private: private:
// views::WidgetObserver: // views::WidgetObserver:
void OnWidgetActivationChanged( void OnWidgetActivationChanged(
@ -127,10 +131,6 @@ class NativeWindowViews : public NativeWindow,
bool CanMinimize() const override; bool CanMinimize() const override;
base::string16 GetWindowTitle() const override; base::string16 GetWindowTitle() const override;
bool ShouldHandleSystemCommands() const override; bool ShouldHandleSystemCommands() const override;
#if defined(USE_X11)
gfx::ImageSkia GetWindowAppIcon() override;
gfx::ImageSkia GetWindowIcon() override;
#endif
views::Widget* GetWidget() override; views::Widget* GetWidget() override;
const views::Widget* GetWidget() const override; const views::Widget* GetWidget() const override;
views::View* GetContentsView() override; views::View* GetContentsView() override;
@ -188,9 +188,6 @@ class NativeWindowViews : public NativeWindow,
// we need to make sure size constraints are restored when window becomes // we need to make sure size constraints are restored when window becomes
// resizable again. // resizable again.
extensions::SizeConstraints old_size_constraints_; extensions::SizeConstraints old_size_constraints_;
// Window icon.
gfx::ImageSkia icon_;
#elif defined(OS_WIN) #elif defined(OS_WIN)
// Weak ref. // Weak ref.
AtomDesktopWindowTreeHostWin* atom_desktop_window_tree_host_win_; AtomDesktopWindowTreeHostWin* atom_desktop_window_tree_host_win_;

View file

@ -295,22 +295,21 @@ mate::Handle<NativeImage> NativeImage::CreateFromJPEG(
mate::Handle<NativeImage> NativeImage::CreateFromPath( mate::Handle<NativeImage> NativeImage::CreateFromPath(
v8::Isolate* isolate, const base::FilePath& path) { v8::Isolate* isolate, const base::FilePath& path) {
base::FilePath image_path = NormalizePath(path); base::FilePath image_path = NormalizePath(path);
if (image_path.MatchesExtension(FILE_PATH_LITERAL(".ico"))) {
#if defined(OS_WIN) #if defined(OS_WIN)
if (image_path.MatchesExtension(FILE_PATH_LITERAL(".ico"))) {
return mate::CreateHandle(isolate, return mate::CreateHandle(isolate,
new NativeImage(isolate, image_path)); new NativeImage(isolate, image_path));
#endif
} else {
gfx::ImageSkia image_skia;
PopulateImageSkiaRepsFromPath(&image_skia, image_path);
gfx::Image image(image_skia);
mate::Handle<NativeImage> handle = Create(isolate, image);
#if defined(OS_MACOSX)
if (IsTemplateFilename(image_path))
handle->SetTemplateImage(true);
#endif
return handle;
} }
#endif
gfx::ImageSkia image_skia;
PopulateImageSkiaRepsFromPath(&image_skia, image_path);
gfx::Image image(image_skia);
mate::Handle<NativeImage> handle = Create(isolate, image);
#if defined(OS_MACOSX)
if (IsTemplateFilename(image_path))
handle->SetTemplateImage(true);
#endif
return handle;
} }
// static // static

View file

@ -886,6 +886,12 @@ The `flags` is an array that can include following `String`s:
Shows pop-up dictionary that searches the selected word on the page. Shows pop-up dictionary that searches the selected word on the page.
### `win.setIcon(icon)` _Windows_ _Linux_
* `icon` [NativeImage](native-image.md)
Changes window icon.
### `win.setAutoHideMenuBar(hide)` ### `win.setAutoHideMenuBar(hide)`
* `hide` Boolean * `hide` Boolean