refactor: use QuickLookThumbnailing where applicable (#32456)
This commit is contained in:
parent
c3d11e2ea2
commit
4c39eb32b0
2 changed files with 79 additions and 26 deletions
2
BUILD.gn
2
BUILD.gn
|
@ -501,6 +501,8 @@ source_set("electron_lib") {
|
|||
"StoreKit.framework",
|
||||
]
|
||||
|
||||
weak_frameworks = [ "QuickLookThumbnailing.framework" ]
|
||||
|
||||
sources += [
|
||||
"shell/browser/ui/views/autofill_popup_view.cc",
|
||||
"shell/browser/ui/views/autofill_popup_view.h",
|
||||
|
|
|
@ -10,8 +10,10 @@
|
|||
|
||||
#import <Cocoa/Cocoa.h>
|
||||
#import <QuickLook/QuickLook.h>
|
||||
#import <QuickLookThumbnailing/QuickLookThumbnailing.h>
|
||||
|
||||
#include "base/mac/foundation_util.h"
|
||||
#include "base/mac/scoped_nsobject.h"
|
||||
#include "base/strings/sys_string_conversions.h"
|
||||
#include "gin/arguments.h"
|
||||
#include "shell/common/gin_converters/image_converter.h"
|
||||
|
@ -53,12 +55,59 @@ v8::Local<v8::Promise> NativeImage::CreateThumbnailFromPath(
|
|||
}
|
||||
|
||||
CGSize cg_size = size.ToCGSize();
|
||||
|
||||
if (@available(macOS 10.15, *)) {
|
||||
NSURL* nsurl = base::mac::FilePathToNSURL(path);
|
||||
|
||||
// We need to explicitly check if the user has passed an invalid path
|
||||
// because QLThumbnailGenerationRequest will generate a stock file icon
|
||||
// and pass silently if we do not.
|
||||
if (![[NSFileManager defaultManager] fileExistsAtPath:[nsurl path]]) {
|
||||
promise.RejectWithErrorMessage(
|
||||
"unable to retrieve thumbnail preview image for the given path");
|
||||
return handle;
|
||||
}
|
||||
|
||||
NSScreen* screen = [[NSScreen screens] firstObject];
|
||||
base::scoped_nsobject<QLThumbnailGenerationRequest> request(
|
||||
[[QLThumbnailGenerationRequest alloc]
|
||||
initWithFileAtURL:nsurl
|
||||
size:cg_size
|
||||
scale:[screen backingScaleFactor]
|
||||
representationTypes:
|
||||
QLThumbnailGenerationRequestRepresentationTypeAll]);
|
||||
__block gin_helper::Promise<gfx::Image> p = std::move(promise);
|
||||
[[QLThumbnailGenerator sharedGenerator]
|
||||
generateBestRepresentationForRequest:request
|
||||
completionHandler:^(
|
||||
QLThumbnailRepresentation* thumbnail,
|
||||
NSError* error) {
|
||||
if (error || !thumbnail) {
|
||||
std::string err_msg(
|
||||
[error.localizedDescription UTF8String]);
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
p.RejectWithErrorMessage(
|
||||
"unable to retrieve thumbnail preview "
|
||||
"image for the given path: " +
|
||||
err_msg);
|
||||
});
|
||||
} else {
|
||||
NSImage* result = [[[NSImage alloc]
|
||||
initWithCGImage:[thumbnail CGImage]
|
||||
size:cg_size] autorelease];
|
||||
gfx::Image image(result);
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
p.Resolve(image);
|
||||
});
|
||||
}
|
||||
}];
|
||||
} else {
|
||||
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
|
||||
// Do not block the main thread waiting for quicklook to generate the
|
||||
// thumbnail.
|
||||
QLThumbnailDispatchAsync(
|
||||
ql_thumbnail,
|
||||
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, /*flags*/ 0), ^{
|
||||
|
@ -79,6 +128,8 @@ v8::Local<v8::Promise> NativeImage::CreateThumbnailFromPath(
|
|||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue