From 28d5c8bbdec6c636b83b23235707496945a29bc6 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Fri, 16 Dec 2016 18:01:49 +1100 Subject: [PATCH] Add updateConfig ability to other items --- atom/browser/native_window_mac.mm | 31 +++++++++++++++++++++++++------ default_app/default_app.js | 19 ++++++++++--------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 09317221c8ac..eb2ad60f5d22 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -413,9 +413,18 @@ bool ScopedDisableResize::disable_resize_ = false; mate::PersistentDictionary dict; if (args->GetNext(&dict) && dict.Get("type", &type) && dict.Get("id", &item_id)) { if (item_map.find(item_id) != item_map.end()) { - if (type == "slider") { - NSSliderTouchBarItem* item = (NSSliderTouchBarItem *)item_map[item_id]; - [self updateSlider:item withOpts:dict]; + if (type == "button") { + [self updateButton:(NSCustomTouchBarItem *)item_map[item_id] withOpts:dict withID:[NSString stringWithUTF8String:item_id.c_str()] andCreate:false]; + } else if (type == "label") { + [self updateLabel:(NSCustomTouchBarItem *)item_map[item_id] withOpts:dict]; + } else if (type == "colorpicker") { + [self updateColorPicker:(NSColorPickerTouchBarItem *)item_map[item_id] withOpts:dict]; + } else if (type == "slider") { + [self updateSlider:(NSSliderTouchBarItem *)item_map[item_id] withOpts:dict]; + } else if (type == "popover") { + [self updatePopOver:(NSPopoverTouchBarItem *)item_map[item_id] withOpts:dict]; + } else if (type == "group") { + args->ThrowError("You can not update the config of a group. Update the individual items or replace the group"); } } } @@ -495,6 +504,10 @@ bool ScopedDisableResize::disable_resize_ = false; - (NSButton*)makeButtonForDict:(mate::PersistentDictionary)dict withLabel:(std::string)label { NSButton *theButton = [NSButton buttonWithTitle:[NSString stringWithUTF8String:label.c_str()] target:self action:@selector(buttonAction:)]; + return [self updateNSButton:theButton forDict:dict withLabel:label]; +} + +- (NSButton*)updateNSButton:(NSButton*)theButton forDict:(mate::PersistentDictionary)dict withLabel:(std::string)label { std::string backgroundColor; if (dict.Get("backgroundColor", &backgroundColor)) { theButton.bezelColor = [self colorFromHexColorString:[NSString stringWithUTF8String:backgroundColor.c_str()]]; @@ -522,13 +535,19 @@ bool ScopedDisableResize::disable_resize_ = false; if (![self hasTBDict:s_id]) return nil; mate::PersistentDictionary item = item_id_map[s_id]; NSCustomTouchBarItem *customItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier]; - return [self updateButton:customItem withOpts:item withID:id]; + return [self updateButton:customItem withOpts:item withID:id andCreate:true]; } -- (nullable NSTouchBarItem *)updateButton:(NSCustomTouchBarItem*)customItem withOpts:(mate::PersistentDictionary)item withID:(NSString*)id { +- (nullable NSTouchBarItem *)updateButton:(NSCustomTouchBarItem*)customItem withOpts:(mate::PersistentDictionary)item withID:(NSString*)id andCreate:(bool)create { std::string label; if (item.Get("label", &label)) { - NSButton* theButton = [self makeButtonForDict:item withLabel:label]; + NSButton* theButton = nil; + if (!create) { + theButton = (NSButton*)customItem.view; + [self updateNSButton:theButton forDict:item withLabel:label]; + } else { + theButton = [self makeButtonForDict:item withLabel:label]; + } theButton.tag = [id floatValue]; customItem.view = theButton; diff --git a/default_app/default_app.js b/default_app/default_app.js index 42644ac36c40..549c6d73114d 100644 --- a/default_app/default_app.js +++ b/default_app/default_app.js @@ -36,17 +36,18 @@ exports.load = (appUrl) => { }); global.slider = slider; + global.button = new (TouchBar.Button)({ + label: 'Hello World!', + // image: '/path/to/image', + backgroundColor: 'FF0000', + labelColor: '0000FF', + click: () => { + console.log('Hello World Clicked') + } + }); mainWindow.setTouchBar(new TouchBar([ - new (TouchBar.Button)({ - label: 'Hello World!', - // image: '/path/to/image', - backgroundColor: 'FF0000', - labelColor: '0000FF', - click: () => { - console.log('Hello World Clicked') - } - }), + button, new (TouchBar.Label)({ label: 'This is a Label' }),