Implement NSScrubberDataSource in AtomTouchBar

This commit is contained in:
Kevin Sawicki 2017-03-13 11:06:41 -07:00
parent 4f31a5c452
commit e52ece1a1e
5 changed files with 45 additions and 98 deletions

View file

@ -1,26 +0,0 @@
// Copyright (c) 2017 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_UI_COCOA_ATOM_SCRUBBER_DATA_SOURCE_H_
#define ATOM_BROWSER_UI_COCOA_ATOM_SCRUBBER_DATA_SOURCE_H_
#import <Cocoa/Cocoa.h>
#include <string>
#include <vector>
#include "atom/browser/ui/cocoa/touch_bar_forward_declarations.h"
#include "native_mate/persistent_dictionary.h"
@interface AtomScrubberDataSource : NSObject<NSScrubberDataSource> {
@protected
std::vector<mate::PersistentDictionary> items_;
}
- (id)initWithItems:(const std::vector<mate::PersistentDictionary>&)items;
- (void)setItems:(const std::vector<mate::PersistentDictionary>&)items;
@end
#endif // ATOM_BROWSER_UI_COCOA_ATOM_SCRUBBER_DATA_SOURCE_H_

View file

@ -1,56 +0,0 @@
// Copyright (c) 2017 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#import "atom/browser/ui/cocoa/atom_scrubber_data_source.h"
#include "atom/common/native_mate_converters/image_converter.h"
#include "base/strings/sys_string_conversions.h"
#include "ui/gfx/image/image.h"
@implementation AtomScrubberDataSource
static NSString* const TextItemIdentifier = @"scrubber.text.item";
static NSString* const ImageItemIdentifier = @"scrubber.image.item";
- (id)initWithItems:(const std::vector<mate::PersistentDictionary>&)items {
if ((self = [super init])) {
items_ = items;
}
return self;
}
- (NSInteger)numberOfItemsForScrubber:(NSScrubber*)theScrubber {
return items_.size();
}
- (NSScrubberItemView*)scrubber:(NSScrubber*)scrubber
viewForItemAtIndex:(NSInteger)index {
mate::PersistentDictionary item = items_[index];
NSScrubberItemView* itemView;
std::string title;
if (item.Get("label", &title)) {
NSScrubberTextItemView* view = [scrubber makeItemWithIdentifier:TextItemIdentifier owner:self];
view.title = base::SysUTF8ToNSString(title);
itemView = view;
} else {
NSScrubberImageItemView* view = [scrubber makeItemWithIdentifier:ImageItemIdentifier owner:self];
gfx::Image image;
if (item.Get("image", &image)) {
view.image = image.AsNSImage();
}
itemView = view;
}
return itemView;
}
- (void)setItems:(const std::vector<mate::PersistentDictionary>&)items {
items_ = items;
}
@end

View file

@ -17,7 +17,7 @@
#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<NSScrubberDelegate> { @interface AtomTouchBar : NSObject<NSScrubberDelegate, NSScrubberDataSource> {
@protected @protected
std::vector<mate::PersistentDictionary> ordered_settings_; std::vector<mate::PersistentDictionary> ordered_settings_;
std::map<std::string, mate::PersistentDictionary> settings_; std::map<std::string, mate::PersistentDictionary> settings_;

View file

