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 colorWithDisplayP3Red:((aColor >> 16) & 0xFF) / 255.0
103 green:((aColor >> 8) & 0xFF) / 255.0
104 blue:((aColor)&0xFF) / 255.0
108 uint32_t itemCount = 0;
109 aChildren->GetLength(&itemCount);
110 NSMutableArray* orderedChildren = [NSMutableArray arrayWithCapacity:itemCount];
111 for (uint32_t i = 0; i < itemCount; ++i) {
112 nsCOMPtr<nsITouchBarInput> child = do_QueryElementAt(aChildren, i);
116 TouchBarInput* convertedChild = [[TouchBarInput alloc] initWithXPCOM:child];
117 if (convertedChild) {
118 orderedChildren[i] = convertedChild;
121 [self setChildren:orderedChildren];
128 - (TouchBarInput*)initWithXPCOM:(nsCOMPtr<nsITouchBarInput>)aInput {
130 nsresult rv = aInput->GetKey(keyStr);
135 nsAutoString titleStr;
136 rv = aInput->GetTitle(titleStr);
141 nsCOMPtr<nsIURI> imageURI;
142 rv = aInput->GetImage(getter_AddRefs(imageURI));
147 nsAutoString typeStr;
148 rv = aInput->GetType(typeStr);
153 nsCOMPtr<nsITouchBarInputCallback> callback;
154 rv = aInput->GetCallback(getter_AddRefs(callback));
160 rv = aInput->GetColor(&colorInt);
165 bool disabled = false;
166 rv = aInput->GetDisabled(&disabled);
171 nsCOMPtr<nsIArray> children;
172 rv = aInput->GetChildren(getter_AddRefs(children));
177 return [self initWithKey:nsCocoaUtils::ToNSString(keyStr)
178 title:nsCocoaUtils::ToNSString(titleStr)
180 type:nsCocoaUtils::ToNSString(typeStr)
183 disabled:(BOOL)disabled
187 - (void)releaseJSObjects {
192 [self setCallback:nil];
193 [self setImageURI:nil];
194 for (TouchBarInput* child in mChildren) {
195 [child releaseJSObjects];
205 [mChildren removeAllObjects];
210 + (NSTouchBarItemIdentifier)nativeIdentifierWithType:(NSString*)aType withKey:(NSString*)aKey {
211 NSTouchBarItemIdentifier identifier;
212 identifier = [kTouchBarBaseIdentifier stringByAppendingPathExtension:aType];
214 identifier = [identifier stringByAppendingPathExtension:aKey];
219 + (NSTouchBarItemIdentifier)nativeIdentifierWithXPCOM:(nsCOMPtr<nsITouchBarInput>)aInput {
221 nsresult rv = aInput->GetKey(keyStr);
225 NSString* key = nsCocoaUtils::ToNSString(keyStr);
227 nsAutoString typeStr;
228 rv = aInput->GetType(typeStr);
232 NSString* type = nsCocoaUtils::ToNSString(typeStr);
234 return [TouchBarInput nativeIdentifierWithType:type withKey:key];
237 + (NSTouchBarItemIdentifier)shareScrubberIdentifier {
238 return [TouchBarInput nativeIdentifierWithType:@"scrubber" withKey:@"share"];
241 + (NSTouchBarItemIdentifier)searchPopoverIdentifier {
242 return [TouchBarInput nativeIdentifierWithType:@"popover" withKey:@"search-popover"];