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 #include "base/mac/foundation_util.h" | |
| 8 #import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.h" | 9 #import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.h" |
| 9 | 10 |
| 10 namespace { | 11 namespace { |
| 11 | 12 |
| 12 // NSEvent -buttonNumber for middle mouse button. | 13 // NSEvent -buttonNumber for middle mouse button. |
| 13 const NSInteger kMiddleButtonNumber = 2; | 14 const NSInteger kMiddleButtonNumber = 2; |
| 14 | 15 |
| 15 } // namespace | 16 } // namespace |
| 16 | 17 |
| 17 @interface OmniboxPopupMatrix() | 18 @implementation OmniboxPopupTableController |
| 19 | |
| 20 - (id)init { | |
| 21 if ((self = [super init])) { | |
| 22 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
| |
| 23 hovered_index_ = -1; | |
| 24 } | |
| 25 return self; | |
| 26 } | |
| 27 | |
| 28 - (NSInteger)numberOfRowsInTableView:(NSTableView*)tableView { | |
| 29 return [array_ count]; | |
| 30 } | |
| 31 | |
| 32 - (id)tableView:(NSTableView*)tableView | |
| 33 objectValueForTableColumn:(NSTableColumn*)tableColumn | |
| 34 row:(NSInteger)rowIndex { | |
| 35 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.
| |
| 36 } | |
| 37 | |
| 38 - (void)tableView:(NSTableView*)aTableView | |
| 39 setObjectValue:(id)anObject | |
| 40 forTableColumn:(NSTableColumn*)aTableColumn | |
| 41 row:(NSInteger)rowIndex { | |
| 42 } | |
| 43 | |
| 44 - (void)tableView:(NSTableView*)tableView | |
| 45 willDisplayCell:(id)cell | |
| 46 forTableColumn:(NSTableColumn*)tableColumn | |
| 47 row:(NSInteger)rowIndex { | |
| 48 OmniboxPopupCell* popupCell = | |
| 49 base::mac::ObjCCastStrict<OmniboxPopupCell>(cell); | |
| 50 [popupCell setCellData:[array_ objectAtIndex:rowIndex]]; | |
| 51 [popupCell | |
| 52 setState:([tableView selectedRow] == rowIndex) ? NSOnState : NSOffState]; | |
| 53 [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
| |
| 54 withFrame:[tableView bounds] | |
| 55 inView:tableView]; | |
| 56 } | |
| 57 | |
| 58 - (NSInteger)highlightedRow { | |
| 59 return hovered_index_; | |
| 60 } | |
| 61 | |
| 62 - (void)highlightRowAt:(NSInteger)rowIndex withView:(NSView*)view { | |
| 63 hovered_index_ = rowIndex; | |
| 64 } | |
| 65 | |
| 66 - (void)setDataArray:(NSArray*)array { | |
| 67 hovered_index_ = -1; | |
| 68 array_.reset([array retain]); | |
| 69 } | |
| 70 | |
| 71 - (CGFloat)tableView:(NSTableView*)tableView heightOfRow:(NSInteger)row { | |
| 72 OmniboxPopupCellData* cellData = [array_ objectAtIndex:row]; | |
| 73 return [cellData rowHeight]; | |
| 74 } | |
| 75 | |
| 76 @end | |
| 77 | |
| 78 @interface OmniboxPopupMatrix () | |
| 18 - (void)resetTrackingArea; | 79 - (void)resetTrackingArea; |
| 19 - (void)highlightRowAt:(NSInteger)rowIndex; | |
| 20 - (void)highlightRowUnder:(NSEvent*)theEvent; | 80 - (void)highlightRowUnder:(NSEvent*)theEvent; |
| 21 - (BOOL)selectCellForEvent:(NSEvent*)theEvent; | 81 - (BOOL)selectCellForEvent:(NSEvent*)theEvent; |
| 22 @end | 82 @end |
| 23 | 83 |
| 24 @implementation OmniboxPopupMatrix | 84 @implementation OmniboxPopupMatrix |
| 25 | 85 |
| 26 - (id)initWithObserver:(OmniboxPopupMatrixObserver*)observer { | 86 - (id)initWithObserver:(OmniboxPopupMatrixObserver*)observer { |
| 27 if ((self = [super initWithFrame:NSZeroRect])) { | 87 if ((self = [super initWithFrame:NSZeroRect])) { |
| 28 observer_ = observer; | 88 observer_ = observer; |
| 29 [self setCellClass:[OmniboxPopupCell class]]; | 89 controller_.reset([[OmniboxPopupTableController alloc] init]); |
| 90 | |
| 91 base::scoped_nsobject<NSTableColumn> column( | |
| 92 [[NSTableColumn alloc] initWithIdentifier:@"MainCell"]); | |
| 93 base::scoped_nsobject<OmniboxPopupCell> columnCell( | |
| 94 [[OmniboxPopupCell alloc] init]); | |
| 95 [column setDataCell:columnCell]; | |
| 96 [self addTableColumn:column]; | |
| 97 | |
| 98 [self setDelegate:controller_]; | |
| 99 [self setDataSource:controller_]; | |
| 30 | 100 |
| 31 // Cells pack with no spacing. | 101 // Cells pack with no spacing. |
| 32 [self setIntercellSpacing:NSMakeSize(0.0, 0.0)]; | 102 [self setIntercellSpacing:NSMakeSize(0.0, 0.0)]; |
| 33 | 103 |
| 34 [self setDrawsBackground:YES]; | 104 [self setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone]; |
| 35 [self setBackgroundColor:[NSColor controlBackgroundColor]]; | 105 [self setBackgroundColor:[NSColor controlBackgroundColor]]; |
| 36 [self renewRows:0 columns:1]; | |
| 37 [self setAllowsEmptySelection:YES]; | 106 [self setAllowsEmptySelection:YES]; |
| 38 [self setMode:NSRadioModeMatrix]; | 107 [self deselectAll:self]; |
| 39 [self deselectAllCells]; | |
| 40 | 108 |
| 41 [self resetTrackingArea]; | 109 [self resetTrackingArea]; |
| 42 } | 110 } |
| 43 return self; | 111 return self; |
| 44 } | 112 } |
| 45 | 113 |
| 46 - (void)setObserver:(OmniboxPopupMatrixObserver*)observer { | 114 - (void)setObserver:(OmniboxPopupMatrixObserver*)observer { |
| 47 observer_ = observer; | 115 observer_ = observer; |
| 48 } | 116 } |
| 49 | 117 |
| 50 - (NSInteger)highlightedRow { | |
| 51 NSArray* cells = [self cells]; | |
| 52 const NSUInteger count = [cells count]; | |
| 53 for(NSUInteger i = 0; i < count; ++i) { | |
| 54 if ([[cells objectAtIndex:i] isHighlighted]) { | |
| 55 return i; | |
| 56 } | |
| 57 } | |
| 58 return -1; | |
| 59 } | |
| 60 | |
| 61 - (void)updateTrackingAreas { | 118 - (void)updateTrackingAreas { |
| 62 [self resetTrackingArea]; | 119 [self resetTrackingArea]; |
| 63 [super updateTrackingAreas]; | 120 [super updateTrackingAreas]; |
| 64 } | 121 } |
| 65 | 122 |
| 66 // Callbacks from tracking area. | 123 // Callbacks from tracking area. |
| 67 - (void)mouseEntered:(NSEvent*)theEvent { | 124 - (void)mouseEntered:(NSEvent*)theEvent { |
| 68 [self highlightRowUnder:theEvent]; | 125 [self highlightRowUnder:theEvent]; |
| 69 } | 126 } |
| 70 | 127 |
| 71 - (void)mouseMoved:(NSEvent*)theEvent { | 128 - (void)mouseMoved:(NSEvent*)theEvent { |
| 72 [self highlightRowUnder:theEvent]; | 129 [self highlightRowUnder:theEvent]; |
| 73 } | 130 } |
| 74 | 131 |
| 75 - (void)mouseExited:(NSEvent*)theEvent { | 132 - (void)mouseExited:(NSEvent*)theEvent { |
| 76 [self highlightRowAt:-1]; | 133 [controller_ highlightRowAt:-1 withView:self]; |
| 77 } | 134 } |
| 78 | 135 |
| 79 // The tracking area events aren't forwarded during a drag, so handle | 136 // The tracking area events aren't forwarded during a drag, so handle |
| 80 // highlighting manually for middle-click and middle-drag. | 137 // highlighting manually for middle-click and middle-drag. |
| 81 - (void)otherMouseDown:(NSEvent*)theEvent { | 138 - (void)otherMouseDown:(NSEvent*)theEvent { |
| 82 if ([theEvent buttonNumber] == kMiddleButtonNumber) { | 139 if ([theEvent buttonNumber] == kMiddleButtonNumber) { |
| 83 [self highlightRowUnder:theEvent]; | 140 [self highlightRowUnder:theEvent]; |
| 84 } | 141 } |
| 85 [super otherMouseDown:theEvent]; | 142 [super otherMouseDown:theEvent]; |
| 86 } | 143 } |
| 87 | 144 |
| 88 - (void)otherMouseDragged:(NSEvent*)theEvent { | 145 - (void)otherMouseDragged:(NSEvent*)theEvent { |
| 89 if ([theEvent buttonNumber] == kMiddleButtonNumber) { | 146 if ([theEvent buttonNumber] == kMiddleButtonNumber) { |
| 90 [self highlightRowUnder:theEvent]; | 147 [self highlightRowUnder:theEvent]; |
| 91 } | 148 } |
| 92 [super otherMouseDragged:theEvent]; | 149 [super otherMouseDragged:theEvent]; |
| 93 } | 150 } |
| 94 | 151 |
| 95 - (void)otherMouseUp:(NSEvent*)theEvent { | 152 - (void)otherMouseUp:(NSEvent*)theEvent { |
| 96 // Only intercept middle button. | 153 // Only intercept middle button. |
| 97 if ([theEvent buttonNumber] != kMiddleButtonNumber) { | 154 if ([theEvent buttonNumber] != kMiddleButtonNumber) { |
| 98 [super otherMouseUp:theEvent]; | 155 [super otherMouseUp:theEvent]; |
| 99 return; | 156 return; |
| 100 } | 157 } |
| 101 | 158 |
| 102 // -otherMouseDragged: should always have been called at this location, but | 159 // -otherMouseDragged: should always have been called at this location, but |
| 103 // make sure the user is getting the right feedback. | 160 // make sure the user is getting the right feedback. |
| 104 [self highlightRowUnder:theEvent]; | 161 [self highlightRowUnder:theEvent]; |
| 105 | 162 |
| 106 const NSInteger highlightedRow = [self highlightedRow]; | 163 const NSInteger highlightedRow = [controller_ highlightedRow]; |
| 107 if (highlightedRow != -1) { | 164 if (highlightedRow != -1) { |
| 108 DCHECK(observer_); | 165 DCHECK(observer_); |
| 109 observer_->OnMatrixRowMiddleClicked(self, highlightedRow); | 166 observer_->OnMatrixRowMiddleClicked(self, highlightedRow); |
| 110 } | 167 } |
| 111 } | 168 } |
| 112 | 169 |
| 113 // Track the mouse until released, keeping the cell under the mouse selected. | 170 // 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 | 171 // 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. | 172 // the mouse is released over a cell, call the delegate to open the row's URL. |
| 116 - (void)mouseDown:(NSEvent*)theEvent { | 173 - (void)mouseDown:(NSEvent*)theEvent { |
| 117 NSCell* selectedCell = [self selectedCell]; | 174 NSCell* selectedCell = [self selectedCell]; |
| 118 | 175 |
| 119 // Clear any existing highlight. | 176 // Clear any existing highlight. |
| 120 [self highlightRowAt:-1]; | 177 [controller_ highlightRowAt:-1 withView:self]; |
| 121 | 178 |
| 122 do { | 179 do { |
| 123 if (![self selectCellForEvent:theEvent]) { | 180 if (![self selectCellForEvent:theEvent]) { |
| 124 [self selectCell:selectedCell]; | 181 [self selectCell:selectedCell]; |
| 125 } | 182 } |
| 126 | 183 |
| 127 const NSUInteger mask = NSLeftMouseUpMask | NSLeftMouseDraggedMask; | 184 const NSUInteger mask = NSLeftMouseUpMask | NSLeftMouseDraggedMask; |
| 128 theEvent = [[self window] nextEventMatchingMask:mask]; | 185 theEvent = [[self window] nextEventMatchingMask:mask]; |
| 129 } while ([theEvent type] == NSLeftMouseDragged); | 186 } while ([theEvent type] == NSLeftMouseDragged); |
| 130 | 187 |
| 131 // Do not message the delegate if released outside view. | 188 // Do not message the delegate if released outside view. |
| 132 if (![self selectCellForEvent:theEvent]) { | 189 if (![self selectCellForEvent:theEvent]) { |
| 133 [self selectCell:selectedCell]; | 190 [self selectCell:selectedCell]; |
| 134 } else { | 191 } else { |
| 135 const NSInteger selectedRow = [self selectedRow]; | 192 const NSInteger selectedRow = [self selectedRow]; |
| 136 | 193 |
| 137 // No row could be selected if the model failed to update. | 194 // No row could be selected if the model failed to update. |
| 138 if (selectedRow == -1) { | 195 if (selectedRow == -1) { |
| 139 NOTREACHED(); | 196 NOTREACHED(); |
| 140 return; | 197 return; |
| 141 } | 198 } |
| 142 | 199 |
| 143 DCHECK(observer_); | 200 DCHECK(observer_); |
| 144 observer_->OnMatrixRowClicked(self, selectedRow); | 201 observer_->OnMatrixRowClicked(self, selectedRow); |
| 145 } | 202 } |
| 146 } | 203 } |
| 147 | 204 |
| 205 - (void)selectRowAt:(NSInteger)rowIndex { | |
| 206 NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:rowIndex]; | |
| 207 [self selectRowIndexes:indexSet byExtendingSelection:NO]; | |
| 208 } | |
| 209 | |
| 210 - (NSInteger)highlightedRow { | |
| 211 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
| |
| 212 } | |
| 213 | |
| 214 - (void)setDataArray:(NSArray*)array { | |
| 215 [controller_ setDataArray:array]; | |
| 216 } | |
| 217 | |
| 148 - (void)resetTrackingArea { | 218 - (void)resetTrackingArea { |
| 149 if (trackingArea_.get()) | 219 if (trackingArea_.get()) |
| 150 [self removeTrackingArea:trackingArea_.get()]; | 220 [self removeTrackingArea:trackingArea_.get()]; |
| 151 | 221 |
| 152 trackingArea_.reset([[CrTrackingArea alloc] | 222 trackingArea_.reset([[CrTrackingArea alloc] |
| 153 initWithRect:[self frame] | 223 initWithRect:[self frame] |
| 154 options:NSTrackingMouseEnteredAndExited | | 224 options:NSTrackingMouseEnteredAndExited | |
| 155 NSTrackingMouseMoved | | 225 NSTrackingMouseMoved | |
| 156 NSTrackingActiveInActiveApp | | 226 NSTrackingActiveInActiveApp | |
| 157 NSTrackingInVisibleRect | 227 NSTrackingInVisibleRect |
| 158 owner:self | 228 owner:self |
| 159 userInfo:nil]); | 229 userInfo:nil]); |
| 160 [self addTrackingArea:trackingArea_.get()]; | 230 [self addTrackingArea:trackingArea_.get()]; |
| 161 } | 231 } |
| 162 | 232 |
| 163 - (void)highlightRowAt:(NSInteger)rowIndex { | |
| 164 // highlightCell will be nil if rowIndex is out of range, so no cell will be | |
| 165 // highlighted. | |
| 166 NSCell* highlightCell = [self cellAtRow:rowIndex column:0]; | |
| 167 | |
| 168 for (NSCell* cell in [self cells]) { | |
| 169 [cell setHighlighted:(cell == highlightCell)]; | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 - (void)highlightRowUnder:(NSEvent*)theEvent { | 233 - (void)highlightRowUnder:(NSEvent*)theEvent { |
| 174 NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil]; | 234 NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil]; |
| 175 NSInteger row, column; | 235 NSInteger oldRow = [controller_ highlightedRow]; |
| 176 if ([self getRow:&row column:&column forPoint:point]) { | 236 NSInteger newRow = [self rowAtPoint:point]; |
| 177 [self highlightRowAt:row]; | 237 if (oldRow != newRow) { |
| 178 } else { | 238 [controller_ highlightRowAt:newRow withView:self]; |
| 179 [self highlightRowAt:-1]; | 239 [self setNeedsDisplayInRect:[self rectOfRow:oldRow]]; |
| 240 [self setNeedsDisplayInRect:[self rectOfRow:newRow]]; | |
| 180 } | 241 } |
| 181 } | 242 } |
| 182 | 243 |
| 183 // Select cell under |theEvent|, returning YES if a selection is made. | |
| 184 - (BOOL)selectCellForEvent:(NSEvent*)theEvent { | 244 - (BOOL)selectCellForEvent:(NSEvent*)theEvent { |
| 185 NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil]; | 245 NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil]; |
| 186 | 246 |
| 187 NSInteger row, column; | 247 NSInteger row = [self rowAtPoint:point]; |
| 188 if ([self getRow:&row column:&column forPoint:point]) { | 248 [self selectRowAt:row]; |
| 189 DCHECK_EQ(column, 0); | 249 if (row != -1) { |
| 190 DCHECK(observer_); | 250 DCHECK(observer_); |
| 191 observer_->OnMatrixRowSelected(self, row); | 251 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
| |
| 192 return YES; | 252 return YES; |
| 193 } | 253 } |
| 194 return NO; | 254 return NO; |
| 195 } | 255 } |
| 196 | 256 |
| 197 @end | 257 @end |
| OLD | NEW |