initial work on updating touch bar item config without rerender
authorSamuel Attard <samuel.r.attard@gmail.com>
Fri, 16 Dec 2016 06:24:51 +0000 (17:24 +1100)
committerKevin Sawicki <kevinsawicki@gmail.com>
Fri, 3 Mar 2017 22:00:38 +0000 (14:00 -0800)
atom/browser/api/atom_api_window.cc
atom/browser/api/atom_api_window.h
atom/browser/native_window.cc
atom/browser/native_window.h
atom/browser/native_window_mac.h
atom/browser/native_window_mac.mm
default_app/default_app.js
default_app/icon.png
lib/browser/api/browser-window.js
lib/browser/api/touch-bar.js

index e031916..adafab9 100644 (file)
@@ -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)
index 7241982..2620292 100644 (file)
@@ -207,6 +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);
 
   v8::Local<v8::Value> WebContents(v8::Isolate* isolate);
 
index 1aead89..ab7e108 100644 (file)
@@ -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();
 }
index b76a49a..23c393e 100644 (file)
@@ -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();
index b960290..f8ebfec 100644 (file)
@@ -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<mate::PersistentDictionary> GetTouchBarItems();
 
   // content::RenderWidgetHost::InputEventObserver:
index dfd7155..0931722 100644 (file)
@@ -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<NSTouchBarItemIdentifier>*)items;
 @end
@@ -363,6 +364,7 @@ bool ScopedDisableResize::disable_resize_ = false;
 @implementation AtomNSWindow
   NSMutableArray<NSTouchBarItemIdentifier>* bar_items_ = [[NSMutableArray alloc] init];
   std::map<std::string, mate::PersistentDictionary> item_id_map;
+  std::map<std::string, NSTouchBarItem*> 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<std::string, mate::PersistentDictionary> 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<mate::PersistentDictionary> NativeWindowMac::GetTouchBarItems() {
   return touch_bar_items_;
 }
index 57110e9..42644ac 100644 (file)
@@ -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,
     ]))
   })
 }
index ac3a654..1e8be4b 100644 (file)
Binary files a/default_app/icon.png and b/default_app/icon.png differ
index ac6e3b4..9f57469 100644 (file)
@@ -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
index 1b18656..a008cc7 100644 (file)
@@ -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
   }