Convert to mate::Handle<NativeImage> instead of gfx::Image

This commit is contained in:
Cheng Zhao 2016-05-20 16:14:40 +09:00
parent 7c34d8333c
commit adfd99f5f0
6 changed files with 80 additions and 31 deletions

View file

@ -341,6 +341,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 {

View file

@ -53,6 +53,9 @@ class NativeImage : public mate::Wrappable<NativeImage> {
v8::Local<v8::ObjectTemplate> prototype);
const gfx::Image& image() const { return image_; }
#if defined(OS_WIN)
HICON hicon() const { return hicon_.get(); }
#endif
protected:
NativeImage(v8::Isolate* isolate, const gfx::Image& image);
@ -89,4 +92,19 @@ class NativeImage : public mate::Wrappable<NativeImage> {
} // namespace atom
namespace mate {
// A custom converter that allows converting path to NativeImage.
template<>
struct Converter<mate::Handle<atom::api::NativeImage>> {
static v8::Local<v8::Value> ToV8(
v8::Isolate* isolate,
const mate::Handle<atom::api::NativeImage>& val);
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
mate::Handle<atom::api::NativeImage>* out);
};
} // namespace mate
#endif // ATOM_COMMON_API_ATOM_API_NATIVE_IMAGE_H_