Chromium Code Reviews| Index: chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.mm |
| diff --git a/chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.mm b/chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.mm |
| index 3548b9f9881a2f951b9912894c79b92dfc3d4e94..80f28aec3342a4a1841075870b299c082e59f29b 100644 |
| --- a/chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.mm |
| +++ b/chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.mm |
| @@ -5,6 +5,7 @@ |
| #import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.h" |
| #include "base/logging.h" |
| +#include "base/mac/foundation_util.h" |
| #import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.h" |
| namespace { |
| @@ -14,9 +15,68 @@ const NSInteger kMiddleButtonNumber = 2; |
| } // namespace |
| -@interface OmniboxPopupMatrix() |
| +@implementation OmniboxPopupTableController |
| + |
| +- (id)init { |
| + if ((self = [super init])) { |
| + array_.reset([[NSArray alloc] init]); |
|
groby-ooo-7-16
2015/05/20 01:02:30
Why reset the array_ to an empty array?
dschuyler
2015/05/21 00:38:39
Likely because I only just learned about nil as a
|
| + hovered_index_ = -1; |
| + } |
| + return self; |
| +} |
| + |
| +- (NSInteger)numberOfRowsInTableView:(NSTableView*)tableView { |
| + return [array_ count]; |
| +} |
| + |
| +- (id)tableView:(NSTableView*)tableView |
| + objectValueForTableColumn:(NSTableColumn*)tableColumn |
| + row:(NSInteger)rowIndex { |
| + return nil; |
|
groby-ooo-7-16
2015/05/20 01:02:31
This still makes me queasy. You are currently not
dschuyler
2015/05/21 00:38:39
It is being called.
Here's a link to a prior vers
groby-ooo-7-16
2015/05/21 03:01:48
No, we should try to understand what's actually go
Scott Hess - ex-Googler
2015/05/21 04:46:35
Something I have often done is created a distinct
dschuyler
2015/05/26 18:40:19
I'll look into this further. I'm going to put an
groby-ooo-7-16
2015/05/26 21:23:22
Waiting for the final patch before I do a full rev
dschuyler
2015/05/28 20:34:22
Done.
|
| +} |
| + |
| +- (void)tableView:(NSTableView*)aTableView |
| + setObjectValue:(id)anObject |
| + forTableColumn:(NSTableColumn*)aTableColumn |
| + row:(NSInteger)rowIndex { |
| +} |
| + |
| +- (void)tableView:(NSTableView*)tableView |
| + willDisplayCell:(id)cell |
| + forTableColumn:(NSTableColumn*)tableColumn |
| + row:(NSInteger)rowIndex { |
| + OmniboxPopupCell* popupCell = |
| + base::mac::ObjCCastStrict<OmniboxPopupCell>(cell); |
| + [popupCell setCellData:[array_ objectAtIndex:rowIndex]]; |
| + [popupCell |
| + setState:([tableView selectedRow] == rowIndex) ? NSOnState : NSOffState]; |
| + [popupCell highlight:(hovered_index_ == rowIndex) |
|
groby-ooo-7-16
2015/05/20 01:02:30
hoveredIndex_ :)
Which brings up a question - can
dschuyler
2015/05/21 00:38:39
Highlighted and hovered are always in sync (or sho
|
| + withFrame:[tableView bounds] |
| + inView:tableView]; |
| +} |
| + |
| +- (NSInteger)highlightedRow { |
| + return hovered_index_; |
| +} |
| + |
| +- (void)highlightRowAt:(NSInteger)rowIndex withView:(NSView*)view { |
| + hovered_index_ = rowIndex; |
| +} |
| + |
| +- (void)setDataArray:(NSArray*)array { |
| + hovered_index_ = -1; |
| + array_.reset([array retain]); |
| +} |
| + |
| +- (CGFloat)tableView:(NSTableView*)tableView heightOfRow:(NSInteger)row { |
| + OmniboxPopupCellData* cellData = [array_ objectAtIndex:row]; |
| + return [cellData rowHeight]; |
| +} |
| + |
| +@end |
| + |
| +@interface OmniboxPopupMatrix () |
| - (void)resetTrackingArea; |
| -- (void)highlightRowAt:(NSInteger)rowIndex; |
| - (void)highlightRowUnder:(NSEvent*)theEvent; |
| - (BOOL)selectCellForEvent:(NSEvent*)theEvent; |
| @end |
| @@ -26,17 +86,25 @@ const NSInteger kMiddleButtonNumber = 2; |
| - (id)initWithObserver:(OmniboxPopupMatrixObserver*)observer { |
| if ((self = [super initWithFrame:NSZeroRect])) { |
| observer_ = observer; |
| - [self setCellClass:[OmniboxPopupCell class]]; |
| + controller_.reset([[OmniboxPopupTableController alloc] init]); |
| + |
| + base::scoped_nsobject<NSTableColumn> column( |
| + [[NSTableColumn alloc] initWithIdentifier:@"MainCell"]); |
| + base::scoped_nsobject<OmniboxPopupCell> columnCell( |
| + [[OmniboxPopupCell alloc] init]); |
| + [column setDataCell:columnCell]; |
| + [self addTableColumn:column]; |
| + |
| + [self setDelegate:controller_]; |
| + [self setDataSource:controller_]; |
| // Cells pack with no spacing. |
| [self setIntercellSpacing:NSMakeSize(0.0, 0.0)]; |
| - [self setDrawsBackground:YES]; |
| + [self setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone]; |
| [self setBackgroundColor:[NSColor controlBackgroundColor]]; |
| - [self renewRows:0 columns:1]; |
| [self setAllowsEmptySelection:YES]; |
| - [self setMode:NSRadioModeMatrix]; |
| - [self deselectAllCells]; |
| + [self deselectAll:self]; |
| [self resetTrackingArea]; |
| } |
| @@ -47,17 +115,6 @@ const NSInteger kMiddleButtonNumber = 2; |
| observer_ = observer; |
| } |
| -- (NSInteger)highlightedRow { |
| - NSArray* cells = [self cells]; |
| - const NSUInteger count = [cells count]; |
| - for(NSUInteger i = 0; i < count; ++i) { |
| - if ([[cells objectAtIndex:i] isHighlighted]) { |
| - return i; |
| - } |
| - } |
| - return -1; |
| -} |
| - |
| - (void)updateTrackingAreas { |
| [self resetTrackingArea]; |
| [super updateTrackingAreas]; |
| @@ -73,7 +130,7 @@ const NSInteger kMiddleButtonNumber = 2; |
| } |
| - (void)mouseExited:(NSEvent*)theEvent { |
| - [self highlightRowAt:-1]; |
| + [controller_ highlightRowAt:-1 withView:self]; |
| } |
| // The tracking area events aren't forwarded during a drag, so handle |
| @@ -103,7 +160,7 @@ const NSInteger kMiddleButtonNumber = 2; |
| // make sure the user is getting the right feedback. |
| [self highlightRowUnder:theEvent]; |
| - const NSInteger highlightedRow = [self highlightedRow]; |
| + const NSInteger highlightedRow = [controller_ highlightedRow]; |
| if (highlightedRow != -1) { |
| DCHECK(observer_); |
| observer_->OnMatrixRowMiddleClicked(self, highlightedRow); |
| @@ -117,7 +174,7 @@ const NSInteger kMiddleButtonNumber = 2; |
| NSCell* selectedCell = [self selectedCell]; |
| // Clear any existing highlight. |
| - [self highlightRowAt:-1]; |
| + [controller_ highlightRowAt:-1 withView:self]; |
| do { |
| if (![self selectCellForEvent:theEvent]) { |
| @@ -145,6 +202,19 @@ const NSInteger kMiddleButtonNumber = 2; |
| } |
| } |
| +- (void)selectRowAt:(NSInteger)rowIndex { |
| + NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:rowIndex]; |
| + [self selectRowIndexes:indexSet byExtendingSelection:NO]; |
| +} |
| + |
| +- (NSInteger)highlightedRow { |
| + return [controller_ highlightedRow]; |
|
groby-ooo-7-16
2015/05/20 01:02:31
Why not [self selectedRow] ?
dschuyler
2015/05/21 00:38:39
Highlighting is for mouse hovering. Selection is
|
| +} |
| + |
| +- (void)setDataArray:(NSArray*)array { |
| + [controller_ setDataArray:array]; |
| +} |
| + |
| - (void)resetTrackingArea { |
| if (trackingArea_.get()) |
| [self removeTrackingArea:trackingArea_.get()]; |
| @@ -160,33 +230,23 @@ const NSInteger kMiddleButtonNumber = 2; |
| [self addTrackingArea:trackingArea_.get()]; |
| } |
| -- (void)highlightRowAt:(NSInteger)rowIndex { |
| - // highlightCell will be nil if rowIndex is out of range, so no cell will be |
| - // highlighted. |
| - NSCell* highlightCell = [self cellAtRow:rowIndex column:0]; |
| - |
| - for (NSCell* cell in [self cells]) { |
| - [cell setHighlighted:(cell == highlightCell)]; |
| - } |
| -} |
| - |
| - (void)highlightRowUnder:(NSEvent*)theEvent { |
| NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil]; |
| - NSInteger row, column; |
| - if ([self getRow:&row column:&column forPoint:point]) { |
| - [self highlightRowAt:row]; |
| - } else { |
| - [self highlightRowAt:-1]; |
| + NSInteger oldRow = [controller_ highlightedRow]; |
| + NSInteger newRow = [self rowAtPoint:point]; |
| + if (oldRow != newRow) { |
| + [controller_ highlightRowAt:newRow withView:self]; |
| + [self setNeedsDisplayInRect:[self rectOfRow:oldRow]]; |
| + [self setNeedsDisplayInRect:[self rectOfRow:newRow]]; |
| } |
| } |
| -// Select cell under |theEvent|, returning YES if a selection is made. |
| - (BOOL)selectCellForEvent:(NSEvent*)theEvent { |
| NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil]; |
| - NSInteger row, column; |
| - if ([self getRow:&row column:&column forPoint:point]) { |
| - DCHECK_EQ(column, 0); |
| + NSInteger row = [self rowAtPoint:point]; |
| + [self selectRowAt:row]; |
| + if (row != -1) { |
| DCHECK(observer_); |
| observer_->OnMatrixRowSelected(self, row); |
|
groby-ooo-7-16
2015/05/20 01:02:31
Do we not want to tell the observer when nothing i
dschuyler
2015/05/21 00:38:39
We keep the last selected item selected. Telling
|
| return YES; |