From f153d082975ef3018b08ce7585cd0fa3dea1abfb Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 1 Mar 2017 12:50:36 -0800 Subject: [PATCH] Support setting the initial/available colors --- atom/browser/ui/cocoa/atom_touch_bar.mm | 21 ++++++++++++++++++- .../ui/cocoa/touch_bar_forward_declarations.h | 1 + lib/browser/api/touch-bar.js | 8 ++++--- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/atom/browser/ui/cocoa/atom_touch_bar.mm b/atom/browser/ui/cocoa/atom_touch_bar.mm index f7656fa8031f..0613b2afc3b3 100644 --- a/atom/browser/ui/cocoa/atom_touch_bar.mm +++ b/atom/browser/ui/cocoa/atom_touch_bar.mm @@ -245,7 +245,7 @@ static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.touchbar.slide } - (NSTouchBarItem*)makeColorPickerForID:(NSString*)id - withIdentifier:(NSString*)identifier { + withIdentifier:(NSString*)identifier { std::string s_id([id UTF8String]); if (![self hasItemWithID:s_id]) return nil; @@ -253,6 +253,25 @@ static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.touchbar.slide NSColorPickerTouchBarItem* item = [[NSClassFromString(@"NSColorPickerTouchBarItem") alloc] initWithIdentifier:identifier]; item.target = self; item.action = @selector(colorPickerAction:); + + std::string selectedColor; + if (settings.Get("selectedColor", &selectedColor)) { + item.color = [self colorFromHexColorString:selectedColor]; + } + + std::vector 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; + } + + item.showsAlpha = NO; + [self updateColorPicker:item withSettings:settings]; return item; } diff --git a/atom/browser/ui/cocoa/touch_bar_forward_declarations.h b/atom/browser/ui/cocoa/touch_bar_forward_declarations.h index e9a6e5525b66..e4e2bc15a5dd 100644 --- a/atom/browser/ui/cocoa/touch_bar_forward_declarations.h +++ b/atom/browser/ui/cocoa/touch_bar_forward_declarations.h @@ -98,6 +98,7 @@ static const NSTouchBarItemIdentifier NSTouchBarItemIdentifierOtherItemsProxy = @property SEL action; @property(weak) id target; @property(copy) NSColor *color; +@property(strong) NSColorList *colorList; @end diff --git a/lib/browser/api/touch-bar.js b/lib/browser/api/touch-bar.js index 93cfb8988da2..62008b70d88d 100644 --- a/lib/browser/api/touch-bar.js +++ b/lib/browser/api/touch-bar.js @@ -1,6 +1,6 @@ const {EventEmitter} = require('events') -let itemIdIncrementor = 1 +let nextItemID = 1 class TouchBar extends EventEmitter { @@ -94,7 +94,7 @@ class TouchBar extends EventEmitter { class TouchBarItem extends EventEmitter { constructor (config) { super() - this.id = `${itemIdIncrementor++}` + this.id = `${nextItemID++}` } } @@ -113,6 +113,8 @@ TouchBar.ColorPicker = class TouchBarColorPicker extends TouchBarItem { constructor (config) { super(config) this.type = 'colorpicker' + this.availableColors = config.availableColors + this.selectedColor = config.selectedColor const {change} = config if (typeof change === 'function') { @@ -151,7 +153,7 @@ TouchBar.Label = class TouchBarLabel extends TouchBarItem { } } -TouchBar.PopOver = class TouchBarPopOver extends TouchBarItem { +TouchBar.Popover = class TouchBarPopover extends TouchBarItem { constructor (config) { super(config) this.type = 'popover'