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

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: Fix for unused var 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 @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 {
Scott Hess - ex-Googler 2015/06/12 22:21:59 I think if they fit, these should align the colon,
dschuyler 2015/06/12 23:07:54 This is formatted the way cl format wants to do it
Scott Hess - ex-Googler 2015/06/12 23:18:13 Acknowledged.
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 ()
18 - (void)resetTrackingArea; 107 - (void)resetTrackingArea;
19 - (void)highlightRowAt:(NSInteger)rowIndex;
20 - (void)highlightRowUnder:(NSEvent*)theEvent; 108 - (void)highlightRowUnder:(NSEvent*)theEvent;
21 - (BOOL)selectCellForEvent:(NSEvent*)theEvent; 109 - (BOOL)selectCellForEvent:(NSEvent*)theEvent;
22 @end 110 @end
23 111
24 @implementation OmniboxPopupMatrix 112 @implementation OmniboxPopupMatrix
25 113
114 @synthesize separator = separator_;
115 @synthesize maxMatchContentsWidth = maxMatchContentsWidth_;
116
26 - (instancetype)initWithObserver:(OmniboxPopupMatrixObserver*)observer { 117 - (instancetype)initWithObserver:(OmniboxPopupMatrixObserver*)observer {
27 if ((self = [super initWithFrame:NSZeroRect])) { 118 if ((self = [super initWithFrame:NSZeroRect])) {
28 observer_ = observer; 119 observer_ = observer;
29 [self setCellClass:[OmniboxPopupCell class]]; 120
121 base::scoped_nsobject<NSTableColumn> column(
122 [[NSTableColumn alloc] initWithIdentifier:@"MainColumn"]);
123 [column setDataCell:[[OmniboxPopupCell alloc] init]];
Scott Hess - ex-Googler 2015/06/12 22:21:59 I really really wish that the documentation was mo
groby-ooo-7-16 2015/06/12 22:43:01 The docs say "@property (strong) id dataCell". It
Scott Hess - ex-Googler 2015/06/12 22:49:20 I think that means it takes a reference under non-
groby-ooo-7-16 2015/06/12 22:58:46 Gah. Of course. Sorry, Friday-afternoon-CL-fatigue
dschuyler 2015/06/12 23:07:54 Does that mean that this should be an autorelease?
Scott Hess - ex-Googler 2015/06/12 23:18:13 Yep.
dschuyler 2015/06/13 00:11:04 Done.
124 [self addTableColumn:column];
30 125
31 // Cells pack with no spacing. 126 // Cells pack with no spacing.
32 [self setIntercellSpacing:NSMakeSize(0.0, 0.0)]; 127 [self setIntercellSpacing:NSMakeSize(0.0, 0.0)];
33 128
34 [self setDrawsBackground:YES]; 129 [self setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone];
35 [self setBackgroundColor:[NSColor controlBackgroundColor]]; 130 [self setBackgroundColor:[NSColor controlBackgroundColor]];
36 [self renewRows:0 columns:1];
37 [self setAllowsEmptySelection:YES]; 131 [self setAllowsEmptySelection:YES];
38 [self setMode:NSRadioModeMatrix]; 132 [self deselectAll:self];
39 [self deselectAllCells];
40 133
41 [self resetTrackingArea]; 134 [self resetTrackingArea];
42 } 135 }
43 return self; 136 return self;
44 } 137 }
45 138
139 - (OmniboxPopupTableController*)controller {
140 return base::mac::ObjCCastStrict<OmniboxPopupTableController>(
141 [self delegate]);
142 }
143
46 - (void)setObserver:(OmniboxPopupMatrixObserver*)observer { 144 - (void)setObserver:(OmniboxPopupMatrixObserver*)observer {
47 observer_ = observer; 145 observer_ = observer;
48 } 146 }
49 147
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 { 148 - (void)updateTrackingAreas {
62 [self resetTrackingArea]; 149 [self resetTrackingArea];
63 [super updateTrackingAreas]; 150 [super updateTrackingAreas];
64 } 151 }
65 152
66 // Callbacks from tracking area. 153 // Callbacks from tracking area.
67 - (void)mouseEntered:(NSEvent*)theEvent { 154 - (void)mouseEntered:(NSEvent*)theEvent {
68 [self highlightRowUnder:theEvent]; 155 [self highlightRowUnder:theEvent];
69 } 156 }
70 157
71 - (void)mouseMoved:(NSEvent*)theEvent { 158 - (void)mouseMoved:(NSEvent*)theEvent {
72 [self highlightRowUnder:theEvent]; 159 [self highlightRowUnder:theEvent];
73 } 160 }
74 161
75 - (void)mouseExited:(NSEvent*)theEvent { 162 - (void)mouseExited:(NSEvent*)theEvent {
76 [self highlightRowAt:-1]; 163 [[self controller] setHighlightedRow:-1];
77 } 164 }
78 165
79 // The tracking area events aren't forwarded during a drag, so handle 166 // The tracking area events aren't forwarded during a drag, so handle
80 // highlighting manually for middle-click and middle-drag. 167 // highlighting manually for middle-click and middle-drag.
81 - (void)otherMouseDown:(NSEvent*)theEvent { 168 - (void)otherMouseDown:(NSEvent*)theEvent {
82 if ([theEvent buttonNumber] == kMiddleButtonNumber) { 169 if ([theEvent buttonNumber] == kMiddleButtonNumber) {
83 [self highlightRowUnder:theEvent]; 170 [self highlightRowUnder:theEvent];
84 } 171 }
85 [super otherMouseDown:theEvent]; 172 [super otherMouseDown:theEvent];
86 } 173 }
87 174
88 - (void)otherMouseDragged:(NSEvent*)theEvent { 175 - (void)otherMouseDragged:(NSEvent*)theEvent {
89 if ([theEvent buttonNumber] == kMiddleButtonNumber) { 176 if ([theEvent buttonNumber] == kMiddleButtonNumber) {
90 [self highlightRowUnder:theEvent]; 177 [self highlightRowUnder:theEvent];
91 } 178 }
92 [super otherMouseDragged:theEvent]; 179 [super otherMouseDragged:theEvent];
93 } 180 }
94 181
95 - (void)otherMouseUp:(NSEvent*)theEvent { 182 - (void)otherMouseUp:(NSEvent*)theEvent {
96 // Only intercept middle button. 183 // Only intercept middle button.
97 if ([theEvent buttonNumber] != kMiddleButtonNumber) { 184 if ([theEvent buttonNumber] != kMiddleButtonNumber) {
98 [super otherMouseUp:theEvent]; 185 [super otherMouseUp:theEvent];
99 return; 186 return;
100 } 187 }
101 188
102 // -otherMouseDragged: should always have been called at this location, but 189 // -otherMouseDragged: should always have been called at this location, but
103 // make sure the user is getting the right feedback. 190 // make sure the user is getting the right feedback.
104 [self highlightRowUnder:theEvent]; 191 [self highlightRowUnder:theEvent];
105 192
106 const NSInteger highlightedRow = [self highlightedRow]; 193 const NSInteger highlightedRow = [[self controller] highlightedRow];
107 if (highlightedRow != -1) { 194 if (highlightedRow != -1) {
108 DCHECK(observer_); 195 DCHECK(observer_);
109 observer_->OnMatrixRowMiddleClicked(self, highlightedRow); 196 observer_->OnMatrixRowMiddleClicked(self, highlightedRow);
110 } 197 }
111 } 198 }
112 199
113 // Track the mouse until released, keeping the cell under the mouse selected. 200 // 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 201 // 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. 202 // the mouse is released over a cell, call the delegate to open the row's URL.
116 - (void)mouseDown:(NSEvent*)theEvent { 203 - (void)mouseDown:(NSEvent*)theEvent {
117 NSCell* selectedCell = [self selectedCell]; 204 NSCell* selectedCell = [self selectedCell];
118 205
119 // Clear any existing highlight. 206 // Clear any existing highlight.
120 [self highlightRowAt:-1]; 207 [[self controller] setHighlightedRow:-1];
121 208
122 do { 209 do {
123 if (![self selectCellForEvent:theEvent]) { 210 if (![self selectCellForEvent:theEvent]) {
124 [self selectCell:selectedCell]; 211 [self selectCell:selectedCell];
125 } 212 }
126 213
127 const NSUInteger mask = NSLeftMouseUpMask | NSLeftMouseDraggedMask; 214 const NSUInteger mask = NSLeftMouseUpMask | NSLeftMouseDraggedMask;
128 theEvent = [[self window] nextEventMatchingMask:mask]; 215 theEvent = [[self window] nextEventMatchingMask:mask];
129 } while ([theEvent type] == NSLeftMouseDragged); 216 } while ([theEvent type] == NSLeftMouseDragged);
130 217
131 // Do not message the delegate if released outside view. 218 // Do not message the delegate if released outside view.
132 if (![self selectCellForEvent:theEvent]) { 219 if (![self selectCellForEvent:theEvent]) {
133 [self selectCell:selectedCell]; 220 [self selectCell:selectedCell];
134 } else { 221 } else {
135 const NSInteger selectedRow = [self selectedRow]; 222 const NSInteger selectedRow = [self selectedRow];
136 223
137 // No row could be selected if the model failed to update. 224 // No row could be selected if the model failed to update.
138 if (selectedRow == -1) { 225 if (selectedRow == -1) {
139 NOTREACHED(); 226 NOTREACHED();
140 return; 227 return;
141 } 228 }
142 229
143 DCHECK(observer_); 230 DCHECK(observer_);
144 observer_->OnMatrixRowClicked(self, selectedRow); 231 observer_->OnMatrixRowClicked(self, selectedRow);
145 } 232 }
146 } 233 }
147 234
235 - (void)selectRowIndex:(NSInteger)rowIndex {
236 NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:rowIndex];
237 [self selectRowIndexes:indexSet byExtendingSelection:NO];
238 }
239
240 - (NSInteger)highlightedRow {
241 return [[self controller] highlightedRow];
242 }
243
244 - (void)setController:(OmniboxPopupTableController*)controller {
245 matrixController_.reset([controller retain]);
246 [self setDelegate:controller];
247 [self setDataSource:controller];
248 [self reloadData];
249 }
250
148 - (void)resetTrackingArea { 251 - (void)resetTrackingArea {
149 if (trackingArea_.get()) 252 if (trackingArea_.get())
150 [self removeTrackingArea:trackingArea_.get()]; 253 [self removeTrackingArea:trackingArea_.get()];
151 254
152 trackingArea_.reset([[CrTrackingArea alloc] 255 trackingArea_.reset([[CrTrackingArea alloc]
153 initWithRect:[self frame] 256 initWithRect:[self frame]
154 options:NSTrackingMouseEnteredAndExited | 257 options:NSTrackingMouseEnteredAndExited |
155 NSTrackingMouseMoved | 258 NSTrackingMouseMoved |
156 NSTrackingActiveInActiveApp | 259 NSTrackingActiveInActiveApp |
157 NSTrackingInVisibleRect 260 NSTrackingInVisibleRect
158 owner:self 261 owner:self
159 userInfo:nil]); 262 userInfo:nil]);
160 [self addTrackingArea:trackingArea_.get()]; 263 [self addTrackingArea:trackingArea_.get()];
161 } 264 }
162 265
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 { 266 - (void)highlightRowUnder:(NSEvent*)theEvent {
174 NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil]; 267 NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil];
175 NSInteger row, column; 268 NSInteger oldRow = [[self controller] highlightedRow];
176 if ([self getRow:&row column:&column forPoint:point]) { 269 NSInteger newRow = [self rowAtPoint:point];
177 [self highlightRowAt:row]; 270 if (oldRow != newRow) {
178 } else { 271 [[self controller] setHighlightedRow:newRow];
179 [self highlightRowAt:-1]; 272 [self setNeedsDisplayInRect:[self rectOfRow:oldRow]];
273 [self setNeedsDisplayInRect:[self rectOfRow:newRow]];
180 } 274 }
181 } 275 }
182 276
183 // Select cell under |theEvent|, returning YES if a selection is made.
184 - (BOOL)selectCellForEvent:(NSEvent*)theEvent { 277 - (BOOL)selectCellForEvent:(NSEvent*)theEvent {
185 NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil]; 278 NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil];
186 279
187 NSInteger row, column; 280 NSInteger row = [self rowAtPoint:point];
188 if ([self getRow:&row column:&column forPoint:point]) { 281 [self selectRowIndex:row];
189 DCHECK_EQ(column, 0); 282 if (row != -1) {
190 DCHECK(observer_); 283 DCHECK(observer_);
191 observer_->OnMatrixRowSelected(self, row); 284 observer_->OnMatrixRowSelected(self, row);
192 return YES; 285 return YES;
193 } 286 }
194 return NO; 287 return NO;
195 } 288 }
196 289
197 @end 290 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698