electron/shell/common/api/electron_api_native_image_mac.mm
Charles Kerr e70ce89235
chore: more iwyu (#43063)
* chore: iwyu shell/browser/electron_pdf_document_helper_client.h

* chore: iwyu shell/browser/hid/electron_hid_delegate.h

* chore: iwyu content/public/browser/web_contents.h

* chore: iwyu shell/browser/usb/electron_usb_delegate.h

* chore: iwyu shell/browser/browser_observer.h

* chore: iwyu shell/browser/bluetooth/electron_bluetooth_delegate.h

* chore: iwyu shell/browser/serial/electron_serial_delegate.h

* chore: iwyu shell/browser/api/frame_subscriber.h

* chore: iwyu mojo/public/cpp/bindings/

* chore: iwyu components/

* chore: iwyu extensions/

* chore: iwyu shell/common/gin_helper/

* chore: iwyu v8/

* chore: iwyu base/containers/linked_list.h

* chore: iwyu shell/browser/native_window.h

* chore: iwyu shell/browser/api/electron_api_base_window.h

* chore: iwyu shell/common/node_includes.h

* chore: iwyu gin/handle.h

* chore: iwyu base/functional/callback.h

* chore: iwyu ui/gfx/

* chore: iwyu content/public/browser/render_frame_host.h

* fix: mac

* fix: mac

* fix: win

* chore: iwyu base/files/file_path.h

* chore: iwyu base/unguessable_token.h

* chore: iwyu ui/display/screen.h

* chore: iwyu chrome/browser/predictors/preconnect_manager.h

* chore: iwyu base/observer_list_types.h

* chore: iwyu content/public/browser/web_contents.h

* chore: iwyu chrome/browser/devtools/devtools_eye_dropper.h

* chore: iwyu shell/browser/ui/inspectable_web_contents.h

* chore: iwyu content/public/browser/keyboard_event_processing_result.h

* chore: iwyu net/cookies/canonical_cookie.h

* chore: iwyu net/base/address_list.h

* chore: iwyu net/cert/x509_certificate.h

* chore: iwyu net/cookies/cookie_change_dispatcher.h

* chore: iwyu net/dns/public/host_resolver_results.h

* fix: mac

* Revert "chore: iwyu net/cert/x509_certificate.h"

This reverts commit 002896f71146e90f1e29e090a1d6eede48cee11e.
2024-07-29 12:42:57 -05:00

155 lines
5.4 KiB
Text

// Copyright (c) 2015 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 <string>
#include <utility>
#include <vector>
#import <Cocoa/Cocoa.h>
#import <QuickLook/QuickLook.h>
#import <QuickLookThumbnailing/QuickLookThumbnailing.h>
#include "base/apple/foundation_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/task/bind_post_task.h"
#include "gin/arguments.h"
#include "gin/handle.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/geometry/size.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_operations.h"
namespace electron::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:NSBitmapImageFileTypePNG
properties:[[NSDictionary alloc] init]];
}
double safeShift(double in, double def) {
if (in >= 0 || in <= 1 || in == def)
return in;
return def;
}
void ReceivedThumbnailResult(CGSize size,
gin_helper::Promise<gfx::Image> p,
QLThumbnailRepresentation* thumbnail,
NSError* error) {
if (error || !thumbnail) {
std::string err_msg([error.localizedDescription UTF8String]);
p.RejectWithErrorMessage("unable to retrieve thumbnail preview "
"image for the given path: " +
err_msg);
} else {
NSImage* result = [[NSImage alloc] initWithCGImage:[thumbnail CGImage]
size:size];
gfx::Image image(result);
p.Resolve(image);
}
}
// 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();
NSURL* nsurl = base::apple::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];
QLThumbnailGenerationRequest* request([[QLThumbnailGenerationRequest alloc]
initWithFileAtURL:nsurl
size:cg_size
scale:[screen backingScaleFactor]
representationTypes:QLThumbnailGenerationRequestRepresentationTypeAll]);
__block auto block_callback = base::BindPostTaskToCurrentDefault(
base::BindOnce(&ReceivedThumbnailResult, cg_size, std::move(promise)));
auto completionHandler =
^(QLThumbnailRepresentation* thumbnail, NSError* error) {
std::move(block_callback).Run(thumbnail, error);
};
[[QLThumbnailGenerator sharedGenerator]
generateBestRepresentationForRequest:request
completionHandler:completionHandler];
return handle;
}
gin::Handle<NativeImage> NativeImage::CreateFromNamedImage(gin::Arguments* args,
std::string name) {
@autoreleasepool {
std::vector<double> hsl_shift;
// The string representations of NSImageNames don't match the strings
// themselves; they instead follow the following pattern:
// * NSImageNameActionTemplate -> "NSActionTemplate"
// * NSImageNameMultipleDocuments -> "NSMultipleDocuments"
// To account for this, we strip out "ImageName" from the passed string.
std::string to_remove("ImageName");
size_t pos = name.find(to_remove);
if (pos != std::string::npos) {
name.erase(pos, to_remove.length());
}
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))
.AsNSImage());
}
return CreateFromPNG(args->isolate(), (char*)[png_data bytes],
[png_data length]);
}
}
void NativeImage::SetTemplateImage(bool setAsTemplate) {
[image_.AsNSImage() setTemplate:setAsTemplate];
}
bool NativeImage::IsTemplateImage() {
return [image_.AsNSImage() isTemplate];
}
} // namespace electron::api