1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #import "chrome/browser/ui/cocoa/location_bar/keyword_hint_decoration.h"
9 #include "base/logging.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "chrome/grit/generated_resources.h"
13 #include "grit/theme_resources.h"
14 #include "skia/ext/skia_utils_mac.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/base/resource/resource_bundle.h"
20 // How far to inset the hint image from sides. Lines baseline of text
21 // in image with baseline of prefix and suffix.
22 const CGFloat kHintImageYInset = 4.0;
24 // Extra padding right and left of the image.
25 const CGFloat kHintImagePadding = 1.0;
27 // Maxmimum of the available space to allow the hint to take over.
28 // Should leave enough so that the user has space to edit things.
29 const CGFloat kHintAvailableRatio = 2.0 / 3.0;
31 // Helper to convert |s| to an |NSString|, trimming whitespace at
33 NSString* TrimAndConvert(const base::string16& s) {
34 base::string16 output;
35 base::TrimWhitespace(s, base::TRIM_ALL, &output);
36 return base::SysUTF16ToNSString(output);
41 KeywordHintDecoration::KeywordHintDecoration() {
42 NSColor* text_color = [NSColor lightGrayColor];
43 attributes_.reset([@{ NSFontAttributeName : GetFont(),
44 NSForegroundColorAttributeName : text_color
48 KeywordHintDecoration::~KeywordHintDecoration() {
51 NSImage* KeywordHintDecoration::GetHintImage() {
53 hint_image_.reset(ResourceBundle::GetSharedInstance().
54 GetNativeImageNamed(IDR_OMNIBOX_KEYWORD_HINT_TAB).CopyNSImage());
59 void KeywordHintDecoration::SetKeyword(const base::string16& short_name,
60 bool is_extension_keyword) {
61 // KEYWORD_HINT is a message like "Press [tab] to search <site>".
62 // [tab] is a parameter to be replaced by an image. "<site>" is
63 // derived from |short_name|.
64 std::vector<size_t> content_param_offsets;
65 int message_id = is_extension_keyword ?
66 IDS_OMNIBOX_EXTENSION_KEYWORD_HINT : IDS_OMNIBOX_KEYWORD_HINT;
67 const base::string16 keyword_hint(
68 l10n_util::GetStringFUTF16(message_id,
69 base::string16(), short_name,
70 &content_param_offsets));
72 // Should always be 2 offsets, see the comment in
73 // location_bar_view.cc after IDS_OMNIBOX_KEYWORD_HINT fetch.
74 DCHECK_EQ(content_param_offsets.size(), 2U);
76 // Where to put the [tab] image.
77 const size_t split = content_param_offsets.front();
79 // Trim the spaces from the edges (there is space in the image) and
80 // convert to |NSString|.
81 hint_prefix_.reset([TrimAndConvert(keyword_hint.substr(0, split)) retain]);
82 hint_suffix_.reset([TrimAndConvert(keyword_hint.substr(split)) retain]);
85 CGFloat KeywordHintDecoration::GetWidthForSpace(CGFloat width) {
86 NSImage* image = GetHintImage();
87 const CGFloat image_width = image ? [image size].width : 0.0;
89 // AFAICT, on Windows the choices are "everything" if it fits, then
90 // "image only" if it fits.
92 // Entirely too small to fit, omit.
93 if (width < image_width)
96 // Show the full hint if it won't take up too much space. The image
97 // needs to be placed at a pixel boundary, round the text widths so
98 // that any partially-drawn pixels don't look too close (or too
101 std::floor(GetLabelSize(hint_prefix_, attributes_).width + 0.5) +
102 kHintImagePadding + image_width + kHintImagePadding +
103 std::floor(GetLabelSize(hint_suffix_, attributes_).width + 0.5);
104 if (full_width <= width * kHintAvailableRatio)
110 void KeywordHintDecoration::DrawInFrame(NSRect frame, NSView* control_view) {
111 NSImage* image = GetHintImage();
112 const CGFloat image_width = image ? [image size].width : 0.0;
114 const bool draw_full = NSWidth(frame) > image_width;
117 NSRect prefix_rect = frame;
118 const CGFloat prefix_width = GetLabelSize(hint_prefix_, attributes_).width;
119 DCHECK_GE(NSWidth(prefix_rect), prefix_width);
120 DrawLabel(hint_prefix_, attributes_, prefix_rect);
122 // The image should be drawn at a pixel boundary, round the prefix
123 // so that partial pixels aren't oddly close (or distant).
124 frame.origin.x += std::floor(prefix_width + 0.5) + kHintImagePadding;
125 frame.size.width -= std::floor(prefix_width + 0.5) + kHintImagePadding;
128 NSRect image_rect = NSInsetRect(frame, 0.0, kHintImageYInset);
129 image_rect.size = [image size];
130 [image drawInRect:image_rect
131 fromRect:NSZeroRect // Entire image
132 operation:NSCompositeSourceOver
136 frame.origin.x += NSWidth(image_rect);
137 frame.size.width -= NSWidth(image_rect);
140 NSRect suffix_rect = frame;
141 const CGFloat suffix_width = GetLabelSize(hint_suffix_, attributes_).width;
143 // Draw the text kHintImagePadding away from [tab] icon so that
144 // equal amount of space is maintained on either side of the icon.
145 // This also ensures that suffix text is at the same distance
146 // from [tab] icon in different web pages.
147 suffix_rect.origin.x += kHintImagePadding;
148 DCHECK_GE(NSWidth(suffix_rect), suffix_width);
149 DrawLabel(hint_suffix_, attributes_, suffix_rect);