OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #import "chrome/browser/ui/cocoa/location_bar/keyword_hint_decoration.h" | 9 #import "chrome/browser/ui/cocoa/location_bar/keyword_hint_decoration.h" |
10 | 10 |
(...skipping 14 matching lines...) Expand all Loading... |
25 const CGFloat kHintImageYInset = 4.0; | 25 const CGFloat kHintImageYInset = 4.0; |
26 | 26 |
27 // Extra padding right and left of the image. | 27 // Extra padding right and left of the image. |
28 const CGFloat kHintImagePadding = 1.0; | 28 const CGFloat kHintImagePadding = 1.0; |
29 | 29 |
30 // Maxmimum of the available space to allow the hint to take over. | 30 // Maxmimum of the available space to allow the hint to take over. |
31 // Should leave enough so that the user has space to edit things. | 31 // Should leave enough so that the user has space to edit things. |
32 const CGFloat kHintAvailableRatio = 2.0 / 3.0; | 32 const CGFloat kHintAvailableRatio = 2.0 / 3.0; |
33 | 33 |
34 // Extra padding at the right of the decoration. | 34 // Extra padding at the right of the decoration. |
35 const CGFloat kHintRightPadding = 5.0; | 35 const CGFloat kHintTrailingPadding = 5.0; |
36 | 36 |
37 // Helper to convert |s| to an |NSString|, trimming whitespace at | 37 // Helper to convert |s| to an |NSString|, trimming whitespace at |
38 // ends. | 38 // ends. |
39 NSString* TrimAndConvert(const base::string16& s) { | 39 NSString* TrimAndConvert(const base::string16& s) { |
40 base::string16 output; | 40 base::string16 output; |
41 base::TrimWhitespace(s, base::TRIM_ALL, &output); | 41 base::TrimWhitespace(s, base::TRIM_ALL, &output); |
42 return base::SysUTF16ToNSString(output); | 42 return base::SysUTF16ToNSString(output); |
43 } | 43 } |
44 | 44 |
45 } // namespace | 45 } // namespace |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 return kOmittedWidth; | 100 return kOmittedWidth; |
101 | 101 |
102 // Show the full hint if it won't take up too much space. The image | 102 // Show the full hint if it won't take up too much space. The image |
103 // needs to be placed at a pixel boundary, round the text widths so | 103 // needs to be placed at a pixel boundary, round the text widths so |
104 // that any partially-drawn pixels don't look too close (or too | 104 // that any partially-drawn pixels don't look too close (or too |
105 // far). | 105 // far). |
106 CGFloat full_width = | 106 CGFloat full_width = |
107 std::floor(GetLabelSize(hint_prefix_, attributes_).width + 0.5) + | 107 std::floor(GetLabelSize(hint_prefix_, attributes_).width + 0.5) + |
108 kHintImagePadding + image_width + kHintImagePadding + | 108 kHintImagePadding + image_width + kHintImagePadding + |
109 std::floor(GetLabelSize(hint_suffix_, attributes_).width + 0.5) + | 109 std::floor(GetLabelSize(hint_suffix_, attributes_).width + 0.5) + |
110 kHintRightPadding; | 110 kHintTrailingPadding; |
111 if (full_width <= width * kHintAvailableRatio) | 111 if (full_width <= width * kHintAvailableRatio) |
112 return full_width; | 112 return full_width; |
113 | 113 |
114 return image_width; | 114 return image_width; |
115 } | 115 } |
116 | 116 |
117 void KeywordHintDecoration::DrawInFrame(NSRect frame, NSView* control_view) { | 117 void KeywordHintDecoration::DrawInFrame(NSRect frame, NSView* control_view) { |
118 NSImage* image = GetHintImage(); | 118 NSImage* image = GetHintImage(); |
119 const CGFloat image_width = image ? [image size].width : 0.0; | 119 const CGFloat image_width = image ? [image size].width : 0.0; |
120 | 120 |
121 const bool draw_full = NSWidth(frame) > image_width; | 121 const bool draw_full = NSWidth(frame) > image_width; |
122 | 122 |
123 BOOL is_rtl = cocoa_l10n_util::ShouldDoExperimentalRTLLayout(); | 123 BOOL is_rtl = cocoa_l10n_util::ShouldDoExperimentalRTLLayout(); |
124 NSString* left_string = is_rtl ? hint_suffix_ : hint_prefix_; | 124 NSString* left_string = is_rtl ? hint_suffix_ : hint_prefix_; |
125 NSString* right_string = is_rtl ? hint_prefix_ : hint_suffix_; | 125 NSString* right_string = is_rtl ? hint_prefix_ : hint_suffix_; |
| 126 if (is_rtl) { |
| 127 frame.origin.x += kHintTrailingPadding; |
| 128 frame.size.width -= kHintTrailingPadding; |
| 129 } |
126 | 130 |
127 if (draw_full) { | 131 if (draw_full) { |
128 NSRect left_rect = frame; | 132 NSRect left_rect = frame; |
129 const CGFloat left_width = GetLabelSize(left_string, attributes_).width; | 133 const CGFloat left_width = GetLabelSize(left_string, attributes_).width; |
130 left_rect.size.width = left_width; | 134 left_rect.size.width = left_width; |
131 DrawLabel(left_string, attributes_, left_rect); | 135 DrawLabel(left_string, attributes_, left_rect); |
132 // The image should be drawn at a pixel boundary, round the prefix | 136 // The image should be drawn at a pixel boundary, round the prefix |
133 // so that partial pixels aren't oddly close (or distant). | 137 // so that partial pixels aren't oddly close (or distant). |
134 frame.origin.x += std::floor(left_width + 0.5) + kHintImagePadding; | 138 frame.origin.x += std::floor(left_width + 0.5) + kHintImagePadding; |
135 frame.size.width -= std::floor(left_width + 0.5) + kHintImagePadding; | 139 frame.size.width -= std::floor(left_width + 0.5) + kHintImagePadding; |
(...skipping 16 matching lines...) Expand all Loading... |
152 | 156 |
153 // Draw the text kHintImagePadding away from [tab] icon so that | 157 // Draw the text kHintImagePadding away from [tab] icon so that |
154 // equal amount of space is maintained on either side of the icon. | 158 // equal amount of space is maintained on either side of the icon. |
155 // This also ensures that suffix text is at the same distance | 159 // This also ensures that suffix text is at the same distance |
156 // from [tab] icon in different web pages. | 160 // from [tab] icon in different web pages. |
157 right_rect.origin.x += kHintImagePadding; | 161 right_rect.origin.x += kHintImagePadding; |
158 DCHECK_GE(NSWidth(right_rect), right_width); | 162 DCHECK_GE(NSWidth(right_rect), right_width); |
159 DrawLabel(right_string, attributes_, right_rect); | 163 DrawLabel(right_string, attributes_, right_rect); |
160 } | 164 } |
161 } | 165 } |
OLD | NEW |