Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.h" | 5 #import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.h" | 8 #import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| 11 | 11 |
| 12 // NSEvent -buttonNumber for middle mouse button. | 12 // NSEvent -buttonNumber for middle mouse button. |
| 13 const NSInteger kMiddleButtonNumber = 2; | 13 const NSInteger kMiddleButtonNumber = 2; |
| 14 | 14 |
| 15 } // namespace | 15 } // namespace |
| 16 | 16 |
| 17 @interface OmniboxPopupMatrix() | 17 @implementation OmniboxPopupTableController |
| 18 | |
| 19 - (id)init { | |
| 20 if ((self = [super init])) { | |
| 21 array_ = [[NSMutableArray alloc] init]; | |
| 22 } | |
| 23 return self; | |
| 24 } | |
| 25 | |
| 26 - (NSInteger)numberOfRowsInTableView:(NSTableView*)tableView { | |
| 27 return [array_ count]; | |
| 28 } | |
| 29 | |
| 30 - (id)tableView:(NSTableView*)aTableView | |
| 31 objectValueForTableColumn:(NSTableColumn*)aTableColumn | |
| 32 row:(NSInteger)rowIndex { | |
| 33 // Returning the cell allows selection highlighting to work properly. | |
| 34 return [array_ objectAtIndex:rowIndex]; | |
| 35 } | |
| 36 | |
| 37 - (NSCell*)tableView:(NSTableView*)tableView | |
| 38 dataCellForTableColumn:(NSTableColumn*)tableColumn | |
| 39 row:(NSInteger)row { | |
| 40 // Returning the cell populates data in the table. | |
| 41 return [array_ objectAtIndex:row]; | |
| 42 } | |
| 43 | |
| 44 - (void)setCellCount:(NSUInteger)count { | |
| 45 // Add more. | |
| 46 while (count >= [array_ count]) { | |
|
Scott Hess - ex-Googler
2015/04/25 06:04:27
Suggest while ([array_ count] < count)... Might b
dschuyler
2015/05/01 21:50:54
I'm sure that in previous test I've done, it was b
| |
| 47 [array_ addObject:[[[OmniboxPopupCell alloc] init] autorelease]]; | |
| 48 } | |
| 49 // Remove excess. | |
| 50 if (count < [array_ count]) | |
| 51 [array_ removeObjectsInRange:NSMakeRange(count, [array_ count] - count)]; | |
|
Scott Hess - ex-Googler
2015/04/25 06:04:27
This is documented as using -removeObjectsAtIndex:
dschuyler
2015/05/01 21:50:54
Previously in this CL, it was calling removeLastOb
Scott Hess - ex-Googler
2015/05/04 19:40:20
I'm mixed. Rachel suggested that change, and I ca
| |
| 52 DCHECK_EQ(count, [array_ count]); | |
| 53 // Set selection to be the first cell. | |
| 54 if (count) | |
| 55 [[array_ objectAtIndex:0] setState:NSOnState]; | |
|
Scott Hess - ex-Googler
2015/04/25 06:04:27
Since you may have re-used existing cells in array
dschuyler
2015/05/01 21:50:54
Good catch.
Done.
| |
| 56 } | |
| 57 | |
| 58 - (NSArray*)getCellArray { | |
| 59 return array_; | |
|
Scott Hess - ex-Googler
2015/04/25 06:04:27
You document this as an immutable copy, but it can
groby-ooo-7-16
2015/04/25 19:01:12
Technically, if the controller managed highlight/r
dschuyler
2015/05/01 21:50:53
Done.
dschuyler
2015/05/01 21:50:54
That comment was odd - the array itself would not
| |
| 60 } | |
| 61 | |
| 62 - (CGFloat)tableView:(NSTableView*)tableView heightOfRow:(NSInteger)row { | |
| 63 return [[array_ objectAtIndex:row] cellSize].height; | |
| 64 } | |
| 65 | |
| 66 @end | |
| 67 | |
| 68 @interface OmniboxPopupTableView () | |
| 18 - (void)resetTrackingArea; | 69 - (void)resetTrackingArea; |
| 19 - (void)highlightRowAt:(NSInteger)rowIndex; | 70 - (void)highlightRowAt:(NSInteger)rowIndex; |
| 20 - (void)highlightRowUnder:(NSEvent*)theEvent; | 71 - (void)highlightRowUnder:(NSEvent*)theEvent; |
| 21 - (BOOL)selectCellForEvent:(NSEvent*)theEvent; | 72 - (BOOL)selectCellForEvent:(NSEvent*)theEvent; |
| 22 @end | 73 @end |
| 23 | 74 |
| 24 @implementation OmniboxPopupMatrix | 75 @implementation OmniboxPopupTableView |
| 25 | 76 |
| 26 - (id)initWithDelegate:(OmniboxPopupMatrixDelegate*)delegate { | 77 - (id)initWithDelegate:(OmniboxPopupTableDelegate*)delegate { |
| 27 if ((self = [super initWithFrame:NSZeroRect])) { | 78 if ((self = [super initWithFrame:NSZeroRect])) { |
| 28 delegate_ = delegate; | 79 delegate_ = delegate; |
| 29 [self setCellClass:[OmniboxPopupCell class]]; | 80 controller_ = [[OmniboxPopupTableController alloc] init]; |
| 81 | |
| 82 NSTableColumn* column = | |
|
Scott Hess - ex-Googler
2015/04/25 06:04:27
scoped_nsobject<>.
dschuyler
2015/05/01 21:50:54
Could I have done autorelease here instead of the
Scott Hess - ex-Googler
2015/05/04 19:40:20
scoped_nsobject<> means "Add a reference here, dro
| |
| 83 [[NSTableColumn alloc] initWithIdentifier:@"MainCell"]; | |
| 84 [self addTableColumn:column]; | |
| 85 | |
| 86 [self setDelegate:controller_]; | |
| 87 [self setDataSource:controller_]; | |
| 30 | 88 |
| 31 // Cells pack with no spacing. | 89 // Cells pack with no spacing. |
| 32 [self setIntercellSpacing:NSMakeSize(0.0, 0.0)]; | 90 [self setIntercellSpacing:NSMakeSize(0.0, 0.0)]; |
| 33 | 91 |
| 34 [self setDrawsBackground:YES]; | |
| 35 [self setBackgroundColor:[NSColor controlBackgroundColor]]; | 92 [self setBackgroundColor:[NSColor controlBackgroundColor]]; |
| 36 [self renewRows:0 columns:1]; | |
| 37 [self setAllowsEmptySelection:YES]; | 93 [self setAllowsEmptySelection:YES]; |
| 38 [self setMode:NSRadioModeMatrix]; | 94 [self deselectAll:self]; |
| 39 [self deselectAllCells]; | 95 [self setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone]; |
| 40 | 96 |
| 41 [self resetTrackingArea]; | 97 [self resetTrackingArea]; |
| 42 } | 98 } |
| 43 return self; | 99 return self; |
| 44 } | 100 } |
| 45 | 101 |
| 46 - (void)setDelegate:(OmniboxPopupMatrixDelegate*)delegate { | 102 - (void)setCppDelegate:(OmniboxPopupTableDelegate*)delegate { |
| 47 delegate_ = delegate; | 103 delegate_ = delegate; |
| 48 } | 104 } |
| 49 | 105 |
| 50 - (NSInteger)highlightedRow { | 106 - (NSInteger)highlightedRow { |
| 51 NSArray* cells = [self cells]; | 107 NSArray* cells = [controller_ getCellArray]; |
| 52 const NSUInteger count = [cells count]; | 108 const NSUInteger count = [cells count]; |
| 53 for(NSUInteger i = 0; i < count; ++i) { | 109 for(NSUInteger i = 0; i < count; ++i) { |
| 54 if ([[cells objectAtIndex:i] isHighlighted]) { | 110 if ([[cells objectAtIndex:i] isHighlighted]) { |
| 55 return i; | 111 return i; |
| 56 } | 112 } |
| 57 } | 113 } |
| 58 return -1; | 114 return -1; |
| 59 } | 115 } |
| 60 | 116 |
| 61 - (void)updateTrackingAreas { | 117 - (void)updateTrackingAreas { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 99 return; | 155 return; |
| 100 } | 156 } |
| 101 | 157 |
| 102 // -otherMouseDragged: should always have been called at this location, but | 158 // -otherMouseDragged: should always have been called at this location, but |
| 103 // make sure the user is getting the right feedback. | 159 // make sure the user is getting the right feedback. |
| 104 [self highlightRowUnder:theEvent]; | 160 [self highlightRowUnder:theEvent]; |
| 105 | 161 |
| 106 const NSInteger highlightedRow = [self highlightedRow]; | 162 const NSInteger highlightedRow = [self highlightedRow]; |
| 107 if (highlightedRow != -1) { | 163 if (highlightedRow != -1) { |
| 108 DCHECK(delegate_); | 164 DCHECK(delegate_); |
| 109 delegate_->OnMatrixRowMiddleClicked(self, highlightedRow); | 165 delegate_->OnTableRowMiddleClicked(self, highlightedRow); |
| 110 } | 166 } |
| 111 } | 167 } |
| 112 | 168 |
| 113 // Track the mouse until released, keeping the cell under the mouse selected. | 169 // Track the mouse until released, keeping the cell under the mouse selected. |
| 114 // If the mouse wanders off-view, revert to the originally-selected cell. If | 170 // If the mouse wanders off-view, revert to the originally-selected cell. If |
| 115 // the mouse is released over a cell, call the delegate to open the row's URL. | 171 // the mouse is released over a cell, call the delegate to open the row's URL. |
| 116 - (void)mouseDown:(NSEvent*)theEvent { | 172 - (void)mouseDown:(NSEvent*)theEvent { |
| 117 NSCell* selectedCell = [self selectedCell]; | 173 NSCell* selectedCell = [self selectedCell]; |
| 118 | 174 |
| 119 // Clear any existing highlight. | 175 // Clear any existing highlight. |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 134 } else { | 190 } else { |
| 135 const NSInteger selectedRow = [self selectedRow]; | 191 const NSInteger selectedRow = [self selectedRow]; |
| 136 | 192 |
| 137 // No row could be selected if the model failed to update. | 193 // No row could be selected if the model failed to update. |
| 138 if (selectedRow == -1) { | 194 if (selectedRow == -1) { |
| 139 NOTREACHED(); | 195 NOTREACHED(); |
| 140 return; | 196 return; |
| 141 } | 197 } |
| 142 | 198 |
| 143 DCHECK(delegate_); | 199 DCHECK(delegate_); |
| 144 delegate_->OnMatrixRowClicked(self, selectedRow); | 200 delegate_->OnTableRowClicked(self, selectedRow); |
| 145 } | 201 } |
| 146 } | 202 } |
| 147 | 203 |
| 204 - (void)setCellCount:(NSUInteger)count { | |
| 205 return [controller_ setCellCount:count]; | |
| 206 } | |
| 207 | |
| 208 - (OmniboxPopupCell*)cellAtRow:(NSUInteger)row { | |
| 209 NSArray* array = [controller_ getCellArray]; | |
| 210 if (row >= [array count]) | |
| 211 return nil; | |
| 212 return [array objectAtIndex:row]; | |
| 213 } | |
| 214 | |
| 148 - (void)resetTrackingArea { | 215 - (void)resetTrackingArea { |
| 149 if (trackingArea_.get()) | 216 if (trackingArea_.get()) |
| 150 [self removeTrackingArea:trackingArea_.get()]; | 217 [self removeTrackingArea:trackingArea_.get()]; |
| 151 | 218 |
| 152 trackingArea_.reset([[CrTrackingArea alloc] | 219 trackingArea_.reset([[CrTrackingArea alloc] |
| 153 initWithRect:[self frame] | 220 initWithRect:[self frame] |
| 154 options:NSTrackingMouseEnteredAndExited | | 221 options:NSTrackingMouseEnteredAndExited | |
| 155 NSTrackingMouseMoved | | 222 NSTrackingMouseMoved | |
| 156 NSTrackingActiveInActiveApp | | 223 NSTrackingActiveInActiveApp | |
| 157 NSTrackingInVisibleRect | 224 NSTrackingInVisibleRect |
| 158 owner:self | 225 owner:self |
| 159 userInfo:nil]); | 226 userInfo:nil]); |
| 160 [self addTrackingArea:trackingArea_.get()]; | 227 [self addTrackingArea:trackingArea_.get()]; |
| 161 } | 228 } |
| 162 | 229 |
| 163 - (void)highlightRowAt:(NSInteger)rowIndex { | 230 - (void)highlightRowAt:(NSInteger)rowIndex { |
| 164 // highlightCell will be nil if rowIndex is out of range, so no cell will be | 231 // highlightCell will be nil if rowIndex is out of range, so no cell will be |
| 165 // highlighted. | 232 // highlighted. |
|
Scott Hess - ex-Googler
2015/04/25 06:04:27
I think the out-of-range at this point is if the m
dschuyler
2015/05/01 21:50:53
This code has been reworked in the new patch.
| |
| 166 NSCell* highlightCell = [self cellAtRow:rowIndex column:0]; | 233 OmniboxPopupCell* highlightCell = [self cellAtRow:rowIndex]; |
| 234 if ([highlightCell isHighlighted]) | |
| 235 return; | |
| 167 | 236 |
| 168 for (NSCell* cell in [self cells]) { | 237 for (OmniboxPopupCell* cell in controller_.getCellArray) { |
|
Scott Hess - ex-Googler
2015/04/25 06:04:27
I have no particular preference WRT controller_.ge
| |
| 169 [cell setHighlighted:(cell == highlightCell)]; | 238 if ([cell isHighlighted] || cell == highlightCell) { |
| 239 [cell highlight:(cell == highlightCell) withFrame:_bounds inView:self]; | |
| 240 } | |
|
Scott Hess - ex-Googler
2015/04/25 06:04:27
I'm not really sure what this is saying. The prev
groby-ooo-7-16
2015/04/25 19:01:12
I'll re-ask the question I asked in my last commen
dschuyler
2015/05/01 21:50:54
Done.
dschuyler
2015/05/01 21:50:54
This code has been reworked in the new patch. It
| |
| 170 } | 241 } |
| 171 } | 242 } |
| 172 | 243 |
| 173 - (void)highlightRowUnder:(NSEvent*)theEvent { | 244 - (void)highlightRowUnder:(NSEvent*)theEvent { |
| 174 NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil]; | 245 NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil]; |
| 175 NSInteger row, column; | 246 [self highlightRowAt:[self rowAtPoint:point]]; |
| 176 if ([self getRow:&row column:&column forPoint:point]) { | |
| 177 [self highlightRowAt:row]; | |
| 178 } else { | |
| 179 [self highlightRowAt:-1]; | |
| 180 } | |
| 181 } | 247 } |
| 182 | 248 |
| 183 // Select cell under |theEvent|, returning YES if a selection is made. | 249 // Select cell under |theEvent|, returning YES if a selection is made. |
| 184 - (BOOL)selectCellForEvent:(NSEvent*)theEvent { | 250 - (BOOL)selectCellForEvent:(NSEvent*)theEvent { |
| 185 NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil]; | 251 NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil]; |
| 186 | 252 |
| 187 NSInteger row, column; | 253 NSInteger row = [self rowAtPoint:point]; |
| 188 if ([self getRow:&row column:&column forPoint:point]) { | 254 if (row != -1) { |
| 189 DCHECK_EQ(column, 0); | |
| 190 DCHECK(delegate_); | 255 DCHECK(delegate_); |
| 191 delegate_->OnMatrixRowSelected(self, row); | 256 delegate_->OnTableRowSelected(self, row); |
| 192 return YES; | 257 return YES; |
| 193 } | 258 } |
| 194 return NO; | 259 return NO; |
| 195 } | 260 } |
| 196 | 261 |
| 197 @end | 262 @end |
| OLD | NEW |