2015-02-11 16:03:19 +08:00
|
|
|
// 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_
|
|
|
|
|
2016-05-20 17:51:05 +09:00
|
|
|
#include <map>
|
2015-02-12 14:32:51 +08:00
|
|
|
#include <string>
|
|
|
|
|
2016-10-04 10:26:01 -07:00
|
|
|
#include "base/values.h"
|
2015-02-11 16:03:19 +08:00
|
|
|
#include "native_mate/handle.h"
|
|
|
|
#include "native_mate/wrappable.h"
|
2016-10-04 11:23:18 -07:00
|
|
|
#include "ui/gfx/geometry/rect.h"
|
2015-02-11 16:03:19 +08:00
|
|
|
#include "ui/gfx/image/image.h"
|
|
|
|
|
2016-05-20 11:31:02 +09:00
|
|
|
#if defined(OS_WIN)
|
2016-05-20 17:51:05 +09:00
|
|
|
#include "base/files/file_path.h"
|
2016-05-20 11:31:02 +09:00
|
|
|
#include "base/win/scoped_gdi_object.h"
|
|
|
|
#endif
|
|
|
|
|
2015-02-12 14:27:53 +08:00
|
|
|
class GURL;
|
|
|
|
|
2015-02-11 18:41:06 +08:00
|
|
|
namespace base {
|
|
|
|
class FilePath;
|
|
|
|
}
|
|
|
|
|
2015-02-11 16:49:43 +08:00
|
|
|
namespace gfx {
|
|
|
|
class Size;
|
|
|
|
}
|
|
|
|
|
2015-02-12 15:19:05 +08:00
|
|
|
namespace mate {
|
|
|
|
class Arguments;
|
|
|
|
}
|
|
|
|
|
2015-02-11 16:03:19 +08:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2016-04-25 10:17:54 +09:00
|
|
|
class NativeImage : public mate::Wrappable<NativeImage> {
|
2015-02-11 16:03:19 +08:00
|
|
|
public:
|
2015-02-12 13:55:45 +08:00
|
|
|
static mate::Handle<NativeImage> CreateEmpty(v8::Isolate* isolate);
|
2015-02-11 18:02:59 +08:00
|
|
|
static mate::Handle<NativeImage> Create(
|
|
|
|
v8::Isolate* isolate, const gfx::Image& image);
|
|
|
|
static mate::Handle<NativeImage> CreateFromPNG(
|
2015-02-12 14:27:53 +08:00
|
|
|
v8::Isolate* isolate, const char* buffer, size_t length);
|
2015-02-11 18:02:59 +08:00
|
|
|
static mate::Handle<NativeImage> CreateFromJPEG(
|
2015-02-12 14:27:53 +08:00
|
|
|
v8::Isolate* isolate, const char* buffer, size_t length);
|
2015-02-11 18:41:06 +08:00
|
|
|
static mate::Handle<NativeImage> CreateFromPath(
|
|
|
|
v8::Isolate* isolate, const base::FilePath& path);
|
2015-02-12 15:19:05 +08:00
|
|
|
static mate::Handle<NativeImage> CreateFromBuffer(
|
2015-05-22 19:11:22 +08:00
|
|
|
mate::Arguments* args, v8::Local<v8::Value> buffer);
|
2015-02-12 14:27:53 +08:00
|
|
|
static mate::Handle<NativeImage> CreateFromDataURL(
|
|
|
|
v8::Isolate* isolate, const GURL& url);
|
2015-02-11 16:03:19 +08:00
|
|
|
|
2016-04-25 10:17:54 +09:00
|
|
|
static void BuildPrototype(v8::Isolate* isolate,
|
2016-08-02 18:08:12 +09:00
|
|
|
v8::Local<v8::FunctionTemplate> prototype);
|
2015-02-11 19:22:41 +08:00
|
|
|
|
2016-05-20 16:14:40 +09:00
|
|
|
#if defined(OS_WIN)
|
2016-05-20 17:51:05 +09:00
|
|
|
HICON GetHICON(int size);
|
2016-05-20 16:14:40 +09:00
|
|
|
#endif
|
2015-02-11 19:22:41 +08:00
|
|
|
|
2016-05-20 16:55:22 +09:00
|
|
|
const gfx::Image& image() const { return image_; }
|
|
|
|
|
2015-02-11 16:03:19 +08:00
|
|
|
protected:
|
2016-04-25 10:17:54 +09:00
|
|
|
NativeImage(v8::Isolate* isolate, const gfx::Image& image);
|
2016-05-20 11:31:02 +09:00
|
|
|
#if defined(OS_WIN)
|
2016-05-20 17:51:05 +09:00
|
|
|
NativeImage(v8::Isolate* isolate, const base::FilePath& hicon_path);
|
2016-05-20 11:31:02 +09:00
|
|
|
#endif
|
2016-04-25 10:17:54 +09:00
|
|
|
~NativeImage() override;
|
2015-02-11 16:03:19 +08:00
|
|
|
|
|
|
|
private:
|
2015-05-22 19:11:22 +08:00
|
|
|
v8::Local<v8::Value> ToPNG(v8::Isolate* isolate);
|
|
|
|
v8::Local<v8::Value> ToJPEG(v8::Isolate* isolate, int quality);
|
2016-08-01 02:13:31 +02:00
|
|
|
v8::Local<v8::Value> ToBitmap(v8::Isolate* isolate);
|
2016-08-04 19:36:01 +02:00
|
|
|
v8::Local<v8::Value> GetBitmap(v8::Isolate* isolate);
|
2016-03-14 19:50:31 -07:00
|
|
|
v8::Local<v8::Value> GetNativeHandle(
|
2016-03-13 20:18:03 -07:00
|
|
|
v8::Isolate* isolate,
|
|
|
|
mate::Arguments* args);
|
2016-10-04 10:26:01 -07:00
|
|
|
mate::Handle<NativeImage> Resize(v8::Isolate* isolate,
|
|
|
|
const base::DictionaryValue& options);
|
2016-10-04 11:23:18 -07:00
|
|
|
mate::Handle<NativeImage> Crop(v8::Isolate* isolate,
|
2016-10-05 08:02:04 -07:00
|
|
|
const gfx::Rect& rect);
|
2015-02-12 14:32:51 +08:00
|
|
|
std::string ToDataURL();
|
2015-02-11 16:49:43 +08:00
|
|
|
bool IsEmpty();
|
|
|
|
gfx::Size GetSize();
|
2016-10-05 09:15:06 -07:00
|
|
|
float GetAspectRatio();
|
2015-02-11 16:03:19 +08:00
|
|
|
|
2015-04-13 11:53:24 +08:00
|
|
|
// Mark the image as template image.
|
|
|
|
void SetTemplateImage(bool setAsTemplate);
|
2015-07-26 21:58:48 -07:00
|
|
|
// Determine if the image is a template image.
|
|
|
|
bool IsTemplateImage();
|
2015-04-13 11:53:24 +08:00
|
|
|
|
2016-05-20 11:31:02 +09:00
|
|
|
#if defined(OS_WIN)
|
2016-05-20 17:51:05 +09:00
|
|
|
base::FilePath hicon_path_;
|
|
|
|
std::map<int, base::win::ScopedHICON> hicons_;
|
2016-05-20 11:31:02 +09:00
|
|
|
#endif
|
|
|
|
|
2015-02-11 16:03:19 +08:00
|
|
|
gfx::Image image_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(NativeImage);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
2016-05-20 16:14:40 +09:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2015-02-11 16:03:19 +08:00
|
|
|
#endif // ATOM_COMMON_API_ATOM_API_NATIVE_IMAGE_H_
|