1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "nsTouchBarInput.h"
7 #include "mozilla/MacStringHelpers.h"
8 #include "nsArrayUtils.h"
9 #include "nsCocoaUtils.h"
10 #include "nsTouchBar.h"
11 #include "nsTouchBarInputIcon.h"
13 @implementation TouchBarInput
15 - (nsCOMPtr<nsIURI>)imageURI {
19 - (void)setImageURI:(nsCOMPtr<nsIURI>)aImageURI {
20 mImageURI = aImageURI;
23 - (RefPtr<nsTouchBarInputIcon>)icon {
27 - (void)setIcon:(RefPtr<nsTouchBarInputIcon>)aIcon {
31 - (TouchBarInputBaseType)baseType {
39 - (void)setType:(NSString*)aType {
42 if ([aType hasSuffix:@"button"]) {
43 mBaseType = TouchBarInputBaseType::kButton;
44 } else if ([aType hasSuffix:@"label"]) {
45 mBaseType = TouchBarInputBaseType::kLabel;
46 } else if ([aType hasSuffix:@"mainButton"]) {
47 mBaseType = TouchBarInputBaseType::kMainButton;
48 } else if ([aType hasSuffix:@"popover"]) {
49 mBaseType = TouchBarInputBaseType::kPopover;
50 } else if ([aType hasSuffix:@"scrollView"]) {
51 mBaseType = TouchBarInputBaseType::kScrollView;
52 } else if ([aType hasSuffix:@"scrubber"]) {
53 mBaseType = TouchBarInputBaseType::kScrubber;
58 - (NSTouchBarItemIdentifier)nativeIdentifier {
59 return [TouchBarInput nativeIdentifierWithType:mType withKey:self.key];
62 - (nsCOMPtr<nsITouchBarInputCallback>)callback {
66 - (void)setCallback:(nsCOMPtr<nsITouchBarInputCallback>)aCallback {
67 mCallback = aCallback;
70 - (NSMutableArray<TouchBarInput*>*)children {
74 - (void)setChildren:(NSMutableArray<TouchBarInput*>*)aChildren {
76 for (TouchBarInput* child in mChildren) {
77 [child releaseJSObjects];
79 [mChildren removeAllObjects];
81 mChildren = aChildren;
84 - (id)initWithKey:(NSString*)aKey
85 title:(NSString*)aTitle
86 imageURI:(nsCOMPtr<nsIURI>)aImageURI
88 callback:(nsCOMPtr<nsITouchBarInputCallback>)aCallback
89 color:(uint32_t)aColor
90 disabled:(BOOL)aDisabled
91 children:(nsCOMPtr<nsIArray>)aChildren {
92 if (self = [super init]) {
98 self.disabled = aDisabled;
99 [self setImageURI:aImageURI];
100 [self setCallback:aCallback];
102 [self setColor:[NSColor
103 colorWithDisplayP3Red:((aColor >> 16) & 0xFF) / 255.0
104 green:((aColor >> 8) & 0xFF) / 255.0
105 blue:((aColor) & 0xFF) / 255.0
109 uint32_t itemCount = 0;
110 aChildren->GetLength(&itemCount);
111 NSMutableArray* orderedChildren =
112 [NSMutableArray arrayWithCapacity:itemCount];
113 for (uint32_t i = 0; i < itemCount; ++i) {
114 nsCOMPtr<nsITouchBarInput> child = do_QueryElementAt(aChildren, i);
118 TouchBarInput* convertedChild =
119 [[TouchBarInput alloc] initWithXPCOM:child];
120 if (convertedChild) {
121 orderedChildren[i] = convertedChild;
124 [self setChildren:orderedChildren];
131 - (TouchBarInput*)initWithXPCOM:(nsCOMPtr<nsITouchBarInput>)aInput {
133 nsresult rv = aInput->GetKey(keyStr);
138 nsAutoString titleStr;
139 rv = aInput->GetTitle(titleStr);
144 nsCOMPtr<nsIURI> imageURI;
145 rv = aInput->GetImage(getter_AddRefs(imageURI));
150 nsAutoString typeStr;
151 rv = aInput->GetType(typeStr);
156 nsCOMPtr<nsITouchBarInputCallback> callback;
157 rv = aInput->GetCallback(getter_AddRefs(callback));
163 rv = aInput->GetColor(&colorInt);
168 bool disabled = false;
169 rv = aInput->GetDisabled(&disabled);
174 nsCOMPtr<nsIArray> children;
175 rv = aInput->GetChildren(getter_AddRefs(children));
180 return [self initWithKey:nsCocoaUtils::ToNSString(keyStr)
181 title:nsCocoaUtils::ToNSString(titleStr)
183 type:nsCocoaUtils::ToNSString(typeStr)
186 disabled:(BOOL)disabled
190 - (void)releaseJSObjects {
195 [self setCallback:nil];
196 [self setImageURI:nil];
197 for (TouchBarInput* child in mChildren) {
198 [child releaseJSObjects];
208 [mChildren removeAllObjects];
213 + (NSTouchBarItemIdentifier)nativeIdentifierWithType:(NSString*)aType
214 withKey:(NSString*)aKey {
215 NSTouchBarItemIdentifier identifier;
216 identifier = [kTouchBarBaseIdentifier stringByAppendingPathExtension:aType];
218 identifier = [identifier stringByAppendingPathExtension:aKey];
223 + (NSTouchBarItemIdentifier)nativeIdentifierWithXPCOM:
224 (nsCOMPtr<nsITouchBarInput>)aInput {
226 nsresult rv = aInput->GetKey(keyStr);
230 NSString* key = nsCocoaUtils::ToNSString(keyStr);
232 nsAutoString typeStr;
233 rv = aInput->GetType(typeStr);
237 NSString* type = nsCocoaUtils::ToNSString(typeStr);
239 return [TouchBarInput nativeIdentifierWithType:type withKey:key];
242 + (NSTouchBarItemIdentifier)shareScrubberIdentifier {
243 return [TouchBarInput nativeIdentifierWithType:@"scrubber" withKey:@"share"];
246 + (NSTouchBarItemIdentifier)searchPopoverIdentifier {
247 return [TouchBarInput nativeIdentifierWithType:@"popover"
248 withKey:@"search-popover"];