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 "chrome/browser/ui/views/location_bar/location_bar_view.h" | 5 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 using views::View; | 106 using views::View; |
107 | 107 |
108 | 108 |
109 namespace { | 109 namespace { |
110 | 110 |
111 Browser* GetBrowserFromDelegate(LocationBarView::Delegate* delegate) { | 111 Browser* GetBrowserFromDelegate(LocationBarView::Delegate* delegate) { |
112 WebContents* contents = delegate->GetWebContents(); | 112 WebContents* contents = delegate->GetWebContents(); |
113 return contents ? chrome::FindBrowserWithWebContents(contents) : NULL; | 113 return contents ? chrome::FindBrowserWithWebContents(contents) : NULL; |
114 } | 114 } |
115 | 115 |
116 // Given a containing |height| and a base |font_list|, shrinks the fonts until | 116 // Given a containing |height| and a |base_font_list|, shrinks the font list |
Peter Kasting
2013/10/18 21:11:09
Nit: font list -> font size
Yuki
2013/10/21 09:44:41
Done.
| |
117 // the primary font will fit within |height| while having its cap height | 117 // until the font list will fit within |height| while having its cap height |
118 // vertically centered. Returns the |font_y_offset| needed to produce this | 118 // vertically centered. Returns the shrinked font list. |
Peter Kasting
2013/10/18 21:11:09
Nit: shrinked -> correctly-sized
Yuki
2013/10/21 09:44:41
Done.
| |
119 // centering. | 119 gfx::FontList GetLargestFontListWithHeightBound( |
120 void CalculateFontAndOffsetForHeight(int height, | 120 const gfx::FontList& base_font_list, |
121 gfx::FontList* font_list, | 121 int height) { |
122 int* font_y_offset) { | 122 gfx::FontList font_list = base_font_list; |
123 #if defined(OS_WIN) | |
124 base::win::ScopedGetDC screen_dc(NULL); | |
125 #endif | |
126 | |
127 while (true) { | 123 while (true) { |
128 // TODO(pkasting): Expand the gfx::Font metrics (and underlying Skia | 124 const int y_offset = (height - font_list.GetCapHeight()) / 2 - |
129 // metrics) enough to expose the cap height directly. | 125 (font_list.GetBaseline() - font_list.GetCapHeight()); |
Peter Kasting
2013/10/18 21:11:09
Nit: Indent 4, not even
Yuki
2013/10/21 09:44:41
Done.
| |
130 #if defined(OS_WIN) | 126 if ((y_offset >= 0 && |
Peter Kasting
2013/10/18 21:11:09
Nit: Parens around binary subexprs (omnibox subsys
Yuki
2013/10/21 09:44:41
Done.
| |
131 const gfx::Font& font = font_list->GetPrimaryFont(); | 127 y_offset + font_list.GetHeight() <= height) || |
132 base::win::ScopedSelectObject font_in_dc(screen_dc, font.GetNativeFont()); | 128 font_list.GetFontSize() <= 1) |
133 TEXTMETRIC tm = {0}; | 129 return font_list; |
134 GetTextMetrics(screen_dc, &tm); | 130 font_list = font_list.DeriveFontListWithSizeDelta(-1); |
135 int cap_height = font.GetBaseline() - tm.tmInternalLeading; | |
136 *font_y_offset = ((height - cap_height) / 2) - tm.tmInternalLeading; | |
137 #else | |
138 // Without cap height available, we fall back to centering the full height. | |
139 *font_y_offset = (height - font_list->GetHeight()) / 2; | |
140 #endif | |
141 | |
142 const int font_size = font_list->GetFontSize(); | |
143 if (((*font_y_offset >= 0) && | |
144 ((*font_y_offset + font_list->GetHeight()) <= height)) || | |
145 (font_size <= 1)) | |
146 return; | |
147 *font_list = font_list->DeriveFontListWithSize(font_size - 1); | |
148 } | 131 } |
149 } | 132 } |
150 | 133 |
151 } // namespace | 134 } // namespace |
152 | 135 |
153 | 136 |
154 // LocationBarView ----------------------------------------------------------- | 137 // LocationBarView ----------------------------------------------------------- |
155 | 138 |
156 // static | 139 // static |
157 const int LocationBarView::kNormalEdgeThickness = 2; | 140 const int LocationBarView::kNormalEdgeThickness = 2; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
242 // Determine the main font. | 225 // Determine the main font. |
243 gfx::FontList font_list = ResourceBundle::GetSharedInstance().GetFontList( | 226 gfx::FontList font_list = ResourceBundle::GetSharedInstance().GetFontList( |
244 ResourceBundle::BaseFont); | 227 ResourceBundle::BaseFont); |
245 const int current_font_size = font_list.GetFontSize(); | 228 const int current_font_size = font_list.GetFontSize(); |
246 const int desired_font_size = browser_defaults::kOmniboxFontPixelSize; | 229 const int desired_font_size = browser_defaults::kOmniboxFontPixelSize; |
247 if (current_font_size < desired_font_size) | 230 if (current_font_size < desired_font_size) |
248 font_list = font_list.DeriveFontListWithSize(desired_font_size); | 231 font_list = font_list.DeriveFontListWithSize(desired_font_size); |
249 // Shrink large fonts to make them fit. | 232 // Shrink large fonts to make them fit. |
250 // TODO(pkasting): Stretch the location bar instead in this case. | 233 // TODO(pkasting): Stretch the location bar instead in this case. |
251 int location_height = GetInternalHeight(true); | 234 int location_height = GetInternalHeight(true); |
252 int font_y_offset; | 235 font_list = GetLargestFontListWithHeightBound(font_list, location_height); |
253 CalculateFontAndOffsetForHeight(location_height, &font_list, &font_y_offset); | |
254 | 236 |
255 // Determine the font for use inside the bubbles. | 237 // Determine the font for use inside the bubbles. |
256 gfx::FontList bubble_font_list(font_list); | |
257 int bubble_font_y_offset; | |
258 // The bubble background images have 1 px thick edges, which we don't want to | 238 // The bubble background images have 1 px thick edges, which we don't want to |
259 // overlap. | 239 // overlap. |
260 const int kBubbleInteriorVerticalPadding = 1; | 240 const int kBubbleInteriorVerticalPadding = 1; |
261 CalculateFontAndOffsetForHeight( | 241 const int bubble_vertical_padding = |
262 location_height - ((kBubblePadding + kBubbleInteriorVerticalPadding) * 2), | 242 (kBubblePadding + kBubbleInteriorVerticalPadding) * 2; |
263 &bubble_font_list, &bubble_font_y_offset); | 243 const gfx::FontList bubble_font_list( |
264 bubble_font_y_offset += kBubbleInteriorVerticalPadding; | 244 GetLargestFontListWithHeightBound( |
245 font_list, location_height - bubble_vertical_padding)); | |
265 | 246 |
266 const SkColor background_color = | 247 const SkColor background_color = |
267 GetColor(ToolbarModel::NONE, LocationBarView::BACKGROUND); | 248 GetColor(ToolbarModel::NONE, LocationBarView::BACKGROUND); |
268 ev_bubble_view_ = new EVBubbleView( | 249 ev_bubble_view_ = new EVBubbleView( |
269 bubble_font_list, bubble_font_y_offset, | 250 bubble_font_list, kBubbleInteriorVerticalPadding, |
Peter Kasting
2013/10/18 21:11:09
Instead of passing this interior padding value to
Yuki
2013/10/21 09:44:41
I've removed kBubbleInteriorVerticalPadding, and i
| |
270 GetColor(ToolbarModel::EV_SECURE, SECURITY_TEXT), background_color, this); | 251 GetColor(ToolbarModel::EV_SECURE, SECURITY_TEXT), background_color, this); |
271 ev_bubble_view_->set_drag_controller(this); | 252 ev_bubble_view_->set_drag_controller(this); |
272 AddChildView(ev_bubble_view_); | 253 AddChildView(ev_bubble_view_); |
273 | 254 |
274 // Initialize the Omnibox view. | 255 // Initialize the Omnibox view. |
275 location_entry_.reset(CreateOmniboxView(this, profile_, command_updater(), | 256 location_entry_.reset(CreateOmniboxView(this, profile_, command_updater(), |
276 is_popup_mode_, this, font_list, | 257 is_popup_mode_, this, font_list)); |
277 font_y_offset)); | |
278 SetLocationEntryFocusable(true); | 258 SetLocationEntryFocusable(true); |
279 location_entry_view_ = location_entry_->AddToView(this); | 259 location_entry_view_ = location_entry_->AddToView(this); |
280 | 260 |
281 // Initialize the inline autocomplete view which is visible only when IME is | 261 // Initialize the inline autocomplete view which is visible only when IME is |
282 // turned on. Use the same font with the omnibox and highlighted background. | 262 // turned on. Use the same font with the omnibox and highlighted background. |
283 ime_inline_autocomplete_view_ = new views::Label(string16(), font_list); | 263 ime_inline_autocomplete_view_ = new views::Label(string16(), font_list); |
284 ime_inline_autocomplete_view_->set_border( | |
285 views::Border::CreateEmptyBorder(font_y_offset, 0, 0, 0)); | |
286 ime_inline_autocomplete_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 264 ime_inline_autocomplete_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
287 ime_inline_autocomplete_view_->SetAutoColorReadabilityEnabled(false); | 265 ime_inline_autocomplete_view_->SetAutoColorReadabilityEnabled(false); |
288 ime_inline_autocomplete_view_->set_background( | 266 ime_inline_autocomplete_view_->set_background( |
289 views::Background::CreateSolidBackground(GetNativeTheme()->GetSystemColor( | 267 views::Background::CreateSolidBackground(GetNativeTheme()->GetSystemColor( |
290 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused))); | 268 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused))); |
291 ime_inline_autocomplete_view_->SetEnabledColor( | 269 ime_inline_autocomplete_view_->SetEnabledColor( |
292 GetNativeTheme()->GetSystemColor( | 270 GetNativeTheme()->GetSystemColor( |
293 ui::NativeTheme::kColorId_TextfieldSelectionColor)); | 271 ui::NativeTheme::kColorId_TextfieldSelectionColor)); |
294 ime_inline_autocomplete_view_->SetVisible(false); | 272 ime_inline_autocomplete_view_->SetVisible(false); |
295 AddChildView(ime_inline_autocomplete_view_); | 273 AddChildView(ime_inline_autocomplete_view_); |
296 | 274 |
297 const SkColor text_color = GetColor(ToolbarModel::NONE, TEXT); | 275 const SkColor text_color = GetColor(ToolbarModel::NONE, TEXT); |
298 selected_keyword_view_ = new SelectedKeywordView( | 276 selected_keyword_view_ = new SelectedKeywordView( |
299 bubble_font_list, bubble_font_y_offset, text_color, background_color, | 277 bubble_font_list, kBubbleInteriorVerticalPadding, text_color, |
300 profile_); | 278 background_color, profile_); |
301 AddChildView(selected_keyword_view_); | 279 AddChildView(selected_keyword_view_); |
302 | 280 |
303 suggested_text_view_ = new views::Label(string16(), font_list); | 281 suggested_text_view_ = new views::Label(string16(), font_list); |
304 suggested_text_view_->set_border( | |
305 views::Border::CreateEmptyBorder(font_y_offset, 0, 0, 0)); | |
306 suggested_text_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 282 suggested_text_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
307 suggested_text_view_->SetAutoColorReadabilityEnabled(false); | 283 suggested_text_view_->SetAutoColorReadabilityEnabled(false); |
308 suggested_text_view_->SetEnabledColor(GetColor( | 284 suggested_text_view_->SetEnabledColor(GetColor( |
309 ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT)); | 285 ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT)); |
310 suggested_text_view_->SetVisible(false); | 286 suggested_text_view_->SetVisible(false); |
311 AddChildView(suggested_text_view_); | 287 AddChildView(suggested_text_view_); |
312 | 288 |
313 keyword_hint_view_ = new KeywordHintView( | 289 keyword_hint_view_ = new KeywordHintView( |
314 profile_, font_list, font_y_offset, | 290 profile_, font_list, |
315 GetColor(ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT), | 291 GetColor(ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT), |
316 background_color); | 292 background_color); |
317 AddChildView(keyword_hint_view_); | 293 AddChildView(keyword_hint_view_); |
318 | 294 |
319 mic_search_view_ = new views::ImageButton(this); | 295 mic_search_view_ = new views::ImageButton(this); |
320 mic_search_view_->set_id(VIEW_ID_MIC_SEARCH_BUTTON); | 296 mic_search_view_->set_id(VIEW_ID_MIC_SEARCH_BUTTON); |
321 mic_search_view_->set_accessibility_focusable(true); | 297 mic_search_view_->set_accessibility_focusable(true); |
322 mic_search_view_->SetTooltipText( | 298 mic_search_view_->SetTooltipText( |
323 l10n_util::GetStringUTF16(IDS_TOOLTIP_MIC_SEARCH)); | 299 l10n_util::GetStringUTF16(IDS_TOOLTIP_MIC_SEARCH)); |
324 mic_search_view_->SetImage( | 300 mic_search_view_->SetImage( |
325 views::Button::STATE_NORMAL, | 301 views::Button::STATE_NORMAL, |
326 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 302 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
327 IDR_OMNIBOX_MIC_SEARCH)); | 303 IDR_OMNIBOX_MIC_SEARCH)); |
328 mic_search_view_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, | 304 mic_search_view_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, |
329 views::ImageButton::ALIGN_MIDDLE); | 305 views::ImageButton::ALIGN_MIDDLE); |
330 mic_search_view_->SetVisible(false); | 306 mic_search_view_->SetVisible(false); |
331 InitTouchableLocationBarChildView(mic_search_view_); | 307 InitTouchableLocationBarChildView(mic_search_view_); |
332 AddChildView(mic_search_view_); | 308 AddChildView(mic_search_view_); |
333 | 309 |
334 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { | 310 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { |
335 ContentSettingImageView* content_blocked_view = | 311 ContentSettingImageView* content_blocked_view = |
336 new ContentSettingImageView(static_cast<ContentSettingsType>(i), this, | 312 new ContentSettingImageView(static_cast<ContentSettingsType>(i), this, |
337 bubble_font_list, bubble_font_y_offset, | 313 bubble_font_list, |
314 kBubbleInteriorVerticalPadding, | |
338 text_color, background_color); | 315 text_color, background_color); |
339 content_setting_views_.push_back(content_blocked_view); | 316 content_setting_views_.push_back(content_blocked_view); |
340 content_blocked_view->SetVisible(false); | 317 content_blocked_view->SetVisible(false); |
341 AddChildView(content_blocked_view); | 318 AddChildView(content_blocked_view); |
342 } | 319 } |
343 | 320 |
344 generated_credit_card_view_ = new GeneratedCreditCardView(delegate_); | 321 generated_credit_card_view_ = new GeneratedCreditCardView(delegate_); |
345 AddChildView(generated_credit_card_view_); | 322 AddChildView(generated_credit_card_view_); |
346 | 323 |
347 zoom_view_ = new ZoomView(delegate_); | 324 zoom_view_ = new ZoomView(delegate_); |
(...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
797 gfx::Size suggested_text_size(suggested_text_view_->GetPreferredSize()); | 774 gfx::Size suggested_text_size(suggested_text_view_->GetPreferredSize()); |
798 if (suggested_text_size.width() > available_width || | 775 if (suggested_text_size.width() > available_width || |
799 text_direction == base::i18n::UNKNOWN_DIRECTION) { | 776 text_direction == base::i18n::UNKNOWN_DIRECTION) { |
800 // Hide the suggested text if the user has scrolled or we can't fit all | 777 // Hide the suggested text if the user has scrolled or we can't fit all |
801 // the suggested text, or we have a mix of RTL and LTR characters. | 778 // the suggested text, or we have a mix of RTL and LTR characters. |
802 suggested_text_view_->SetBounds(0, 0, 0, 0); | 779 suggested_text_view_->SetBounds(0, 0, 0, 0); |
803 } else { | 780 } else { |
804 location_needed_width = | 781 location_needed_width = |
805 std::min(location_needed_width, | 782 std::min(location_needed_width, |
806 location_bounds.width() - suggested_text_size.width()); | 783 location_bounds.width() - suggested_text_size.width()); |
807 gfx::Rect suggested_text_bounds(location_bounds.origin(), | 784 gfx::Rect suggested_text_bounds(location_bounds.x(), location_bounds.y(), |
808 suggested_text_size); | 785 suggested_text_size.width(), |
786 location_bounds.height()); | |
809 // TODO(sky): figure out why this needs the -1. | 787 // TODO(sky): figure out why this needs the -1. |
810 suggested_text_bounds.Offset(location_needed_width - 1, 0); | 788 suggested_text_bounds.Offset(location_needed_width - 1, 0); |
811 // For non-views the omnibox needs to be shrunk so that the suggest text | 789 // For non-views the omnibox needs to be shrunk so that the suggest text |
812 // is visible. | 790 // is visible. |
813 if (!omnibox_views) | 791 if (!omnibox_views) |
814 location_bounds.set_width(location_needed_width); | 792 location_bounds.set_width(location_needed_width); |
815 | 793 |
816 // We reverse the order of the location entry and suggested text if: | 794 // We reverse the order of the location entry and suggested text if: |
817 // - Chrome is RTL but the text is fully LTR, or | 795 // - Chrome is RTL but the text is fully LTR, or |
818 // - Chrome is LTR but the text is fully RTL. | 796 // - Chrome is LTR but the text is fully RTL. |
(...skipping 29 matching lines...) Expand all Loading... | |
848 // All the target languages (IMEs) are LTR, and we do not need to support | 826 // All the target languages (IMEs) are LTR, and we do not need to support |
849 // RTL so far. In other words, no testable RTL environment so far. | 827 // RTL so far. In other words, no testable RTL environment so far. |
850 int x = location_needed_width; | 828 int x = location_needed_width; |
851 if (width > entry_width) | 829 if (width > entry_width) |
852 x = 0; | 830 x = 0; |
853 else if (location_needed_width + width > entry_width) | 831 else if (location_needed_width + width > entry_width) |
854 x = entry_width - width; | 832 x = entry_width - width; |
855 location_bounds.set_width(x); | 833 location_bounds.set_width(x); |
856 ime_inline_autocomplete_view_->SetBounds( | 834 ime_inline_autocomplete_view_->SetBounds( |
857 location_bounds.right(), location_bounds.y(), | 835 location_bounds.right(), location_bounds.y(), |
858 std::min(width, entry_width), | 836 std::min(width, entry_width), location_bounds.height()); |
859 ime_inline_autocomplete_view_->GetPreferredSize().height()); | |
860 } | 837 } |
861 | 838 |
862 location_entry_view_->SetBoundsRect(location_bounds); | 839 location_entry_view_->SetBoundsRect(location_bounds); |
863 } | 840 } |
864 | 841 |
865 void LocationBarView::OnPaint(gfx::Canvas* canvas) { | 842 void LocationBarView::OnPaint(gfx::Canvas* canvas) { |
866 View::OnPaint(canvas); | 843 View::OnPaint(canvas); |
867 | 844 |
868 // Fill the location bar background color behind the border. Parts of the | 845 // Fill the location bar background color behind the border. Parts of the |
869 // border images are meant to rest atop the toolbar background and parts atop | 846 // border images are meant to rest atop the toolbar background and parts atop |
(...skipping 629 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1499 bounds.Inset(-(horizontal_padding + 1) / 2, 0); | 1476 bounds.Inset(-(horizontal_padding + 1) / 2, 0); |
1500 location_bar_util::PaintExtensionActionBackground( | 1477 location_bar_util::PaintExtensionActionBackground( |
1501 *(*page_action_view)->image_view()->page_action(), | 1478 *(*page_action_view)->image_view()->page_action(), |
1502 tab_id, canvas, bounds, text_color, background_color); | 1479 tab_id, canvas, bounds, text_color, background_color); |
1503 } | 1480 } |
1504 } | 1481 } |
1505 | 1482 |
1506 void LocationBarView::AccessibilitySetValue(const string16& new_value) { | 1483 void LocationBarView::AccessibilitySetValue(const string16& new_value) { |
1507 location_entry_->SetUserText(new_value); | 1484 location_entry_->SetUserText(new_value); |
1508 } | 1485 } |
OLD | NEW |