no bug - Import translations from android-l10n r=release a=l10n CLOSED TREE
[gecko.git] / widget / cocoa / nsTouchBarInput.mm
blobaf638f52d300c43581188ca68675f2df62e4568f
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 {
16   return mImageURI;
19 - (void)setImageURI:(nsCOMPtr<nsIURI>)aImageURI {
20   mImageURI = aImageURI;
23 - (RefPtr<nsTouchBarInputIcon>)icon {
24   return mIcon;
27 - (void)setIcon:(RefPtr<nsTouchBarInputIcon>)aIcon {
28   mIcon = aIcon;
31 - (TouchBarInputBaseType)baseType {
32   return mBaseType;
35 - (NSString*)type {
36   return mType;
39 - (void)setType:(NSString*)aType {
40   [aType retain];
41   [mType release];
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;
54   }
55   mType = aType;
58 - (NSTouchBarItemIdentifier)nativeIdentifier {
59   return [TouchBarInput nativeIdentifierWithType:mType withKey:self.key];
62 - (nsCOMPtr<nsITouchBarInputCallback>)callback {
63   return mCallback;
66 - (void)setCallback:(nsCOMPtr<nsITouchBarInputCallback>)aCallback {
67   mCallback = aCallback;
70 - (NSMutableArray<TouchBarInput*>*)children {
71   return mChildren;
74 - (void)setChildren:(NSMutableArray<TouchBarInput*>*)aChildren {
75   [aChildren retain];
76   for (TouchBarInput* child in mChildren) {
77     [child releaseJSObjects];
78   }
79   [mChildren removeAllObjects];
80   [mChildren release];
81   mChildren = aChildren;
84 - (id)initWithKey:(NSString*)aKey
85             title:(NSString*)aTitle
86          imageURI:(nsCOMPtr<nsIURI>)aImageURI
87              type:(NSString*)aType
88          callback:(nsCOMPtr<nsITouchBarInputCallback>)aCallback
89             color:(uint32_t)aColor
90          disabled:(BOOL)aDisabled
91          children:(nsCOMPtr<nsIArray>)aChildren {
92   if (self = [super init]) {
93     mType = nil;
95     self.key = aKey;
96     self.title = aTitle;
97     self.type = aType;
98     self.disabled = aDisabled;
99     [self setImageURI:aImageURI];
100     [self setCallback:aCallback];
101     if (aColor) {
102       [self setColor:[NSColor
103                          colorWithDisplayP3Red:((aColor >> 16) & 0xFF) / 255.0
104                                          green:((aColor >> 8) & 0xFF) / 255.0
105                                           blue:((aColor) & 0xFF) / 255.0
106                                          alpha:1.0]];
107     }
108     if (aChildren) {
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);
115         if (!child) {
116           continue;
117         }
118         TouchBarInput* convertedChild =
119             [[TouchBarInput alloc] initWithXPCOM:child];
120         if (convertedChild) {
121           orderedChildren[i] = convertedChild;
122         }
123       }
124       [self setChildren:orderedChildren];
125     }
126   }
128   return self;
131 - (TouchBarInput*)initWithXPCOM:(nsCOMPtr<nsITouchBarInput>)aInput {
132   nsAutoString keyStr;
133   nsresult rv = aInput->GetKey(keyStr);
134   if (NS_FAILED(rv)) {
135     return nil;
136   }
138   nsAutoString titleStr;
139   rv = aInput->GetTitle(titleStr);
140   if (NS_FAILED(rv)) {
141     return nil;
142   }
144   nsCOMPtr<nsIURI> imageURI;
145   rv = aInput->GetImage(getter_AddRefs(imageURI));
146   if (NS_FAILED(rv)) {
147     return nil;
148   }
150   nsAutoString typeStr;
151   rv = aInput->GetType(typeStr);
152   if (NS_FAILED(rv)) {
153     return nil;
154   }
156   nsCOMPtr<nsITouchBarInputCallback> callback;
157   rv = aInput->GetCallback(getter_AddRefs(callback));
158   if (NS_FAILED(rv)) {
159     return nil;
160   }
162   uint32_t colorInt;
163   rv = aInput->GetColor(&colorInt);
164   if (NS_FAILED(rv)) {
165     return nil;
166   }
168   bool disabled = false;
169   rv = aInput->GetDisabled(&disabled);
170   if (NS_FAILED(rv)) {
171     return nil;
172   }
174   nsCOMPtr<nsIArray> children;
175   rv = aInput->GetChildren(getter_AddRefs(children));
176   if (NS_FAILED(rv)) {
177     return nil;
178   }
180   return [self initWithKey:nsCocoaUtils::ToNSString(keyStr)
181                      title:nsCocoaUtils::ToNSString(titleStr)
182                   imageURI:imageURI
183                       type:nsCocoaUtils::ToNSString(typeStr)
184                   callback:callback
185                      color:colorInt
186                   disabled:(BOOL)disabled
187                   children:children];
190 - (void)releaseJSObjects {
191   if (mIcon) {
192     mIcon->Destroy();
193     mIcon = nil;
194   }
195   [self setCallback:nil];
196   [self setImageURI:nil];
197   for (TouchBarInput* child in mChildren) {
198     [child releaseJSObjects];
199   }
202 - (void)dealloc {
203   if (mIcon) {
204     mIcon->Destroy();
205     mIcon = nil;
206   }
207   [mType release];
208   [mChildren removeAllObjects];
209   [mChildren release];
210   [super dealloc];
213 + (NSTouchBarItemIdentifier)nativeIdentifierWithType:(NSString*)aType
214                                              withKey:(NSString*)aKey {
215   NSTouchBarItemIdentifier identifier;
216   identifier = [kTouchBarBaseIdentifier stringByAppendingPathExtension:aType];
217   if (aKey) {
218     identifier = [identifier stringByAppendingPathExtension:aKey];
219   }
220   return identifier;
223 + (NSTouchBarItemIdentifier)nativeIdentifierWithXPCOM:
224     (nsCOMPtr<nsITouchBarInput>)aInput {
225   nsAutoString keyStr;
226   nsresult rv = aInput->GetKey(keyStr);
227   if (NS_FAILED(rv)) {
228     return nil;
229   }
230   NSString* key = nsCocoaUtils::ToNSString(keyStr);
232   nsAutoString typeStr;
233   rv = aInput->GetType(typeStr);
234   if (NS_FAILED(rv)) {
235     return nil;
236   }
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"];
251 @end