Repository: electron/electron
File: shell/common/api/electron_api_native_image_mac.mm (Objective-C++)

Code before:
    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 = nil;
    NSString* ns_name = base::SysUTF8ToNSString(name);

    // If the name does not include NS, we can assume it is a sf symbol
    std::string has_ns("NS");
    size_t ns_pos = name.find(has_ns);
    if (ns_pos == std::string::npos) {
      if (@available(macOS 11.0, *)) {
        image = [NSImage imageWithSystemSymbolName:ns_name accessibilityDescription:nil];
      }
    }
    if (!image) {
      image = [NSImage imageNamed:ns_name];
    }

    if (!image || !image.valid) {
      return CreateEmpty(args->isolate());
    }

    NSData* png_data = bufferFromNSImage(image);

    if (args->GetNext(&hsl_shift) && hsl_shift.size() == 3) {
      auto gfx_image = gfx::Image::CreateFrom1xPNGBytes(
          electron::util::as_byte_span(png_data));
      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(),
                         electron::util::as_byte_span(png_data));

Review comment (refactor):
I think we can simplify this a bit!

```suggestion
  NSImage* image = nil;
  NSString* ns_name = base::SysUTF8ToNSString(name);

  // Treat non-Cocoa-prefixed names as SF Symbols first.
  if (base::StartsWith(name, "NS")) {
     image = [NSImage imageWithSystemSymbolName:ns_name
                               accessibilityDescription:nil];
  } else {
     image = [NSImage imageNamed:ns_name];
  }
```

Code after:
    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 = nil;
    NSString* ns_name = base::SysUTF8ToNSString(name);

    // Treat non-Cocoa-prefixed names as SF Symbols first.
    if (!base::StartsWith(name, "NS") && !base::StartsWith(name, "NX")) {
      image = [NSImage imageWithSystemSymbolName:ns_name
                        accessibilityDescription:nil];
    } else {
      image = [NSImage imageNamed:ns_name];
    }

    if (!image || !image.valid) {
      return CreateEmpty(args->isolate());
    }

    NSData* png_data = bufferFromNSImage(image);

    if (args->GetNext(&hsl_shift) && hsl_shift.size() == 3) {
      auto gfx_image = gfx::Image::CreateFrom1xPNGBytes(
          electron::util::as_byte_span(png_data));
      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(),
                         electron::util::as_byte_span(png_data));
  }
}

void NativeImage::SetTemplateImage(bool setAsTemplate) {