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

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

Powered by Google App Engine
This is Rietveld 408576698