Merge pull request #10727 from electron/named-nativeimage
Add an API to get a nativeImage from a named NSImage
This commit is contained in:
commit
bdf3552be6
5 changed files with 113 additions and 0 deletions
|
@ -541,6 +541,13 @@ mate::Handle<NativeImage> NativeImage::CreateFromDataURL(
|
|||
return CreateEmpty(isolate);
|
||||
}
|
||||
|
||||
#if !defined(OS_MACOSX)
|
||||
mate::Handle<NativeImage> NativeImage::CreateFromNamedImage(
|
||||
mate::Arguments* args, const std::string& name) {
|
||||
return CreateEmpty(args->isolate());
|
||||
}
|
||||
#endif
|
||||
|
||||
// static
|
||||
void NativeImage::BuildPrototype(
|
||||
v8::Isolate* isolate, v8::Local<v8::FunctionTemplate> prototype) {
|
||||
|
@ -609,6 +616,8 @@ void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
|||
dict.SetMethod("createFromBuffer", &atom::api::NativeImage::CreateFromBuffer);
|
||||
dict.SetMethod("createFromDataURL",
|
||||
&atom::api::NativeImage::CreateFromDataURL);
|
||||
dict.SetMethod("createFromNamedImage",
|
||||
&atom::api::NativeImage::CreateFromNamedImage);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -53,6 +53,8 @@ class NativeImage : public mate::Wrappable<NativeImage> {
|
|||
mate::Arguments* args, v8::Local<v8::Value> buffer);
|
||||
static mate::Handle<NativeImage> CreateFromDataURL(
|
||||
v8::Isolate* isolate, const GURL& url);
|
||||
static mate::Handle<NativeImage> CreateFromNamedImage(
|
||||
mate::Arguments* args, const std::string& name);
|
||||
|
||||
static void BuildPrototype(v8::Isolate* isolate,
|
||||
v8::Local<v8::FunctionTemplate> prototype);
|
||||
|
|
|
@ -6,10 +6,56 @@
|
|||
|
||||
#import <Cocoa/Cocoa.h>
|
||||
|
||||
#include "base/strings/sys_string_conversions.h"
|
||||
#include "ui/gfx/color_utils.h"
|
||||
#include "ui/gfx/image/image.h"
|
||||
#include "ui/gfx/image/image_skia.h"
|
||||
#include "ui/gfx/image/image_skia_operations.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
NSData* bufferFromNSImage(NSImage* image) {
|
||||
CGImageRef ref = [image CGImageForProposedRect:nil context:nil hints:nil];
|
||||
NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithCGImage:ref];
|
||||
[rep setSize:[image size]];
|
||||
return [rep representationUsingType:NSPNGFileType properties:[[NSDictionary alloc] init]];
|
||||
}
|
||||
|
||||
double safeShift(double in, double def) {
|
||||
if (in >= 0 || in <= 1 || in == def) return in;
|
||||
return def;
|
||||
}
|
||||
|
||||
mate::Handle<NativeImage> NativeImage::CreateFromNamedImage(
|
||||
mate::Arguments* args, const std::string& name) {
|
||||
@autoreleasepool {
|
||||
std::vector<double> hsl_shift;
|
||||
NSImage* image = [NSImage imageNamed:base::SysUTF8ToNSString(name)];
|
||||
if (!image.valid) {
|
||||
return CreateEmpty(args->isolate());
|
||||
}
|
||||
|
||||
NSData* png_data = bufferFromNSImage(image);
|
||||
|
||||
if (args->GetNext(&hsl_shift) && hsl_shift.size() == 3) {
|
||||
gfx::Image gfx_image = gfx::Image::CreateFrom1xPNGBytes(
|
||||
reinterpret_cast<const unsigned char*>((char *) [png_data bytes]), [png_data length]);
|
||||
color_utils::HSL shift = {
|
||||
safeShift(hsl_shift[0], -1),
|
||||
safeShift(hsl_shift[1], 0.5),
|
||||
safeShift(hsl_shift[2], 0.5)
|
||||
};
|
||||
png_data = bufferFromNSImage(gfx::Image(
|
||||
gfx::ImageSkiaOperations::CreateHSLShiftedImage(
|
||||
gfx_image.AsImageSkia(), shift)).CopyNSImage());
|
||||
}
|
||||
|
||||
return CreateFromPNG(args->isolate(), (char *) [png_data bytes], [png_data length]);
|
||||
}
|
||||
}
|
||||
|
||||
void NativeImage::SetTemplateImage(bool setAsTemplate) {
|
||||
[image_.AsNSImage() setTemplate:setAsTemplate];
|
||||
}
|
||||
|
|
|
@ -157,6 +157,34 @@ Returns `NativeImage`
|
|||
|
||||
Creates a new `NativeImage` instance from `dataURL`.
|
||||
|
||||
### `nativeImage.createFromNamedImage(imageName[, hslShift])` _macOS_
|
||||
|
||||
* `imageName` String
|
||||
* `hslShift` Number[]
|
||||
|
||||
Returns `NativeImage`
|
||||
|
||||
Creates a new `NativeImage` instance from the NSImage that maps to the
|
||||
given image name. See [`NSImageName`](https://developer.apple.com/documentation/appkit/nsimagename?language=objc)
|
||||
for a list of possible values.
|
||||
|
||||
The `hslShift` is applied to the image with the following rules
|
||||
* `hsl_shift[0]` (hue): The absolute hue value for the image - 0 and 1 map
|
||||
to 0 and 360 on the hue color wheel (red).
|
||||
* `hsl_shift[1]` (saturation): A saturation shift for the image, with the
|
||||
following key values:
|
||||
0 = remove all color.
|
||||
0.5 = leave unchanged.
|
||||
1 = fully saturate the image.
|
||||
* `hsl_shift[2]` (lightness): A lightness shift for the image, with the
|
||||
following key values:
|
||||
0 = remove all lightness (make all pixels black).
|
||||
0.5 = leave unchanged.
|
||||
1 = full lightness (make all pixels white).
|
||||
|
||||
This means that `[-1, 0, 1]` will make the image completely white and
|
||||
`[-1, 1, 0]` will make the image completely black.
|
||||
|
||||
## Class: NativeImage
|
||||
|
||||
> Natively wrap images such as tray, dock, and application icons.
|
||||
|
|
|
@ -190,6 +190,34 @@ describe('nativeImage module', () => {
|
|||
})
|
||||
})
|
||||
|
||||
describe('createFromNamedImage(name)', () => {
|
||||
it('returns empty for invalid options', () => {
|
||||
const image = nativeImage.createFromNamedImage('totally_not_real')
|
||||
assert(image.isEmpty())
|
||||
})
|
||||
|
||||
it('returns empty on non-darwin platforms', () => {
|
||||
if (process.platform === 'darwin') return
|
||||
|
||||
const image = nativeImage.createFromNamedImage('NSActionTemplate')
|
||||
assert(image.isEmpty())
|
||||
})
|
||||
|
||||
it('returns a valid image on darwin', () => {
|
||||
if (process.platform !== 'darwin') return
|
||||
|
||||
const image = nativeImage.createFromNamedImage('NSActionTemplate')
|
||||
assert(!image.isEmpty())
|
||||
})
|
||||
|
||||
it('returns allows an HSL shift for a valid image on darwin', () => {
|
||||
if (process.platform !== 'darwin') return
|
||||
|
||||
const image = nativeImage.createFromNamedImage('NSActionTemplate', [0.5, 0.2, 0.8])
|
||||
assert(!image.isEmpty())
|
||||
})
|
||||
})
|
||||
|
||||
describe('resize(options)', () => {
|
||||
it('returns a resized image', () => {
|
||||
const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
|
||||
|
|
Loading…
Reference in a new issue