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_ = [[NSMutableArray alloc] init]; | |
22 } | |
23 return self; | |
24 } | |
25 | |
26 - (NSInteger)numberOfRowsInTableView:(NSTableView*)tableView { | |
27 if (!array_) | |
dschuyler
2015/04/24 18:43:08
Checking whether the member is initialized comes f
groby-ooo-7-16
2015/04/24 20:11:16
I'm confused. If the member is not initialized, th
dschuyler
2015/04/25 01:05:42
Ah, so I was trying to do right by the 'Important'
| |
28 return 0; | |
29 return [array_ count]; | |
30 } | |
31 | |
32 - (id)tableView:(NSTableView*)aTableView | |
33 objectValueForTableColumn:(NSTableColumn*)aTableColumn | |
34 row:(NSInteger)rowIndex { | |
35 // Returning the cell allows selection highlighting to work properly. | |
36 return [array_ objectAtIndex:rowIndex]; | |
37 } | |
38 | |
39 - (NSCell*)tableView:(NSTableView*)tableView | |
40 dataCellForTableColumn:(NSTableColumn*)tableColumn | |
41 row:(NSInteger)row { | |
42 // Returning the cell populates data in the table. | |
43 return [array_ objectAtIndex:row]; | |
44 } | |
45 | |
46 - (void)setCellCount:(NSInteger)count { | |
47 // Add more. | |
48 while (count >= (NSInteger)[array_ count]) { | |
49 [array_ addObject:[[OmniboxPopupCell alloc] init]]; | |
groby-ooo-7-16
2015/04/24 20:11:16
This leaks. (addObject retains, alloc retains)
dschuyler
2015/04/25 01:05:43
Is autorelease the right thing to do here?
Done.
groby-ooo-7-16
2015/04/25 01:31:11
Yes, it is :)
Normal pattern for classes is to h
dschuyler
2015/05/01 21:50:53
Acknowledged.
| |
50 } | |
51 // Remove excess. | |
52 while (count < (NSInteger)[array_ count]) { | |
groby-ooo-7-16
2015/04/24 20:11:16
Why the cast?
Also, why not
if (count < [array_ c
dschuyler
2015/04/25 01:05:43
It's a compile error to compare signed and unsigne
groby-ooo-7-16
2015/04/25 01:31:11
Excellent. Also, when you cast: Please always use
dschuyler
2015/05/01 21:50:53
Acknowledged.
| |
53 [array_ removeLastObject]; | |
54 } | |
55 DCHECK_EQ(count, (NSInteger)[array_ count]); | |
56 // Setup defaults. | |
groby-ooo-7-16
2015/04/24 20:11:16
That comment is rather meaningless - what defaults
dschuyler
2015/04/25 01:05:42
Done.
| |
57 if (count) | |
58 [[array_ objectAtIndex:0] setState:NSOnState]; | |
59 } | |
60 | |
61 - (NSArray*)getCellArray { | |
62 return array_; | |
63 } | |
64 | |
65 - (CGFloat)tableView:(NSTableView*)tableView heightOfRow:(NSInteger)row { | |
66 return [[array_ objectAtIndex:row] cellSize].height; | |
67 } | |
68 | |
69 @end | |
70 | |
71 @interface OmniboxPopupTableView () | |
18 - (void)resetTrackingArea; | 72 - (void)resetTrackingArea; |
19 - (void)highlightRowAt:(NSInteger)rowIndex; | 73 - (void)highlightRowAt:(NSInteger)rowIndex; |
20 - (void)highlightRowUnder:(NSEvent*)theEvent; | 74 - (void)highlightRowUnder:(NSEvent*)theEvent; |
21 - (BOOL)selectCellForEvent:(NSEvent*)theEvent; | 75 - (BOOL)selectCellForEvent:(NSEvent*)theEvent; |
22 @end | 76 @end |
23 | 77 |
24 @implementation OmniboxPopupMatrix | 78 @implementation OmniboxPopupTableView |
25 | 79 |
26 - (id)initWithDelegate:(OmniboxPopupMatrixDelegate*)delegate { | 80 - (id)initWithDelegate:(OmniboxPopupTableDelegate*)delegate { |
27 if ((self = [super initWithFrame:NSZeroRect])) { | 81 if ((self = [super initWithFrame:NSZeroRect])) { |
28 delegate_ = delegate; | 82 delegate_ = delegate; |
29 [self setCellClass:[OmniboxPopupCell class]]; | 83 controller_ = [[OmniboxPopupTableController alloc] init]; |
84 | |
85 NSTableColumn* column = | |
86 [[NSTableColumn alloc] initWithIdentifier:@"MainCell"]; | |
87 [self addTableColumn:column]; | |
88 | |
89 [self setDelegate:controller_]; | |
90 [self setDataSource:controller_]; | |
30 | 91 |
31 // Cells pack with no spacing. | 92 // Cells pack with no spacing. |
32 [self setIntercellSpacing:NSMakeSize(0.0, 0.0)]; | 93 [self setIntercellSpacing:NSMakeSize(0.0, 0.0)]; |
33 | 94 |
34 [self setDrawsBackground:YES]; | |
35 [self setBackgroundColor:[NSColor controlBackgroundColor]]; | 95 [self setBackgroundColor:[NSColor controlBackgroundColor]]; |
36 [self renewRows:0 columns:1]; | |
dschuyler
2015/04/24 18:43:08
The initial column is now added in a different fun
| |
37 [self setAllowsEmptySelection:YES]; | 96 [self setAllowsEmptySelection:YES]; |
38 [self setMode:NSRadioModeMatrix]; | 97 [self deselectAll:self]; |
dschuyler
2015/04/24 18:43:08
There doesn't seem to be an equivalent for this co
| |
39 [self deselectAllCells]; | 98 [self setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone]; |
40 | 99 |
41 [self resetTrackingArea]; | 100 [self resetTrackingArea]; |
42 } | 101 } |
43 return self; | 102 return self; |
44 } | 103 } |
45 | 104 |
46 - (void)setDelegate:(OmniboxPopupMatrixDelegate*)delegate { | 105 - (void)setCppDelegate:(OmniboxPopupTableDelegate*)delegate { |
47 delegate_ = delegate; | 106 delegate_ = delegate; |
48 } | 107 } |
49 | 108 |
50 - (NSInteger)highlightedRow { | 109 - (NSInteger)highlightedRow { |
51 NSArray* cells = [self cells]; | 110 NSArray* cells = [controller_ getCellArray]; |
52 const NSUInteger count = [cells count]; | 111 const NSUInteger count = [cells count]; |
53 for(NSUInteger i = 0; i < count; ++i) { | 112 for(NSUInteger i = 0; i < count; ++i) { |
54 if ([[cells objectAtIndex:i] isHighlighted]) { | 113 if ([[cells objectAtIndex:i] isHighlighted]) { |
55 return i; | 114 return i; |
56 } | 115 } |
57 } | 116 } |
58 return -1; | 117 return -1; |
59 } | 118 } |
60 | 119 |
61 - (void)updateTrackingAreas { | 120 - (void)updateTrackingAreas { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
99 return; | 158 return; |
100 } | 159 } |
101 | 160 |
102 // -otherMouseDragged: should always have been called at this location, but | 161 // -otherMouseDragged: should always have been called at this location, but |
103 // make sure the user is getting the right feedback. | 162 // make sure the user is getting the right feedback. |
104 [self highlightRowUnder:theEvent]; | 163 [self highlightRowUnder:theEvent]; |
105 | 164 |
106 const NSInteger highlightedRow = [self highlightedRow]; | 165 const NSInteger highlightedRow = [self highlightedRow]; |
107 if (highlightedRow != -1) { | 166 if (highlightedRow != -1) { |
108 DCHECK(delegate_); | 167 DCHECK(delegate_); |
109 delegate_->OnMatrixRowMiddleClicked(self, highlightedRow); | 168 delegate_->OnTableRowMiddleClicked(self, highlightedRow); |
110 } | 169 } |
111 } | 170 } |
112 | 171 |
113 // Track the mouse until released, keeping the cell under the mouse selected. | 172 // 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 | 173 // 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. | 174 // the mouse is released over a cell, call the delegate to open the row's URL. |
116 - (void)mouseDown:(NSEvent*)theEvent { | 175 - (void)mouseDown:(NSEvent*)theEvent { |
117 NSCell* selectedCell = [self selectedCell]; | 176 NSCell* selectedCell = [self selectedCell]; |
118 | 177 |
119 // Clear any existing highlight. | 178 // Clear any existing highlight. |
(...skipping 14 matching lines...) Expand all Loading... | |
134 } else { | 193 } else { |
135 const NSInteger selectedRow = [self selectedRow]; | 194 const NSInteger selectedRow = [self selectedRow]; |
136 | 195 |
137 // No row could be selected if the model failed to update. | 196 // No row could be selected if the model failed to update. |
138 if (selectedRow == -1) { | 197 if (selectedRow == -1) { |
139 NOTREACHED(); | 198 NOTREACHED(); |
140 return; | 199 return; |
141 } | 200 } |
142 | 201 |
143 DCHECK(delegate_); | 202 DCHECK(delegate_); |
144 delegate_->OnMatrixRowClicked(self, selectedRow); | 203 delegate_->OnTableRowClicked(self, selectedRow); |
145 } | 204 } |
146 } | 205 } |
147 | 206 |
207 - (void)setCellCount:(NSInteger)count { | |
208 return [controller_ setCellCount:count]; | |
dschuyler
2015/04/24 18:43:08
I'm torn on whether a wrapper call like this is go
groby-ooo-7-16
2015/04/24 20:11:17
It is :) (Long-form argument at http://en.wikipedi
dschuyler
2015/04/25 01:05:43
Acknowledged.
| |
209 } | |
210 | |
211 - (OmniboxPopupCell*)cellAtRow:(NSInteger)row { | |
212 NSArray* array = [controller_ getCellArray]; | |
213 if (row < 0 || row >= (NSInteger)[array count]) | |
dschuyler
2015/04/24 18:43:08
row == -1 does happen normally. The high guard is
groby-ooo-7-16
2015/04/24 20:11:17
That kind of screams for an NSUInteger
dschuyler
2015/04/25 01:05:43
I heard that screaming as well, though I thought I
groby-ooo-7-16
2015/04/25 01:31:11
Sigh. It's my old habits. We actually *shouldn't*
| |
214 return nil; | |
215 return [array objectAtIndex:row]; | |
216 } | |
217 | |
148 - (void)resetTrackingArea { | 218 - (void)resetTrackingArea { |
149 if (trackingArea_.get()) | 219 if (trackingArea_.get()) |
150 [self removeTrackingArea:trackingArea_.get()]; | 220 [self removeTrackingArea:trackingArea_.get()]; |
151 | 221 |
152 trackingArea_.reset([[CrTrackingArea alloc] | 222 trackingArea_.reset([[CrTrackingArea alloc] |
153 initWithRect:[self frame] | 223 initWithRect:[self frame] |
154 options:NSTrackingMouseEnteredAndExited | | 224 options:NSTrackingMouseEnteredAndExited | |
155 NSTrackingMouseMoved | | 225 NSTrackingMouseMoved | |
156 NSTrackingActiveInActiveApp | | 226 NSTrackingActiveInActiveApp | |
157 NSTrackingInVisibleRect | 227 NSTrackingInVisibleRect |
158 owner:self | 228 owner:self |
159 userInfo:nil]); | 229 userInfo:nil]); |
160 [self addTrackingArea:trackingArea_.get()]; | 230 [self addTrackingArea:trackingArea_.get()]; |
161 } | 231 } |
162 | 232 |
163 - (void)highlightRowAt:(NSInteger)rowIndex { | 233 - (void)highlightRowAt:(NSInteger)rowIndex { |
164 // highlightCell will be nil if rowIndex is out of range, so no cell will be | 234 // highlightCell will be nil if rowIndex is out of range, so no cell will be |
165 // highlighted. | 235 // highlighted. |
166 NSCell* highlightCell = [self cellAtRow:rowIndex column:0]; | 236 OmniboxPopupCell* highlightCell = [self cellAtRow:rowIndex]; |
groby-ooo-7-16
2015/04/24 20:11:16
Why check that here? Why not just tell the control
dschuyler
2015/04/25 01:05:43
It comes from a presumption that the prior Googler
groby-ooo-7-16
2015/04/25 01:31:11
Yeah, probably. I'd ping the Googler in Question (
| |
237 if ([highlightCell isHighlighted]) | |
238 return; | |
167 | 239 |
168 for (NSCell* cell in [self cells]) { | 240 for (OmniboxPopupCell* cell in controller_.getCellArray) { |
169 [cell setHighlighted:(cell == highlightCell)]; | 241 if ([cell isHighlighted] || cell == highlightCell) { |
242 [cell highlight:(cell == highlightCell) withFrame:_bounds inView:self]; | |
243 } | |
170 } | 244 } |
171 } | 245 } |
172 | 246 |
173 - (void)highlightRowUnder:(NSEvent*)theEvent { | 247 - (void)highlightRowUnder:(NSEvent*)theEvent { |
174 NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil]; | 248 NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil]; |
175 NSInteger row, column; | 249 [self highlightRowAt:[self rowAtPoint:point]]; |
176 if ([self getRow:&row column:&column forPoint:point]) { | |
177 [self highlightRowAt:row]; | |
178 } else { | |
179 [self highlightRowAt:-1]; | |
180 } | |
181 } | 250 } |
182 | 251 |
183 // Select cell under |theEvent|, returning YES if a selection is made. | 252 // Select cell under |theEvent|, returning YES if a selection is made. |
184 - (BOOL)selectCellForEvent:(NSEvent*)theEvent { | 253 - (BOOL)selectCellForEvent:(NSEvent*)theEvent { |
185 NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil]; | 254 NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil]; |
186 | 255 |
187 NSInteger row, column; | 256 NSInteger row = [self rowAtPoint:point]; |
188 if ([self getRow:&row column:&column forPoint:point]) { | 257 if (row != -1) { |
189 DCHECK_EQ(column, 0); | |
190 DCHECK(delegate_); | 258 DCHECK(delegate_); |
191 delegate_->OnMatrixRowSelected(self, row); | 259 delegate_->OnTableRowSelected(self, row); |
192 return YES; | 260 return YES; |
193 } | 261 } |
194 return NO; | 262 return NO; |
195 } | 263 } |
196 | 264 |
197 @end | 265 @end |
OLD | NEW |