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_cell.h" | 5 #import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
11 #include "base/mac/foundation_util.h" | |
11 #include "base/mac/scoped_nsobject.h" | 12 #include "base/mac/scoped_nsobject.h" |
12 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
14 #include "base/strings/sys_string_conversions.h" | 15 #include "base/strings/sys_string_conversions.h" |
15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
16 #include "chrome/browser/ui/cocoa/omnibox/omnibox_popup_view_mac.h" | 17 #include "chrome/browser/ui/cocoa/omnibox/omnibox_popup_view_mac.h" |
17 #include "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" | 18 #include "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" |
18 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" | 19 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" |
19 #include "chrome/grit/generated_resources.h" | 20 #include "chrome/grit/generated_resources.h" |
20 #include "components/omnibox/suggestion_answer.h" | 21 #include "components/omnibox/suggestion_answer.h" |
21 #include "skia/ext/skia_utils_mac.h" | 22 #include "skia/ext/skia_utils_mac.h" |
22 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
23 #include "ui/gfx/font.h" | 24 #include "ui/gfx/font.h" |
24 | 25 |
25 namespace { | 26 namespace { |
26 | 27 |
28 // How much to adjust the cell sizing up from the default determined | |
29 // by the font. | |
30 const CGFloat kCellHeightAdjust = 6.0; | |
31 | |
32 // How large the icon should be when displayed. | |
33 const CGFloat kImageSize = 19.0; | |
34 | |
27 // How far to offset image column from the left. | 35 // How far to offset image column from the left. |
28 const CGFloat kImageXOffset = 5.0; | 36 const CGFloat kImageXOffset = 5.0; |
29 | 37 |
30 // How far to offset the text column from the left. | 38 // How far to offset the text column from the left. |
31 const CGFloat kTextStartOffset = 28.0; | 39 const CGFloat kTextStartOffset = 28.0; |
32 | 40 |
33 // Rounding radius of selection and hover background on popup items. | 41 // Rounding radius of selection and hover background on popup items. |
34 const CGFloat kCellRoundingRadius = 2.0; | 42 const CGFloat kCellRoundingRadius = 2.0; |
35 | 43 |
36 // Flips the given |rect| in context of the given |frame|. | 44 // Flips the given |rect| in context of the given |frame|. |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
272 value:DimTextColor() | 280 value:DimTextColor() |
273 range:range]; | 281 range:range]; |
274 } | 282 } |
275 } | 283 } |
276 | 284 |
277 return attributedString; | 285 return attributedString; |
278 } | 286 } |
279 | 287 |
280 } // namespace | 288 } // namespace |
281 | 289 |
282 @implementation OmniboxPopupCell | 290 @interface OmniboxPopupCell () |
291 - (CGFloat)drawMatchPart:(NSAttributedString*)attributedString | |
292 withFrame:(NSRect)cellFrame | |
293 atOffset:(CGFloat)offset | |
294 withMaxWidth:(int)maxWidth; | |
295 - (CGFloat)drawMatchPrefixWithFrame:(NSRect)cellFrame | |
296 tableView:(OmniboxPopupMatrix*)tableView | |
297 inView:(NSView*)controlView | |
298 withContentsMaxWidth:(int*)contentsMaxWidth; | |
299 @end | |
283 | 300 |
284 - (instancetype)init { | 301 @implementation OmniboxPopupCellData |
285 self = [super init]; | |
286 if (self) { | |
287 [self setImagePosition:NSImageLeft]; | |
288 [self setBordered:NO]; | |
289 [self setButtonType:NSRadioButton]; | |
290 | 302 |
291 // Without this highlighting messes up white areas of images. | 303 @synthesize contents = contents_; |
292 [self setHighlightsBy:NSNoCellMask]; | 304 @synthesize description = description_; |
305 @synthesize prefix = prefix_; | |
306 @synthesize image = image_; | |
307 @synthesize answerImage = answerImage_; | |
308 @synthesize contentsOffset = contentsOffset_; | |
309 @synthesize isContentsRTL = isContentsRTL_; | |
310 @synthesize matchType = matchType_; | |
293 | 311 |
294 const base::string16& raw_separator = | 312 - (instancetype)initWithMatch:(const AutocompleteMatch&)match |
295 l10n_util::GetStringUTF16(IDS_AUTOCOMPLETE_MATCH_DESCRIPTION_SEPARATOR); | 313 contentsOffset:(CGFloat)contentsOffset |
296 separator_.reset( | 314 image:(NSImage*)image |
297 [CreateAttributedString(raw_separator, DimTextColor()) retain]); | 315 answerImage:(NSImage*)answerImage { |
316 if ((self = [super init])) { | |
317 image_ = [image retain]; | |
318 answerImage_ = [answerImage retain]; | |
319 | |
320 isContentsRTL_ = | |
321 (base::i18n::RIGHT_TO_LEFT == | |
322 base::i18n::GetFirstStrongCharacterDirection(match.contents)); | |
323 matchType_ = match.type; | |
324 | |
325 // Prefix may not have any characters with strong directionality, and may | |
326 // take the UI directionality. But prefix needs to appear in continuation | |
327 // of the contents so we force the directionality. | |
328 NSTextAlignment textAlignment = | |
329 isContentsRTL_ ? NSRightTextAlignment : NSLeftTextAlignment; | |
330 prefix_ = | |
331 [CreateAttributedString(base::UTF8ToUTF16(match.GetAdditionalInfo( | |
332 kACMatchPropertyContentsPrefix)), | |
333 ContentTextColor(), textAlignment) retain]; | |
334 | |
335 contents_ = [CreateClassifiedAttributedString( | |
336 match.contents, ContentTextColor(), match.contents_class) retain]; | |
337 | |
338 if (match.answer) { | |
339 base::scoped_nsobject<NSMutableAttributedString> answerString( | |
340 [[NSMutableAttributedString alloc] init]); | |
341 DCHECK(!match.answer->second_line().text_fields().empty()); | |
342 for (const SuggestionAnswer::TextField& textField : | |
343 match.answer->second_line().text_fields()) { | |
344 [answerString | |
345 appendAttributedString:CreateAnswerString(textField.text(), | |
346 textField.type())]; | |
347 } | |
348 const base::string16 space(base::ASCIIToUTF16(" ")); | |
349 const SuggestionAnswer::TextField* textField = | |
350 match.answer->second_line().additional_text(); | |
351 if (textField) { | |
352 [answerString | |
353 appendAttributedString:CreateAnswerString(space + textField->text(), | |
354 textField->type())]; | |
355 } | |
356 textField = match.answer->second_line().status_text(); | |
357 if (textField) { | |
358 [answerString | |
359 appendAttributedString:CreateAnswerString(space + textField->text(), | |
360 textField->type())]; | |
361 } | |
362 description_ = answerString.release(); | |
363 } else if (!match.description.empty()) { | |
364 description_ = [CreateClassifiedAttributedString( | |
365 match.description, DimTextColor(), match.description_class) retain]; | |
366 } | |
298 } | 367 } |
299 return self; | 368 return self; |
300 } | 369 } |
301 | 370 |
302 - (void)setAnswerImage:(NSImage*)image { | 371 - (instancetype)copyWithZone:(NSZone*)zone { |
303 answerImage_.reset([image retain]); | 372 return [self retain]; |
304 } | 373 } |
305 | 374 |
306 - (void)setMatch:(const AutocompleteMatch&)match { | 375 - (CGFloat)getMatchContentsWidth { |
307 match_ = match; | 376 return [contents_ size].width; |
308 NSAttributedString *contents = CreateClassifiedAttributedString( | |
309 match_.contents, ContentTextColor(), match_.contents_class); | |
310 [self setAttributedTitle:contents]; | |
311 [self setAnswerImage:nil]; | |
312 if (match_.answer) { | |
313 base::scoped_nsobject<NSMutableAttributedString> answerString( | |
314 [[NSMutableAttributedString alloc] init]); | |
315 DCHECK(!match_.answer->second_line().text_fields().empty()); | |
316 for (const SuggestionAnswer::TextField& textField : | |
317 match_.answer->second_line().text_fields()) { | |
318 NSAttributedString* attributedString = | |
319 CreateAnswerString(textField.text(), textField.type()); | |
320 [answerString appendAttributedString:attributedString]; | |
321 } | |
322 const base::char16 space(' '); | |
323 const SuggestionAnswer::TextField* textField = | |
324 match_.answer->second_line().additional_text(); | |
325 if (textField) { | |
326 [answerString | |
327 appendAttributedString:CreateAnswerString(space + textField->text(), | |
328 textField->type())]; | |
329 } | |
330 textField = match_.answer->second_line().status_text(); | |
331 if (textField) { | |
332 [answerString | |
333 appendAttributedString:CreateAnswerString(space + textField->text(), | |
334 textField->type())]; | |
335 } | |
336 description_.reset(answerString.release()); | |
337 } else if (match_.description.empty()) { | |
338 description_.reset(); | |
339 } else { | |
340 description_.reset([CreateClassifiedAttributedString( | |
341 match_.description, DimTextColor(), match_.description_class) retain]); | |
342 } | |
343 } | 377 } |
344 | 378 |
345 - (NSAttributedString*)description { | 379 - (CGFloat)rowHeight { |
346 return description_; | 380 return kImageSize + kCellHeightAdjust; |
347 } | 381 } |
348 | 382 |
349 - (void)setMaxMatchContentsWidth:(CGFloat)maxMatchContentsWidth { | 383 @end |
350 maxMatchContentsWidth_ = maxMatchContentsWidth; | |
351 } | |
352 | 384 |
353 - (void)setContentsOffset:(CGFloat)contentsOffset { | 385 @implementation OmniboxPopupCell |
354 contentsOffset_ = contentsOffset; | |
355 } | |
356 | 386 |
357 // The default NSButtonCell drawing leaves the image flush left and | 387 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
358 // the title next to the image. This spaces things out to line up | |
359 // with the star button and autocomplete field. | |
360 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { | |
361 if ([self state] == NSOnState || [self isHighlighted]) { | 388 if ([self state] == NSOnState || [self isHighlighted]) { |
362 if ([self state] == NSOnState) | 389 if ([self state] == NSOnState) |
363 [SelectedBackgroundColor() set]; | 390 [SelectedBackgroundColor() set]; |
364 else | 391 else |
365 [HoveredBackgroundColor() set]; | 392 [HoveredBackgroundColor() set]; |
366 NSBezierPath* path = | 393 NSBezierPath* path = |
367 [NSBezierPath bezierPathWithRoundedRect:cellFrame | 394 [NSBezierPath bezierPathWithRoundedRect:cellFrame |
368 xRadius:kCellRoundingRadius | 395 xRadius:kCellRoundingRadius |
369 yRadius:kCellRoundingRadius]; | 396 yRadius:kCellRoundingRadius]; |
370 [path fill]; | 397 [path fill]; |
371 } | 398 } |
372 | 399 |
373 // Put the image centered vertically but in a fixed column. | |
374 NSImage* image = [self image]; | |
375 if (image) { | |
376 NSRect imageRect = cellFrame; | |
377 imageRect.size = [image size]; | |
378 imageRect.origin.y += | |
379 std::floor((NSHeight(cellFrame) - NSHeight(imageRect)) / 2.0); | |
380 imageRect.origin.x += kImageXOffset; | |
381 [image drawInRect:FlipIfRTL(imageRect, cellFrame) | |
382 fromRect:NSZeroRect // Entire image | |
383 operation:NSCompositeSourceOver | |
384 fraction:1.0 | |
385 respectFlipped:YES | |
386 hints:nil]; | |
387 } | |
388 | |
389 [self drawMatchWithFrame:cellFrame inView:controlView]; | 400 [self drawMatchWithFrame:cellFrame inView:controlView]; |
390 } | 401 } |
391 | 402 |
392 - (void)drawMatchWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { | 403 - (void)drawMatchWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
393 NSAttributedString* contents = [self attributedTitle]; | 404 OmniboxPopupCellData* cellData = [self objectValue]; |
Scott Hess - ex-Googler
2015/06/12 22:21:59
ObjCCastStrict for this, too (unless it can be nil
dschuyler
2015/06/12 23:07:54
Done.
| |
394 | 405 OmniboxPopupMatrix* tableView = |
406 base::mac::ObjCCastStrict<OmniboxPopupMatrix>(controlView); | |
395 CGFloat remainingWidth = GetContentAreaWidth(cellFrame); | 407 CGFloat remainingWidth = GetContentAreaWidth(cellFrame); |
396 CGFloat contentsWidth = [self getMatchContentsWidth]; | 408 CGFloat contentsWidth = [cellData getMatchContentsWidth]; |
397 CGFloat separatorWidth = [separator_ size].width; | 409 CGFloat separatorWidth = [[tableView separator] size].width; |
398 CGFloat descriptionWidth = description_.get() ? [description_ size].width : 0; | 410 CGFloat descriptionWidth = |
411 [cellData description] ? [[cellData description] size].width : 0; | |
399 int contentsMaxWidth, descriptionMaxWidth; | 412 int contentsMaxWidth, descriptionMaxWidth; |
400 OmniboxPopupModel::ComputeMatchMaxWidths( | 413 OmniboxPopupModel::ComputeMatchMaxWidths( |
401 ceilf(contentsWidth), | 414 ceilf(contentsWidth), ceilf(separatorWidth), ceilf(descriptionWidth), |
402 ceilf(separatorWidth), | |
403 ceilf(descriptionWidth), | |
404 ceilf(remainingWidth), | 415 ceilf(remainingWidth), |
405 !AutocompleteMatch::IsSearchType(match_.type), | 416 !AutocompleteMatch::IsSearchType([cellData matchType]), &contentsMaxWidth, |
406 &contentsMaxWidth, | |
407 &descriptionMaxWidth); | 417 &descriptionMaxWidth); |
408 | 418 |
409 CGFloat offset = kTextStartOffset; | 419 // Put the image centered vertically but in a fixed column. |
410 if (match_.type == AutocompleteMatchType::SEARCH_SUGGEST_TAIL) { | 420 NSRect imageRect = cellFrame; |
411 // Infinite suggestions are rendered with a prefix (usually ellipsis), which | 421 imageRect.size = [[cellData image] size]; |
412 // appear vertically stacked. | 422 imageRect.origin.y += |
413 offset += [self drawMatchPrefixWithFrame:cellFrame | 423 std::floor((NSHeight(cellFrame) - NSHeight(imageRect)) / 2.0); |
414 inView:controlView | 424 imageRect.origin.x += kImageXOffset; |
415 withContentsMaxWidth:&contentsMaxWidth]; | 425 [[cellData image] drawInRect:FlipIfRTL(imageRect, cellFrame) |
416 } | |
417 offset += [self drawMatchPart:contents | |
418 withFrame:cellFrame | |
419 atOffset:offset | |
420 withMaxWidth:contentsMaxWidth | |
421 inView:controlView]; | |
422 | |
423 if (descriptionMaxWidth != 0) { | |
424 offset += [self drawMatchPart:separator_ | |
425 withFrame:cellFrame | |
426 atOffset:offset | |
427 withMaxWidth:separatorWidth | |
428 inView:controlView]; | |
429 if (answerImage_) { | |
430 NSRect imageRect = NSMakeRect(offset, NSMinY(cellFrame), | |
431 NSHeight(cellFrame), NSHeight(cellFrame)); | |
432 [answerImage_ drawInRect:FlipIfRTL(imageRect, cellFrame) | |
433 fromRect:NSZeroRect | 426 fromRect:NSZeroRect |
434 operation:NSCompositeSourceOver | 427 operation:NSCompositeSourceOver |
435 fraction:1.0 | 428 fraction:1.0 |
436 respectFlipped:YES | 429 respectFlipped:YES |
437 hints:nil]; | 430 hints:nil]; |
438 offset += NSWidth(imageRect); | 431 |
439 } | 432 CGFloat offset = kTextStartOffset; |
440 offset += [self drawMatchPart:description_ | 433 if ([cellData matchType] == AutocompleteMatchType::SEARCH_SUGGEST_TAIL) { |
434 // Infinite suggestions are rendered with a prefix (usually ellipsis), which | |
435 // appear vertically stacked. | |
436 offset += [self drawMatchPrefixWithFrame:cellFrame | |
437 tableView:tableView | |
438 inView:controlView | |
439 withContentsMaxWidth:&contentsMaxWidth]; | |
440 } | |
441 offset += [self drawMatchPart:[cellData contents] | |
442 withFrame:cellFrame | |
443 atOffset:offset | |
444 withMaxWidth:contentsMaxWidth]; | |
445 | |
446 if (descriptionMaxWidth != 0) { | |
447 offset += [self drawMatchPart:[tableView separator] | |
441 withFrame:cellFrame | 448 withFrame:cellFrame |
442 atOffset:offset | 449 atOffset:offset |
443 withMaxWidth:descriptionMaxWidth | 450 withMaxWidth:separatorWidth]; |
444 inView:controlView]; | 451 NSRect imageRect = NSMakeRect(offset, NSMinY(cellFrame), |
452 NSHeight(cellFrame), NSHeight(cellFrame)); | |
453 [[cellData answerImage] drawInRect:FlipIfRTL(imageRect, cellFrame) | |
454 fromRect:NSZeroRect | |
455 operation:NSCompositeSourceOver | |
456 fraction:1.0 | |
457 respectFlipped:YES | |
458 hints:nil]; | |
459 if ([cellData answerImage]) | |
460 offset += NSWidth(imageRect); | |
Scott Hess - ex-Googler
2015/06/12 22:21:59
Something is definitely wrong here, probably inden
dschuyler
2015/06/12 23:07:54
Hmm, cl format didn't want to move it until I put
| |
461 offset += [self drawMatchPart:[cellData description] | |
462 withFrame:cellFrame | |
463 atOffset:offset | |
464 withMaxWidth:descriptionMaxWidth]; | |
445 } | 465 } |
446 } | 466 } |
447 | 467 |
448 - (CGFloat)drawMatchPrefixWithFrame:(NSRect)cellFrame | 468 - (CGFloat)drawMatchPrefixWithFrame:(NSRect)cellFrame |
469 tableView:(OmniboxPopupMatrix*)tableView | |
449 inView:(NSView*)controlView | 470 inView:(NSView*)controlView |
450 withContentsMaxWidth:(int*)contentsMaxWidth { | 471 withContentsMaxWidth:(int*)contentsMaxWidth { |
472 OmniboxPopupCellData* cellData = [self objectValue]; | |
Scott Hess - ex-Googler
2015/06/12 22:21:59
ObjCCastStrict
dschuyler
2015/06/12 23:07:54
Done.
| |
451 CGFloat offset = 0.0f; | 473 CGFloat offset = 0.0f; |
452 CGFloat remainingWidth = GetContentAreaWidth(cellFrame); | 474 CGFloat remainingWidth = GetContentAreaWidth(cellFrame); |
453 bool isContentsRTL = (base::i18n::RIGHT_TO_LEFT == | 475 CGFloat prefixWidth = [[cellData prefix] size].width; |
454 base::i18n::GetFirstStrongCharacterDirection(match_.contents)); | |
455 // Prefix may not have any characters with strong directionality, and may take | |
456 // the UI directionality. But prefix needs to appear in continuation of the | |
457 // contents so we force the directionality. | |
458 NSTextAlignment textAlignment = isContentsRTL ? | |
459 NSRightTextAlignment : NSLeftTextAlignment; | |
460 prefix_.reset([CreateAttributedString(base::UTF8ToUTF16( | |
461 match_.GetAdditionalInfo(kACMatchPropertyContentsPrefix)), | |
462 ContentTextColor(), textAlignment) retain]); | |
463 CGFloat prefixWidth = [prefix_ size].width; | |
464 | 476 |
465 CGFloat prefixOffset = 0.0f; | 477 CGFloat prefixOffset = 0.0f; |
466 if (base::i18n::IsRTL() != isContentsRTL) { | 478 if (base::i18n::IsRTL() != [cellData isContentsRTL]) { |
467 // The contents is rendered between the contents offset extending towards | 479 // The contents is rendered between the contents offset extending towards |
468 // the start edge, while prefix is rendered in opposite direction. Ideally | 480 // the start edge, while prefix is rendered in opposite direction. Ideally |
469 // the prefix should be rendered at |contentsOffset_|. If that is not | 481 // the prefix should be rendered at |contentsOffset_|. If that is not |
470 // sufficient to render the widest suggestion, we increase it to | 482 // sufficient to render the widest suggestion, we increase it to |
471 // |maxMatchContentsWidth_|. If |remainingWidth| is not sufficient to | 483 // |maxMatchContentsWidth|. If |remainingWidth| is not sufficient to |
472 // accommodate that, we reduce the offset so that the prefix gets rendered. | 484 // accommodate that, we reduce the offset so that the prefix gets rendered. |
473 prefixOffset = std::min( | 485 prefixOffset = std::min( |
474 remainingWidth - prefixWidth, std::max(contentsOffset_, | 486 remainingWidth - prefixWidth, |
475 maxMatchContentsWidth_)); | 487 std::max([cellData contentsOffset], [tableView maxMatchContentsWidth])); |
476 offset = std::max<CGFloat>(0.0, prefixOffset - *contentsMaxWidth); | 488 offset = std::max<CGFloat>(0.0, prefixOffset - *contentsMaxWidth); |
477 } else { // The direction of contents is same as UI direction. | 489 } else { // The direction of contents is same as UI direction. |
478 // Ideally the offset should be |contentsOffset_|. If the max total width | 490 // Ideally the offset should be |contentsOffset_|. If the max total width |
479 // (|prefixWidth| + |maxMatchContentsWidth_|) from offset will exceed the | 491 // (|prefixWidth| + |maxMatchContentsWidth|) from offset will exceed the |
480 // |remainingWidth|, then we shift the offset to the left , so that all | 492 // |remainingWidth|, then we shift the offset to the left , so that all |
481 // postfix suggestions are visible. | 493 // postfix suggestions are visible. |
482 // We have to render the prefix, so offset has to be at least |prefixWidth|. | 494 // We have to render the prefix, so offset has to be at least |prefixWidth|. |
483 offset = std::max(prefixWidth, | 495 offset = |
484 std::min(remainingWidth - maxMatchContentsWidth_, contentsOffset_)); | 496 std::max(prefixWidth, |
497 std::min(remainingWidth - [tableView maxMatchContentsWidth], | |
498 [cellData contentsOffset])); | |
485 prefixOffset = offset - prefixWidth; | 499 prefixOffset = offset - prefixWidth; |
486 } | 500 } |
487 *contentsMaxWidth = std::min((int)ceilf(remainingWidth - prefixWidth), | 501 *contentsMaxWidth = std::min((int)ceilf(remainingWidth - prefixWidth), |
488 *contentsMaxWidth); | 502 *contentsMaxWidth); |
489 [self drawMatchPart:prefix_ | 503 [self drawMatchPart:[cellData prefix] |
490 withFrame:cellFrame | 504 withFrame:cellFrame |
491 atOffset:prefixOffset + kTextStartOffset | 505 atOffset:prefixOffset + kTextStartOffset |
492 withMaxWidth:prefixWidth | 506 withMaxWidth:prefixWidth]; |
493 inView:controlView]; | |
494 return offset; | 507 return offset; |
495 } | 508 } |
496 | 509 |
497 - (CGFloat)drawMatchPart:(NSAttributedString*)attributedString | 510 - (CGFloat)drawMatchPart:(NSAttributedString*)attributedString |
498 withFrame:(NSRect)cellFrame | 511 withFrame:(NSRect)cellFrame |
499 atOffset:(CGFloat)offset | 512 atOffset:(CGFloat)offset |
500 withMaxWidth:(int)maxWidth | 513 withMaxWidth:(int)maxWidth { |
501 inView:(NSView*)controlView { | |
502 if (offset > NSWidth(cellFrame)) | 514 if (offset > NSWidth(cellFrame)) |
503 return 0.0f; | 515 return 0.0f; |
504 NSRect renderRect = ShiftRect(cellFrame, offset); | 516 NSRect renderRect = ShiftRect(cellFrame, offset); |
505 renderRect.size.width = | 517 renderRect.size.width = |
506 std::min(NSWidth(renderRect), static_cast<CGFloat>(maxWidth)); | 518 std::min(NSWidth(renderRect), static_cast<CGFloat>(maxWidth)); |
507 if (renderRect.size.width != 0) { | 519 if (NSWidth(renderRect) > 0.0) |
508 [self drawTitle:attributedString | 520 [attributedString drawInRect:FlipIfRTL(renderRect, cellFrame)]; |
509 withFrame:FlipIfRTL(renderRect, cellFrame) | |
510 inView:controlView]; | |
511 } | |
512 return NSWidth(renderRect); | 521 return NSWidth(renderRect); |
513 } | 522 } |
514 | 523 |
515 - (CGFloat)getMatchContentsWidth { | |
516 NSAttributedString* contents = [self attributedTitle]; | |
517 return contents ? [contents size].width : 0; | |
518 } | |
519 | |
520 | |
521 + (CGFloat)computeContentsOffset:(const AutocompleteMatch&)match { | 524 + (CGFloat)computeContentsOffset:(const AutocompleteMatch&)match { |
522 const base::string16& inputText = base::UTF8ToUTF16( | 525 const base::string16& inputText = base::UTF8ToUTF16( |
523 match.GetAdditionalInfo(kACMatchPropertyInputText)); | 526 match.GetAdditionalInfo(kACMatchPropertyInputText)); |
524 int contentsStartIndex = 0; | 527 int contentsStartIndex = 0; |
525 base::StringToInt( | 528 base::StringToInt( |
526 match.GetAdditionalInfo(kACMatchPropertyContentsStartIndex), | 529 match.GetAdditionalInfo(kACMatchPropertyContentsStartIndex), |
527 &contentsStartIndex); | 530 &contentsStartIndex); |
528 // Ignore invalid state. | 531 // Ignore invalid state. |
529 if (!StartsWith(match.fill_into_edit, inputText, true) | 532 if (!StartsWith(match.fill_into_edit, inputText, true) |
530 || !EndsWith(match.fill_into_edit, match.contents, true) | 533 || !EndsWith(match.fill_into_edit, match.contents, true) |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
577 glyphWidth = std::min(glyphWidth, offset - glyphOffset); | 580 glyphWidth = std::min(glyphWidth, offset - glyphOffset); |
578 } | 581 } |
579 glyphOffset -= minOffset; | 582 glyphOffset -= minOffset; |
580 if (glyphWidth == 0) | 583 if (glyphWidth == 0) |
581 glyphWidth = inputWidth - glyphOffset; | 584 glyphWidth = inputWidth - glyphOffset; |
582 if (isContentsRTL) | 585 if (isContentsRTL) |
583 glyphOffset += glyphWidth; | 586 glyphOffset += glyphWidth; |
584 return base::i18n::IsRTL() ? (inputWidth - glyphOffset) : glyphOffset; | 587 return base::i18n::IsRTL() ? (inputWidth - glyphOffset) : glyphOffset; |
585 } | 588 } |
586 | 589 |
590 + (NSAttributedString*)createSeparatorString { | |
591 base::string16 raw_separator = | |
592 l10n_util::GetStringUTF16(IDS_AUTOCOMPLETE_MATCH_DESCRIPTION_SEPARATOR); | |
593 return CreateAttributedString(raw_separator, DimTextColor()); | |
594 } | |
595 | |
587 @end | 596 @end |
OLD | NEW |