1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 * Creates icons for display in native menu items on macOS.
10 #include "MOZIconHelper.h"
12 #include "imgIContainer.h"
13 #include "nsCocoaUtils.h"
15 @implementation MOZIconHelper
17 // Returns an autoreleased empty NSImage.
18 + (NSImage*)placeholderIconWithSize:(NSSize)aSize {
19 return [[[NSImage alloc] initWithSize:aSize] autorelease];
22 // Returns an autoreleased NSImage.
23 + (NSImage*)iconImageFromImageContainer:(imgIContainer*)aImage
24 withSize:(NSSize)aSize
26 (const mozilla::SVGImageContext*)aSVGContext
27 scaleFactor:(CGFloat)aScaleFactor {
28 bool isEntirelyBlack = false;
29 NSImage* retainedImage = nil;
31 if (aScaleFactor != 0.0f) {
32 rv = nsCocoaUtils::CreateNSImageFromImageContainer(
33 aImage, imgIContainer::FRAME_CURRENT, aSVGContext, aSize,
34 &retainedImage, aScaleFactor, &isEntirelyBlack);
36 rv = nsCocoaUtils::CreateDualRepresentationNSImageFromImageContainer(
37 aImage, imgIContainer::FRAME_CURRENT, aSVGContext, aSize,
38 &retainedImage, &isEntirelyBlack);
41 NSImage* image = [retainedImage autorelease];
43 if (NS_FAILED(rv) || !image) {
47 // If all the color channels in the image are black, treat the image as a
48 // template. This will cause macOS to use the image's alpha channel as a mask
49 // and it will fill it with a color that looks good in the context that it's
50 // used in. For example, for regular menu items, the image will be black, but
51 // when the menu item is hovered (and its background is blue), it will be
53 [image setTemplate:isEntirelyBlack];
55 [image setSize:aSize];