electron/atom/common/api/atom_api_native_image.h
2017-02-07 09:48:12 -08:00

130 lines
3.7 KiB
C++

// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_COMMON_API_ATOM_API_NATIVE_IMAGE_H_
#define ATOM_COMMON_API_ATOM_API_NATIVE_IMAGE_H_
#include <map>
#include <string>
#include "base/values.h"
#include "native_mate/handle.h"
#include "native_mate/wrappable.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/image/image.h"
#if defined(OS_WIN)
#include "base/files/file_path.h"
#include "base/win/scoped_gdi_object.h"
#endif
class GURL;
namespace base {
class FilePath;
}
namespace gfx {
class Size;
}
namespace mate {
class Arguments;
}
namespace atom {
namespace api {
class NativeImage : public mate::Wrappable<NativeImage> {
using IconLoadedCallback = base::Callback<void(mate::Handle<NativeImage>)>;
public:
static mate::Handle<NativeImage> CreateEmpty(v8::Isolate* isolate);
static mate::Handle<NativeImage> Create(
v8::Isolate* isolate, const gfx::Image& image);
static mate::Handle<NativeImage> CreateFromPNG(
v8::Isolate* isolate, const char* buffer, size_t length);
static mate::Handle<NativeImage> CreateFromJPEG(
v8::Isolate* isolate, const char* buffer, size_t length);
static mate::Handle<NativeImage> CreateFromPath(
v8::Isolate* isolate, const base::FilePath& path);
static mate::Handle<NativeImage> CreateFromBuffer(
mate::Arguments* args, v8::Local<v8::Value> buffer);
static mate::Handle<NativeImage> CreateFromDataURL(
v8::Isolate* isolate, const GURL& url);
static void CreateFromFileIcon(v8::Isolate* isolate,
const base::FilePath& path,
const IconLoadedCallback& callback);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
#if defined(OS_WIN)
HICON GetHICON(int size);
#endif
const gfx::Image& image() const { return image_; }
protected:
NativeImage(v8::Isolate* isolate, const gfx::Image& image);
#if defined(OS_WIN)
NativeImage(v8::Isolate* isolate, const base::FilePath& hicon_path);
#endif
~NativeImage() override;
private:
v8::Local<v8::Value> ToPNG(v8::Isolate* isolate);
v8::Local<v8::Value> ToJPEG(v8::Isolate* isolate, int quality);
v8::Local<v8::Value> ToBitmap(v8::Isolate* isolate);
v8::Local<v8::Value> GetBitmap(v8::Isolate* isolate);
v8::Local<v8::Value> GetNativeHandle(
v8::Isolate* isolate,
mate::Arguments* args);
mate::Handle<NativeImage> Resize(v8::Isolate* isolate,
const base::DictionaryValue& options);
mate::Handle<NativeImage> Crop(v8::Isolate* isolate,
const gfx::Rect& rect);
std::string ToDataURL();
bool IsEmpty();
gfx::Size GetSize();
float GetAspectRatio();
void OnIconLoaded(v8::Isolate* isolate,
const IconLoadedCallback& callback,
gfx::Image& image);
// Mark the image as template image.
void SetTemplateImage(bool setAsTemplate);
// Determine if the image is a template image.
bool IsTemplateImage();
#if defined(OS_WIN)
base::FilePath hicon_path_;
std::map<int, base::win::ScopedHICON> hicons_;
#endif
gfx::Image image_;
DISALLOW_COPY_AND_ASSIGN(NativeImage);
};
} // namespace api
} // 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_