2019-12-31 20:26:00 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "nsTouchBarInput.h"
|
|
|
|
|
|
|
|
#include "mozilla/MacStringHelpers.h"
|
2020-12-11 04:09:45 +00:00
|
|
|
#include "nsArrayUtils.h"
|
|
|
|
#include "nsCocoaUtils.h"
|
2020-12-11 04:09:18 +00:00
|
|
|
#include "nsTouchBar.h"
|
2019-12-31 20:26:00 +00:00
|
|
|
#include "nsTouchBarInputIcon.h"
|
|
|
|
|
|
|
|
@implementation TouchBarInput
|
2020-12-11 04:09:52 +00:00
|
|
|
|
2019-12-31 20:26:00 +00:00
|
|
|
- (nsCOMPtr<nsIURI>)imageURI {
|
|
|
|
return mImageURI;
|
|
|
|
}
|
2020-12-11 04:09:52 +00:00
|
|
|
|
|
|
|
- (void)setImageURI:(nsCOMPtr<nsIURI>)aImageURI {
|
|
|
|
mImageURI = aImageURI;
|
|
|
|
}
|
|
|
|
|
2019-12-31 20:26:00 +00:00
|
|
|
- (RefPtr<nsTouchBarInputIcon>)icon {
|
|
|
|
return mIcon;
|
|
|
|
}
|
|
|
|
|
2020-12-11 04:09:52 +00:00
|
|
|
- (void)setIcon:(RefPtr<nsTouchBarInputIcon>)aIcon {
|
|
|
|
mIcon = aIcon;
|
2019-12-31 20:26:00 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 04:09:52 +00:00
|
|
|
- (TouchBarInputBaseType)baseType {
|
|
|
|
return mBaseType;
|
2019-12-31 20:26:00 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 04:09:52 +00:00
|
|
|
- (NSString*)type {
|
|
|
|
return mType;
|
2019-12-31 20:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setType:(NSString*)aType {
|
|
|
|
[aType retain];
|
|
|
|
[mType release];
|
2020-01-03 17:07:25 +00:00
|
|
|
if ([aType hasSuffix:@"button"]) {
|
|
|
|
mBaseType = TouchBarInputBaseType::kButton;
|
|
|
|
} else if ([aType hasSuffix:@"label"]) {
|
|
|
|
mBaseType = TouchBarInputBaseType::kLabel;
|
|
|
|
} else if ([aType hasSuffix:@"mainButton"]) {
|
|
|
|
mBaseType = TouchBarInputBaseType::kMainButton;
|
|
|
|
} else if ([aType hasSuffix:@"popover"]) {
|
|
|
|
mBaseType = TouchBarInputBaseType::kPopover;
|
|
|
|
} else if ([aType hasSuffix:@"scrollView"]) {
|
|
|
|
mBaseType = TouchBarInputBaseType::kScrollView;
|
|
|
|
} else if ([aType hasSuffix:@"scrubber"]) {
|
|
|
|
mBaseType = TouchBarInputBaseType::kScrubber;
|
|
|
|
}
|
2019-12-31 20:26:00 +00:00
|
|
|
mType = aType;
|
|
|
|
}
|
|
|
|
|
2020-12-11 04:09:52 +00:00
|
|
|
- (NSTouchBarItemIdentifier)nativeIdentifier {
|
|
|
|
return [TouchBarInput nativeIdentifierWithType:mType withKey:self.key];
|
2019-12-31 20:26:00 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 04:09:52 +00:00
|
|
|
- (nsCOMPtr<nsITouchBarInputCallback>)callback {
|
|
|
|
return mCallback;
|
2019-12-31 20:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setCallback:(nsCOMPtr<nsITouchBarInputCallback>)aCallback {
|
|
|
|
mCallback = aCallback;
|
|
|
|
}
|
|
|
|
|
2020-12-11 04:09:52 +00:00
|
|
|
- (NSMutableArray<TouchBarInput*>*)children {
|
|
|
|
return mChildren;
|
|
|
|
}
|
|
|
|
|
2019-12-31 20:26:00 +00:00
|
|
|
- (void)setChildren:(NSMutableArray<TouchBarInput*>*)aChildren {
|
|
|
|
[aChildren retain];
|
|
|
|
for (TouchBarInput* child in mChildren) {
|
|
|
|
[child releaseJSObjects];
|
|
|
|
}
|
|
|
|
[mChildren removeAllObjects];
|
|
|
|
[mChildren release];
|
|
|
|
mChildren = aChildren;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)initWithKey:(NSString*)aKey
|
|
|
|
title:(NSString*)aTitle
|
|
|
|
imageURI:(nsCOMPtr<nsIURI>)aImageURI
|
|
|
|
type:(NSString*)aType
|
|
|
|
callback:(nsCOMPtr<nsITouchBarInputCallback>)aCallback
|
|
|
|
color:(uint32_t)aColor
|
|
|
|
disabled:(BOOL)aDisabled
|
|
|
|
children:(nsCOMPtr<nsIArray>)aChildren {
|
|
|
|
if (self = [super init]) {
|
2020-12-11 04:09:52 +00:00
|
|
|
mType = nil;
|
|
|
|
|
|
|
|
self.key = aKey;
|
|
|
|
self.title = aTitle;
|
|
|
|
self.type = aType;
|
|
|
|
self.disabled = aDisabled;
|
2019-12-31 20:26:00 +00:00
|
|
|
[self setImageURI:aImageURI];
|
|
|
|
[self setCallback:aCallback];
|
|
|
|
if (aColor) {
|
|
|
|
[self setColor:[NSColor colorWithDisplayP3Red:((aColor >> 16) & 0xFF) / 255.0
|
|
|
|
green:((aColor >> 8) & 0xFF) / 255.0
|
|
|
|
blue:((aColor)&0xFF) / 255.0
|
|
|
|
alpha:1.0]];
|
|
|
|
}
|
|
|
|
if (aChildren) {
|
|
|
|
uint32_t itemCount = 0;
|
|
|
|
aChildren->GetLength(&itemCount);
|
|
|
|
NSMutableArray* orderedChildren = [NSMutableArray arrayWithCapacity:itemCount];
|
|
|
|
for (uint32_t i = 0; i < itemCount; ++i) {
|
|
|
|
nsCOMPtr<nsITouchBarInput> child = do_QueryElementAt(aChildren, i);
|
|
|
|
if (!child) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
TouchBarInput* convertedChild = [[TouchBarInput alloc] initWithXPCOM:child];
|
|
|
|
if (convertedChild) {
|
|
|
|
orderedChildren[i] = convertedChild;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[self setChildren:orderedChildren];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (TouchBarInput*)initWithXPCOM:(nsCOMPtr<nsITouchBarInput>)aInput {
|
|
|
|
nsAutoString keyStr;
|
|
|
|
nsresult rv = aInput->GetKey(keyStr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString titleStr;
|
|
|
|
rv = aInput->GetTitle(titleStr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> imageURI;
|
|
|
|
rv = aInput->GetImage(getter_AddRefs(imageURI));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString typeStr;
|
|
|
|
rv = aInput->GetType(typeStr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsITouchBarInputCallback> callback;
|
|
|
|
rv = aInput->GetCallback(getter_AddRefs(callback));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t colorInt;
|
|
|
|
rv = aInput->GetColor(&colorInt);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool disabled = false;
|
|
|
|
rv = aInput->GetDisabled(&disabled);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIArray> children;
|
|
|
|
rv = aInput->GetChildren(getter_AddRefs(children));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [self initWithKey:nsCocoaUtils::ToNSString(keyStr)
|
|
|
|
title:nsCocoaUtils::ToNSString(titleStr)
|
|
|
|
imageURI:imageURI
|
|
|
|
type:nsCocoaUtils::ToNSString(typeStr)
|
|
|
|
callback:callback
|
|
|
|
color:colorInt
|
|
|
|
disabled:(BOOL)disabled
|
|
|
|
children:children];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)releaseJSObjects {
|
|
|
|
if (mIcon) {
|
2020-01-03 17:07:25 +00:00
|
|
|
mIcon->Destroy();
|
|
|
|
mIcon = nil;
|
2019-12-31 20:26:00 +00:00
|
|
|
}
|
|
|
|
[self setCallback:nil];
|
|
|
|
[self setImageURI:nil];
|
|
|
|
for (TouchBarInput* child in mChildren) {
|
|
|
|
[child releaseJSObjects];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc {
|
|
|
|
if (mIcon) {
|
|
|
|
mIcon->Destroy();
|
|
|
|
mIcon = nil;
|
|
|
|
}
|
|
|
|
[mType release];
|
|
|
|
[mChildren removeAllObjects];
|
|
|
|
[mChildren release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSTouchBarItemIdentifier)nativeIdentifierWithType:(NSString*)aType withKey:(NSString*)aKey {
|
|
|
|
NSTouchBarItemIdentifier identifier;
|
2020-12-11 04:09:18 +00:00
|
|
|
identifier = [kTouchBarBaseIdentifier stringByAppendingPathExtension:aType];
|
2019-12-31 20:26:00 +00:00
|
|
|
if (aKey) {
|
|
|
|
identifier = [identifier stringByAppendingPathExtension:aKey];
|
|
|
|
}
|
|
|
|
return identifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSTouchBarItemIdentifier)nativeIdentifierWithXPCOM:(nsCOMPtr<nsITouchBarInput>)aInput {
|
|
|
|
nsAutoString keyStr;
|
|
|
|
nsresult rv = aInput->GetKey(keyStr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
NSString* key = nsCocoaUtils::ToNSString(keyStr);
|
|
|
|
|
|
|
|
nsAutoString typeStr;
|
|
|
|
rv = aInput->GetType(typeStr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
NSString* type = nsCocoaUtils::ToNSString(typeStr);
|
|
|
|
|
|
|
|
return [TouchBarInput nativeIdentifierWithType:type withKey:key];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|