From fe396ef002fca13b2ef403cffaac39815e6dca61 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Mon, 10 Nov 2025 21:18:00 +0100 Subject: [PATCH] feat: add SF Symbol support to NativeImage::CreateFromNamedImage (#48772) * feat: add SF Symbol support to NativeImage::CreateFromNamedImage Co-authored-by: TheCommieAxolotl <87679354+TheCommieAxolotl@users.noreply.github.com> * use obj-c name in NSImage constructor Co-authored-by: TheCommieAxolotl <87679354+TheCommieAxolotl@users.noreply.github.com> * add test for named symbol image Co-authored-by: TheCommieAxolotl <87679354+TheCommieAxolotl@users.noreply.github.com> * apply suggested simplification Co-authored-by: TheCommieAxolotl <87679354+TheCommieAxolotl@users.noreply.github.com> * fix: support NX cocoa prefix Co-authored-by: TheCommieAxolotl <87679354+TheCommieAxolotl@users.noreply.github.com> --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: TheCommieAxolotl <87679354+TheCommieAxolotl@users.noreply.github.com> --- docs/api/native-image.md | 3 +-- shell/common/api/electron_api_native_image_mac.mm | 13 +++++++++++-- spec/api-native-image-spec.ts | 5 +++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/docs/api/native-image.md b/docs/api/native-image.md index 61b3c5ab31..d9b87c0c33 100644 --- a/docs/api/native-image.md +++ b/docs/api/native-image.md @@ -202,8 +202,7 @@ Creates a new `NativeImage` instance from `dataUrl`, a base 64 encoded [Data URL Returns `NativeImage` Creates a new `NativeImage` instance from the `NSImage` that maps to the -given image name. See Apple's [`NSImageName`](https://developer.apple.com/documentation/appkit/nsimagename#2901388) -documentation for a list of possible values. +given image name. See Apple's [`NSImageName`](https://developer.apple.com/documentation/appkit/nsimagename#2901388) documentation and [SF Symbols](https://developer.apple.com/sf-symbols/) for a list of possible values. The `hslShift` is applied to the image with the following rules: diff --git a/shell/common/api/electron_api_native_image_mac.mm b/shell/common/api/electron_api_native_image_mac.mm index f25a4e0b64..2075fdcdd6 100644 --- a/shell/common/api/electron_api_native_image_mac.mm +++ b/shell/common/api/electron_api_native_image_mac.mm @@ -120,9 +120,18 @@ gin_helper::Handle NativeImage::CreateFromNamedImage( name.erase(pos, to_remove.length()); } - NSImage* image = [NSImage imageNamed:base::SysUTF8ToNSString(name)]; + NSImage* image = nil; + NSString* ns_name = base::SysUTF8ToNSString(name); - if (!image.valid) { + // 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()); } diff --git a/spec/api-native-image-spec.ts b/spec/api-native-image-spec.ts index 9d32af6d7c..3ad3e08be6 100644 --- a/spec/api-native-image-spec.ts +++ b/spec/api-native-image-spec.ts @@ -348,6 +348,11 @@ describe('nativeImage module', () => { expect(image.isEmpty()).to.be.false(); }); + ifit(process.platform === 'darwin')('returns a valid named symbol on darwin', function () { + const image = nativeImage.createFromNamedImage('atom'); + expect(image.isEmpty()).to.be.false(); + }); + ifit(process.platform === 'darwin')('returns allows an HSL shift for a valid image on darwin', function () { const image = nativeImage.createFromNamedImage('NSActionTemplate', [0.5, 0.2, 0.8]); expect(image.isEmpty()).to.be.false();