fix: NSImageName string conversion (#23467)

This commit is contained in:
Shelley Vohr 2020-05-10 18:24:45 -07:00 committed by GitHub
parent 392ea320cf
commit 6114518463
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 8 deletions

View file

@ -33,12 +33,24 @@ double safeShift(double in, double def) {
return def;
}
gin::Handle<NativeImage> NativeImage::CreateFromNamedImage(
gin::Arguments* args,
const std::string& name) {
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());
}