electron/atom/common/api/atom_api_native_image.h

130 lines
3.9 KiB
C
Raw Normal View History

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