Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(212)

Side by Side Diff: chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.mm

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

Powered by Google App Engine
This is Rietveld 408576698