feat: add nativeImage.createThumbnailFromPath API (#24802)
* initial commit, mac implementation * add documentation * convert createThumbnailFromPath to async function * windows impl protoype * add tests * added test * fix * fix test * clean up * update docs * cleaning up code * fix test * retrigger CI * retrigger CI * refactor from app to native_image * windows build * lint * lint * add smart pointers, fix test * change tests and update docs * fix test, remove nolint * add renderer-main process routing to fix tests * lint * thanks sam * thanks sam
This commit is contained in:
parent
b403e64ef2
commit
beaf60de0a
17 changed files with 221 additions and 8 deletions
|
@ -619,6 +619,10 @@ void Initialize(v8::Local<v8::Object> exports,
|
|||
native_image.SetMethod("createFromDataURL", &NativeImage::CreateFromDataURL);
|
||||
native_image.SetMethod("createFromNamedImage",
|
||||
&NativeImage::CreateFromNamedImage);
|
||||
#if !defined(OS_LINUX)
|
||||
native_image.SetMethod("createThumbnailFromPath",
|
||||
&NativeImage::CreateThumbnailFromPath);
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -68,6 +68,12 @@ class NativeImage : public gin::Wrappable<NativeImage> {
|
|||
const GURL& url);
|
||||
static gin::Handle<NativeImage> CreateFromNamedImage(gin::Arguments* args,
|
||||
std::string name);
|
||||
#if !defined(OS_LINUX)
|
||||
static v8::Local<v8::Promise> CreateThumbnailFromPath(
|
||||
v8::Isolate* isolate,
|
||||
const base::FilePath& path,
|
||||
const gfx::Size& size);
|
||||
#endif
|
||||
|
||||
static v8::Local<v8::FunctionTemplate> GetConstructor(v8::Isolate* isolate);
|
||||
|
||||
|
|
|
@ -5,12 +5,17 @@
|
|||
#include "shell/common/api/electron_api_native_image.h"
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#import <Cocoa/Cocoa.h>
|
||||
#import <QuickLook/QuickLook.h>
|
||||
|
||||
#include "base/mac/foundation_util.h"
|
||||
#include "base/strings/sys_string_conversions.h"
|
||||
#include "gin/arguments.h"
|
||||
#include "shell/common/gin_converters/image_converter.h"
|
||||
#include "shell/common/gin_helper/promise.h"
|
||||
#include "ui/gfx/color_utils.h"
|
||||
#include "ui/gfx/image/image.h"
|
||||
#include "ui/gfx/image/image_skia.h"
|
||||
|
@ -34,6 +39,49 @@ double safeShift(double in, double def) {
|
|||
return def;
|
||||
}
|
||||
|
||||
// static
|
||||
v8::Local<v8::Promise> NativeImage::CreateThumbnailFromPath(
|
||||
v8::Isolate* isolate,
|
||||
const base::FilePath& path,
|
||||
const gfx::Size& size) {
|
||||
gin_helper::Promise<gfx::Image> promise(isolate);
|
||||
v8::Local<v8::Promise> handle = promise.GetHandle();
|
||||
|
||||
if (size.IsEmpty()) {
|
||||
promise.RejectWithErrorMessage("size must not be empty");
|
||||
return handle;
|
||||
}
|
||||
|
||||
CGSize cg_size = size.ToCGSize();
|
||||
base::ScopedCFTypeRef<CFURLRef> cfurl = base::mac::FilePathToCFURL(path);
|
||||
base::ScopedCFTypeRef<QLThumbnailRef> ql_thumbnail(
|
||||
QLThumbnailCreate(kCFAllocatorDefault, cfurl, cg_size, NULL));
|
||||
__block gin_helper::Promise<gfx::Image> p = std::move(promise);
|
||||
// we do not want to blocking the main thread while waiting for quicklook to
|
||||
// generate the thumbnail
|
||||
QLThumbnailDispatchAsync(
|
||||
ql_thumbnail,
|
||||
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, /*flags*/ 0), ^{
|
||||
base::ScopedCFTypeRef<CGImageRef> cg_thumbnail(
|
||||
QLThumbnailCopyImage(ql_thumbnail));
|
||||
if (cg_thumbnail) {
|
||||
NSImage* result =
|
||||
[[[NSImage alloc] initWithCGImage:cg_thumbnail
|
||||
size:cg_size] autorelease];
|
||||
gfx::Image thumbnail(result);
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
p.Resolve(thumbnail);
|
||||
});
|
||||
} else {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
p.RejectWithErrorMessage("unable to retrieve thumbnail preview "
|
||||
"image for the given path");
|
||||
});
|
||||
}
|
||||
});
|
||||
return handle;
|
||||
}
|
||||
|
||||
gin::Handle<NativeImage> NativeImage::CreateFromNamedImage(gin::Arguments* args,
|
||||
std::string name) {
|
||||
@autoreleasepool {
|
||||
|
|
106
shell/common/api/electron_api_native_image_win.cc
Normal file
106
shell/common/api/electron_api_native_image_win.cc
Normal file
|
@ -0,0 +1,106 @@
|
|||
// Copyright (c) 2020 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "shell/common/api/electron_api_native_image.h"
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include <thumbcache.h>
|
||||
#include <wrl/client.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "shell/common/gin_converters/image_converter.h"
|
||||
#include "shell/common/gin_helper/promise.h"
|
||||
#include "shell/common/skia_util.h"
|
||||
#include "ui/gfx/icon_util.h"
|
||||
|
||||
namespace electron {
|
||||
|
||||
namespace api {
|
||||
|
||||
// static
|
||||
v8::Local<v8::Promise> NativeImage::CreateThumbnailFromPath(
|
||||
v8::Isolate* isolate,
|
||||
const base::FilePath& path,
|
||||
const gfx::Size& size) {
|
||||
gin_helper::Promise<gfx::Image> promise(isolate);
|
||||
v8::Local<v8::Promise> handle = promise.GetHandle();
|
||||
HRESULT hr;
|
||||
|
||||
if (size.IsEmpty()) {
|
||||
promise.RejectWithErrorMessage("size must not be empty");
|
||||
return handle;
|
||||
}
|
||||
|
||||
// create an IShellItem
|
||||
Microsoft::WRL::ComPtr<IShellItem> pItem;
|
||||
std::wstring image_path = path.AsUTF16Unsafe();
|
||||
hr = SHCreateItemFromParsingName(image_path.c_str(), nullptr,
|
||||
IID_PPV_ARGS(&pItem));
|
||||
|
||||
if (FAILED(hr)) {
|
||||
promise.RejectWithErrorMessage(
|
||||
"failed to create IShellItem from the given path");
|
||||
return handle;
|
||||
}
|
||||
|
||||
// Init thumbnail cache
|
||||
Microsoft::WRL::ComPtr<IThumbnailCache> pThumbnailCache;
|
||||
hr = CoCreateInstance(CLSID_LocalThumbnailCache, nullptr, CLSCTX_INPROC,
|
||||
IID_PPV_ARGS(&pThumbnailCache));
|
||||
if (FAILED(hr)) {
|
||||
promise.RejectWithErrorMessage(
|
||||
"failed to acquire local thumbnail cache reference");
|
||||
return handle;
|
||||
}
|
||||
|
||||
// Populate the IShellBitmap
|
||||
Microsoft::WRL::ComPtr<ISharedBitmap> pThumbnail;
|
||||
WTS_CACHEFLAGS flags;
|
||||
WTS_THUMBNAILID thumbId;
|
||||
hr = pThumbnailCache->GetThumbnail(pItem.Get(), size.width(),
|
||||
WTS_FLAGS::WTS_NONE, &pThumbnail, &flags,
|
||||
&thumbId);
|
||||
|
||||
if (FAILED(hr)) {
|
||||
promise.RejectWithErrorMessage(
|
||||
"failed to get thumbnail from local thumbnail cache reference");
|
||||
return handle;
|
||||
}
|
||||
|
||||
// Init HBITMAP
|
||||
HBITMAP hBitmap = NULL;
|
||||
hr = pThumbnail->GetSharedBitmap(&hBitmap);
|
||||
if (FAILED(hr)) {
|
||||
promise.RejectWithErrorMessage("failed to extract bitmap from thumbnail");
|
||||
return handle;
|
||||
}
|
||||
|
||||
// convert HBITMAP to gfx::Image
|
||||
BITMAP bitmap;
|
||||
if (!GetObject(hBitmap, sizeof(bitmap), &bitmap)) {
|
||||
promise.RejectWithErrorMessage("could not convert HBITMAP to BITMAP");
|
||||
return handle;
|
||||
}
|
||||
|
||||
ICONINFO icon_info;
|
||||
icon_info.fIcon = TRUE;
|
||||
icon_info.hbmMask = hBitmap;
|
||||
icon_info.hbmColor = hBitmap;
|
||||
|
||||
base::win::ScopedHICON icon(CreateIconIndirect(&icon_info));
|
||||
SkBitmap skbitmap = IconUtil::CreateSkBitmapFromHICON(icon.get());
|
||||
gfx::ImageSkia image_skia;
|
||||
image_skia.AddRepresentation(
|
||||
gfx::ImageSkiaRep(skbitmap, 1.0 /*scale factor*/));
|
||||
gfx::Image gfx_image = gfx::Image(image_skia);
|
||||
promise.Resolve(gfx_image);
|
||||
return handle;
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace electron
|
Loading…
Add table
Add a link
Reference in a new issue