First hack at being able to return NSImage pointers
This commit is contained in:
parent
b3da5370c0
commit
262abc43f8
1 changed files with 56 additions and 0 deletions
|
@ -58,6 +58,20 @@ ScaleFactorPair kScaleFactorPairs[] = {
|
||||||
{ "@2.5x" , 2.5f },
|
{ "@2.5x" , 2.5f },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum NativeRepresentation {
|
||||||
|
INVALID = 0,
|
||||||
|
AS_NSIMAGE,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct NativeRepresentationPair {
|
||||||
|
const char* name;
|
||||||
|
NativeRepresentation rep;
|
||||||
|
};
|
||||||
|
|
||||||
|
NativeRepresentationPair kNativeRepresentations[] {
|
||||||
|
{ "nsimage", NativeRepresentation::AS_NSIMAGE },
|
||||||
|
};
|
||||||
|
|
||||||
float GetScaleFactorFromPath(const base::FilePath& path) {
|
float GetScaleFactorFromPath(const base::FilePath& path) {
|
||||||
std::string filename(path.BaseName().RemoveExtension().AsUTF8Unsafe());
|
std::string filename(path.BaseName().RemoveExtension().AsUTF8Unsafe());
|
||||||
|
|
||||||
|
@ -184,6 +198,7 @@ mate::ObjectTemplateBuilder NativeImage::GetObjectTemplateBuilder(
|
||||||
template_.Reset(isolate, mate::ObjectTemplateBuilder(isolate)
|
template_.Reset(isolate, mate::ObjectTemplateBuilder(isolate)
|
||||||
.SetMethod("toPng", &NativeImage::ToPNG)
|
.SetMethod("toPng", &NativeImage::ToPNG)
|
||||||
.SetMethod("toJpeg", &NativeImage::ToJPEG)
|
.SetMethod("toJpeg", &NativeImage::ToJPEG)
|
||||||
|
.SetMethod("asNativeRepresentation", &NativeImage::AsNativeRepresentation)
|
||||||
.SetMethod("toDataURL", &NativeImage::ToDataURL)
|
.SetMethod("toDataURL", &NativeImage::ToDataURL)
|
||||||
.SetMethod("toDataUrl", &NativeImage::ToDataURL) // deprecated.
|
.SetMethod("toDataUrl", &NativeImage::ToDataURL) // deprecated.
|
||||||
.SetMethod("isEmpty", &NativeImage::IsEmpty)
|
.SetMethod("isEmpty", &NativeImage::IsEmpty)
|
||||||
|
@ -221,6 +236,47 @@ std::string NativeImage::ToDataURL() {
|
||||||
return data_url;
|
return data_url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
v8::Local<v8::Value> NativeImage::AsNativeRepresentation(v8::Isolate* isolate, mate::Arguments* args) {
|
||||||
|
NativeRepresentation desiredRep = NativeRepresentation::INVALID;
|
||||||
|
void* ptr = nullptr;
|
||||||
|
std::string type;
|
||||||
|
|
||||||
|
if (!args->GetNext(&type)) {
|
||||||
|
args->ThrowError();
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const NativeRepresentationPair& item : kNativeRepresentations) {
|
||||||
|
if (type.compare(item.name) == 0) {
|
||||||
|
desiredRep = item.rep;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (desiredRep == NativeRepresentation::INVALID) {
|
||||||
|
args->ThrowError();
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (desiredRep) {
|
||||||
|
#if defined(OS_MACOSX)
|
||||||
|
case NativeRepresentation::AS_NSIMAGE:
|
||||||
|
ptr = reinterpret_cast<void*>(image_.AsNSImage());
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
default:
|
||||||
|
args->ThrowError();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
out:
|
||||||
|
|
||||||
|
return node::Buffer::Copy(
|
||||||
|
isolate,
|
||||||
|
reinterpret_cast<char*>(ptr),
|
||||||
|
sizeof(void*)).ToLocalChecked();
|
||||||
|
}
|
||||||
|
|
||||||
bool NativeImage::IsEmpty() {
|
bool NativeImage::IsEmpty() {
|
||||||
return image_.IsEmpty();
|
return image_.IsEmpty();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue