From ec500b285238dd46cf484e0dfb7499911e23b0ff Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 28 Feb 2017 16:44:03 -0800 Subject: [PATCH] Use scoped_nsobject to match Chrome implementation --- atom/browser/native_window_mac.mm | 5 ++- atom/browser/ui/cocoa/atom_touch_bar.h | 3 +- atom/browser/ui/cocoa/atom_touch_bar.mm | 45 +++++++++++++------------ 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index d93b13645848..66fc97480c7a 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -383,7 +383,10 @@ bool ScopedDisableResize::disable_resize_ = false; - (nullable NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { - return [atom_touch_bar_ makeItemForIdentifier:identifier]; + if (touchBar) + return [atom_touch_bar_ makeItemForIdentifier:identifier]; + else + return nil; } // NSWindow overrides. diff --git a/atom/browser/ui/cocoa/atom_touch_bar.h b/atom/browser/ui/cocoa/atom_touch_bar.h index 2efabe6cf0ee..50f03cda6213 100644 --- a/atom/browser/ui/cocoa/atom_touch_bar.h +++ b/atom/browser/ui/cocoa/atom_touch_bar.h @@ -13,13 +13,14 @@ #include "atom/browser/native_window.h" #include "atom/browser/ui/cocoa/touch_bar_forward_declarations.h" +#include "base/mac/scoped_nsobject.h" #include "native_mate/constructor.h" #include "native_mate/persistent_dictionary.h" @interface AtomTouchBar : NSObject { @protected std::map item_id_map; - std::map item_map; + std::map> item_map; id delegate_; atom::NativeWindow* window_; } diff --git a/atom/browser/ui/cocoa/atom_touch_bar.mm b/atom/browser/ui/cocoa/atom_touch_bar.mm index b2053f7df045..c7a002081e3f 100644 --- a/atom/browser/ui/cocoa/atom_touch_bar.mm +++ b/atom/browser/ui/cocoa/atom_touch_bar.mm @@ -38,10 +38,11 @@ static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.touchbar.slide } - (NSTouchBar*)touchBarFromItemIdentifiers:(NSMutableArray*)items { - NSTouchBar* bar = [[NSClassFromString(@"NSTouchBar") alloc] init]; - bar.delegate = delegate_; - bar.defaultItemIdentifiers = items; - return bar; + base::scoped_nsobject bar( + [[NSClassFromString(@"NSTouchBar") alloc] init]); + [bar setDelegate:delegate_]; + [bar setDefaultItemIdentifiers:items]; + return bar.autorelease(); } - (NSMutableArray*)identifierArrayFromDicts:(const std::vector&)dicts { @@ -51,7 +52,7 @@ static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.touchbar.slide std::string type; std::string item_id; if (item.Get("type", &type) && item.Get("id", &item_id)) { - item_id_map.insert(make_pair(item_id, item)); + item_id_map[item_id] = item; if (type == "button") { [idents addObject:[NSString stringWithFormat:@"%@%@", ButtonIdentifier, base::SysUTF8ToNSString(item_id)]]; } else if (type == "label") { @@ -73,31 +74,33 @@ static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.touchbar.slide } - (NSTouchBarItem*)makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { - NSTouchBarItem* item = nil; - NSString* id = nil; + base::scoped_nsobject item; + NSString* item_id = nil; + if ([identifier hasPrefix:ButtonIdentifier]) { - id = [self idFromIdentifier:identifier withPrefix:ButtonIdentifier]; - item = [self makeButtonForID:id withIdentifier:identifier]; + item_id = [self idFromIdentifier:identifier withPrefix:ButtonIdentifier]; + item.reset([self makeButtonForID:item_id withIdentifier:identifier]); } else if ([identifier hasPrefix:LabelIdentifier]) { - id = [self idFromIdentifier:identifier withPrefix:LabelIdentifier]; - item = [self makeLabelForID:id withIdentifier:identifier]; + item_id = [self idFromIdentifier:identifier withPrefix:LabelIdentifier]; + item.reset([self makeLabelForID:item_id withIdentifier:identifier]); } else if ([identifier hasPrefix:ColorPickerIdentifier]) { - id = [self idFromIdentifier:identifier withPrefix:ColorPickerIdentifier]; - item = [self makeColorPickerForID:id withIdentifier:identifier]; + item_id = [self idFromIdentifier:identifier withPrefix:ColorPickerIdentifier]; + item.reset([self makeColorPickerForID:item_id withIdentifier:identifier]); } else if ([identifier hasPrefix:SliderIdentifier]) { - id = [self idFromIdentifier:identifier withPrefix:SliderIdentifier]; - item = [self makeSliderForID:id withIdentifier:identifier]; + item_id = [self idFromIdentifier:identifier withPrefix:SliderIdentifier]; + item.reset([self makeSliderForID:item_id withIdentifier:identifier]); } else if ([identifier hasPrefix:PopOverIdentifier]) { - id = [self idFromIdentifier:identifier withPrefix:PopOverIdentifier]; - item = [self makePopoverForID:id withIdentifier:identifier]; + item_id = [self idFromIdentifier:identifier withPrefix:PopOverIdentifier]; + item.reset([self makePopoverForID:item_id withIdentifier:identifier]); } else if ([identifier hasPrefix:GroupIdentifier]) { - id = [self idFromIdentifier:identifier withPrefix:GroupIdentifier]; - item = [self makeGroupForID:id withIdentifier:identifier]; + item_id = [self idFromIdentifier:identifier withPrefix:GroupIdentifier]; + item.reset([self makeGroupForID:item_id withIdentifier:identifier]); } - item_map.insert(make_pair(std::string([id UTF8String]), item)); + if (item_id) + item_map[[item_id UTF8String]] = item; - return item; + return item.autorelease(); }