refactor: remove path from nativeImage converter (#26546)
This commit is contained in:
parent
4db3e3a08a
commit
3455136e9d
13 changed files with 167 additions and 103 deletions
|
@ -99,13 +99,9 @@ BaseWindow::BaseWindow(v8::Isolate* isolate,
|
|||
window_->AddObserver(this);
|
||||
|
||||
#if defined(TOOLKIT_VIEWS)
|
||||
{
|
||||
v8::TryCatch try_catch(isolate);
|
||||
gin::Handle<NativeImage> icon;
|
||||
if (options.Get(options::kIcon, &icon) && !icon.IsEmpty())
|
||||
SetIcon(icon);
|
||||
if (try_catch.HasCaught())
|
||||
LOG(ERROR) << "Failed to convert NativeImage";
|
||||
v8::Local<v8::Value> icon;
|
||||
if (options.Get(options::kIcon, &icon)) {
|
||||
SetIcon(isolate, icon);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
@ -1003,14 +999,18 @@ bool BaseWindow::SetThumbarButtons(gin_helper::Arguments* args) {
|
|||
}
|
||||
|
||||
#if defined(TOOLKIT_VIEWS)
|
||||
void BaseWindow::SetIcon(gin::Handle<NativeImage> icon) {
|
||||
void BaseWindow::SetIcon(v8::Isolate* isolate, v8::Local<v8::Value> icon) {
|
||||
NativeImage* native_image = nullptr;
|
||||
if (!NativeImage::TryConvertNativeImage(isolate, icon, &native_image))
|
||||
return;
|
||||
|
||||
#if defined(OS_WIN)
|
||||
static_cast<NativeWindowViews*>(window_.get())
|
||||
->SetIcon(icon->GetHICON(GetSystemMetrics(SM_CXSMICON)),
|
||||
icon->GetHICON(GetSystemMetrics(SM_CXICON)));
|
||||
->SetIcon(native_image->GetHICON(GetSystemMetrics(SM_CXSMICON)),
|
||||
native_image->GetHICON(GetSystemMetrics(SM_CXICON)));
|
||||
#elif defined(OS_LINUX)
|
||||
static_cast<NativeWindowViews*>(window_.get())
|
||||
->SetIcon(icon->image().AsImageSkia());
|
||||
->SetIcon(native_image->image().AsImageSkia());
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
#include "shell/browser/native_window.h"
|
||||
#include "shell/browser/native_window_observer.h"
|
||||
#include "shell/common/api/electron_api_native_image.h"
|
||||
#include "shell/common/gin_helper/error_thrower.h"
|
||||
#include "shell/common/gin_helper/trackable_object.h"
|
||||
|
||||
namespace electron {
|
||||
|
@ -221,7 +222,7 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
|
|||
// Extra APIs added in JS.
|
||||
bool SetThumbarButtons(gin_helper::Arguments* args);
|
||||
#if defined(TOOLKIT_VIEWS)
|
||||
void SetIcon(gin::Handle<NativeImage> icon);
|
||||
void SetIcon(v8::Isolate* isolate, v8::Local<v8::Value> icon);
|
||||
#endif
|
||||
#if defined(OS_WIN)
|
||||
typedef base::RepeatingCallback<void(v8::Local<v8::Value>,
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
#include "shell/browser/browser.h"
|
||||
#include "shell/browser/javascript_environment.h"
|
||||
#include "shell/common/api/electron_api_native_image.h"
|
||||
#include "shell/common/gin_converters/file_path_converter.h"
|
||||
#include "shell/common/gin_converters/gfx_converter.h"
|
||||
#include "shell/common/gin_converters/guid_converter.h"
|
||||
#include "shell/common/gin_converters/image_converter.h"
|
||||
|
@ -61,11 +62,11 @@ namespace api {
|
|||
|
||||
gin::WrapperInfo Tray::kWrapperInfo = {gin::kEmbedderNativeGin};
|
||||
|
||||
Tray::Tray(gin::Handle<NativeImage> image,
|
||||
base::Optional<UUID> guid,
|
||||
gin::Arguments* args)
|
||||
Tray::Tray(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> image,
|
||||
base::Optional<UUID> guid)
|
||||
: tray_icon_(TrayIcon::Create(guid)) {
|
||||
SetImage(image);
|
||||
SetImage(isolate, image);
|
||||
tray_icon_->AddObserver(this);
|
||||
}
|
||||
|
||||
|
@ -73,7 +74,7 @@ Tray::~Tray() = default;
|
|||
|
||||
// static
|
||||
gin::Handle<Tray> Tray::New(gin_helper::ErrorThrower thrower,
|
||||
gin::Handle<NativeImage> image,
|
||||
v8::Local<v8::Value> image,
|
||||
base::Optional<UUID> guid,
|
||||
gin::Arguments* args) {
|
||||
if (!Browser::Get()->is_ready()) {
|
||||
|
@ -88,7 +89,8 @@ gin::Handle<Tray> Tray::New(gin_helper::ErrorThrower thrower,
|
|||
}
|
||||
#endif
|
||||
|
||||
return gin::CreateHandle(thrower.isolate(), new Tray(image, guid, args));
|
||||
return gin::CreateHandle(thrower.isolate(),
|
||||
new Tray(args->isolate(), image, guid));
|
||||
}
|
||||
|
||||
void Tray::OnClicked(const gfx::Rect& bounds,
|
||||
|
@ -186,23 +188,34 @@ bool Tray::IsDestroyed() {
|
|||
return !tray_icon_;
|
||||
}
|
||||
|
||||
void Tray::SetImage(gin::Handle<NativeImage> image) {
|
||||
void Tray::SetImage(v8::Isolate* isolate, v8::Local<v8::Value> image) {
|
||||
if (!CheckAlive())
|
||||
return;
|
||||
|
||||
NativeImage* native_image = nullptr;
|
||||
if (!NativeImage::TryConvertNativeImage(isolate, image, &native_image))
|
||||
return;
|
||||
|
||||
#if defined(OS_WIN)
|
||||
tray_icon_->SetImage(image->GetHICON(GetSystemMetrics(SM_CXSMICON)));
|
||||
tray_icon_->SetImage(native_image->GetHICON(GetSystemMetrics(SM_CXSMICON)));
|
||||
#else
|
||||
tray_icon_->SetImage(image->image());
|
||||
tray_icon_->SetImage(native_image->image());
|
||||
#endif
|
||||
}
|
||||
|
||||
void Tray::SetPressedImage(gin::Handle<NativeImage> image) {
|
||||
void Tray::SetPressedImage(v8::Isolate* isolate, v8::Local<v8::Value> image) {
|
||||
if (!CheckAlive())
|
||||
return;
|
||||
|
||||
NativeImage* native_image = nullptr;
|
||||
if (!NativeImage::TryConvertNativeImage(isolate, image, &native_image))
|
||||
return;
|
||||
|
||||
#if defined(OS_WIN)
|
||||
tray_icon_->SetPressedImage(image->GetHICON(GetSystemMetrics(SM_CXSMICON)));
|
||||
tray_icon_->SetPressedImage(
|
||||
native_image->GetHICON(GetSystemMetrics(SM_CXSMICON)));
|
||||
#else
|
||||
tray_icon_->SetPressedImage(image->image());
|
||||
tray_icon_->SetPressedImage(native_image->image());
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -282,14 +295,20 @@ void Tray::DisplayBalloon(gin_helper::ErrorThrower thrower,
|
|||
return;
|
||||
}
|
||||
|
||||
gin::Handle<NativeImage> icon;
|
||||
options.Get("icon", &icon);
|
||||
v8::Local<v8::Value> icon_value;
|
||||
NativeImage* icon = nullptr;
|
||||
if (options.Get("icon", &icon_value) &&
|
||||
!NativeImage::TryConvertNativeImage(thrower.isolate(), icon_value,
|
||||
&icon)) {
|
||||
return;
|
||||
}
|
||||
|
||||
options.Get("iconType", &balloon_options.icon_type);
|
||||
options.Get("largeIcon", &balloon_options.large_icon);
|
||||
options.Get("noSound", &balloon_options.no_sound);
|
||||
options.Get("respectQuietTime", &balloon_options.respect_quiet_time);
|
||||
|
||||
if (!icon.IsEmpty()) {
|
||||
if (icon) {
|
||||
#if defined(OS_WIN)
|
||||
balloon_options.icon = icon->GetHICON(
|
||||
GetSystemMetrics(balloon_options.large_icon ? SM_CXICON : SM_CXSMICON));
|
||||
|
|
|
@ -43,7 +43,7 @@ class Tray : public gin::Wrappable<Tray>,
|
|||
public:
|
||||
// gin_helper::Constructible
|
||||
static gin::Handle<Tray> New(gin_helper::ErrorThrower thrower,
|
||||
gin::Handle<NativeImage> image,
|
||||
v8::Local<v8::Value> image,
|
||||
base::Optional<UUID> guid,
|
||||
gin::Arguments* args);
|
||||
static v8::Local<v8::ObjectTemplate> FillObjectTemplate(
|
||||
|
@ -54,9 +54,9 @@ class Tray : public gin::Wrappable<Tray>,
|
|||
static gin::WrapperInfo kWrapperInfo;
|
||||
|
||||
private:
|
||||
Tray(gin::Handle<NativeImage> image,
|
||||
base::Optional<UUID> guid,
|
||||
gin::Arguments* args);
|
||||
Tray(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> image,
|
||||
base::Optional<UUID> guid);
|
||||
~Tray() override;
|
||||
|
||||
// TrayIconObserver:
|
||||
|
@ -83,8 +83,8 @@ class Tray : public gin::Wrappable<Tray>,
|
|||
// JS API:
|
||||
void Destroy();
|
||||
bool IsDestroyed();
|
||||
void SetImage(gin::Handle<NativeImage> image);
|
||||
void SetPressedImage(gin::Handle<NativeImage> image);
|
||||
void SetImage(v8::Isolate* isolate, v8::Local<v8::Value> image);
|
||||
void SetPressedImage(v8::Isolate* isolate, v8::Local<v8::Value> image);
|
||||
void SetToolTip(const std::string& tool_tip);
|
||||
void SetTitle(const std::string& title,
|
||||
const base::Optional<gin_helper::Dictionary>& options,
|
||||
|
|
|
@ -2819,10 +2819,16 @@ void WebContents::StartDrag(const gin_helper::Dictionary& item,
|
|||
files.push_back(file);
|
||||
}
|
||||
|
||||
gin::Handle<NativeImage> icon;
|
||||
if (!item.Get("icon", &icon) || icon->image().IsEmpty()) {
|
||||
v8::Local<v8::Value> icon_value;
|
||||
if (!item.Get("icon", &icon_value)) {
|
||||
gin_helper::ErrorThrower(args->isolate())
|
||||
.ThrowError("Must specify non-empty 'icon' option");
|
||||
.ThrowError("'icon' parameter is required");
|
||||
return;
|
||||
}
|
||||
|
||||
NativeImage* icon = nullptr;
|
||||
if (!NativeImage::TryConvertNativeImage(args->isolate(), icon_value, &icon) ||
|
||||
icon->image().IsEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue