Add move live updating properties
This commit is contained in:
parent
61aa9bbff4
commit
5f9e9d4b36
2 changed files with 53 additions and 84 deletions
|
@ -196,11 +196,6 @@ static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.touchbar.slide
|
||||||
withSettings:(const mate::PersistentDictionary&)settings {
|
withSettings:(const mate::PersistentDictionary&)settings {
|
||||||
NSButton* button = (NSButton*)item.view;
|
NSButton* button = (NSButton*)item.view;
|
||||||
|
|
||||||
std::string customizationLabel;
|
|
||||||
if (settings.Get("customizationLabel", &customizationLabel)) {
|
|
||||||
item.customizationLabel = base::SysUTF8ToNSString(customizationLabel);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string backgroundColor;
|
std::string backgroundColor;
|
||||||
if (settings.Get("backgroundColor", &backgroundColor)) {
|
if (settings.Get("backgroundColor", &backgroundColor)) {
|
||||||
button.bezelColor = [self colorFromHexColorString:backgroundColor];
|
button.bezelColor = [self colorFromHexColorString:backgroundColor];
|
||||||
|
@ -211,17 +206,6 @@ static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.touchbar.slide
|
||||||
button.title = base::SysUTF8ToNSString(label);
|
button.title = base::SysUTF8ToNSString(label);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string labelColor;
|
|
||||||
if (!label.empty() && settings.Get("labelColor", &labelColor)) {
|
|
||||||
NSMutableAttributedString* attrTitle = [[[NSMutableAttributedString alloc] initWithString:base::SysUTF8ToNSString(label)] autorelease];
|
|
||||||
NSRange range = NSMakeRange(0, [attrTitle length]);
|
|
||||||
[attrTitle addAttribute:NSForegroundColorAttributeName
|
|
||||||
value:[self colorFromHexColorString:labelColor]
|
|
||||||
range:range];
|
|
||||||
[attrTitle fixAttributesInRange:range];
|
|
||||||
button.attributedTitle = attrTitle;
|
|
||||||
}
|
|
||||||
|
|
||||||
gfx::Image image;
|
gfx::Image image;
|
||||||
if (settings.Get("image", &image)) {
|
if (settings.Get("image", &image)) {
|
||||||
button.image = image.AsNSImage();
|
button.image = image.AsNSImage();
|
||||||
|
@ -247,11 +231,6 @@ static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.touchbar.slide
|
||||||
settings.Get("label", &label);
|
settings.Get("label", &label);
|
||||||
NSTextField* text_field = (NSTextField*)item.view;
|
NSTextField* text_field = (NSTextField*)item.view;
|
||||||
text_field.stringValue = base::SysUTF8ToNSString(label);
|
text_field.stringValue = base::SysUTF8ToNSString(label);
|
||||||
|
|
||||||
std::string customizationLabel;
|
|
||||||
if (settings.Get("customizationLabel", &customizationLabel)) {
|
|
||||||
item.customizationLabel = base::SysUTF8ToNSString(customizationLabel);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSTouchBarItem*)makeColorPickerForID:(NSString*)id
|
- (NSTouchBarItem*)makeColorPickerForID:(NSString*)id
|
||||||
|
@ -263,32 +242,26 @@ static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.touchbar.slide
|
||||||
NSColorPickerTouchBarItem* item = [[NSClassFromString(@"NSColorPickerTouchBarItem") alloc] initWithIdentifier:identifier];
|
NSColorPickerTouchBarItem* item = [[NSClassFromString(@"NSColorPickerTouchBarItem") alloc] initWithIdentifier:identifier];
|
||||||
item.target = self;
|
item.target = self;
|
||||||
item.action = @selector(colorPickerAction:);
|
item.action = @selector(colorPickerAction:);
|
||||||
|
|
||||||
std::string selectedColor;
|
|
||||||
if (settings.Get("selectedColor", &selectedColor)) {
|
|
||||||
item.color = [self colorFromHexColorString:selectedColor];
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<std::string> colors;
|
|
||||||
if (settings.Get("availableColors", &colors) && colors.size() > 0) {
|
|
||||||
NSColorList* color_list = [[[NSColorList alloc] initWithName:identifier] autorelease];
|
|
||||||
for (size_t i = 0; i < colors.size(); ++i) {
|
|
||||||
[color_list insertColor:[self colorFromHexColorString:colors[i]]
|
|
||||||
key:base::SysUTF8ToNSString(colors[i])
|
|
||||||
atIndex:i];
|
|
||||||
}
|
|
||||||
item.colorList = color_list;
|
|
||||||
}
|
|
||||||
|
|
||||||
[self updateColorPicker:item withSettings:settings];
|
[self updateColorPicker:item withSettings:settings];
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)updateColorPicker:(NSColorPickerTouchBarItem*)item
|
- (void)updateColorPicker:(NSColorPickerTouchBarItem*)item
|
||||||
withSettings:(const mate::PersistentDictionary&)settings {
|
withSettings:(const mate::PersistentDictionary&)settings {
|
||||||
std::string customizationLabel;
|
std::vector<std::string> colors;
|
||||||
if (settings.Get("customizationLabel", &customizationLabel)) {
|
if (settings.Get("availableColors", &colors) && colors.size() > 0) {
|
||||||
item.customizationLabel = base::SysUTF8ToNSString(customizationLabel);
|
NSColorList* color_list = [[[NSColorList alloc] initWithName:@""] autorelease];
|
||||||
|
for (size_t i = 0; i < colors.size(); ++i) {
|
||||||
|
[color_list insertColor:[self colorFromHexColorString:colors[i]]
|
||||||
|
key:base::SysUTF8ToNSString(colors[i])
|
||||||
|
atIndex:i];
|
||||||
|
}
|
||||||
|
item.colorList = color_list;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string selectedColor;
|
||||||
|
if (settings.Get("selectedColor", &selectedColor)) {
|
||||||
|
item.color = [self colorFromHexColorString:selectedColor];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -307,25 +280,20 @@ static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.touchbar.slide
|
||||||
|
|
||||||
- (void)updateSlider:(NSSliderTouchBarItem*)item
|
- (void)updateSlider:(NSSliderTouchBarItem*)item
|
||||||
withSettings:(const mate::PersistentDictionary&)settings {
|
withSettings:(const mate::PersistentDictionary&)settings {
|
||||||
std::string customizationLabel;
|
|
||||||
if (settings.Get("customizationLabel", &customizationLabel)) {
|
|
||||||
item.customizationLabel = base::SysUTF8ToNSString(customizationLabel);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string label;
|
std::string label;
|
||||||
settings.Get("label", &label);
|
settings.Get("label", &label);
|
||||||
item.label = base::SysUTF8ToNSString(label);
|
item.label = base::SysUTF8ToNSString(label);
|
||||||
|
|
||||||
int maxValue = 100;
|
int maxValue = 100;
|
||||||
int minValue = 0;
|
int minValue = 0;
|
||||||
int initialValue = 50;
|
int value = 50;
|
||||||
settings.Get("minValue", &minValue);
|
settings.Get("minValue", &minValue);
|
||||||
settings.Get("maxValue", &maxValue);
|
settings.Get("maxValue", &maxValue);
|
||||||
settings.Get("initialValue", &initialValue);
|
settings.Get("value", &value);
|
||||||
|
|
||||||
item.slider.minValue = minValue;
|
item.slider.minValue = minValue;
|
||||||
item.slider.maxValue = maxValue;
|
item.slider.maxValue = maxValue;
|
||||||
item.slider.doubleValue = initialValue;
|
item.slider.doubleValue = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSTouchBarItem*)makePopoverForID:(NSString*)id
|
- (NSTouchBarItem*)makePopoverForID:(NSString*)id
|
||||||
|
@ -341,11 +309,6 @@ static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.touchbar.slide
|
||||||
|
|
||||||
- (void)updatePopover:(NSPopoverTouchBarItem*)item
|
- (void)updatePopover:(NSPopoverTouchBarItem*)item
|
||||||
withSettings:(const mate::PersistentDictionary&)settings {
|
withSettings:(const mate::PersistentDictionary&)settings {
|
||||||
std::string customizationLabel;
|
|
||||||
if (settings.Get("customizationLabel", &customizationLabel)) {
|
|
||||||
item.customizationLabel = base::SysUTF8ToNSString(customizationLabel);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string label;
|
std::string label;
|
||||||
gfx::Image image;
|
gfx::Image image;
|
||||||
if (settings.Get("label", &label)) {
|
if (settings.Get("label", &label)) {
|
||||||
|
@ -387,13 +350,8 @@ static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.touchbar.slide
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return [NSClassFromString(@"NSGroupTouchBarItem") groupItemWithIdentifier:identifier
|
||||||
NSGroupTouchBarItem* item = [NSClassFromString(@"NSGroupTouchBarItem") groupItemWithIdentifier:identifier items:generatedItems];
|
items:generatedItems];
|
||||||
std::string customizationLabel;
|
|
||||||
if (settings.Get("customizationLabel", &customizationLabel)) {
|
|
||||||
item.customizationLabel = base::SysUTF8ToNSString(customizationLabel);;
|
|
||||||
}
|
|
||||||
return item;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -96,16 +96,33 @@ class TouchBarItem extends EventEmitter {
|
||||||
super()
|
super()
|
||||||
this.id = `${nextItemID++}`
|
this.id = `${nextItemID++}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_addLiveProperty (name, initialValue) {
|
||||||
|
const privateName = `_${name}`
|
||||||
|
this[privateName] = initialValue
|
||||||
|
Object.defineProperty(this, name, {
|
||||||
|
get: function () {
|
||||||
|
return this[privateName]
|
||||||
|
},
|
||||||
|
set: function (value) {
|
||||||
|
this[privateName] = value
|
||||||
|
this.emit('change')
|
||||||
|
},
|
||||||
|
enumerable: true
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TouchBar.Button = class TouchBarButton extends TouchBarItem {
|
TouchBar.Button = class TouchBarButton extends TouchBarItem {
|
||||||
constructor (config) {
|
constructor (config) {
|
||||||
super(config)
|
super(config)
|
||||||
this.type = 'button'
|
this.type = 'button'
|
||||||
this.label = config.label
|
const {click, label, backgroundColor} = config
|
||||||
this.backgroundColor = config.backgroundColor
|
this._addLiveProperty('label', label)
|
||||||
this.labelColor = config.labelColor
|
this._addLiveProperty('backgroundColor', backgroundColor)
|
||||||
this.onInteraction = config.click
|
if (typeof click === 'function') {
|
||||||
|
this.onInteraction = config.click
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,12 +130,13 @@ TouchBar.ColorPicker = class TouchBarColorPicker extends TouchBarItem {
|
||||||
constructor (config) {
|
constructor (config) {
|
||||||
super(config)
|
super(config)
|
||||||
this.type = 'colorpicker'
|
this.type = 'colorpicker'
|
||||||
this.availableColors = config.availableColors
|
const {availableColors, change, selectedColor} = config
|
||||||
this.selectedColor = config.selectedColor
|
this._addLiveProperty('availableColors', availableColors)
|
||||||
|
this._addLiveProperty('selectedColor', selectedColor)
|
||||||
|
|
||||||
const {change} = config
|
|
||||||
if (typeof change === 'function') {
|
if (typeof change === 'function') {
|
||||||
this.onInteraction = (details) => {
|
this.onInteraction = (details) => {
|
||||||
|
this._selectedColor = details.color
|
||||||
change(details.color)
|
change(details.color)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -140,16 +158,7 @@ TouchBar.Label = class TouchBarLabel extends TouchBarItem {
|
||||||
constructor (config) {
|
constructor (config) {
|
||||||
super(config)
|
super(config)
|
||||||
this.type = 'label'
|
this.type = 'label'
|
||||||
this._label = config.label
|
this._addLiveProperty('label', config.label)
|
||||||
}
|
|
||||||
|
|
||||||
set label (newLabel) {
|
|
||||||
this._label = newLabel
|
|
||||||
this.emit('change')
|
|
||||||
}
|
|
||||||
|
|
||||||
get label () {
|
|
||||||
return this._label
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,7 +166,7 @@ TouchBar.Spacer = class TouchBarSpacer extends TouchBarItem {
|
||||||
constructor (config) {
|
constructor (config) {
|
||||||
super(config)
|
super(config)
|
||||||
this.type = 'spacer'
|
this.type = 'spacer'
|
||||||
this.size = config.size
|
this._addLiveProperty('size', config.size)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,7 +174,7 @@ TouchBar.Popover = class TouchBarPopover extends TouchBarItem {
|
||||||
constructor (config) {
|
constructor (config) {
|
||||||
super(config)
|
super(config)
|
||||||
this.type = 'popover'
|
this.type = 'popover'
|
||||||
this.label = config.label
|
this._addLiveProperty('label', config.label)
|
||||||
this.showCloseButton = config.showCloseButton
|
this.showCloseButton = config.showCloseButton
|
||||||
this.child = config.items
|
this.child = config.items
|
||||||
if (!(this.child instanceof TouchBar)) {
|
if (!(this.child instanceof TouchBar)) {
|
||||||
|
@ -178,13 +187,15 @@ TouchBar.Slider = class TouchBarSlider extends TouchBarItem {
|
||||||
constructor (config) {
|
constructor (config) {
|
||||||
super(config)
|
super(config)
|
||||||
this.type = 'slider'
|
this.type = 'slider'
|
||||||
this.minValue = config.minValue
|
const {change, label, minValue, maxValue, value} = config
|
||||||
this.maxValue = config.maxValue
|
this._addLiveProperty('label', label)
|
||||||
this.initialValue = config.initialValue
|
this._addLiveProperty('minValue', minValue)
|
||||||
|
this._addLiveProperty('maxValue', maxValue)
|
||||||
|
this._addLiveProperty('value', value)
|
||||||
|
|
||||||
const {change} = config
|
|
||||||
if (typeof change === 'function') {
|
if (typeof change === 'function') {
|
||||||
this.onInteraction = (details) => {
|
this.onInteraction = (details) => {
|
||||||
|
this._value = details.value
|
||||||
change(details.value)
|
change(details.value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue