From f05dfc74daba55603e5a4d478947617e3ec97c6f Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Mon, 27 Mar 2017 20:45:27 +1100 Subject: [PATCH 1/5] Store parent popover in popover touch bar items --- atom/browser/ui/cocoa/atom_touch_bar.mm | 10 +++++++++- lib/browser/api/touch-bar.js | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/atom/browser/ui/cocoa/atom_touch_bar.mm b/atom/browser/ui/cocoa/atom_touch_bar.mm index 3a8037b1c5..2bd290bd9a 100644 --- a/atom/browser/ui/cocoa/atom_touch_bar.mm +++ b/atom/browser/ui/cocoa/atom_touch_bar.mm @@ -126,7 +126,15 @@ static NSString* const ImageScrubberItemIdentifier = @"scrubber.image.item"; type:item_type]; if (!identifier) return; - NSTouchBarItem* item = [touchBar itemForIdentifier:identifier]; + NSTouchBar* targetTouchBar = touchBar; + + std::string popover_id; + if (settings.Get("_popover", &popover_id)) { + NSPopoverTouchBarItem* popoverItem = [touchBar itemForIdentifier:[self identifierFromID:popover_id type:"popover"]]; + targetTouchBar = popoverItem.popoverTouchBar; + } + + NSTouchBarItem* item = [targetTouchBar itemForIdentifier:identifier]; if (!item) return; if (item_type == "button") { diff --git a/lib/browser/api/touch-bar.js b/lib/browser/api/touch-bar.js index 1070398c75..2bdbdbe2ae 100644 --- a/lib/browser/api/touch-bar.js +++ b/lib/browser/api/touch-bar.js @@ -223,6 +223,7 @@ TouchBar.TouchBarPopover = class TouchBarPopover extends TouchBarItem { if (!(this.child instanceof TouchBar)) { this.child = new TouchBar(this.child) } + this.child.ordereredItems.forEach((item) => item._popover = this.id) } } From 2fd62d090a7d50cb27a5be089bc34b1aa9c90f5c Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Mon, 27 Mar 2017 21:10:14 +1100 Subject: [PATCH 2/5] Allow items to be assigned to multiple popovers --- atom/browser/ui/cocoa/atom_touch_bar.mm | 51 ++++++++++++++----------- lib/browser/api/touch-bar.js | 5 ++- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/atom/browser/ui/cocoa/atom_touch_bar.mm b/atom/browser/ui/cocoa/atom_touch_bar.mm index 2bd290bd9a..4b60627488 100644 --- a/atom/browser/ui/cocoa/atom_touch_bar.mm +++ b/atom/browser/ui/cocoa/atom_touch_bar.mm @@ -113,28 +113,11 @@ static NSString* const ImageScrubberItemIdentifier = @"scrubber.image.item"; return nil; } - - (void)refreshTouchBarItem:(NSTouchBar*)touchBar - id:(const std::string&)item_id { - if (![self hasItemWithID:item_id]) return; - - mate::PersistentDictionary settings = settings_[item_id]; - std::string item_type; - settings.Get("type", &item_type); - - NSTouchBarItemIdentifier identifier = [self identifierFromID:item_id - type:item_type]; - if (!identifier) return; - - NSTouchBar* targetTouchBar = touchBar; - - std::string popover_id; - if (settings.Get("_popover", &popover_id)) { - NSPopoverTouchBarItem* popoverItem = [touchBar itemForIdentifier:[self identifierFromID:popover_id type:"popover"]]; - targetTouchBar = popoverItem.popoverTouchBar; - } - - NSTouchBarItem* item = [targetTouchBar itemForIdentifier:identifier]; + id:(NSTouchBarItemIdentifier)identifier + withType:(std::string)item_type + withSettings:(mate::PersistentDictionary)settings { + NSTouchBarItem* item = [touchBar itemForIdentifier:identifier]; if (!item) return; if (item_type == "button") { @@ -143,7 +126,7 @@ static NSString* const ImageScrubberItemIdentifier = @"scrubber.image.item"; [self updateLabel:(NSCustomTouchBarItem*)item withSettings:settings]; } else if (item_type == "colorpicker") { [self updateColorPicker:(NSColorPickerTouchBarItem*)item - withSettings:settings]; + withSettings:settings]; } else if (item_type == "slider") { [self updateSlider:(NSSliderTouchBarItem*)item withSettings:settings]; } else if (item_type == "popover") { @@ -174,6 +157,30 @@ static NSString* const ImageScrubberItemIdentifier = @"scrubber.image.item"; } } +- (void)refreshTouchBarItem:(NSTouchBar*)touchBar + id:(const std::string&)item_id { + if (![self hasItemWithID:item_id]) return; + + mate::PersistentDictionary settings = settings_[item_id]; + std::string item_type; + settings.Get("type", &item_type); + + NSTouchBarItemIdentifier identifier = [self identifierFromID:item_id + type:item_type]; + if (!identifier) return; + + std::vector popover_ids; + if (settings.Get("_popover", &popover_ids)) { + for (size_t i = 0; i < popover_ids.size(); ++i) { + std::string popover_id = popover_ids[i]; + NSPopoverTouchBarItem* popoverItem = [touchBar itemForIdentifier:[self identifierFromID:popover_id type:"popover"]]; + NSTouchBar* targetTouchBar = popoverItem.popoverTouchBar; + [self refreshTouchBarItem:targetTouchBar id:identifier withType:item_type withSettings:settings]; + } + } + [self refreshTouchBarItem:touchBar id:identifier withType:item_type withSettings:settings]; +} + - (void)buttonAction:(id)sender { NSString* item_id = [NSString stringWithFormat:@"%ld", ((NSButton*)sender).tag]; window_->NotifyTouchBarItemInteraction([item_id UTF8String], diff --git a/lib/browser/api/touch-bar.js b/lib/browser/api/touch-bar.js index 2bdbdbe2ae..d0b7d54a59 100644 --- a/lib/browser/api/touch-bar.js +++ b/lib/browser/api/touch-bar.js @@ -223,7 +223,10 @@ TouchBar.TouchBarPopover = class TouchBarPopover extends TouchBarItem { if (!(this.child instanceof TouchBar)) { this.child = new TouchBar(this.child) } - this.child.ordereredItems.forEach((item) => item._popover = this.id) + this.child.ordereredItems.forEach((item) => { + item._popover = item._popover || [] + if (!item._popover.find(itemID => itemID === this.id)) item._popover.push(this.id) + }) } } From bea56bbdc8507f6f7ba250e999dc6d099f1e6d68 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Wed, 29 Mar 2017 15:01:14 +1100 Subject: [PATCH 3/5] Update as per feedback --- atom/browser/ui/cocoa/atom_touch_bar.mm | 12 +++++------- lib/browser/api/touch-bar.js | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/atom/browser/ui/cocoa/atom_touch_bar.mm b/atom/browser/ui/cocoa/atom_touch_bar.mm index 4b60627488..674f572b1e 100644 --- a/atom/browser/ui/cocoa/atom_touch_bar.mm +++ b/atom/browser/ui/cocoa/atom_touch_bar.mm @@ -115,8 +115,8 @@ static NSString* const ImageScrubberItemIdentifier = @"scrubber.image.item"; - (void)refreshTouchBarItem:(NSTouchBar*)touchBar id:(NSTouchBarItemIdentifier)identifier - withType:(std::string)item_type - withSettings:(mate::PersistentDictionary)settings { + withType:(std::string)item_type + withSettings:(mate::PersistentDictionary)settings { NSTouchBarItem* item = [touchBar itemForIdentifier:identifier]; if (!item) return; @@ -126,7 +126,7 @@ static NSString* const ImageScrubberItemIdentifier = @"scrubber.image.item"; [self updateLabel:(NSCustomTouchBarItem*)item withSettings:settings]; } else if (item_type == "colorpicker") { [self updateColorPicker:(NSColorPickerTouchBarItem*)item - withSettings:settings]; + withSettings:settings]; } else if (item_type == "slider") { [self updateSlider:(NSSliderTouchBarItem*)item withSettings:settings]; } else if (item_type == "popover") { @@ -171,11 +171,9 @@ static NSString* const ImageScrubberItemIdentifier = @"scrubber.image.item"; std::vector popover_ids; if (settings.Get("_popover", &popover_ids)) { - for (size_t i = 0; i < popover_ids.size(); ++i) { - std::string popover_id = popover_ids[i]; + for (auto& popover_id : popover_ids) { NSPopoverTouchBarItem* popoverItem = [touchBar itemForIdentifier:[self identifierFromID:popover_id type:"popover"]]; - NSTouchBar* targetTouchBar = popoverItem.popoverTouchBar; - [self refreshTouchBarItem:targetTouchBar id:identifier withType:item_type withSettings:settings]; + [self refreshTouchBarItem:popoverItem.popoverTouchBar id:identifier withType:item_type withSettings:settings]; } } [self refreshTouchBarItem:touchBar id:identifier withType:item_type withSettings:settings]; diff --git a/lib/browser/api/touch-bar.js b/lib/browser/api/touch-bar.js index d0b7d54a59..1a9c3238a9 100644 --- a/lib/browser/api/touch-bar.js +++ b/lib/browser/api/touch-bar.js @@ -225,7 +225,7 @@ TouchBar.TouchBarPopover = class TouchBarPopover extends TouchBarItem { } this.child.ordereredItems.forEach((item) => { item._popover = item._popover || [] - if (!item._popover.find(itemID => itemID === this.id)) item._popover.push(this.id) + if (!item._popover.includes(this.id)) item._popover.push(this.id) }) } } From 9c73c991d726ad1decf980e4535890634cc5d964 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 3 Apr 2017 10:57:57 -0700 Subject: [PATCH 4/5] Use const references for params --- atom/browser/ui/cocoa/atom_touch_bar.mm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/atom/browser/ui/cocoa/atom_touch_bar.mm b/atom/browser/ui/cocoa/atom_touch_bar.mm index 674f572b1e..944501471c 100644 --- a/atom/browser/ui/cocoa/atom_touch_bar.mm +++ b/atom/browser/ui/cocoa/atom_touch_bar.mm @@ -115,8 +115,8 @@ static NSString* const ImageScrubberItemIdentifier = @"scrubber.image.item"; - (void)refreshTouchBarItem:(NSTouchBar*)touchBar id:(NSTouchBarItemIdentifier)identifier - withType:(std::string)item_type - withSettings:(mate::PersistentDictionary)settings { + withType:(const std::string&)item_type + withSettings:(const mate::PersistentDictionary&)settings { NSTouchBarItem* item = [touchBar itemForIdentifier:identifier]; if (!item) return; From 6a22c6645e1417de44ee2ddd6ad2dd003d9f2738 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 3 Apr 2017 11:05:16 -0700 Subject: [PATCH 5/5] Guard against missing popover identifier --- atom/browser/ui/cocoa/atom_touch_bar.mm | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/atom/browser/ui/cocoa/atom_touch_bar.mm b/atom/browser/ui/cocoa/atom_touch_bar.mm index 944501471c..a3e846df48 100644 --- a/atom/browser/ui/cocoa/atom_touch_bar.mm +++ b/atom/browser/ui/cocoa/atom_touch_bar.mm @@ -165,18 +165,27 @@ static NSString* const ImageScrubberItemIdentifier = @"scrubber.image.item"; std::string item_type; settings.Get("type", &item_type); - NSTouchBarItemIdentifier identifier = [self identifierFromID:item_id - type:item_type]; + auto identifier = [self identifierFromID:item_id type:item_type]; if (!identifier) return; std::vector popover_ids; - if (settings.Get("_popover", &popover_ids)) { - for (auto& popover_id : popover_ids) { - NSPopoverTouchBarItem* popoverItem = [touchBar itemForIdentifier:[self identifierFromID:popover_id type:"popover"]]; - [self refreshTouchBarItem:popoverItem.popoverTouchBar id:identifier withType:item_type withSettings:settings]; - } + settings.Get("_popover", &popover_ids); + for (auto& popover_id : popover_ids) { + auto popoverIdentifier = [self identifierFromID:popover_id type:"popover"]; + if (!popoverIdentifier) continue; + + NSPopoverTouchBarItem* popoverItem = + [touchBar itemForIdentifier:popoverIdentifier]; + [self refreshTouchBarItem:popoverItem.popoverTouchBar + id:identifier + withType:item_type + withSettings:settings]; } - [self refreshTouchBarItem:touchBar id:identifier withType:item_type withSettings:settings]; + + [self refreshTouchBarItem:touchBar + id:identifier + withType:item_type + withSettings:settings]; } - (void)buttonAction:(id)sender {