@ -504,40 +504,71 @@ static NSString* const ImageScrubberItemIdentifier = @"scrubber.image.item";
base::scoped_nsobject<NSCustomTouchBarItem> item([[NSClassFromString( base::scoped_nsobject<NSCustomTouchBarItem> item([[NSClassFromString(
@"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]); @"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]);
int width = 320; int width = 320;
int height = 30; int height = 30;
settings.Get("frameWidth", &width); settings.Get("frameWidth", &width);
settings.Get("frameHeight", &height); settings.Get("frameHeight", &height);
NSScrubber* scrubber = [[NSClassFromString(@"NSScrubber") alloc] initWithFrame:NSMakeRect(0, 0, width, height)]; NSScrubber* scrubber = [[[NSClassFromString(@"NSScrubber") alloc] initWithFrame:NSMakeRect(0, 0, width, height)] autorelease];
[scrubber registerClass:NSClassFromString(@"NSScrubberTextItemView") forItemIdentifier:TextScrubberItemIdentifier]; [scrubber registerClass:NSClassFromString(@"NSScrubberTextItemView") forItemIdentifier:TextScrubberItemIdentifier];
[scrubber registerClass:NSClassFromString(@"NSScrubberImageItemView") forItemIdentifier:ImageScrubberItemIdentifier]; [scrubber registerClass:NSClassFromString(@"NSScrubberImageItemView") forItemIdentifier:ImageScrubberItemIdentifier];
scrubber.delegate = self; scrubber.delegate = self;
scrubber.dataSource = self;
scrubber.identifier = id; scrubber.identifier = id;
std::vector<mate::PersistentDictionary> items;
settings.Get("items", &items);
scrubber.dataSource = [[AtomScrubberDataSource alloc] initWithItems:items];
scrubber.mode = NSScrubberModeFree; scrubber.mode = NSScrubberModeFree;
[item setView:scrubber]; [item setView:scrubber];
[self updateScrubber:item withSettings:settings]; [self updateScrubber:item withSettings:settings];
return item.autorelease(); return item.autorelease();
} }
- (void)updateScrubber:(NSCustomTouchBarItem*)item - (void)updateScrubber:(NSCustomTouchBarItem*)item
withSettings:(const mate::PersistentDictionary&)settings { withSettings:(const mate::PersistentDictionary&)settings {
NSScrubber* scrubber = item.view; NSScrubber* scrubber = item.view;
std::vector<mate::PersistentDictionary> items;
settings.Get("items", &items);
AtomScrubberDataSource* source = scrubber.dataSource;
[source setItems:items];
[scrubber reloadData]; [scrubber reloadData];
} }
- (NSInteger)numberOfItemsForScrubber:(NSScrubber*)scrubber {
std::string s_id([[scrubber identifier] UTF8String]);
if (![self hasItemWithID:s_id]) return 0;
mate::PersistentDictionary settings = settings_[s_id];
std::vector<mate::PersistentDictionary> items;
settings.Get("items", &items);
return items.size();
}
- (NSScrubberItemView*)scrubber:(NSScrubber*)scrubber
viewForItemAtIndex:(NSInteger)index {
std::string s_id([[scrubber identifier] UTF8String]);
if (![self hasItemWithID:s_id]) return nil;
mate::PersistentDictionary settings = settings_[s_id];
std::vector<mate::PersistentDictionary> items;
settings.Get("items", &items);
mate::PersistentDictionary item = items[index];
NSScrubberItemView* itemView;
std::string title;
if (item.Get("label", &title)) {
NSScrubberTextItemView* view = [scrubber makeItemWithIdentifier:TextScrubberItemIdentifier
owner:self];
view.title = base::SysUTF8ToNSString(title);
itemView = view;
} else {
NSScrubberImageItemView* view = [scrubber makeItemWithIdentifier:ImageScrubberItemIdentifier
owner:self];
gfx::Image image;
if (item.Get("image", &image)) {
view.image = image.AsNSImage();
}
itemView = view;
}
return itemView;
}
@end @end

View file

@ -284,8 +284,6 @@
'atom/browser/ui/atom_menu_model.h', 'atom/browser/ui/atom_menu_model.h',
'atom/browser/ui/cocoa/atom_menu_controller.h', 'atom/browser/ui/cocoa/atom_menu_controller.h',
'atom/browser/ui/cocoa/atom_menu_controller.mm', 'atom/browser/ui/cocoa/atom_menu_controller.mm',
'atom/browser/ui/cocoa/atom_scrubber_data_source.h',
'atom/browser/ui/cocoa/atom_scrubber_data_source.mm',
'atom/browser/ui/cocoa/atom_touch_bar.h', 'atom/browser/ui/cocoa/atom_touch_bar.h',
'atom/browser/ui/cocoa/atom_touch_bar.mm', 'atom/browser/ui/cocoa/atom_touch_bar.mm',
'atom/browser/ui/cocoa/touch_bar_forward_declarations.h', 'atom/browser/ui/cocoa/touch_bar_forward_declarations.h',