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 "ui/app_list/cocoa/apps_search_results_controller.h" | 5 #import "ui/app_list/cocoa/apps_search_results_controller.h" |
6 | 6 |
7 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
8 #include "base/mac/mac_util.h" | 8 #include "base/mac/mac_util.h" |
9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
10 #include "skia/ext/skia_utils_mac.h" | 10 #include "skia/ext/skia_utils_mac.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 @end | 43 @end |
44 | 44 |
45 @interface AppsSearchResultsCell : NSTextFieldCell | 45 @interface AppsSearchResultsCell : NSTextFieldCell |
46 @end | 46 @end |
47 | 47 |
48 // Immutable class representing a search result in the NSTableView. | 48 // Immutable class representing a search result in the NSTableView. |
49 @interface AppsSearchResultRep : NSObject<NSCopying> { | 49 @interface AppsSearchResultRep : NSObject<NSCopying> { |
50 @private | 50 @private |
51 base::scoped_nsobject<NSAttributedString> attributedStringValue_; | 51 base::scoped_nsobject<NSAttributedString> attributedStringValue_; |
52 base::scoped_nsobject<NSImage> resultIcon_; | 52 base::scoped_nsobject<NSImage> resultIcon_; |
| 53 |
| 54 // Width of the table cell that needs to be set aside for action buttons. |
| 55 CGFloat actionButtonWidth_; |
53 } | 56 } |
54 | 57 |
55 @property(readonly, nonatomic) NSAttributedString* attributedStringValue; | 58 @property(readonly, nonatomic) NSAttributedString* attributedStringValue; |
56 @property(readonly, nonatomic) NSImage* resultIcon; | 59 @property(readonly, nonatomic) NSImage* resultIcon; |
| 60 @property(readonly, nonatomic) CGFloat actionButtonWidth; |
57 | 61 |
58 - (id)initWithSearchResult:(app_list::SearchResult*)result; | 62 - (id)initWithSearchResult:(app_list::SearchResult*)result |
| 63 actionButtonWidth:(CGFloat)actionButtonWidth; |
59 | 64 |
60 - (NSMutableAttributedString*)createRenderText:(const base::string16&)content | 65 - (NSMutableAttributedString*)createRenderText:(const base::string16&)content |
61 tags:(const app_list::SearchResult::Tags&)tags; | 66 tags:(const app_list::SearchResult::Tags&)tags; |
62 | 67 |
63 - (NSAttributedString*)createResultsAttributedStringWithModel | 68 - (NSAttributedString*)createResultsAttributedStringWithModel |
64 :(app_list::SearchResult*)result; | 69 :(app_list::SearchResult*)result; |
65 | 70 |
66 @end | 71 @end |
67 | 72 |
68 // Simple extension to NSTableView that passes mouseDown events to the | 73 // Simple extension to NSTableView that passes mouseDown events to the |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 row:(NSInteger)rowIndex { | 256 row:(NSInteger)rowIndex { |
252 // When the results were previously cleared, nothing will be selected. For | 257 // When the results were previously cleared, nothing will be selected. For |
253 // that case, select the first row when it appears. | 258 // that case, select the first row when it appears. |
254 if (rowIndex == 0 && [tableView_ selectedRow] == -1) { | 259 if (rowIndex == 0 && [tableView_ selectedRow] == -1) { |
255 [tableView_ selectRowIndexes:[NSIndexSet indexSetWithIndex:0] | 260 [tableView_ selectRowIndexes:[NSIndexSet indexSetWithIndex:0] |
256 byExtendingSelection:NO]; | 261 byExtendingSelection:NO]; |
257 } | 262 } |
258 | 263 |
259 base::scoped_nsobject<AppsSearchResultRep> resultRep( | 264 base::scoped_nsobject<AppsSearchResultRep> resultRep( |
260 [[AppsSearchResultRep alloc] | 265 [[AppsSearchResultRep alloc] |
261 initWithSearchResult:[self searchResults]->GetItemAt(rowIndex)]); | 266 initWithSearchResult:[self searchResults]->GetItemAt(rowIndex) |
| 267 actionButtonWidth:bridge_->ActionButtonWidthForItem(rowIndex)]); |
262 return resultRep.autorelease(); | 268 return resultRep.autorelease(); |
263 } | 269 } |
264 | 270 |
265 - (void)tableView:(NSTableView*)tableView | 271 - (void)tableView:(NSTableView*)tableView |
266 willDisplayCell:(id)cell | 272 willDisplayCell:(id)cell |
267 forTableColumn:(NSTableColumn*)tableColumn | 273 forTableColumn:(NSTableColumn*)tableColumn |
268 row:(NSInteger)rowIndex { | 274 row:(NSInteger)rowIndex { |
269 if (rowIndex == [tableView selectedRow]) | 275 if (rowIndex == [tableView selectedRow]) |
270 [cell setBackgroundStyle:kBackgroundSelected]; | 276 [cell setBackgroundStyle:kBackgroundSelected]; |
271 else if (rowIndex == hoveredRowIndex_) | 277 else if (rowIndex == hoveredRowIndex_) |
(...skipping 21 matching lines...) Expand all Loading... |
293 [tableView_ setNeedsDisplayInRect:[tableView_ rectOfRow:newIndex]]; | 299 [tableView_ setNeedsDisplayInRect:[tableView_ rectOfRow:newIndex]]; |
294 if (hoveredRowIndex_ != -1) | 300 if (hoveredRowIndex_ != -1) |
295 [tableView_ setNeedsDisplayInRect:[tableView_ rectOfRow:hoveredRowIndex_]]; | 301 [tableView_ setNeedsDisplayInRect:[tableView_ rectOfRow:hoveredRowIndex_]]; |
296 hoveredRowIndex_ = newIndex; | 302 hoveredRowIndex_ = newIndex; |
297 } | 303 } |
298 | 304 |
299 @end | 305 @end |
300 | 306 |
301 @implementation AppsSearchResultRep | 307 @implementation AppsSearchResultRep |
302 | 308 |
| 309 @synthesize actionButtonWidth = actionButtonWidth_; |
| 310 |
303 - (NSAttributedString*)attributedStringValue { | 311 - (NSAttributedString*)attributedStringValue { |
304 return attributedStringValue_; | 312 return attributedStringValue_; |
305 } | 313 } |
306 | 314 |
307 - (NSImage*)resultIcon { | 315 - (NSImage*)resultIcon { |
308 return resultIcon_; | 316 return resultIcon_; |
309 } | 317 } |
310 | 318 |
311 - (id)initWithSearchResult:(app_list::SearchResult*)result { | 319 - (id)initWithSearchResult:(app_list::SearchResult*)result |
| 320 actionButtonWidth:(CGFloat)actionButtonWidth { |
312 if ((self = [super init])) { | 321 if ((self = [super init])) { |
313 attributedStringValue_.reset( | 322 attributedStringValue_.reset( |
314 [[self createResultsAttributedStringWithModel:result] retain]); | 323 [[self createResultsAttributedStringWithModel:result] retain]); |
315 if (!result->icon().isNull()) { | 324 if (!result->icon().isNull()) { |
316 resultIcon_.reset([gfx::NSImageFromImageSkiaWithColorSpace( | 325 resultIcon_.reset([gfx::NSImageFromImageSkiaWithColorSpace( |
317 result->icon(), base::mac::GetSRGBColorSpace()) retain]); | 326 result->icon(), base::mac::GetSRGBColorSpace()) retain]); |
318 } | 327 } |
| 328 actionButtonWidth_ = actionButtonWidth; |
319 } | 329 } |
320 return self; | 330 return self; |
321 } | 331 } |
322 | 332 |
323 - (NSMutableAttributedString*)createRenderText:(const base::string16&)content | 333 - (NSMutableAttributedString*)createRenderText:(const base::string16&)content |
324 tags:(const app_list::SearchResult::Tags&)tags { | 334 tags:(const app_list::SearchResult::Tags&)tags { |
325 NSFont* boldFont = nil; | 335 NSFont* boldFont = nil; |
326 base::scoped_nsobject<NSMutableParagraphStyle> paragraphStyle( | 336 base::scoped_nsobject<NSMutableParagraphStyle> paragraphStyle( |
327 [[NSMutableParagraphStyle alloc] init]); | 337 [[NSMutableParagraphStyle alloc] init]); |
328 [paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail]; | 338 [paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail]; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 | 436 |
427 // Extend up by one pixel to draw over cell border. | 437 // Extend up by one pixel to draw over cell border. |
428 NSRect backgroundRect = cellFrame; | 438 NSRect backgroundRect = cellFrame; |
429 backgroundRect.origin.y -= 1; | 439 backgroundRect.origin.y -= 1; |
430 backgroundRect.size.height += 1; | 440 backgroundRect.size.height += 1; |
431 NSRectFill(backgroundRect); | 441 NSRectFill(backgroundRect); |
432 } | 442 } |
433 | 443 |
434 NSAttributedString* titleText = [self attributedStringValue]; | 444 NSAttributedString* titleText = [self attributedStringValue]; |
435 NSRect titleRect = cellFrame; | 445 NSRect titleRect = cellFrame; |
436 titleRect.size.width -= kTextTrailPadding + kIconViewWidth; | 446 titleRect.size.width -= kTextTrailPadding + kIconViewWidth + |
| 447 [[self objectValue] actionButtonWidth]; |
| 448 |
437 titleRect.origin.x += kIconViewWidth; | 449 titleRect.origin.x += kIconViewWidth; |
438 titleRect.origin.y += | 450 titleRect.origin.y += |
439 floor(NSHeight(cellFrame) / 2 - [titleText size].height / 2); | 451 floor(NSHeight(cellFrame) / 2 - [titleText size].height / 2); |
440 // Ensure no drawing occurs outside of the cell. | 452 // Ensure no drawing occurs outside of the cell. |
441 titleRect = NSIntersectionRect(titleRect, cellFrame); | 453 titleRect = NSIntersectionRect(titleRect, cellFrame); |
442 | 454 |
443 [titleText drawInRect:titleRect]; | 455 [titleText drawInRect:titleRect]; |
444 | 456 |
445 NSImage* resultIcon = [[self objectValue] resultIcon]; | 457 NSImage* resultIcon = [[self objectValue] resultIcon]; |
446 if (!resultIcon) | 458 if (!resultIcon) |
447 return; | 459 return; |
448 | 460 |
449 NSSize iconSize = [resultIcon size]; | 461 NSSize iconSize = [resultIcon size]; |
450 NSRect iconRect = NSMakeRect( | 462 NSRect iconRect = NSMakeRect( |
451 floor(NSMinX(cellFrame) + kIconViewWidth / 2 - iconSize.width / 2), | 463 floor(NSMinX(cellFrame) + kIconViewWidth / 2 - iconSize.width / 2), |
452 floor(NSMinY(cellFrame) + kPreferredRowHeight / 2 - iconSize.height / 2), | 464 floor(NSMinY(cellFrame) + kPreferredRowHeight / 2 - iconSize.height / 2), |
453 std::min(iconSize.width, kIconDimension), | 465 std::min(iconSize.width, kIconDimension), |
454 std::min(iconSize.height, kIconDimension)); | 466 std::min(iconSize.height, kIconDimension)); |
455 [resultIcon drawInRect:iconRect | 467 [resultIcon drawInRect:iconRect |
456 fromRect:NSZeroRect | 468 fromRect:NSZeroRect |
457 operation:NSCompositeSourceOver | 469 operation:NSCompositeSourceOver |
458 fraction:1.0 | 470 fraction:1.0 |
459 respectFlipped:YES | 471 respectFlipped:YES |
460 hints:nil]; | 472 hints:nil]; |
461 } | 473 } |
462 | 474 |
463 @end | 475 @end |
OLD | NEW |