diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index a75df27fce61..ae52b03f079a 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -282,8 +282,8 @@ void Window::OnExecuteWindowsCommand(const std::string& command_name) { Emit("app-command", command_name); } -void Window::OnTouchBarItemResult(const std::string& item_type, const std::string& item_id) { - Emit("-touch-bar-interaction", item_type, item_id); +void Window::OnTouchBarItemResult(const std::string& item_type, const std::vector& args) { + Emit("-touch-bar-interaction", item_type, args); } #if defined(OS_WIN) diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index 54aa01d3886f..55347bb54da2 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -85,7 +85,7 @@ class Window : public mate::TrackableObject, void OnRendererUnresponsive() override; void OnRendererResponsive() override; void OnExecuteWindowsCommand(const std::string& command_name) override; - void OnTouchBarItemResult(const std::string& item_type, const std::string& item_id) override; + void OnTouchBarItemResult(const std::string& item_type, const std::vector& args) override; #if defined(OS_WIN) void OnWindowMessage(UINT message, WPARAM w_param, LPARAM l_param) override; diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index 53068045ea77..1aead897d990 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -574,9 +574,9 @@ void NativeWindow::NotifyWindowExecuteWindowsCommand( void NativeWindow::NotifyTouchBarItemInteraction( const std::string& type, - const std::string& item_id) { + const std::vector& args) { FOR_EACH_OBSERVER(NativeWindowObserver, observers_, - OnTouchBarItemResult(type, item_id)); + OnTouchBarItemResult(type, args)); } #if defined(OS_WIN) diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index bd7584e0d103..38636fa2a5a9 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -233,7 +233,7 @@ class NativeWindow : public base::SupportsUserData, void NotifyWindowEnterHtmlFullScreen(); void NotifyWindowLeaveHtmlFullScreen(); void NotifyWindowExecuteWindowsCommand(const std::string& command); - void NotifyTouchBarItemInteraction(const std::string& item_type, const std::string& item_id); + void NotifyTouchBarItemInteraction(const std::string& item_type, const std::vector& args); #if defined(OS_WIN) void NotifyWindowMessage(UINT message, WPARAM w_param, LPARAM l_param); diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 87a27cae07a0..1e83d133cfbd 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -361,7 +361,7 @@ bool ScopedDisableResize::disable_resize_ = false; @implementation AtomNSWindow NSMutableArray* bar_items_ = [[NSMutableArray alloc] init]; - std::map button_labels; + std::map item_labels; - (void)setShell:(atom::NativeWindowMac*)shell { shell_ = shell; @@ -380,8 +380,8 @@ bool ScopedDisableResize::disable_resize_ = false; - (void)reloadTouchBar { bar_items_ = [[NSMutableArray alloc] init]; std::vector items = shell_->GetTouchBarItems(); - std::map new_button_labels; - button_labels = new_button_labels; + std::map new_labels; + item_labels = new_labels; NSLog(@"reload"); for (mate::Dictionary &item : items ) { @@ -395,8 +395,16 @@ bool ScopedDisableResize::disable_resize_ = false; std::string label; if (item.Get("label", &label)) { [bar_items_ addObject:[NSString stringWithFormat:@"%@%@", ButtonIdentifier, [NSString stringWithUTF8String:item_id.c_str()]]]; - button_labels.insert(make_pair(item_id, label)); + item_labels.insert(make_pair(item_id, label)); } + } else if (type == "label") { + std::string label; + if (item.Get("label", &label)) { + [bar_items_ addObject:[NSString stringWithFormat:@"%@%@", LabelIdentifier, [NSString stringWithUTF8String:item_id.c_str()]]]; + item_labels.insert(make_pair(item_id, label)); + } + } else if (type == "colorpicker") { + [bar_items_ addObject:[NSString stringWithFormat:@"%@%@", ColorPickerIdentifier, [NSString stringWithUTF8String:item_id.c_str()]]]; } } } @@ -424,13 +432,17 @@ bool ScopedDisableResize::disable_resize_ = false; - (void)buttonAction:(id)sender { NSString* item_id = [NSString stringWithFormat:@"com.electron.tb.button.%d", (int)((NSButton *)sender).tag]; NSLog(@"Button with ID: '%@' was pressed", item_id); - shell_->NotifyTouchBarItemInteraction("button", std::string([item_id UTF8String])); + shell_->NotifyTouchBarItemInteraction("button", { std::string([item_id UTF8String]) }); } - (void)colorPickerAction:(id)sender { NSString* item_id = ((NSColorPickerTouchBarItem *)sender).identifier; - NSLog(@"ColorPicker with ID: '%@' was updated with color '%@'", item_id, ((NSColorPickerTouchBarItem *)sender).color); - shell_->NotifyTouchBarItemInteraction("color_picker", std::string([item_id UTF8String])); + NSColor* color = ((NSColorPickerTouchBarItem *)sender).color; + NSString* colorHexString = [NSString stringWithFormat:@"#%02X%02X%02X", + (int) (color.redComponent * 0xFF), (int) (color.greenComponent * 0xFF), + (int) (color.blueComponent * 0xFF)]; + NSLog(@"ColorPicker with ID: '%@' was updated with color '%@'", item_id, colorHexString); + shell_->NotifyTouchBarItemInteraction("color_picker", { std::string([item_id UTF8String]), std::string([colorHexString UTF8String]) }); } static NSTouchBarItemIdentifier ButtonIdentifier = @"com.electron.tb.button."; @@ -439,19 +451,31 @@ static NSTouchBarItemIdentifier ColorPickerIdentifier = @"com.electron.tb.colorp static NSTouchBarItemIdentifier LabelIdentifier = @"com.electron.tb.label."; // static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.tb.slider."; +- (NSString*)idFromIdentifier:(NSString*)identifier withPrefix:(NSString*)prefix { + NSString *idCopy = [identifier copy]; + idCopy = [identifier substringFromIndex:[prefix length]]; + return idCopy; +} + +- (bool)hasLabel:(NSString*)id { + return item_labels.find(std::string([id UTF8String])) != item_labels.end(); +} + - (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { if ([identifier hasPrefix:ButtonIdentifier]) { - NSString *idCopy = [identifier copy]; - idCopy = [identifier substringFromIndex:[ButtonIdentifier length]]; - NSButton *theButton = [NSButton buttonWithTitle:[NSString stringWithUTF8String:button_labels[std::string([idCopy UTF8String])].c_str()] target:self action:@selector(buttonAction:)]; - theButton.tag = [idCopy floatValue]; + NSString* id = [self idFromIdentifier:identifier withPrefix:ButtonIdentifier]; + if (![self hasLabel:id]) return nil; + NSButton *theButton = [NSButton buttonWithTitle:[NSString stringWithUTF8String:item_labels[std::string([id UTF8String])].c_str()] target:self action:@selector(buttonAction:)]; + theButton.tag = [id floatValue]; NSCustomTouchBarItem *customItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier]; customItem.view = theButton; return customItem; } else if ([identifier hasPrefix:LabelIdentifier]) { - NSTextField *theLabel = [NSTextField labelWithString:@"Hello From Electron"]; + NSString* id = [self idFromIdentifier:identifier withPrefix:LabelIdentifier]; + if (![self hasLabel:id]) return nil; + NSTextField *theLabel = [NSTextField labelWithString:[NSString stringWithUTF8String:item_labels[std::string([id UTF8String])].c_str()]]; NSCustomTouchBarItem *customItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier]; customItem.view = theLabel; diff --git a/atom/browser/native_window_observer.h b/atom/browser/native_window_observer.h index e3d9deaed2ee..bd9ff21f56c9 100644 --- a/atom/browser/native_window_observer.h +++ b/atom/browser/native_window_observer.h @@ -70,7 +70,7 @@ class NativeWindowObserver { virtual void OnWindowLeaveFullScreen() {} virtual void OnWindowEnterHtmlFullScreen() {} virtual void OnWindowLeaveHtmlFullScreen() {} - virtual void OnTouchBarItemResult(const std::string& item_type, const std::string& item_id) {} + virtual void OnTouchBarItemResult(const std::string& item_type, const std::vector& args) {} // Called when window message received #if defined(OS_WIN) diff --git a/default_app/default_app.js b/default_app/default_app.js index 35e290b4e9d7..3856c8e90abe 100644 --- a/default_app/default_app.js +++ b/default_app/default_app.js @@ -31,6 +31,14 @@ exports.load = (appUrl) => { click: () => { console.log('Hello World Clicked') } + }), + new (TouchBar.Label)({ + label: 'This is a Label' + }), + new (TouchBar.ColorPicker)({ + change: (...args) => { + console.log('Color was changed', ...args) + } }) ])) }) diff --git a/lib/browser/api/touch-bar.js b/lib/browser/api/touch-bar.js index d8ce377bc86e..f62d7c2ff03a 100644 --- a/lib/browser/api/touch-bar.js +++ b/lib/browser/api/touch-bar.js @@ -23,8 +23,9 @@ class TouchBar { let item_id_incrementor = 1 const item_event_handlers = {} -TouchBar._event = (id, ...args) => { - const id_parts = id.split('.') +TouchBar._event = (eventArgs) => { + const args = eventArgs.slice(1) + const id_parts = eventArgs[0].split('.') const item_id = id_parts[id_parts.length - 1] if (item_event_handlers[item_id]) item_event_handlers[item_id](...args) } @@ -39,7 +40,6 @@ class TouchBarItem { get: () => mConfig }) this.config.id = `${this.config.id || this.id}`; - this.config.type = 'button'; if (typeof this.config !== 'object' || this.config === null) { throw new Error('Provided config must be a non-null object') } @@ -53,7 +53,7 @@ class TouchBarItem { TouchBar.Button = class TouchBarButton extends TouchBarItem { constructor (config) { super(config) - this.config.type = 'button'; + this.config.type = 'button' const click = config.click if (typeof click === 'function') { item_event_handlers[`${this.id}`] = click @@ -64,22 +64,27 @@ TouchBar.Button = class TouchBarButton extends TouchBarItem { TouchBar.ColorPicker = class TouchBarColorPicker extends TouchBarItem { constructor (config) { super(config) - this.config.type = 'colorpicker'; - const change = config.change + this.config.type = 'colorpicker' + const change = this.config.change if (typeof change === 'function') { item_event_handlers[`${this.id}`] = change } } } -TouchBar.Label = class TouchBarLabel extends TouchBarItem {} +TouchBar.Label = class TouchBarLabel extends TouchBarItem { + constructor (config) { + super(config) + this.config.type = 'label' + } +} TouchBar.List = class TouchBarList extends TouchBarItem {} TouchBar.Slider = class TouchBarSlider extends TouchBarItem { constructor (config) { super(config) - const change = config.change + const change = this.config.change if (typeof change === 'function') { item_event_handlers[this.id] = change }