initial work on updating touch bar item config without rerender

This commit is contained in:
Samuel Attard 2016-12-16 17:24:51 +11:00 committed by Kevin Sawicki
parent 61949657f0
commit dd09c91cf2
10 changed files with 98 additions and 27 deletions

View file

@ -853,6 +853,10 @@ void Window::SetTouchBar(mate::Arguments* args) {
window_->SetTouchBar(args); window_->SetTouchBar(args);
} }
void Window::RefreshTouchBarItem(mate::Arguments* args) {
window_->RefreshTouchBarItem(args);
}
int32_t Window::ID() const { int32_t Window::ID() const {
return weak_map_id(); return weak_map_id();
} }
@ -975,6 +979,7 @@ void Window::BuildPrototype(v8::Isolate* isolate,
.SetMethod("setVibrancy", &Window::SetVibrancy) .SetMethod("setVibrancy", &Window::SetVibrancy)
.SetMethod("_destroyTouchBar", &Window::DestroyTouchBar) .SetMethod("_destroyTouchBar", &Window::DestroyTouchBar)
.SetMethod("_setTouchBar", &Window::SetTouchBar) .SetMethod("_setTouchBar", &Window::SetTouchBar)
.SetMethod("_refreshTouchBarItem", &Window::RefreshTouchBarItem)
#if defined(OS_WIN) #if defined(OS_WIN)
.SetMethod("hookWindowMessage", &Window::HookWindowMessage) .SetMethod("hookWindowMessage", &Window::HookWindowMessage)
.SetMethod("isWindowMessageHooked", &Window::IsWindowMessageHooked) .SetMethod("isWindowMessageHooked", &Window::IsWindowMessageHooked)

View file

@ -207,6 +207,7 @@ class Window : public mate::TrackableObject<Window>,
void SetVibrancy(mate::Arguments* args); void SetVibrancy(mate::Arguments* args);
void DestroyTouchBar(); void DestroyTouchBar();
void SetTouchBar(mate::Arguments* args); void SetTouchBar(mate::Arguments* args);
void RefreshTouchBarItem(mate::Arguments* args);
v8::Local<v8::Value> WebContents(v8::Isolate* isolate); v8::Local<v8::Value> WebContents(v8::Isolate* isolate);

View file

@ -347,6 +347,9 @@ void NativeWindow::DestroyTouchBar() {
void NativeWindow::SetTouchBar(mate::Arguments* args) { void NativeWindow::SetTouchBar(mate::Arguments* args) {
} }
void NativeWindow::RefreshTouchBarItem(mate::Arguments* args) {
}
void NativeWindow::FocusOnWebView() { void NativeWindow::FocusOnWebView() {
web_contents()->GetRenderViewHost()->GetWidget()->Focus(); web_contents()->GetRenderViewHost()->GetWidget()->Focus();
} }

View file

@ -173,6 +173,7 @@ class NativeWindow : public base::SupportsUserData,
// Touchbar API // Touchbar API
virtual void DestroyTouchBar(); virtual void DestroyTouchBar();
virtual void SetTouchBar(mate::Arguments* args); virtual void SetTouchBar(mate::Arguments* args);
virtual void RefreshTouchBarItem(mate::Arguments* args);
// Webview APIs. // Webview APIs.
virtual void FocusOnWebView(); virtual void FocusOnWebView();

View file

@ -104,6 +104,7 @@ class NativeWindowMac : public NativeWindow,
void SetVibrancy(const std::string& type) override; void SetVibrancy(const std::string& type) override;
void DestroyTouchBar() override; void DestroyTouchBar() override;
void SetTouchBar(mate::Arguments* args) override; void SetTouchBar(mate::Arguments* args) override;
void RefreshTouchBarItem(mate::Arguments* args) override;
std::vector<mate::PersistentDictionary> GetTouchBarItems(); std::vector<mate::PersistentDictionary> GetTouchBarItems();
// content::RenderWidgetHost::InputEventObserver: // content::RenderWidgetHost::InputEventObserver:

View file

@ -353,6 +353,7 @@ bool ScopedDisableResize::disable_resize_ = false;
- (void)setEnableLargerThanScreen:(bool)enable; - (void)setEnableLargerThanScreen:(bool)enable;
- (void)enableWindowButtonsOffset; - (void)enableWindowButtonsOffset;
- (void)reloadTouchBar; - (void)reloadTouchBar;
- (void)refreshTouchBarItem:(mate::Arguments*)args;
- (void)resetTouchBar; - (void)resetTouchBar;
- (NSTouchBar*)touchBarFromMutatableArray:(NSMutableArray<NSTouchBarItemIdentifier>*)items; - (NSTouchBar*)touchBarFromMutatableArray:(NSMutableArray<NSTouchBarItemIdentifier>*)items;
@end @end
@ -363,6 +364,7 @@ bool ScopedDisableResize::disable_resize_ = false;
@implementation AtomNSWindow @implementation AtomNSWindow
NSMutableArray<NSTouchBarItemIdentifier>* bar_items_ = [[NSMutableArray alloc] init]; NSMutableArray<NSTouchBarItemIdentifier>* bar_items_ = [[NSMutableArray alloc] init];
std::map<std::string, mate::PersistentDictionary> item_id_map; std::map<std::string, mate::PersistentDictionary> item_id_map;
std::map<std::string, NSTouchBarItem*> item_map;
- (void)setShell:(atom::NativeWindowMac*)shell { - (void)setShell:(atom::NativeWindowMac*)shell {
shell_ = shell; shell_ = shell;
@ -405,6 +407,20 @@ bool ScopedDisableResize::disable_resize_ = false;
return idents; 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 { - (void)reloadTouchBar {
std::map<std::string, mate::PersistentDictionary> new_map; std::map<std::string, mate::PersistentDictionary> new_map;
item_id_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]); std::string s_id = std::string([id UTF8String]);
if (![self hasTBDict:s_id]) return nil; if (![self hasTBDict:s_id]) return nil;
mate::PersistentDictionary item = item_id_map[s_id]; 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; std::string label;
if (item.Get("label", &label)) { if (item.Get("label", &label)) {
NSButton* theButton = [self makeButtonForDict:item withLabel:label]; NSButton* theButton = [self makeButtonForDict:item withLabel:label];
theButton.tag = [id floatValue]; theButton.tag = [id floatValue];
NSCustomTouchBarItem *customItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];
customItem.view = theButton; customItem.view = theButton;
std::string customizationLabel; std::string customizationLabel;
@ -527,11 +547,15 @@ bool ScopedDisableResize::disable_resize_ = false;
std::string s_id = std::string([id UTF8String]); std::string s_id = std::string([id UTF8String]);
if (![self hasTBDict:s_id]) return nil; if (![self hasTBDict:s_id]) return nil;
mate::PersistentDictionary item = item_id_map[s_id]; 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; std::string label;
if (item.Get("label", &label)) { if (item.Get("label", &label)) {
NSTextField *theLabel = [NSTextField labelWithString:[NSString stringWithUTF8String:label.c_str()]]; NSTextField *theLabel = [NSTextField labelWithString:[NSString stringWithUTF8String:label.c_str()]];
NSCustomTouchBarItem *customItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];
customItem.view = theLabel; customItem.view = theLabel;
std::string customizationLabel; std::string customizationLabel;
@ -548,8 +572,11 @@ bool ScopedDisableResize::disable_resize_ = false;
std::string s_id = std::string([id UTF8String]); std::string s_id = std::string([id UTF8String]);
if (![self hasTBDict:s_id]) return nil; if (![self hasTBDict:s_id]) return nil;
mate::PersistentDictionary item = item_id_map[s_id]; mate::PersistentDictionary item = item_id_map[s_id];
NSColorPickerTouchBarItem *colorPickerItem = [[NSColorPickerTouchBarItem alloc] initWithIdentifier:identifier]; 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.target = self;
colorPickerItem.action = @selector(colorPickerAction:); colorPickerItem.action = @selector(colorPickerAction:);
@ -565,8 +592,11 @@ bool ScopedDisableResize::disable_resize_ = false;
std::string s_id = std::string([id UTF8String]); std::string s_id = std::string([id UTF8String]);
if (![self hasTBDict:s_id]) return nil; if (![self hasTBDict:s_id]) return nil;
mate::PersistentDictionary item = item_id_map[s_id]; mate::PersistentDictionary item = item_id_map[s_id];
NSSliderTouchBarItem *sliderItem = [[NSSliderTouchBarItem alloc] initWithIdentifier:identifier]; 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.target = self;
sliderItem.action = @selector(sliderAction:); sliderItem.action = @selector(sliderAction:);
@ -598,9 +628,11 @@ bool ScopedDisableResize::disable_resize_ = false;
std::string s_id = std::string([id UTF8String]); std::string s_id = std::string([id UTF8String]);
if (![self hasTBDict:s_id]) return nil; if (![self hasTBDict:s_id]) return nil;
mate::PersistentDictionary item = item_id_map[s_id]; mate::PersistentDictionary item = item_id_map[s_id];
NSPopoverTouchBarItem *popOverItem = [[NSPopoverTouchBarItem alloc] initWithIdentifier:identifier]; NSPopoverTouchBarItem *popOverItem = [[NSPopoverTouchBarItem alloc] initWithIdentifier:identifier];
return [self updatePopOver:popOverItem withOpts:item];
}
- (nullable NSTouchBarItem*) updatePopOver:(NSPopoverTouchBarItem*)popOverItem withOpts:(mate::PersistentDictionary)item {
std::string customizationLabel; std::string customizationLabel;
if (item.Get("customizationLabel", &customizationLabel)) { if (item.Get("customizationLabel", &customizationLabel)) {
popOverItem.customizationLabel = [NSString stringWithUTF8String:customizationLabel.c_str()]; 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."; static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.tb.slider.";
- (nullable NSTouchBarItem *)makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { - (nullable NSTouchBarItem *)makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier {
NSTouchBarItem * item = nil;
NSString * id = nil;
if ([identifier hasPrefix:ButtonIdentifier]) { if ([identifier hasPrefix:ButtonIdentifier]) {
NSString* id = [self idFromIdentifier:identifier withPrefix:ButtonIdentifier]; id = [self idFromIdentifier:identifier withPrefix:ButtonIdentifier];
return [self makeButtonForID:id withIdentifier:identifier]; item = [self makeButtonForID:id withIdentifier:identifier];
} else if ([identifier hasPrefix:LabelIdentifier]) { } else if ([identifier hasPrefix:LabelIdentifier]) {
NSString* id = [self idFromIdentifier:identifier withPrefix:LabelIdentifier]; id = [self idFromIdentifier:identifier withPrefix:LabelIdentifier];
return [self makeLabelForID:id withIdentifier:identifier]; item = [self makeLabelForID:id withIdentifier:identifier];
} else if ([identifier hasPrefix:ColorPickerIdentifier]) { } else if ([identifier hasPrefix:ColorPickerIdentifier]) {
NSString* id = [self idFromIdentifier:identifier withPrefix:ColorPickerIdentifier]; id = [self idFromIdentifier:identifier withPrefix:ColorPickerIdentifier];
return [self makeColorPickerForID:id withIdentifier:identifier]; item = [self makeColorPickerForID:id withIdentifier:identifier];
} else if ([identifier hasPrefix:SliderIdentifier]) { } else if ([identifier hasPrefix:SliderIdentifier]) {
NSString* id = [self idFromIdentifier:identifier withPrefix:SliderIdentifier]; id = [self idFromIdentifier:identifier withPrefix:SliderIdentifier];
return [self makeSliderForID:id withIdentifier:identifier]; item = [self makeSliderForID:id withIdentifier:identifier];
} else if ([identifier hasPrefix:PopOverIdentifier]) { } else if ([identifier hasPrefix:PopOverIdentifier]) {
NSString* id = [self idFromIdentifier:identifier withPrefix:PopOverIdentifier]; id = [self idFromIdentifier:identifier withPrefix:PopOverIdentifier];
return [self makePopOverForID:id withIdentifier:identifier]; item = [self makePopOverForID:id withIdentifier:identifier];
} else if ([identifier hasPrefix:GroupIdentifier]) { } else if ([identifier hasPrefix:GroupIdentifier]) {
NSString* id = [self idFromIdentifier:identifier withPrefix:GroupIdentifier]; id = [self idFromIdentifier:identifier withPrefix:GroupIdentifier];
return [self makeGroupForID:id withIdentifier:identifier]; 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 { - (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() { std::vector<mate::PersistentDictionary> NativeWindowMac::GetTouchBarItems() {
return touch_bar_items_; return touch_bar_items_;
} }

View file

@ -25,6 +25,18 @@ exports.load = (appUrl) => {
mainWindow.loadURL(appUrl) mainWindow.loadURL(appUrl)
mainWindow.focus() 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([ mainWindow.setTouchBar(new TouchBar([
new (TouchBar.Button)({ new (TouchBar.Button)({
label: 'Hello World!', label: 'Hello World!',
@ -60,15 +72,7 @@ exports.load = (appUrl) => {
}) })
]) ])
}), }),
new (TouchBar.Slider)({ slider,
label: 'Slider 123',
minValue: 50,
maxValue: 1000,
initialValue: 300,
change: (newVal) => {
console.log('Slider was changed', newVal, typeof newVal)
}
})
])) ]))
}) })
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 120 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Before After
Before After

View file

@ -212,6 +212,12 @@ BrowserWindow.prototype.setTouchBar = function (touchBar) {
} else { } else {
this._setTouchBar(touchBar.toJSON()) 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 module.exports = BrowserWindow

View 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 () { toJSON () {
return this.config return this.config
} }