Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / widget / cocoa / MOZIconHelper.mm
blobada013f4c104c5d23e6a3b85f558d1c052d4f4fe
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/. */
6 /*
7  * Creates icons for display in native menu items on macOS.
8  */
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
25                              svgContext:
26                                  (const mozilla::SVGImageContext*)aSVGContext
27                             scaleFactor:(CGFloat)aScaleFactor {
28   bool isEntirelyBlack = false;
29   NSImage* retainedImage = nil;
30   nsresult rv;
31   if (aScaleFactor != 0.0f) {
32     rv = nsCocoaUtils::CreateNSImageFromImageContainer(
33         aImage, imgIContainer::FRAME_CURRENT, aSVGContext, aSize,
34         &retainedImage, aScaleFactor, &isEntirelyBlack);
35   } else {
36     rv = nsCocoaUtils::CreateDualRepresentationNSImageFromImageContainer(
37         aImage, imgIContainer::FRAME_CURRENT, aSVGContext, aSize,
38         &retainedImage, &isEntirelyBlack);
39   }
41   NSImage* image = [retainedImage autorelease];
43   if (NS_FAILED(rv) || !image) {
44     return nil;
45   }
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
52   // filled with white.
53   [image setTemplate:isEntirelyBlack];
55   [image setSize:aSize];
57   return image;
60 @end