|
@@ -142,7 +142,7 @@ bool IsTemplateFilename(const base::FilePath& path) {
|
|
|
#endif
|
|
|
|
|
|
#if defined(OS_WIN)
|
|
|
-bool ReadImageSkiaFromICO(gfx::ImageSkia* image, const base::FilePath& path) {
|
|
|
+base::win::ScopedHICON ReadICOFromPath(int size, const base::FilePath& path) {
|
|
|
// If file is in asar archive, we extract it to a temp file so LoadImage can
|
|
|
// load it.
|
|
|
base::FilePath asar_path, relative_path;
|
|
@@ -155,16 +155,15 @@ bool ReadImageSkiaFromICO(gfx::ImageSkia* image, const base::FilePath& path) {
|
|
|
}
|
|
|
|
|
|
// Load the icon from file.
|
|
|
- base::win::ScopedHICON icon(static_cast<HICON>(
|
|
|
- LoadImage(NULL, image_path.value().c_str(), IMAGE_ICON, 0, 0,
|
|
|
- LR_DEFAULTSIZE | LR_LOADFROMFILE)));
|
|
|
- if (!icon.get())
|
|
|
- return false;
|
|
|
+ return base::win::ScopedHICON(static_cast<HICON>(
|
|
|
+ LoadImage(NULL, image_path.value().c_str(), IMAGE_ICON, size, size,
|
|
|
+ LR_LOADFROMFILE)));
|
|
|
+}
|
|
|
|
|
|
+void ReadImageSkiaFromICO(gfx::ImageSkia* image, HICON icon) {
|
|
|
// Convert the icon from the Windows specific HICON to gfx::ImageSkia.
|
|
|
- scoped_ptr<SkBitmap> bitmap(IconUtil:: CreateSkBitmapFromHICON(icon.get()));
|
|
|
+ scoped_ptr<SkBitmap> bitmap(IconUtil::CreateSkBitmapFromHICON(icon));
|
|
|
image->AddRepresentation(gfx::ImageSkiaRep(*bitmap, 1.0f));
|
|
|
- return true;
|
|
|
}
|
|
|
#endif
|
|
|
|
|
@@ -175,8 +174,40 @@ NativeImage::NativeImage(v8::Isolate* isolate, const gfx::Image& image)
|
|
|
Init(isolate);
|
|
|
}
|
|
|
|
|
|
+#if defined(OS_WIN)
|
|
|
+NativeImage::NativeImage(v8::Isolate* isolate, const base::FilePath& hicon_path)
|
|
|
+ : hicon_path_(hicon_path) {
|
|
|
+ // Use the 256x256 icon as fallback icon.
|
|
|
+ gfx::ImageSkia image_skia;
|
|
|
+ ReadImageSkiaFromICO(&image_skia, GetHICON(256));
|
|
|
+ image_ = gfx::Image(image_skia);
|
|
|
+ Init(isolate);
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
NativeImage::~NativeImage() {}
|
|
|
|
|
|
+#if defined(OS_WIN)
|
|
|
+HICON NativeImage::GetHICON(int size) {
|
|
|
+ auto iter = hicons_.find(size);
|
|
|
+ if (iter != hicons_.end())
|
|
|
+ return iter->second.get();
|
|
|
+
|
|
|
+ // First try loading the icon with specified size.
|
|
|
+ if (!hicon_path_.empty()) {
|
|
|
+ hicons_[size] = std::move(ReadICOFromPath(size, hicon_path_));
|
|
|
+ return hicons_[size].get();
|
|
|
+ }
|
|
|
+
|
|
|
+ // Then convert the image to ICO.
|
|
|
+ if (image_.IsEmpty())
|
|
|
+ return NULL;
|
|
|
+ hicons_[size] = std::move(
|
|
|
+ IconUtil::CreateHICONFromSkBitmap(image_.AsBitmap()));
|
|
|
+ return hicons_[size].get();
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
v8::Local<v8::Value> NativeImage::ToPNG(v8::Isolate* isolate) {
|
|
|
scoped_refptr<base::RefCountedMemory> png = image_.As1xPNGBytes();
|
|
|
return node::Buffer::Copy(isolate,
|
|
@@ -263,16 +294,15 @@ mate::Handle<NativeImage> NativeImage::CreateFromJPEG(
|
|
|
// static
|
|
|
mate::Handle<NativeImage> NativeImage::CreateFromPath(
|
|
|
v8::Isolate* isolate, const base::FilePath& path) {
|
|
|
- gfx::ImageSkia image_skia;
|
|
|
base::FilePath image_path = NormalizePath(path);
|
|
|
-
|
|
|
- if (image_path.MatchesExtension(FILE_PATH_LITERAL(".ico"))) {
|
|
|
#if defined(OS_WIN)
|
|
|
- ReadImageSkiaFromICO(&image_skia, image_path);
|
|
|
-#endif
|
|
|
- } else {
|
|
|
- PopulateImageSkiaRepsFromPath(&image_skia, image_path);
|
|
|
+ if (image_path.MatchesExtension(FILE_PATH_LITERAL(".ico"))) {
|
|
|
+ return mate::CreateHandle(isolate,
|
|
|
+ new NativeImage(isolate, image_path));
|
|
|
}
|
|
|
+#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)
|
|
@@ -328,6 +358,35 @@ void NativeImage::BuildPrototype(
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
|
+namespace mate {
|
|
|
+
|
|
|
+v8::Local<v8::Value> Converter<mate::Handle<atom::api::NativeImage>>::ToV8(
|
|
|
+ v8::Isolate* isolate,
|
|
|
+ const mate::Handle<atom::api::NativeImage>& val) {
|
|
|
+ return val.ToV8();
|
|
|
+}
|
|
|
+
|
|
|
+bool Converter<mate::Handle<atom::api::NativeImage>>::FromV8(
|
|
|
+ v8::Isolate* isolate, v8::Local<v8::Value> val,
|
|
|
+ mate::Handle<atom::api::NativeImage>* out) {
|
|
|
+ // Try converting from file path.
|
|
|
+ base::FilePath path;
|
|
|
+ if (ConvertFromV8(isolate, val, &path)) {
|
|
|
+ *out = atom::api::NativeImage::CreateFromPath(isolate, path);
|
|
|
+ // Should throw when failed to initialize from path.
|
|
|
+ return !(*out)->image().IsEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
+ WrappableBase* wrapper = static_cast<WrappableBase*>(internal::FromV8Impl(
|
|
|
+ isolate, val));
|
|
|
+ if (!wrapper)
|
|
|
+ return false;
|
|
|
+
|
|
|
+ *out = CreateHandle(isolate, static_cast<atom::api::NativeImage*>(wrapper));
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+} // namespace mate
|
|
|
|
|
|
namespace {
|
|
|
|