2015-02-11 08:03:19 +00: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.
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
#ifndef SHELL_COMMON_API_ELECTRON_API_NATIVE_IMAGE_H_
|
|
|
|
#define SHELL_COMMON_API_ELECTRON_API_NATIVE_IMAGE_H_
|
2015-02-11 08:03:19 +00:00
|
|
|
|
2016-05-20 08:51:05 +00:00
|
|
|
#include <map>
|
2015-02-12 06:32:51 +00:00
|
|
|
#include <string>
|
2020-05-18 16:29:24 +00:00
|
|
|
#include <vector>
|
2015-02-12 06:32:51 +00:00
|
|
|
|
2016-10-04 17:26:01 +00:00
|
|
|
#include "base/values.h"
|
2019-10-25 13:03:28 +00:00
|
|
|
#include "gin/handle.h"
|
2020-07-13 21:44:12 +00:00
|
|
|
#include "gin/wrappable.h"
|
2019-10-18 00:31:29 +00:00
|
|
|
#include "shell/common/gin_helper/error_thrower.h"
|
2015-02-11 08:03:19 +00:00
|
|
|
#include "ui/gfx/image/image.h"
|
|
|
|
|
2016-05-20 02:31:02 +00:00
|
|
|
#if defined(OS_WIN)
|
2016-05-20 08:51:05 +00:00
|
|
|
#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;
|
|
|
|
|
2015-02-11 10:41:06 +00:00
|
|
|
namespace base {
|
|
|
|
class FilePath;
|
|
|
|
}
|
|
|
|
|
2015-02-11 08:49:43 +00:00
|
|
|
namespace gfx {
|
2019-10-18 00:31:29 +00:00
|
|
|
class Rect;
|
2015-02-11 08:49:43 +00:00
|
|
|
class Size;
|
2019-10-18 00:31:29 +00:00
|
|
|
} // namespace gfx
|
2015-02-11 08:49:43 +00:00
|
|
|
|
2019-10-18 00:31:29 +00:00
|
|
|
namespace gin_helper {
|
|
|
|
class Dictionary;
|
2015-02-12 07:19:05 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 21:44:12 +00:00
|
|
|
namespace gin {
|
|
|
|
class Arguments;
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2015-02-11 08:03:19 +00:00
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2020-07-13 21:44:12 +00:00
|
|
|
class NativeImage : public gin::Wrappable<NativeImage> {
|
2015-02-11 08:03:19 +00:00
|
|
|
public:
|
2019-10-25 13:03:28 +00:00
|
|
|
static gin::Handle<NativeImage> CreateEmpty(v8::Isolate* isolate);
|
|
|
|
static gin::Handle<NativeImage> Create(v8::Isolate* isolate,
|
|
|
|
const gfx::Image& image);
|
|
|
|
static gin::Handle<NativeImage> CreateFromPNG(v8::Isolate* isolate,
|
|
|
|
const char* buffer,
|
|
|
|
size_t length);
|
|
|
|
static gin::Handle<NativeImage> CreateFromJPEG(v8::Isolate* isolate,
|
2018-04-18 01:44:10 +00:00
|
|
|
const char* buffer,
|
|
|
|
size_t length);
|
2019-10-25 13:03:28 +00:00
|
|
|
static gin::Handle<NativeImage> CreateFromPath(v8::Isolate* isolate,
|
|
|
|
const base::FilePath& path);
|
|
|
|
static gin::Handle<NativeImage> CreateFromBitmap(
|
2019-10-18 00:31:29 +00:00
|
|
|
gin_helper::ErrorThrower thrower,
|
2019-03-14 18:00:38 +00:00
|
|
|
v8::Local<v8::Value> buffer,
|
2019-10-18 00:31:29 +00:00
|
|
|
const gin_helper::Dictionary& options);
|
2019-10-25 13:03:28 +00:00
|
|
|
static gin::Handle<NativeImage> CreateFromBuffer(
|
2019-10-18 00:31:29 +00:00
|
|
|
gin_helper::ErrorThrower thrower,
|
|
|
|
v8::Local<v8::Value> buffer,
|
|
|
|
gin::Arguments* args);
|
2019-10-25 13:03:28 +00:00
|
|
|
static gin::Handle<NativeImage> CreateFromDataURL(v8::Isolate* isolate,
|
|
|
|
const GURL& url);
|
|
|
|
static gin::Handle<NativeImage> CreateFromNamedImage(gin::Arguments* args,
|
2020-05-11 01:24:45 +00:00
|
|
|
std::string name);
|
2020-08-24 16:36:13 +00:00
|
|
|
#if !defined(OS_LINUX)
|
|
|
|
static v8::Local<v8::Promise> CreateThumbnailFromPath(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
const base::FilePath& path,
|
|
|
|
const gfx::Size& size);
|
|
|
|
#endif
|
2015-02-11 08:03:19 +00:00
|
|
|
|
2020-07-13 21:44:12 +00:00
|
|
|
static v8::Local<v8::FunctionTemplate> GetConstructor(v8::Isolate* isolate);
|
|
|
|
|
|
|
|
// gin::Wrappable
|
|
|
|
static gin::WrapperInfo kWrapperInfo;
|
|
|
|
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate) override;
|
|
|
|
const char* GetTypeName() override;
|
2015-02-11 11:22:41 +00:00
|
|
|
|
2016-05-20 07:14:40 +00:00
|
|
|
#if defined(OS_WIN)
|
2016-05-20 08:51:05 +00:00
|
|
|
HICON GetHICON(int size);
|
2016-05-20 07:14:40 +00:00
|
|
|
#endif
|
2015-02-11 11:22:41 +00:00
|
|
|
|
2016-05-20 07:55:22 +00:00
|
|
|
const gfx::Image& image() const { return image_; }
|
|
|
|
|
2015-02-11 08:03:19 +00:00
|
|
|
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)
|
2016-05-20 08:51:05 +00:00
|
|
|
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;
|
2015-02-11 08:03:19 +00:00
|
|
|
|
|
|
|
private:
|
2019-10-18 00:31:29 +00:00
|
|
|
v8::Local<v8::Value> ToPNG(gin::Arguments* args);
|
2015-05-22 11:11:22 +00:00
|
|
|
v8::Local<v8::Value> ToJPEG(v8::Isolate* isolate, int quality);
|
2019-10-18 00:31:29 +00:00
|
|
|
v8::Local<v8::Value> ToBitmap(gin::Arguments* args);
|
2020-05-18 16:29:24 +00:00
|
|
|
std::vector<float> GetScaleFactors();
|
2019-10-18 00:31:29 +00:00
|
|
|
v8::Local<v8::Value> GetBitmap(gin::Arguments* args);
|
|
|
|
v8::Local<v8::Value> GetNativeHandle(gin_helper::ErrorThrower thrower);
|
2020-05-18 16:29:24 +00:00
|
|
|
gin::Handle<NativeImage> Resize(gin::Arguments* args,
|
2019-10-30 05:30:59 +00:00
|
|
|
base::DictionaryValue options);
|
2019-10-25 13:03:28 +00:00
|
|
|
gin::Handle<NativeImage> Crop(v8::Isolate* isolate, const gfx::Rect& rect);
|
2019-10-18 00:31:29 +00:00
|
|
|
std::string ToDataURL(gin::Arguments* args);
|
2015-02-11 08:49:43 +00:00
|
|
|
bool IsEmpty();
|
2020-05-18 16:29:24 +00:00
|
|
|
gfx::Size GetSize(const base::Optional<float> scale_factor);
|
|
|
|
float GetAspectRatio(const base::Optional<float> scale_factor);
|
2019-10-18 00:31:29 +00:00
|
|
|
void AddRepresentation(const gin_helper::Dictionary& options);
|
2015-02-11 08:03:19 +00:00
|
|
|
|
2015-04-13 03:53:24 +00:00
|
|
|
// Mark the image as template image.
|
|
|
|
void SetTemplateImage(bool setAsTemplate);
|
2015-07-27 04:58:48 +00:00
|
|
|
// Determine if the image is a template image.
|
|
|
|
bool IsTemplateImage();
|
2015-04-13 03:53:24 +00:00
|
|
|
|
2016-05-20 02:31:02 +00:00
|
|
|
#if defined(OS_WIN)
|
2016-05-20 08:51:05 +00:00
|
|
|
base::FilePath hicon_path_;
|
|
|
|
std::map<int, base::win::ScopedHICON> hicons_;
|
2016-05-20 02:31:02 +00:00
|
|
|
#endif
|
|
|
|
|
2015-02-11 08:03:19 +00:00
|
|
|
gfx::Image image_;
|
|
|
|
|
2020-07-13 21:44:12 +00:00
|
|
|
v8::Isolate* isolate_;
|
|
|
|
|
2015-02-11 08:03:19 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(NativeImage);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|
2015-02-11 08:03:19 +00:00
|
|
|
|
2019-10-18 00:31:29 +00:00
|
|
|
namespace gin {
|
2016-05-20 07:14:40 +00:00
|
|
|
|
|
|
|
// A custom converter that allows converting path to NativeImage.
|
2018-04-18 01:44:10 +00:00
|
|
|
template <>
|
2019-10-25 13:03:28 +00:00
|
|
|
struct Converter<electron::api::NativeImage*> {
|
|
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
|
|
|
electron::api::NativeImage* val);
|
2018-04-18 01:44:10 +00:00
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
2019-10-25 13:03:28 +00:00
|
|
|
electron::api::NativeImage** out);
|
2016-05-20 07:14:40 +00:00
|
|
|
};
|
|
|
|
|
2019-10-18 00:31:29 +00:00
|
|
|
} // namespace gin
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
#endif // SHELL_COMMON_API_ELECTRON_API_NATIVE_IMAGE_H_
|