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