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