| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #include "chrome/browser/autocomplete/autocomplete_popup_view_mac.h" | 5 #include "chrome/browser/autocomplete/autocomplete_popup_view_mac.h" |
| 6 | 6 |
| 7 #include "base/sys_string_conversions.h" | 7 #include "base/sys_string_conversions.h" |
| 8 #include "chrome/browser/autocomplete/autocomplete_edit.h" | 8 #include "chrome/browser/autocomplete/autocomplete_edit.h" |
| 9 #include "chrome/browser/autocomplete/autocomplete_edit_view_mac.h" | 9 #include "chrome/browser/autocomplete/autocomplete_edit_view_mac.h" |
| 10 #include "chrome/browser/autocomplete/autocomplete_popup_model.h" | 10 #include "chrome/browser/autocomplete/autocomplete_popup_model.h" |
| 11 #include "chrome/browser/cocoa/nsimage_cache.h" | 11 #include "chrome/browser/cocoa/nsimage_cache.h" |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 // The size delta between the font used for the edit and the result |
| 16 // rows. |
| 17 const int kEditFontAdjust = -1; |
| 18 |
| 19 // How much to adjust the cell sizing up from the default determined |
| 20 // by the font. |
| 21 const int kCellHeightAdjust = 7.0; |
| 22 |
| 15 // Background colors for different states of the popup elements. | 23 // Background colors for different states of the popup elements. |
| 16 NSColor* BackgroundColor() { | 24 NSColor* BackgroundColor() { |
| 17 return [NSColor controlBackgroundColor]; | 25 return [NSColor controlBackgroundColor]; |
| 18 } | 26 } |
| 19 NSColor* SelectedBackgroundColor() { | 27 NSColor* SelectedBackgroundColor() { |
| 20 return [NSColor selectedControlColor]; | 28 return [NSColor selectedControlColor]; |
| 21 } | 29 } |
| 22 NSColor* HoveredBackgroundColor() { | 30 NSColor* HoveredBackgroundColor() { |
| 23 return [NSColor controlColor]; | 31 return [NSColor controlColor]; |
| 24 } | 32 } |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 // Return the text to show for the match, based on the match's | 132 // Return the text to show for the match, based on the match's |
| 125 // contents and description. Result will be in |font|, with the | 133 // contents and description. Result will be in |font|, with the |
| 126 // boldfaced version used for matches. | 134 // boldfaced version used for matches. |
| 127 NSAttributedString* AutocompletePopupViewMac::MatchText( | 135 NSAttributedString* AutocompletePopupViewMac::MatchText( |
| 128 const AutocompleteMatch& match, NSFont* font) { | 136 const AutocompleteMatch& match, NSFont* font) { |
| 129 NSMutableAttributedString *as = | 137 NSMutableAttributedString *as = |
| 130 DecorateMatchedString(match.contents, match.contents_class, | 138 DecorateMatchedString(match.contents, match.contents_class, |
| 131 ContentTextColor(), font); | 139 ContentTextColor(), font); |
| 132 | 140 |
| 133 // If there is a description, append it, separated from the contents | 141 // If there is a description, append it, separated from the contents |
| 134 // with an em dash, and decorated with a distinct color. | 142 // with an en dash, and decorated with a distinct color. |
| 135 if (!match.description.empty()) { | 143 if (!match.description.empty()) { |
| 136 NSDictionary* attributes = | 144 NSDictionary* attributes = |
| 137 [NSDictionary dictionaryWithObjectsAndKeys: | 145 [NSDictionary dictionaryWithObjectsAndKeys: |
| 138 font, NSFontAttributeName, | 146 font, NSFontAttributeName, |
| 139 ContentTextColor(), NSForegroundColorAttributeName, | 147 ContentTextColor(), NSForegroundColorAttributeName, |
| 140 nil]; | 148 nil]; |
| 141 NSString* rawEmDash = [NSString stringWithFormat:@" %C ", 0x2014]; | 149 NSString* rawEnDash = [NSString stringWithFormat:@" %C ", 0x2013]; |
| 142 NSAttributedString* emDash = | 150 NSAttributedString* enDash = |
| 143 [[[NSAttributedString alloc] initWithString:rawEmDash | 151 [[[NSAttributedString alloc] initWithString:rawEnDash |
| 144 attributes:attributes] autorelease]; | 152 attributes:attributes] autorelease]; |
| 145 | 153 |
| 146 NSAttributedString* description = | 154 NSAttributedString* description = |
| 147 DecorateMatchedString(match.description, match.description_class, | 155 DecorateMatchedString(match.description, match.description_class, |
| 148 DescriptionTextColor(), font); | 156 DescriptionTextColor(), font); |
| 149 | 157 |
| 150 [as appendAttributedString:emDash]; | 158 [as appendAttributedString:enDash]; |
| 151 [as appendAttributedString:description]; | 159 [as appendAttributedString:description]; |
| 152 } | 160 } |
| 153 | 161 |
| 154 NSMutableParagraphStyle* style = | 162 NSMutableParagraphStyle* style = |
| 155 [[[NSMutableParagraphStyle alloc] init] autorelease]; | 163 [[[NSMutableParagraphStyle alloc] init] autorelease]; |
| 156 [style setLineBreakMode:NSLineBreakByTruncatingTail]; | 164 [style setLineBreakMode:NSLineBreakByTruncatingTail]; |
| 157 [as addAttribute:NSParagraphStyleAttributeName value:style | 165 [as addAttribute:NSParagraphStyleAttributeName value:style |
| 158 range:NSMakeRange(0, [as length])]; | 166 range:NSMakeRange(0, [as length])]; |
| 159 | 167 |
| 160 return as; | 168 return as; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 NSMatrix* matrix = [popup_ contentView]; | 262 NSMatrix* matrix = [popup_ contentView]; |
| 255 [matrix setTarget:nil]; | 263 [matrix setTarget:nil]; |
| 256 | 264 |
| 257 popup_.reset(nil); | 265 popup_.reset(nil); |
| 258 | 266 |
| 259 return; | 267 return; |
| 260 } | 268 } |
| 261 | 269 |
| 262 CreatePopupIfNeeded(); | 270 CreatePopupIfNeeded(); |
| 263 | 271 |
| 272 // The popup's font is a slightly smaller version of what |field_| |
| 273 // uses. |
| 274 NSFont* fieldFont = [field_ font]; |
| 275 const CGFloat resultFontSize = [fieldFont pointSize] + kEditFontAdjust; |
| 276 NSFont* resultFont = |
| 277 [NSFont fontWithName:[fieldFont fontName] size:resultFontSize]; |
| 278 |
| 264 // Load the results into the popup's matrix. | 279 // Load the results into the popup's matrix. |
| 265 AutocompleteMatrix* matrix = [popup_ contentView]; | 280 AutocompleteMatrix* matrix = [popup_ contentView]; |
| 266 const size_t rows = model_->result().size(); | 281 const size_t rows = model_->result().size(); |
| 267 DCHECK_GT(rows, 0U); | 282 DCHECK_GT(rows, 0U); |
| 268 [matrix renewRows:rows columns:1]; | 283 [matrix renewRows:rows columns:1]; |
| 269 for (size_t ii = 0; ii < rows; ++ii) { | 284 for (size_t ii = 0; ii < rows; ++ii) { |
| 270 AutocompleteButtonCell* cell = [matrix cellAtRow:ii column:0]; | 285 AutocompleteButtonCell* cell = [matrix cellAtRow:ii column:0]; |
| 271 const AutocompleteMatch& match = model_->result().match_at(ii); | 286 const AutocompleteMatch& match = model_->result().match_at(ii); |
| 272 [cell setImage:MatchIcon(match)]; | 287 [cell setImage:MatchIcon(match)]; |
| 273 [cell setAttributedTitle:MatchText(match, [field_ font])]; | 288 [cell setAttributedTitle:MatchText(match, resultFont)]; |
| 274 } | 289 } |
| 275 | 290 |
| 276 // Layout the popup and size it to land underneath the field. | 291 // Layout the popup and size it to land underneath the field. |
| 277 // TODO(shess) Consider refactoring to remove this depenency, | 292 // TODO(shess) Consider refactoring to remove this depenency, |
| 278 // because the popup doesn't need any of the field-like aspects of | 293 // because the popup doesn't need any of the field-like aspects of |
| 279 // field_. The edit view could expose helper methods for attaching | 294 // field_. The edit view could expose helper methods for attaching |
| 280 // the window to the field. | 295 // the window to the field. |
| 281 | 296 |
| 282 // Locate |field_| on the screen. | 297 // Locate |field_| on the screen. |
| 283 NSRect r = [field_ convertRect:[field_ bounds] toView:nil]; | 298 NSRect r = [field_ convertRect:[field_ bounds] toView:nil]; |
| 284 r.origin = [[field_ window] convertBaseToScreen:r.origin]; | 299 r.origin = [[field_ window] convertBaseToScreen:r.origin]; |
| 285 DCHECK_GT(r.size.width, 0.0); | 300 DCHECK_GT(r.size.width, 0.0); |
| 286 | 301 |
| 287 // Set the cell size to fit a line of text in the cell's font. All | 302 // Set the cell size to fit a line of text in the cell's font. All |
| 288 // cells should use the same font and each should layout in one | 303 // cells should use the same font and each should layout in one |
| 289 // line, so they should all be about the same height. | 304 // line, so they should all be about the same height. |
| 290 const NSSize cellSize = [[matrix cellAtRow:0 column:0] cellSize]; | 305 const NSSize cellSize = [[matrix cellAtRow:0 column:0] cellSize]; |
| 291 DCHECK_GT(cellSize.height, 0.0); | 306 DCHECK_GT(cellSize.height, 0.0); |
| 292 [matrix setCellSize:NSMakeSize(r.size.width, cellSize.height)]; | 307 [matrix setCellSize:NSMakeSize(r.size.width, |
| 308 cellSize.height + kCellHeightAdjust)]; |
| 293 | 309 |
| 294 // Make the matrix big enough to hold all the cells. | 310 // Make the matrix big enough to hold all the cells. |
| 295 [matrix sizeToCells]; | 311 [matrix sizeToCells]; |
| 296 | 312 |
| 297 // Make the window just as big. | 313 // Make the window just as big. |
| 298 r.size.height = [matrix frame].size.height; | 314 r.size.height = [matrix frame].size.height; |
| 299 r.origin.y -= r.size.height + 2; | 315 r.origin.y -= r.size.height + 2; |
| 300 | 316 |
| 301 // Update the selection. | 317 // Update the selection. |
| 302 PaintUpdatesNow(); | 318 PaintUpdatesNow(); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 - (void)updateTrackingAreas { | 403 - (void)updateTrackingAreas { |
| 388 [self resetTrackingArea]; | 404 [self resetTrackingArea]; |
| 389 [super updateTrackingAreas]; | 405 [super updateTrackingAreas]; |
| 390 } | 406 } |
| 391 | 407 |
| 392 - initWithFrame:(NSRect)frame { | 408 - initWithFrame:(NSRect)frame { |
| 393 self = [super initWithFrame:frame]; | 409 self = [super initWithFrame:frame]; |
| 394 if (self) { | 410 if (self) { |
| 395 [self setCellClass:[AutocompleteButtonCell class]]; | 411 [self setCellClass:[AutocompleteButtonCell class]]; |
| 396 | 412 |
| 397 [self setIntercellSpacing:NSMakeSize(1.0, 1.0)]; | 413 // Cells pack with no spacing. |
| 414 [self setIntercellSpacing:NSMakeSize(0.0, 0.0)]; |
| 415 |
| 398 [self setDrawsBackground:YES]; | 416 [self setDrawsBackground:YES]; |
| 399 [self setBackgroundColor:BackgroundColor()]; | 417 [self setBackgroundColor:BackgroundColor()]; |
| 400 [self renewRows:0 columns:1]; | 418 [self renewRows:0 columns:1]; |
| 401 [self setAllowsEmptySelection:YES]; | 419 [self setAllowsEmptySelection:YES]; |
| 402 [self setMode:NSRadioModeMatrix]; | 420 [self setMode:NSRadioModeMatrix]; |
| 403 [self deselectAllCells]; | 421 [self deselectAllCells]; |
| 404 | 422 |
| 405 [self resetTrackingArea]; | 423 [self resetTrackingArea]; |
| 406 } | 424 } |
| 407 return self; | 425 return self; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 } | 467 } |
| 450 return self; | 468 return self; |
| 451 } | 469 } |
| 452 | 470 |
| 453 - (void)select:sender { | 471 - (void)select:sender { |
| 454 DCHECK(popup_view_); | 472 DCHECK(popup_view_); |
| 455 popup_view_->AcceptInput(); | 473 popup_view_->AcceptInput(); |
| 456 } | 474 } |
| 457 | 475 |
| 458 @end | 476 @end |
| OLD | NEW |