diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index e031916a0f25..adafab9173b1 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -853,6 +853,10 @@ void Window::SetTouchBar(mate::Arguments* args) { window_->SetTouchBar(args); } +void Window::RefreshTouchBarItem(mate::Arguments* args) { + window_->RefreshTouchBarItem(args); +} + int32_t Window::ID() const { return weak_map_id(); } @@ -975,6 +979,7 @@ void Window::BuildPrototype(v8::Isolate* isolate, .SetMethod("setVibrancy", &Window::SetVibrancy) .SetMethod("_destroyTouchBar", &Window::DestroyTouchBar) .SetMethod("_setTouchBar", &Window::SetTouchBar) + .SetMethod("_refreshTouchBarItem", &Window::RefreshTouchBarItem) #if defined(OS_WIN) .SetMethod("hookWindowMessage", &Window::HookWindowMessage) .SetMethod("isWindowMessageHooked", &Window::IsWindowMessageHooked) diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index 7241982f2b7d..2620292b253e 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -207,6 +207,7 @@ class Window : public mate::TrackableObject, void SetVibrancy(mate::Arguments* args); void DestroyTouchBar(); void SetTouchBar(mate::Arguments* args); + void RefreshTouchBarItem(mate::Arguments* args); v8::Local WebContents(v8::Isolate* isolate); diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index 1aead897d990..ab7e1084924c 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -347,6 +347,9 @@ void NativeWindow::DestroyTouchBar() { void NativeWindow::SetTouchBar(mate::Arguments* args) { } +void NativeWindow::RefreshTouchBarItem(mate::Arguments* args) { +} + void NativeWindow::FocusOnWebView() { web_contents()->GetRenderViewHost()->GetWidget()->Focus(); } diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index b76a49a061df..23c393e7aee2 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -173,6 +173,7 @@ class NativeWindow : public base::SupportsUserData, // Touchbar API virtual void DestroyTouchBar(); virtual void SetTouchBar(mate::Arguments* args); + virtual void RefreshTouchBarItem(mate::Arguments* args); // Webview APIs. virtual void FocusOnWebView(); diff --git a/atom/browser/native_window_mac.h b/atom/browser/native_window_mac.h index b960290c82d3..f8ebfec3adc8 100644 --- a/atom/browser/native_window_mac.h +++ b/atom/browser/native_window_mac.h @@ -104,6 +104,7 @@ class NativeWindowMac : public NativeWindow, void SetVibrancy(const std::string& type) override; void DestroyTouchBar() override; void SetTouchBar(mate::Arguments* args) override; + void RefreshTouchBarItem(mate::Arguments* args) override; std::vector GetTouchBarItems(); // content::RenderWidgetHost::InputEventObserver: diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index dfd71555ad9b..09317221c8ac 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -353,6 +353,7 @@ bool ScopedDisableResize::disable_resize_ = false; - (void)setEnableLargerThanScreen:(bool)enable; - (void)enableWindowButtonsOffset; - (void)reloadTouchBar; +- (void)refreshTouchBarItem:(mate::Arguments*)args; - (void)resetTouchBar; - (NSTouchBar*)touchBarFromMutatableArray:(NSMutableArray*)items; @end @@ -363,6 +364,7 @@ bool ScopedDisableResize::disable_resize_ = false; @implementation AtomNSWindow NSMutableArray* bar_items_ = [[NSMutableArray alloc] init]; std::map item_id_map; + std::map item_map; - (void)setShell:(atom::NativeWindowMac*)shell { shell_ = shell; @@ -405,6 +407,20 @@ bool ScopedDisableResize::disable_resize_ = false; return idents; } +- (void)refreshTouchBarItem:(mate::Arguments*)args { + std::string item_id; + std::string type; + 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]; + } + } + } +} + - (void)reloadTouchBar { std::map new_map; item_id_map = new_map; @@ -505,12 +521,16 @@ bool ScopedDisableResize::disable_resize_ = false; std::string s_id = std::string([id UTF8String]); 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]; +} + +- (nullable NSTouchBarItem *)updateButton:(NSCustomTouchBarItem*)customItem withOpts:(mate::PersistentDictionary)item withID:(NSString*)id { std::string label; if (item.Get("label", &label)) { NSButton* theButton = [self makeButtonForDict:item withLabel:label]; theButton.tag = [id floatValue]; - NSCustomTouchBarItem *customItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier]; customItem.view = theButton; std::string customizationLabel; @@ -527,11 +547,15 @@ bool ScopedDisableResize::disable_resize_ = false; std::string s_id = std::string([id UTF8String]); if (![self hasTBDict:s_id]) return nil; mate::PersistentDictionary item = item_id_map[s_id]; + NSCustomTouchBarItem *customItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier]; + return [self updateLabel:customItem withOpts:item]; +} + +- (nullable NSTouchBarItem*) updateLabel:(NSCustomTouchBarItem*)customItem withOpts:(mate::PersistentDictionary)item { std::string label; if (item.Get("label", &label)) { NSTextField *theLabel = [NSTextField labelWithString:[NSString stringWithUTF8String:label.c_str()]]; - NSCustomTouchBarItem *customItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier]; customItem.view = theLabel; std::string customizationLabel; @@ -548,8 +572,11 @@ bool ScopedDisableResize::disable_resize_ = false; std::string s_id = std::string([id UTF8String]); if (![self hasTBDict:s_id]) return nil; mate::PersistentDictionary item = item_id_map[s_id]; - NSColorPickerTouchBarItem *colorPickerItem = [[NSColorPickerTouchBarItem alloc] initWithIdentifier:identifier]; + return [self updateColorPicker:colorPickerItem withOpts:item]; +} + +- (nullable NSTouchBarItem*) updateColorPicker:(NSColorPickerTouchBarItem*)colorPickerItem withOpts:(mate::PersistentDictionary)item { colorPickerItem.target = self; colorPickerItem.action = @selector(colorPickerAction:); @@ -565,8 +592,11 @@ bool ScopedDisableResize::disable_resize_ = false; std::string s_id = std::string([id UTF8String]); if (![self hasTBDict:s_id]) return nil; mate::PersistentDictionary item = item_id_map[s_id]; - NSSliderTouchBarItem *sliderItem = [[NSSliderTouchBarItem alloc] initWithIdentifier:identifier]; + return [self updateSlider:sliderItem withOpts:item]; +} + +- (nullable NSTouchBarItem*) updateSlider:(NSSliderTouchBarItem*)sliderItem withOpts:(mate::PersistentDictionary)item { sliderItem.target = self; sliderItem.action = @selector(sliderAction:); @@ -598,9 +628,11 @@ bool ScopedDisableResize::disable_resize_ = false; std::string s_id = std::string([id UTF8String]); if (![self hasTBDict:s_id]) return nil; mate::PersistentDictionary item = item_id_map[s_id]; - NSPopoverTouchBarItem *popOverItem = [[NSPopoverTouchBarItem alloc] initWithIdentifier:identifier]; + return [self updatePopOver:popOverItem withOpts:item]; +} +- (nullable NSTouchBarItem*) updatePopOver:(NSPopoverTouchBarItem*)popOverItem withOpts:(mate::PersistentDictionary)item { std::string customizationLabel; if (item.Get("customizationLabel", &customizationLabel)) { popOverItem.customizationLabel = [NSString stringWithUTF8String:customizationLabel.c_str()]; @@ -665,27 +697,31 @@ static NSTouchBarItemIdentifier PopOverIdentifier = @"com.electron.tb.popover."; static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.tb.slider."; - (nullable NSTouchBarItem *)makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { + NSTouchBarItem * item = nil; + NSString * id = nil; if ([identifier hasPrefix:ButtonIdentifier]) { - NSString* id = [self idFromIdentifier:identifier withPrefix:ButtonIdentifier]; - return [self makeButtonForID:id withIdentifier:identifier]; + id = [self idFromIdentifier:identifier withPrefix:ButtonIdentifier]; + item = [self makeButtonForID:id withIdentifier:identifier]; } else if ([identifier hasPrefix:LabelIdentifier]) { - NSString* id = [self idFromIdentifier:identifier withPrefix:LabelIdentifier]; - return [self makeLabelForID:id withIdentifier:identifier]; + id = [self idFromIdentifier:identifier withPrefix:LabelIdentifier]; + item = [self makeLabelForID:id withIdentifier:identifier]; } else if ([identifier hasPrefix:ColorPickerIdentifier]) { - NSString* id = [self idFromIdentifier:identifier withPrefix:ColorPickerIdentifier]; - return [self makeColorPickerForID:id withIdentifier:identifier]; + id = [self idFromIdentifier:identifier withPrefix:ColorPickerIdentifier]; + item = [self makeColorPickerForID:id withIdentifier:identifier]; } else if ([identifier hasPrefix:SliderIdentifier]) { - NSString* id = [self idFromIdentifier:identifier withPrefix:SliderIdentifier]; - return [self makeSliderForID:id withIdentifier:identifier]; + id = [self idFromIdentifier:identifier withPrefix:SliderIdentifier]; + item = [self makeSliderForID:id withIdentifier:identifier]; } else if ([identifier hasPrefix:PopOverIdentifier]) { - NSString* id = [self idFromIdentifier:identifier withPrefix:PopOverIdentifier]; - return [self makePopOverForID:id withIdentifier:identifier]; + id = [self idFromIdentifier:identifier withPrefix:PopOverIdentifier]; + item = [self makePopOverForID:id withIdentifier:identifier]; } else if ([identifier hasPrefix:GroupIdentifier]) { - NSString* id = [self idFromIdentifier:identifier withPrefix:GroupIdentifier]; - return [self makeGroupForID:id withIdentifier:identifier]; + id = [self idFromIdentifier:identifier withPrefix:GroupIdentifier]; + item = [self makeGroupForID:id withIdentifier:identifier]; } - return nil; + item_map.insert(make_pair(std::string([id UTF8String]), item)); + + return item; } - (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { @@ -1688,6 +1724,10 @@ void NativeWindowMac::SetTouchBar(mate::Arguments* args) { } } +void NativeWindowMac::RefreshTouchBarItem(mate::Arguments* args) { + [window_ refreshTouchBarItem:args]; +} + std::vector NativeWindowMac::GetTouchBarItems() { return touch_bar_items_; } diff --git a/default_app/default_app.js b/default_app/default_app.js index 57110e9c90d8..42644ac36c40 100644 --- a/default_app/default_app.js +++ b/default_app/default_app.js @@ -25,6 +25,18 @@ exports.load = (appUrl) => { mainWindow.loadURL(appUrl) mainWindow.focus() + const slider = new (TouchBar.Slider)({ + label: 'Slider 123', + minValue: 50, + maxValue: 1000, + initialValue: 300, + change: (newVal) => { + console.log('Slider was changed', newVal, typeof newVal) + } + }); + + global.slider = slider; + mainWindow.setTouchBar(new TouchBar([ new (TouchBar.Button)({ label: 'Hello World!', @@ -60,15 +72,7 @@ exports.load = (appUrl) => { }) ]) }), - new (TouchBar.Slider)({ - label: 'Slider 123', - minValue: 50, - maxValue: 1000, - initialValue: 300, - change: (newVal) => { - console.log('Slider was changed', newVal, typeof newVal) - } - }) + slider, ])) }) } diff --git a/default_app/icon.png b/default_app/icon.png index ac3a6547d9ec..1e8be4bab538 100644 Binary files a/default_app/icon.png and b/default_app/icon.png differ diff --git a/lib/browser/api/browser-window.js b/lib/browser/api/browser-window.js index ac6e3b4bb6ed..9f5746980f67 100644 --- a/lib/browser/api/browser-window.js +++ b/lib/browser/api/browser-window.js @@ -212,6 +212,12 @@ BrowserWindow.prototype.setTouchBar = function (touchBar) { } else { this._setTouchBar(touchBar.toJSON()) } + touchBar._owner = this + touchBar.items.forEach((item) => { item._owner = this; }) +} + +BrowserWindow.prototype._updateTouchBarItem = function (itemID) { + this._refreshTouchBarItem(itemID); } module.exports = BrowserWindow diff --git a/lib/browser/api/touch-bar.js b/lib/browser/api/touch-bar.js index 1b18656a6973..a008cc7778df 100644 --- a/lib/browser/api/touch-bar.js +++ b/lib/browser/api/touch-bar.js @@ -44,6 +44,16 @@ class TouchBarItem { } } + updateConfig(newConfig) { + if (!this._owner) { + throw new Error('Cannot call methods on TouchBarItems without assigning to a BrowserWindow'); + } + const dupConfig = Object.assign({}, newConfig); + delete dupConfig.id; + Object.assign(this.config, dupConfig); + this._owner._updateTouchBarItem(this.toJSON()); + } + toJSON () { return this.config }