Browse Source

Initial support for dynamic properties

Kevin Sawicki 8 years ago
parent
commit
98f5858b11

+ 2 - 2
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 {

+ 1 - 1
atom/browser/api/atom_api_window.h

@@ -207,7 +207,7 @@ class Window : public mate::TrackableObject<Window>,
   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<v8::Value> WebContents(v8::Isolate* isolate);
 

+ 1 - 1
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() {

+ 1 - 1
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();

+ 1 - 1
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<mate::PersistentDictionary> GetTouchBarItems();
 
   // content::RenderWidgetHost::InputEventObserver:

+ 6 - 5
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<mate::PersistentDictionary> NativeWindowMac::GetTouchBarItems() {

+ 1 - 1
atom/browser/ui/cocoa/atom_touch_bar.h

@@ -30,7 +30,7 @@
 - (NSTouchBar*)makeTouchBarFromItemOptions:(const std::vector<mate::PersistentDictionary>&)item_options;
 - (NSTouchBar*)touchBarFromItemIdentifiers:(NSMutableArray*)items;
 - (NSMutableArray*)identifierArrayFromDicts:(const std::vector<mate::PersistentDictionary>&)dicts;
-- (void)refreshTouchBarItem:(mate::Arguments*)args;
+- (void)refreshTouchBarItem:(const std::string&)item_id;
 - (void)clear;
 
 - (NSString*)idFromIdentifier:(NSString*)identifier withPrefix:(NSString*)prefix;

+ 11 - 14
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");
   }
 }
 

+ 7 - 5
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

+ 15 - 2
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
   }
 }