Initial support for dynamic properties

This commit is contained in:
Kevin Sawicki 2017-02-28 16:08:12 -08:00
parent cbb6f8c33e
commit 98f5858b11
10 changed files with 46 additions and 33 deletions

View file

@ -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 {

View file

@ -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);

View file

@ -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() {

View file

@ -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();

View file

@ -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:

View file

@ -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() {

View file

@ -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;

View file

@ -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");
}
}

View file

@ -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

View file

@ -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
}
}