| 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 size |
| 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 correctly-sized font list. |
| 119 // centering. | 119 // |
| 120 void CalculateFontAndOffsetForHeight(int height, | 120 // The expected layout: |
| 121 gfx::FontList* font_list, | 121 // +--------+-----------------------------------------------+------------+ |
| 122 int* font_y_offset) { | 122 // | | y offset | space | |
| 123 #if defined(OS_WIN) | 123 // | +--------+-------------------+------------------+ above | |
| 124 base::win::ScopedGetDC screen_dc(NULL); | 124 // | | | | internal leading | cap height | |
| 125 #endif | 125 // | box | font | ascent (baseline) +------------------+------------+ |
| 126 | 126 // | height | height | | cap height | |
| 127 while (true) { | 127 // | | |-------------------+------------------+------------+ |
| 128 // TODO(pkasting): Expand the gfx::Font metrics (and underlying Skia | 128 // | | | descent (height - baseline) | space | |
| 129 // metrics) enough to expose the cap height directly. | 129 // | +--------+--------------------------------------+ below | |
| 130 #if defined(OS_WIN) | 130 // | | space at bottom | cap height | |
| 131 const gfx::Font& font = font_list->GetPrimaryFont(); | 131 // +--------+-----------------------------------------------+------------+ |
| 132 base::win::ScopedSelectObject font_in_dc(screen_dc, font.GetNativeFont()); | 132 // Goal: |
| 133 TEXTMETRIC tm = {0}; | 133 // center of box height == center of cap height |
| 134 GetTextMetrics(screen_dc, &tm); | 134 // (i.e. space above cap height == space below cap height) |
| 135 int cap_height = font.GetBaseline() - tm.tmInternalLeading; | 135 // Restrictions: |
| 136 *font_y_offset = ((height - cap_height) / 2) - tm.tmInternalLeading; | 136 // y offset >= 0 |
| 137 #else | 137 // space at bottom >= 0 |
| 138 // Without cap height available, we fall back to centering the full height. | 138 // (i.e. Entire font must be visible inside the box.) |
| 139 *font_y_offset = (height - font_list->GetHeight()) / 2; | 139 gfx::FontList GetLargestFontListWithHeightBound( |
| 140 #endif | 140 const gfx::FontList& base_font_list, |
| 141 | 141 int height) { |
| 142 const int font_size = font_list->GetFontSize(); | 142 gfx::FontList font_list = base_font_list; |
| 143 if (((*font_y_offset >= 0) && | 143 for (int font_size = font_list.GetFontSize(); font_size > 1; --font_size) { |
| 144 ((*font_y_offset + font_list->GetHeight()) <= height)) || | 144 const int internal_leading = |
| 145 (font_size <= 1)) | 145 font_list.GetBaseline() - font_list.GetCapHeight(); |
| 146 return; | 146 const int space = height - font_list.GetCapHeight(); |
| 147 *font_list = font_list->DeriveFontListWithSize(font_size - 1); | 147 const int y_offset = space / 2 - internal_leading; |
| 148 const int space_at_bottom = height - (y_offset + font_list.GetHeight()); |
| 149 if ((y_offset >= 0) && (space_at_bottom >= 0)) |
| 150 break; |
| 151 font_list = font_list.DeriveFontListWithSizeDelta(-1); |
| 148 } | 152 } |
| 153 return font_list; |
| 149 } | 154 } |
| 150 | 155 |
| 151 } // namespace | 156 } // namespace |
| 152 | 157 |
| 153 | 158 |
| 154 // LocationBarView ----------------------------------------------------------- | 159 // LocationBarView ----------------------------------------------------------- |
| 155 | 160 |
| 156 // static | 161 // static |
| 157 const int LocationBarView::kNormalEdgeThickness = 2; | 162 const int LocationBarView::kNormalEdgeThickness = 2; |
| 158 const int LocationBarView::kPopupEdgeThickness = 1; | 163 const int LocationBarView::kPopupEdgeThickness = 1; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 | 246 |
| 242 // Determine the main font. | 247 // Determine the main font. |
| 243 gfx::FontList font_list = ResourceBundle::GetSharedInstance().GetFontList( | 248 gfx::FontList font_list = ResourceBundle::GetSharedInstance().GetFontList( |
| 244 ResourceBundle::BaseFont); | 249 ResourceBundle::BaseFont); |
| 245 const int current_font_size = font_list.GetFontSize(); | 250 const int current_font_size = font_list.GetFontSize(); |
| 246 const int desired_font_size = browser_defaults::kOmniboxFontPixelSize; | 251 const int desired_font_size = browser_defaults::kOmniboxFontPixelSize; |
| 247 if (current_font_size < desired_font_size) | 252 if (current_font_size < desired_font_size) |
| 248 font_list = font_list.DeriveFontListWithSize(desired_font_size); | 253 font_list = font_list.DeriveFontListWithSize(desired_font_size); |
| 249 // Shrink large fonts to make them fit. | 254 // Shrink large fonts to make them fit. |
| 250 // TODO(pkasting): Stretch the location bar instead in this case. | 255 // TODO(pkasting): Stretch the location bar instead in this case. |
| 251 int location_height = GetInternalHeight(true); | 256 const int location_height = GetInternalHeight(true); |
| 252 int font_y_offset; | 257 font_list = GetLargestFontListWithHeightBound(font_list, location_height); |
| 253 CalculateFontAndOffsetForHeight(location_height, &font_list, &font_y_offset); | |
| 254 | 258 |
| 255 // Determine the font for use inside the bubbles. | 259 // Determine the font for use inside the bubbles. The bubble background |
| 256 gfx::FontList bubble_font_list(font_list); | 260 // images have 1 px thick edges, which we don't want to overlap. |
| 257 int bubble_font_y_offset; | |
| 258 // The bubble background images have 1 px thick edges, which we don't want to | |
| 259 // overlap. | |
| 260 const int kBubbleInteriorVerticalPadding = 1; | 261 const int kBubbleInteriorVerticalPadding = 1; |
| 261 CalculateFontAndOffsetForHeight( | 262 const int bubble_vertical_padding = |
| 262 location_height - ((kBubblePadding + kBubbleInteriorVerticalPadding) * 2), | 263 (kBubblePadding + kBubbleInteriorVerticalPadding) * 2; |
| 263 &bubble_font_list, &bubble_font_y_offset); | 264 const gfx::FontList bubble_font_list( |
| 264 bubble_font_y_offset += kBubbleInteriorVerticalPadding; | 265 GetLargestFontListWithHeightBound( |
| 266 font_list, location_height - bubble_vertical_padding)); |
| 265 | 267 |
| 266 const SkColor background_color = | 268 const SkColor background_color = |
| 267 GetColor(ToolbarModel::NONE, LocationBarView::BACKGROUND); | 269 GetColor(ToolbarModel::NONE, LocationBarView::BACKGROUND); |
| 268 ev_bubble_view_ = new EVBubbleView( | 270 ev_bubble_view_ = new EVBubbleView( |
| 269 bubble_font_list, bubble_font_y_offset, | 271 bubble_font_list, GetColor(ToolbarModel::EV_SECURE, SECURITY_TEXT), |
| 270 GetColor(ToolbarModel::EV_SECURE, SECURITY_TEXT), background_color, this); | 272 background_color, this); |
| 271 ev_bubble_view_->set_drag_controller(this); | 273 ev_bubble_view_->set_drag_controller(this); |
| 272 AddChildView(ev_bubble_view_); | 274 AddChildView(ev_bubble_view_); |
| 273 | 275 |
| 274 // Initialize the Omnibox view. | 276 // Initialize the Omnibox view. |
| 275 location_entry_.reset(CreateOmniboxView(this, profile_, command_updater(), | 277 location_entry_.reset(CreateOmniboxView(this, profile_, command_updater(), |
| 276 is_popup_mode_, this, font_list, | 278 is_popup_mode_, this, font_list)); |
| 277 font_y_offset)); | |
| 278 SetLocationEntryFocusable(true); | 279 SetLocationEntryFocusable(true); |
| 279 location_entry_view_ = location_entry_->AddToView(this); | 280 location_entry_view_ = location_entry_->AddToView(this); |
| 280 | 281 |
| 281 // Initialize the inline autocomplete view which is visible only when IME is | 282 // 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. | 283 // turned on. Use the same font with the omnibox and highlighted background. |
| 283 ime_inline_autocomplete_view_ = new views::Label(string16(), font_list); | 284 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); | 285 ime_inline_autocomplete_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 287 ime_inline_autocomplete_view_->SetAutoColorReadabilityEnabled(false); | 286 ime_inline_autocomplete_view_->SetAutoColorReadabilityEnabled(false); |
| 288 ime_inline_autocomplete_view_->set_background( | 287 ime_inline_autocomplete_view_->set_background( |
| 289 views::Background::CreateSolidBackground(GetNativeTheme()->GetSystemColor( | 288 views::Background::CreateSolidBackground(GetNativeTheme()->GetSystemColor( |
| 290 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused))); | 289 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused))); |
| 291 ime_inline_autocomplete_view_->SetEnabledColor( | 290 ime_inline_autocomplete_view_->SetEnabledColor( |
| 292 GetNativeTheme()->GetSystemColor( | 291 GetNativeTheme()->GetSystemColor( |
| 293 ui::NativeTheme::kColorId_TextfieldSelectionColor)); | 292 ui::NativeTheme::kColorId_TextfieldSelectionColor)); |
| 294 ime_inline_autocomplete_view_->SetVisible(false); | 293 ime_inline_autocomplete_view_->SetVisible(false); |
| 295 AddChildView(ime_inline_autocomplete_view_); | 294 AddChildView(ime_inline_autocomplete_view_); |
| 296 | 295 |
| 297 const SkColor text_color = GetColor(ToolbarModel::NONE, TEXT); | 296 const SkColor text_color = GetColor(ToolbarModel::NONE, TEXT); |
| 298 selected_keyword_view_ = new SelectedKeywordView( | 297 selected_keyword_view_ = new SelectedKeywordView( |
| 299 bubble_font_list, bubble_font_y_offset, text_color, background_color, | 298 bubble_font_list, text_color, background_color, profile_); |
| 300 profile_); | |
| 301 AddChildView(selected_keyword_view_); | 299 AddChildView(selected_keyword_view_); |
| 302 | 300 |
| 303 suggested_text_view_ = new views::Label(string16(), font_list); | 301 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); | 302 suggested_text_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 307 suggested_text_view_->SetAutoColorReadabilityEnabled(false); | 303 suggested_text_view_->SetAutoColorReadabilityEnabled(false); |
| 308 suggested_text_view_->SetEnabledColor(GetColor( | 304 suggested_text_view_->SetEnabledColor(GetColor( |
| 309 ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT)); | 305 ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT)); |
| 310 suggested_text_view_->SetVisible(false); | 306 suggested_text_view_->SetVisible(false); |
| 311 AddChildView(suggested_text_view_); | 307 AddChildView(suggested_text_view_); |
| 312 | 308 |
| 313 keyword_hint_view_ = new KeywordHintView( | 309 keyword_hint_view_ = new KeywordHintView( |
| 314 profile_, font_list, font_y_offset, | 310 profile_, font_list, |
| 315 GetColor(ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT), | 311 GetColor(ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT), |
| 316 background_color); | 312 background_color); |
| 317 AddChildView(keyword_hint_view_); | 313 AddChildView(keyword_hint_view_); |
| 318 | 314 |
| 319 mic_search_view_ = new views::ImageButton(this); | 315 mic_search_view_ = new views::ImageButton(this); |
| 320 mic_search_view_->set_id(VIEW_ID_MIC_SEARCH_BUTTON); | 316 mic_search_view_->set_id(VIEW_ID_MIC_SEARCH_BUTTON); |
| 321 mic_search_view_->set_accessibility_focusable(true); | 317 mic_search_view_->set_accessibility_focusable(true); |
| 322 mic_search_view_->SetTooltipText( | 318 mic_search_view_->SetTooltipText( |
| 323 l10n_util::GetStringUTF16(IDS_TOOLTIP_MIC_SEARCH)); | 319 l10n_util::GetStringUTF16(IDS_TOOLTIP_MIC_SEARCH)); |
| 324 mic_search_view_->SetImage( | 320 mic_search_view_->SetImage( |
| 325 views::Button::STATE_NORMAL, | 321 views::Button::STATE_NORMAL, |
| 326 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 322 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 327 IDR_OMNIBOX_MIC_SEARCH)); | 323 IDR_OMNIBOX_MIC_SEARCH)); |
| 328 mic_search_view_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, | 324 mic_search_view_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, |
| 329 views::ImageButton::ALIGN_MIDDLE); | 325 views::ImageButton::ALIGN_MIDDLE); |
| 330 mic_search_view_->SetVisible(false); | 326 mic_search_view_->SetVisible(false); |
| 331 InitTouchableLocationBarChildView(mic_search_view_); | 327 InitTouchableLocationBarChildView(mic_search_view_); |
| 332 AddChildView(mic_search_view_); | 328 AddChildView(mic_search_view_); |
| 333 | 329 |
| 334 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { | 330 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { |
| 335 ContentSettingImageView* content_blocked_view = | 331 ContentSettingImageView* content_blocked_view = |
| 336 new ContentSettingImageView(static_cast<ContentSettingsType>(i), this, | 332 new ContentSettingImageView(static_cast<ContentSettingsType>(i), this, |
| 337 bubble_font_list, bubble_font_y_offset, | 333 bubble_font_list, text_color, |
| 338 text_color, background_color); | 334 background_color); |
| 339 content_setting_views_.push_back(content_blocked_view); | 335 content_setting_views_.push_back(content_blocked_view); |
| 340 content_blocked_view->SetVisible(false); | 336 content_blocked_view->SetVisible(false); |
| 341 AddChildView(content_blocked_view); | 337 AddChildView(content_blocked_view); |
| 342 } | 338 } |
| 343 | 339 |
| 344 generated_credit_card_view_ = new GeneratedCreditCardView(delegate_); | 340 generated_credit_card_view_ = new GeneratedCreditCardView(delegate_); |
| 345 AddChildView(generated_credit_card_view_); | 341 AddChildView(generated_credit_card_view_); |
| 346 | 342 |
| 347 zoom_view_ = new ZoomView(delegate_); | 343 zoom_view_ = new ZoomView(delegate_); |
| 348 zoom_view_->set_id(VIEW_ID_ZOOM_BUTTON); | 344 zoom_view_->set_id(VIEW_ID_ZOOM_BUTTON); |
| (...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 797 gfx::Size suggested_text_size(suggested_text_view_->GetPreferredSize()); | 793 gfx::Size suggested_text_size(suggested_text_view_->GetPreferredSize()); |
| 798 if (suggested_text_size.width() > available_width || | 794 if (suggested_text_size.width() > available_width || |
| 799 text_direction == base::i18n::UNKNOWN_DIRECTION) { | 795 text_direction == base::i18n::UNKNOWN_DIRECTION) { |
| 800 // Hide the suggested text if the user has scrolled or we can't fit all | 796 // 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. | 797 // the suggested text, or we have a mix of RTL and LTR characters. |
| 802 suggested_text_view_->SetBounds(0, 0, 0, 0); | 798 suggested_text_view_->SetBounds(0, 0, 0, 0); |
| 803 } else { | 799 } else { |
| 804 location_needed_width = | 800 location_needed_width = |
| 805 std::min(location_needed_width, | 801 std::min(location_needed_width, |
| 806 location_bounds.width() - suggested_text_size.width()); | 802 location_bounds.width() - suggested_text_size.width()); |
| 807 gfx::Rect suggested_text_bounds(location_bounds.origin(), | 803 gfx::Rect suggested_text_bounds(location_bounds.x(), location_bounds.y(), |
| 808 suggested_text_size); | 804 suggested_text_size.width(), |
| 805 location_bounds.height()); |
| 809 // TODO(sky): figure out why this needs the -1. | 806 // TODO(sky): figure out why this needs the -1. |
| 810 suggested_text_bounds.Offset(location_needed_width - 1, 0); | 807 suggested_text_bounds.Offset(location_needed_width - 1, 0); |
| 811 // For non-views the omnibox needs to be shrunk so that the suggest text | 808 // For non-views the omnibox needs to be shrunk so that the suggest text |
| 812 // is visible. | 809 // is visible. |
| 813 if (!omnibox_views) | 810 if (!omnibox_views) |
| 814 location_bounds.set_width(location_needed_width); | 811 location_bounds.set_width(location_needed_width); |
| 815 | 812 |
| 816 // We reverse the order of the location entry and suggested text if: | 813 // We reverse the order of the location entry and suggested text if: |
| 817 // - Chrome is RTL but the text is fully LTR, or | 814 // - Chrome is RTL but the text is fully LTR, or |
| 818 // - Chrome is LTR but the text is fully RTL. | 815 // - 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 | 845 // 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. | 846 // RTL so far. In other words, no testable RTL environment so far. |
| 850 int x = location_needed_width; | 847 int x = location_needed_width; |
| 851 if (width > entry_width) | 848 if (width > entry_width) |
| 852 x = 0; | 849 x = 0; |
| 853 else if (location_needed_width + width > entry_width) | 850 else if (location_needed_width + width > entry_width) |
| 854 x = entry_width - width; | 851 x = entry_width - width; |
| 855 location_bounds.set_width(x); | 852 location_bounds.set_width(x); |
| 856 ime_inline_autocomplete_view_->SetBounds( | 853 ime_inline_autocomplete_view_->SetBounds( |
| 857 location_bounds.right(), location_bounds.y(), | 854 location_bounds.right(), location_bounds.y(), |
| 858 std::min(width, entry_width), | 855 std::min(width, entry_width), location_bounds.height()); |
| 859 ime_inline_autocomplete_view_->GetPreferredSize().height()); | |
| 860 } | 856 } |
| 861 | 857 |
| 862 location_entry_view_->SetBoundsRect(location_bounds); | 858 location_entry_view_->SetBoundsRect(location_bounds); |
| 863 } | 859 } |
| 864 | 860 |
| 865 void LocationBarView::OnPaint(gfx::Canvas* canvas) { | 861 void LocationBarView::OnPaint(gfx::Canvas* canvas) { |
| 866 View::OnPaint(canvas); | 862 View::OnPaint(canvas); |
| 867 | 863 |
| 868 // Fill the location bar background color behind the border. Parts of the | 864 // 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 | 865 // 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); | 1495 bounds.Inset(-(horizontal_padding + 1) / 2, 0); |
| 1500 location_bar_util::PaintExtensionActionBackground( | 1496 location_bar_util::PaintExtensionActionBackground( |
| 1501 *(*page_action_view)->image_view()->page_action(), | 1497 *(*page_action_view)->image_view()->page_action(), |
| 1502 tab_id, canvas, bounds, text_color, background_color); | 1498 tab_id, canvas, bounds, text_color, background_color); |
| 1503 } | 1499 } |
| 1504 } | 1500 } |
| 1505 | 1501 |
| 1506 void LocationBarView::AccessibilitySetValue(const string16& new_value) { | 1502 void LocationBarView::AccessibilitySetValue(const string16& new_value) { |
| 1507 location_entry_->SetUserText(new_value); | 1503 location_entry_->SetUserText(new_value); |
| 1508 } | 1504 } |
| OLD | NEW |