Use scoped_nsobject to match Chrome implementation
This commit is contained in:
parent
812beb240b
commit
ec500b2852
3 changed files with 30 additions and 23 deletions
|
@ -383,7 +383,10 @@ bool ScopedDisableResize::disable_resize_ = false;
|
||||||
|
|
||||||
- (nullable NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar
|
- (nullable NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar
|
||||||
makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier {
|
makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier {
|
||||||
return [atom_touch_bar_ makeItemForIdentifier:identifier];
|
if (touchBar)
|
||||||
|
return [atom_touch_bar_ makeItemForIdentifier:identifier];
|
||||||
|
else
|
||||||
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
// NSWindow overrides.
|
// NSWindow overrides.
|
||||||
|
|
|
@ -13,13 +13,14 @@
|
||||||
|
|
||||||
#include "atom/browser/native_window.h"
|
#include "atom/browser/native_window.h"
|
||||||
#include "atom/browser/ui/cocoa/touch_bar_forward_declarations.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/constructor.h"
|
||||||
#include "native_mate/persistent_dictionary.h"
|
#include "native_mate/persistent_dictionary.h"
|
||||||
|
|
||||||
@interface AtomTouchBar : NSObject {
|
@interface AtomTouchBar : NSObject {
|
||||||
@protected
|
@protected
|
||||||
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;
|
std::map<std::string, base::scoped_nsobject<NSTouchBarItem>> item_map;
|
||||||
id<NSTouchBarDelegate> delegate_;
|
id<NSTouchBarDelegate> delegate_;
|
||||||
atom::NativeWindow* window_;
|
atom::NativeWindow* window_;
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,10 +38,11 @@ static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.touchbar.slide
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSTouchBar*)touchBarFromItemIdentifiers:(NSMutableArray*)items {
|
- (NSTouchBar*)touchBarFromItemIdentifiers:(NSMutableArray*)items {
|
||||||
NSTouchBar* bar = [[NSClassFromString(@"NSTouchBar") alloc] init];
|
base::scoped_nsobject<NSTouchBar> bar(
|
||||||
bar.delegate = delegate_;
|
[[NSClassFromString(@"NSTouchBar") alloc] init]);
|
||||||
bar.defaultItemIdentifiers = items;
|
[bar setDelegate:delegate_];
|
||||||
return bar;
|
[bar setDefaultItemIdentifiers:items];
|
||||||
|
return bar.autorelease();
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSMutableArray*)identifierArrayFromDicts:(const std::vector<mate::PersistentDictionary>&)dicts {
|
- (NSMutableArray*)identifierArrayFromDicts:(const std::vector<mate::PersistentDictionary>&)dicts {
|
||||||
|
@ -51,7 +52,7 @@ static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.touchbar.slide
|
||||||
std::string type;
|
std::string type;
|
||||||
std::string item_id;
|
std::string item_id;
|
||||||
if (item.Get("type", &type) && item.Get("id", &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") {
|
if (type == "button") {
|
||||||
[idents addObject:[NSString stringWithFormat:@"%@%@", ButtonIdentifier, base::SysUTF8ToNSString(item_id)]];
|
[idents addObject:[NSString stringWithFormat:@"%@%@", ButtonIdentifier, base::SysUTF8ToNSString(item_id)]];
|
||||||
} else if (type == "label") {
|
} else if (type == "label") {
|
||||||
|
@ -73,31 +74,33 @@ static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.touchbar.slide
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSTouchBarItem*)makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier {
|
- (NSTouchBarItem*)makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier {
|
||||||
NSTouchBarItem* item = nil;
|
base::scoped_nsobject<NSTouchBarItem> item;
|
||||||
NSString* id = nil;
|
NSString* item_id = nil;
|
||||||
|
|
||||||
if ([identifier hasPrefix:ButtonIdentifier]) {
|
if ([identifier hasPrefix:ButtonIdentifier]) {
|
||||||
id = [self idFromIdentifier:identifier withPrefix:ButtonIdentifier];
|
item_id = [self idFromIdentifier:identifier withPrefix:ButtonIdentifier];
|
||||||
item = [self makeButtonForID:id withIdentifier:identifier];
|
item.reset([self makeButtonForID:item_id withIdentifier:identifier]);
|
||||||
} else if ([identifier hasPrefix:LabelIdentifier]) {
|
} else if ([identifier hasPrefix:LabelIdentifier]) {
|
||||||
id = [self idFromIdentifier:identifier withPrefix:LabelIdentifier];
|
item_id = [self idFromIdentifier:identifier withPrefix:LabelIdentifier];
|
||||||
item = [self makeLabelForID:id withIdentifier:identifier];
|
item.reset([self makeLabelForID:item_id withIdentifier:identifier]);
|
||||||
} else if ([identifier hasPrefix:ColorPickerIdentifier]) {
|
} else if ([identifier hasPrefix:ColorPickerIdentifier]) {
|
||||||
id = [self idFromIdentifier:identifier withPrefix:ColorPickerIdentifier];
|
item_id = [self idFromIdentifier:identifier withPrefix:ColorPickerIdentifier];
|
||||||
item = [self makeColorPickerForID:id withIdentifier:identifier];
|
item.reset([self makeColorPickerForID:item_id withIdentifier:identifier]);
|
||||||
} else if ([identifier hasPrefix:SliderIdentifier]) {
|
} else if ([identifier hasPrefix:SliderIdentifier]) {
|
||||||
id = [self idFromIdentifier:identifier withPrefix:SliderIdentifier];
|
item_id = [self idFromIdentifier:identifier withPrefix:SliderIdentifier];
|
||||||
item = [self makeSliderForID:id withIdentifier:identifier];
|
item.reset([self makeSliderForID:item_id withIdentifier:identifier]);
|
||||||
} else if ([identifier hasPrefix:PopOverIdentifier]) {
|
} else if ([identifier hasPrefix:PopOverIdentifier]) {
|
||||||
id = [self idFromIdentifier:identifier withPrefix:PopOverIdentifier];
|
item_id = [self idFromIdentifier:identifier withPrefix:PopOverIdentifier];
|
||||||
item = [self makePopoverForID:id withIdentifier:identifier];
|
item.reset([self makePopoverForID:item_id withIdentifier:identifier]);
|
||||||
} else if ([identifier hasPrefix:GroupIdentifier]) {
|
} else if ([identifier hasPrefix:GroupIdentifier]) {
|
||||||
id = [self idFromIdentifier:identifier withPrefix:GroupIdentifier];
|
item_id = [self idFromIdentifier:identifier withPrefix:GroupIdentifier];
|
||||||
item = [self makeGroupForID:id withIdentifier:identifier];
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue