From 98f5858b1139783f887b68b2e2d2935a2721423f Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 28 Feb 2017 16:08:12 -0800 Subject: [PATCH] Initial support for dynamic properties --- atom/browser/api/atom_api_window.cc | 4 ++-- atom/browser/api/atom_api_window.h | 2 +- atom/browser/native_window.cc | 2 +- atom/browser/native_window.h | 2 +- atom/browser/native_window_mac.h | 2 +- atom/browser/native_window_mac.mm | 11 ++++++----- atom/browser/ui/cocoa/atom_touch_bar.h | 2 +- atom/browser/ui/cocoa/atom_touch_bar.mm | 25 +++++++++++-------------- lib/browser/api/browser-window.js | 12 +++++++----- lib/browser/api/touch-bar.js | 17 +++++++++++++++-- 10 files changed, 46 insertions(+), 33 deletions(-) diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index 6c14d8b3c..3f9b82af5 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -853,8 +853,8 @@ void Window::SetTouchBar(mate::Arguments* args) { window_->SetTouchBar(args); } -void Window::RefreshTouchBarItem(mate::Arguments* args) { - window_->RefreshTouchBarItem(args); +void Window::RefreshTouchBarItem(const std::string& item_id) { + window_->RefreshTouchBarItem(item_id); } int32_t Window::ID() const { diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index bf3e8cb5e..697dfa3e1 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -207,7 +207,7 @@ class Window : public mate::TrackableObject, void SetVibrancy(mate::Arguments* args); void DestroyTouchBar(); void SetTouchBar(mate::Arguments* args); - void RefreshTouchBarItem(mate::Arguments* args); + void RefreshTouchBarItem(const std::string& item_id); v8::Local WebContents(v8::Isolate* isolate); diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index 322ca8a0e..fb34ee021 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -347,7 +347,7 @@ void NativeWindow::DestroyTouchBar() { void NativeWindow::SetTouchBar(mate::Arguments* args) { } -void NativeWindow::RefreshTouchBarItem(mate::Arguments* args) { +void NativeWindow::RefreshTouchBarItem(const std::string& item_id) { } void NativeWindow::FocusOnWebView() { diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index 77188d849..8e8b61302 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -173,7 +173,7 @@ class NativeWindow : public base::SupportsUserData, // Touchbar API virtual void DestroyTouchBar(); virtual void SetTouchBar(mate::Arguments* args); - virtual void RefreshTouchBarItem(mate::Arguments* args); + virtual void RefreshTouchBarItem(const std::string& item_id); // Webview APIs. virtual void FocusOnWebView(); diff --git a/atom/browser/native_window_mac.h b/atom/browser/native_window_mac.h index f8ebfec3a..d80ff4289 100644 --- a/atom/browser/native_window_mac.h +++ b/atom/browser/native_window_mac.h @@ -104,7 +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; + void RefreshTouchBarItem(const std::string& item_id) override; std::vector GetTouchBarItems(); // content::RenderWidgetHost::InputEventObserver: diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 1a20ea74d..0dc3bd952 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -354,7 +354,8 @@ bool ScopedDisableResize::disable_resize_ = false; - (void)setEnableLargerThanScreen:(bool)enable; - (void)enableWindowButtonsOffset; - (void)resetTouchBar; -- (void)refreshTouchBarItem:(mate::Arguments*)args; +- (void)refreshTouchBarItem:(const std::string&)item_id; + @end @implementation AtomNSWindow @@ -371,8 +372,8 @@ bool ScopedDisableResize::disable_resize_ = false; self.touchBar = nil; } -- (void)refreshTouchBarItem:(mate::Arguments*)args { - [atom_touch_bar_ refreshTouchBarItem:args]; +- (void)refreshTouchBarItem:(const std::string&)item_id { + [atom_touch_bar_ refreshTouchBarItem:item_id]; } - (NSTouchBar*)makeTouchBar { @@ -1381,8 +1382,8 @@ void NativeWindowMac::SetTouchBar(mate::Arguments* args) { } } -void NativeWindowMac::RefreshTouchBarItem(mate::Arguments* args) { - [window_ refreshTouchBarItem:args]; +void NativeWindowMac::RefreshTouchBarItem(const std::string& item_id) { + [window_ refreshTouchBarItem:item_id]; } std::vector NativeWindowMac::GetTouchBarItems() { diff --git a/atom/browser/ui/cocoa/atom_touch_bar.h b/atom/browser/ui/cocoa/atom_touch_bar.h index eb23a6a48..2efabe6cf 100644 --- a/atom/browser/ui/cocoa/atom_touch_bar.h +++ b/atom/browser/ui/cocoa/atom_touch_bar.h @@ -30,7 +30,7 @@ - (NSTouchBar*)makeTouchBarFromItemOptions:(const std::vector&)item_options; - (NSTouchBar*)touchBarFromItemIdentifiers:(NSMutableArray*)items; - (NSMutableArray*)identifierArrayFromDicts:(const std::vector&)dicts; -- (void)refreshTouchBarItem:(mate::Arguments*)args; +- (void)refreshTouchBarItem:(const std::string&)item_id; - (void)clear; - (NSString*)idFromIdentifier:(NSString*)identifier withPrefix:(NSString*)prefix; diff --git a/atom/browser/ui/cocoa/atom_touch_bar.mm b/atom/browser/ui/cocoa/atom_touch_bar.mm index 194be6f5a..b2053f7df 100644 --- a/atom/browser/ui/cocoa/atom_touch_bar.mm +++ b/atom/browser/ui/cocoa/atom_touch_bar.mm @@ -101,32 +101,29 @@ static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.touchbar.slide } -- (void)refreshTouchBarItem:(mate::Arguments*)args { - std::string item_id; - std::string type; - mate::PersistentDictionary options; - if (!args->GetNext(&options)) return; - if (!options.Get("type", &type)) return; - if (!options.Get("id", &item_id)) return; +- (void)refreshTouchBarItem:(const std::string&)item_id { if (item_map.find(item_id) == item_map.end()) return; + if (![self hasItemWithID:item_id]) return; - if (type == "button") { + mate::PersistentDictionary options = item_id_map[item_id]; + std::string item_type; + options.Get("type", &item_type); + + if (item_type == "button") { [self updateButton:(NSCustomTouchBarItem*)item_map[item_id] withOptions:options]; - } else if (type == "label") { + } else if (item_type == "label") { [self updateLabel:(NSCustomTouchBarItem*)item_map[item_id] withOptions:options]; - } else if (type == "colorpicker") { + } else if (item_type == "colorpicker") { [self updateColorPicker:(NSColorPickerTouchBarItem*)item_map[item_id] withOptions:options]; - } else if (type == "slider") { + } else if (item_type == "slider") { [self updateSlider:(NSSliderTouchBarItem*)item_map[item_id] withOptions:options]; - } else if (type == "popover") { + } else if (item_type == "popover") { [self updatePopover:(NSPopoverTouchBarItem*)item_map[item_id] withOptions:options]; - } else if (type == "group") { - args->ThrowError("You can not update the config of a group. Update the individual items or replace the group"); } } diff --git a/lib/browser/api/browser-window.js b/lib/browser/api/browser-window.js index a8a81cbd5..27f425c7a 100644 --- a/lib/browser/api/browser-window.js +++ b/lib/browser/api/browser-window.js @@ -138,6 +138,8 @@ BrowserWindow.prototype._init = function () { this._touchBar.emit('interaction', id, details) } }) + + this._touchBarListener = this._refreshTouchBarItem.bind(this) } BrowserWindow.getFocusedWindow = () => { @@ -208,8 +210,11 @@ Object.assign(BrowserWindow.prototype, { // TouchBar API BrowserWindow.prototype.setTouchBar = function (touchBar) { if (touchBar == null) { + if (this._touchBar != null) { + this._touchBar.removeListener('change', this._touchBarListener) + this._touchBar = null + } this._destroyTouchBar() - this._touchBar = null return } @@ -218,11 +223,8 @@ BrowserWindow.prototype.setTouchBar = function (touchBar) { } this._touchBar = touchBar + this._touchBar.on('change', this._touchBarListener) this._setTouchBar(touchBar.ordereredItems) } -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 fe75e3ade..237ef716f 100644 --- a/lib/browser/api/touch-bar.js +++ b/lib/browser/api/touch-bar.js @@ -14,6 +14,9 @@ class TouchBar extends EventEmitter { this.ordereredItems = [] const registerItem = (item) => { this.items[item.id] = item + item.on('change', () => { + this.emit('change', item.id, item.type) + }) if (item.child instanceof TouchBar) { item.child.ordereredItems.forEach(registerItem) } @@ -35,8 +38,9 @@ class TouchBar extends EventEmitter { } } -class TouchBarItem { +class TouchBarItem extends EventEmitter { constructor (config) { + super() this.id = `${itemIdIncrementor++}` } } @@ -81,7 +85,16 @@ TouchBar.Label = class TouchBarLabel extends TouchBarItem { constructor (config) { super(config) this.type = 'label' - this.label = config.label + this._label = config.label + } + + set label (newLabel) { + this._label = newLabel + this.emit('change') + } + + get label () { + return this._label } } -- 2.34.1