Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(282)

Side by Side Diff: chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.mm

Issue 2874923002: Omnibox UI Experiments: Port vertical margin experiment to Cocoa. (Closed)
Patch Set: make constexpr Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <cmath> 10 #include <cmath>
11 11
12 #include "base/i18n/rtl.h" 12 #include "base/i18n/rtl.h"
13 #include "base/mac/foundation_util.h" 13 #include "base/mac/foundation_util.h"
14 #include "base/mac/objc_property_releaser.h" 14 #include "base/mac/objc_property_releaser.h"
15 #include "base/mac/scoped_nsobject.h" 15 #include "base/mac/scoped_nsobject.h"
16 #include "base/metrics/field_trial_params.h"
16 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_util.h" 18 #include "base/strings/string_util.h"
18 #include "base/strings/sys_string_conversions.h" 19 #include "base/strings/sys_string_conversions.h"
19 #include "base/strings/utf_string_conversions.h" 20 #include "base/strings/utf_string_conversions.h"
20 #include "chrome/browser/ui/cocoa/omnibox/omnibox_popup_view_mac.h" 21 #include "chrome/browser/ui/cocoa/omnibox/omnibox_popup_view_mac.h"
21 #include "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" 22 #include "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h"
22 #import "chrome/browser/ui/cocoa/themed_window.h" 23 #import "chrome/browser/ui/cocoa/themed_window.h"
23 #include "chrome/grit/generated_resources.h" 24 #include "chrome/grit/generated_resources.h"
25 #include "components/omnibox/browser/omnibox_field_trial.h"
24 #include "components/omnibox/browser/omnibox_popup_model.h" 26 #include "components/omnibox/browser/omnibox_popup_model.h"
25 #include "components/omnibox/browser/suggestion_answer.h" 27 #include "components/omnibox/browser/suggestion_answer.h"
26 #include "skia/ext/skia_utils_mac.h" 28 #include "skia/ext/skia_utils_mac.h"
27 #include "ui/base/l10n/l10n_util.h" 29 #include "ui/base/l10n/l10n_util.h"
28 #include "ui/base/material_design/material_design_controller.h" 30 #include "ui/base/material_design/material_design_controller.h"
29 #include "ui/gfx/color_palette.h" 31 #include "ui/gfx/color_palette.h"
30 #include "ui/gfx/font.h" 32 #include "ui/gfx/font.h"
31 33
32 namespace { 34 namespace {
33 35
34 // How far to offset text. 36 // Extra padding beyond the vertical text padding.
35 const CGFloat kVerticalTextPadding = 3.0; 37 constexpr CGFloat kMaterialExtraVerticalImagePadding = 2.0;
36 38
37 const CGFloat kMaterialVerticalImagePadding = 5.0; 39 constexpr CGFloat kMaterialTextStartOffset = 27.0;
38 40
39 const CGFloat kMaterialTextStartOffset = 27.0; 41 constexpr CGFloat kMaterialImageXOffset = 6.0;
40 42
41 const CGFloat kMaterialImageXOffset = 6.0; 43 // Returns the margin that should appear at the top and bottom of the result.
44 CGFloat GetVerticalMargin() {
45 constexpr CGFloat kDefaultVerticalMargin = 3.0;
46
47 return base::GetFieldTrialParamByFeatureAsInt(
48 omnibox::kUIExperimentVerticalMargin,
49 OmniboxFieldTrial::kUIVerticalMarginParam, kDefaultVerticalMargin);
50 }
42 51
43 // Flips the given |rect| in context of the given |frame|. 52 // Flips the given |rect| in context of the given |frame|.
44 NSRect FlipIfRTL(NSRect rect, NSRect frame) { 53 NSRect FlipIfRTL(NSRect rect, NSRect frame) {
45 DCHECK_LE(NSMinX(frame), NSMinX(rect)); 54 DCHECK_LE(NSMinX(frame), NSMinX(rect));
46 DCHECK_GE(NSMaxX(frame), NSMaxX(rect)); 55 DCHECK_GE(NSMaxX(frame), NSMaxX(rect));
47 if (base::i18n::IsRTL()) { 56 if (base::i18n::IsRTL()) {
48 NSRect result = rect; 57 NSRect result = rect;
49 result.origin.x = NSMinX(frame) + (NSMaxX(frame) - NSMaxX(rect)); 58 result.origin.x = NSMinX(frame) + (NSMaxX(frame) - NSMaxX(rect));
50 return result; 59 return result;
51 } 60 }
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 &contentsMaxWidth, 482 &contentsMaxWidth,
474 &descriptionMaxWidth); 483 &descriptionMaxWidth);
475 484
476 NSWindow* parentWindow = [[controlView window] parentWindow]; 485 NSWindow* parentWindow = [[controlView window] parentWindow];
477 BOOL isDarkTheme = [parentWindow hasDarkTheme]; 486 BOOL isDarkTheme = [parentWindow hasDarkTheme];
478 NSRect imageRect = cellFrame; 487 NSRect imageRect = cellFrame;
479 NSImage* theImage = 488 NSImage* theImage =
480 isDarkTheme ? [cellData incognitoImage] : [cellData image]; 489 isDarkTheme ? [cellData incognitoImage] : [cellData image];
481 imageRect.size = [theImage size]; 490 imageRect.size = [theImage size];
482 imageRect.origin.x += kMaterialImageXOffset + [tableView contentLeftPadding]; 491 imageRect.origin.x += kMaterialImageXOffset + [tableView contentLeftPadding];
483 imageRect.origin.y += kMaterialVerticalImagePadding; 492 imageRect.origin.y +=
493 GetVerticalMargin() + kMaterialExtraVerticalImagePadding;
484 [theImage drawInRect:FlipIfRTL(imageRect, cellFrame) 494 [theImage drawInRect:FlipIfRTL(imageRect, cellFrame)
485 fromRect:NSZeroRect 495 fromRect:NSZeroRect
486 operation:NSCompositeSourceOver 496 operation:NSCompositeSourceOver
487 fraction:1.0 497 fraction:1.0
488 respectFlipped:YES 498 respectFlipped:YES
489 hints:nil]; 499 hints:nil];
490 500
491 NSPoint origin = 501 NSPoint origin =
492 NSMakePoint(kMaterialTextStartOffset + [tableView contentLeftPadding], 502 NSMakePoint(kMaterialTextStartOffset + [tableView contentLeftPadding],
493 kVerticalTextPadding); 503 GetVerticalMargin());
494 if ([cellData matchType] == AutocompleteMatchType::SEARCH_SUGGEST_TAIL) { 504 if ([cellData matchType] == AutocompleteMatchType::SEARCH_SUGGEST_TAIL) {
495 // Tail suggestions are rendered with a prefix (usually ellipsis), which 505 // Tail suggestions are rendered with a prefix (usually ellipsis), which
496 // appear vertically stacked. 506 // appear vertically stacked.
497 origin.x += [self drawMatchPrefixWithFrame:cellFrame 507 origin.x += [self drawMatchPrefixWithFrame:cellFrame
498 tableView:tableView 508 tableView:tableView
499 withContentsMaxWidth:&contentsMaxWidth 509 withContentsMaxWidth:&contentsMaxWidth
500 forDarkTheme:isDarkTheme]; 510 forDarkTheme:isDarkTheme];
501 } 511 }
502 origin.x += [self drawMatchPart:[cellData contents] 512 origin.x += [self drawMatchPart:[cellData contents]
503 withFrame:cellFrame 513 withFrame:cellFrame
504 origin:origin 514 origin:origin
505 withMaxWidth:contentsMaxWidth 515 withMaxWidth:contentsMaxWidth
506 forDarkTheme:isDarkTheme]; 516 forDarkTheme:isDarkTheme];
507 517
508 if (descriptionMaxWidth > 0) { 518 if (descriptionMaxWidth > 0) {
509 if ([cellData isAnswer]) { 519 if ([cellData isAnswer]) {
510 origin = 520 origin = NSMakePoint(
511 NSMakePoint(kMaterialTextStartOffset + [tableView contentLeftPadding], 521 kMaterialTextStartOffset + [tableView contentLeftPadding],
512 kContentLineHeight - kVerticalTextPadding); 522 [OmniboxPopupCell getContentTextHeight] - GetVerticalMargin());
513 CGFloat imageSize = [tableView answerLineHeight]; 523 CGFloat imageSize = [tableView answerLineHeight];
514 NSRect imageRect = 524 NSRect imageRect =
515 NSMakeRect(NSMinX(cellFrame) + origin.x, NSMinY(cellFrame) + origin.y, 525 NSMakeRect(NSMinX(cellFrame) + origin.x, NSMinY(cellFrame) + origin.y,
516 imageSize, imageSize); 526 imageSize, imageSize);
517 [[cellData answerImage] drawInRect:FlipIfRTL(imageRect, cellFrame) 527 [[cellData answerImage] drawInRect:FlipIfRTL(imageRect, cellFrame)
518 fromRect:NSZeroRect 528 fromRect:NSZeroRect
519 operation:NSCompositeSourceOver 529 operation:NSCompositeSourceOver
520 fraction:1.0 530 fraction:1.0
521 respectFlipped:YES 531 respectFlipped:YES
522 hints:nil]; 532 hints:nil];
523 if ([cellData answerImage]) { 533 if ([cellData answerImage]) {
524 origin.x += imageSize + kMaterialVerticalImagePadding; 534 origin.x += imageSize + kMaterialImageXOffset;
525 535
526 // Have to nudge the baseline down 1pt in Material Design for the text 536 // Have to nudge the baseline down 1pt in Material Design for the text
527 // that follows, so that it's the same as the bottom of the image. 537 // that follows, so that it's the same as the bottom of the image.
528 origin.y += 1; 538 origin.y += 1;
529 } 539 }
530 } else { 540 } else {
531 origin.x += [self drawMatchPart:[tableView separator] 541 origin.x += [self drawMatchPart:[tableView separator]
532 withFrame:cellFrame 542 withFrame:cellFrame
533 origin:origin 543 origin:origin
534 withMaxWidth:separatorWidth 544 withMaxWidth:separatorWidth
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
678 + (NSAttributedString*)createSeparatorStringForDarkTheme:(BOOL)isDarkTheme { 688 + (NSAttributedString*)createSeparatorStringForDarkTheme:(BOOL)isDarkTheme {
679 base::string16 raw_separator = 689 base::string16 raw_separator =
680 l10n_util::GetStringUTF16(IDS_AUTOCOMPLETE_MATCH_DESCRIPTION_SEPARATOR); 690 l10n_util::GetStringUTF16(IDS_AUTOCOMPLETE_MATCH_DESCRIPTION_SEPARATOR);
681 return CreateAttributedString(raw_separator, DimTextColor(isDarkTheme)); 691 return CreateAttributedString(raw_separator, DimTextColor(isDarkTheme));
682 } 692 }
683 693
684 + (CGFloat)getContentAreaWidth:(NSRect)cellFrame { 694 + (CGFloat)getContentAreaWidth:(NSRect)cellFrame {
685 return NSWidth(cellFrame) - kMaterialTextStartOffset; 695 return NSWidth(cellFrame) - kMaterialTextStartOffset;
686 } 696 }
687 697
698 + (CGFloat)getContentTextHeight {
699 constexpr CGFloat kDefaultTextHeight = 19;
700 return kDefaultTextHeight + 2 * GetVerticalMargin();
701 }
702
688 @end 703 @end
OLDNEW
« no previous file with comments | « chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.h ('k') | chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698