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 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) { |
msw
2013/10/23 01:18:00
nit: rewrite to avoid while(true), and/or return b
Yuki
2013/10/24 14:32:54
Done.
| |
128 // TODO(pkasting): Expand the gfx::Font metrics (and underlying Skia | 124 const int y_offset = (height - font_list.GetCapHeight()) / 2 - |
msw
2013/10/23 01:18:00
nit: this calculation isn't entirely straightforwa
Yuki
2013/10/24 14:32:54
Done.
Yuki
2013/10/24 14:32:54
Done.
| |
129 // metrics) enough to expose the cap height directly. | 125 (font_list.GetBaseline() - font_list.GetCapHeight()); |
130 #if defined(OS_WIN) | 126 if (((y_offset >= 0) && |
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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
241 | 224 |
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); |
msw
2013/10/23 01:18:00
nit: const.
Yuki
2013/10/24 14:32:54
Done.
| |
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 |
msw
2013/10/23 01:18:00
nit: combine this comment with the one above (wrap
Yuki
2013/10/24 14:32:54
Done.
| |
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, GetColor(ToolbarModel::EV_SECURE, SECURITY_TEXT), |
270 GetColor(ToolbarModel::EV_SECURE, SECURITY_TEXT), background_color, this); | 251 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, text_color, background_color, profile_); |
300 profile_); | |
301 AddChildView(selected_keyword_view_); | 278 AddChildView(selected_keyword_view_); |
302 | 279 |
303 suggested_text_view_ = new views::Label(string16(), font_list); | 280 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); | 281 suggested_text_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
307 suggested_text_view_->SetAutoColorReadabilityEnabled(false); | 282 suggested_text_view_->SetAutoColorReadabilityEnabled(false); |
308 suggested_text_view_->SetEnabledColor(GetColor( | 283 suggested_text_view_->SetEnabledColor(GetColor( |
309 ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT)); | 284 ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT)); |
310 suggested_text_view_->SetVisible(false); | 285 suggested_text_view_->SetVisible(false); |
311 AddChildView(suggested_text_view_); | 286 AddChildView(suggested_text_view_); |
312 | 287 |
313 keyword_hint_view_ = new KeywordHintView( | 288 keyword_hint_view_ = new KeywordHintView( |
314 profile_, font_list, font_y_offset, | 289 profile_, font_list, |
315 GetColor(ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT), | 290 GetColor(ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT), |
316 background_color); | 291 background_color); |
317 AddChildView(keyword_hint_view_); | 292 AddChildView(keyword_hint_view_); |
318 | 293 |
319 mic_search_view_ = new views::ImageButton(this); | 294 mic_search_view_ = new views::ImageButton(this); |
320 mic_search_view_->set_id(VIEW_ID_MIC_SEARCH_BUTTON); | 295 mic_search_view_->set_id(VIEW_ID_MIC_SEARCH_BUTTON); |
321 mic_search_view_->set_accessibility_focusable(true); | 296 mic_search_view_->set_accessibility_focusable(true); |
322 mic_search_view_->SetTooltipText( | 297 mic_search_view_->SetTooltipText( |
323 l10n_util::GetStringUTF16(IDS_TOOLTIP_MIC_SEARCH)); | 298 l10n_util::GetStringUTF16(IDS_TOOLTIP_MIC_SEARCH)); |
324 mic_search_view_->SetImage( | 299 mic_search_view_->SetImage( |
325 views::Button::STATE_NORMAL, | 300 views::Button::STATE_NORMAL, |
326 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 301 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
327 IDR_OMNIBOX_MIC_SEARCH)); | 302 IDR_OMNIBOX_MIC_SEARCH)); |
328 mic_search_view_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, | 303 mic_search_view_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, |
329 views::ImageButton::ALIGN_MIDDLE); | 304 views::ImageButton::ALIGN_MIDDLE); |
330 mic_search_view_->SetVisible(false); | 305 mic_search_view_->SetVisible(false); |
331 InitTouchableLocationBarChildView(mic_search_view_); | 306 InitTouchableLocationBarChildView(mic_search_view_); |
332 AddChildView(mic_search_view_); | 307 AddChildView(mic_search_view_); |
333 | 308 |
334 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { | 309 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { |
335 ContentSettingImageView* content_blocked_view = | 310 ContentSettingImageView* content_blocked_view = |
336 new ContentSettingImageView(static_cast<ContentSettingsType>(i), this, | 311 new ContentSettingImageView(static_cast<ContentSettingsType>(i), this, |
337 bubble_font_list, bubble_font_y_offset, | 312 bubble_font_list, text_color, |
338 text_color, background_color); | 313 background_color); |
339 content_setting_views_.push_back(content_blocked_view); | 314 content_setting_views_.push_back(content_blocked_view); |
340 content_blocked_view->SetVisible(false); | 315 content_blocked_view->SetVisible(false); |
341 AddChildView(content_blocked_view); | 316 AddChildView(content_blocked_view); |
342 } | 317 } |
343 | 318 |
344 generated_credit_card_view_ = new GeneratedCreditCardView(delegate_); | 319 generated_credit_card_view_ = new GeneratedCreditCardView(delegate_); |
345 AddChildView(generated_credit_card_view_); | 320 AddChildView(generated_credit_card_view_); |
346 | 321 |
347 zoom_view_ = new ZoomView(delegate_); | 322 zoom_view_ = new ZoomView(delegate_); |
348 zoom_view_->set_id(VIEW_ID_ZOOM_BUTTON); | 323 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()); | 772 gfx::Size suggested_text_size(suggested_text_view_->GetPreferredSize()); |
798 if (suggested_text_size.width() > available_width || | 773 if (suggested_text_size.width() > available_width || |
799 text_direction == base::i18n::UNKNOWN_DIRECTION) { | 774 text_direction == base::i18n::UNKNOWN_DIRECTION) { |
800 // Hide the suggested text if the user has scrolled or we can't fit all | 775 // 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. | 776 // the suggested text, or we have a mix of RTL and LTR characters. |
802 suggested_text_view_->SetBounds(0, 0, 0, 0); | 777 suggested_text_view_->SetBounds(0, 0, 0, 0); |
803 } else { | 778 } else { |
804 location_needed_width = | 779 location_needed_width = |
805 std::min(location_needed_width, | 780 std::min(location_needed_width, |
806 location_bounds.width() - suggested_text_size.width()); | 781 location_bounds.width() - suggested_text_size.width()); |
807 gfx::Rect suggested_text_bounds(location_bounds.origin(), | 782 gfx::Rect suggested_text_bounds(location_bounds.x(), location_bounds.y(), |
808 suggested_text_size); | 783 suggested_text_size.width(), |
784 location_bounds.height()); | |
809 // TODO(sky): figure out why this needs the -1. | 785 // TODO(sky): figure out why this needs the -1. |
810 suggested_text_bounds.Offset(location_needed_width - 1, 0); | 786 suggested_text_bounds.Offset(location_needed_width - 1, 0); |
811 // For non-views the omnibox needs to be shrunk so that the suggest text | 787 // For non-views the omnibox needs to be shrunk so that the suggest text |
812 // is visible. | 788 // is visible. |
813 if (!omnibox_views) | 789 if (!omnibox_views) |
814 location_bounds.set_width(location_needed_width); | 790 location_bounds.set_width(location_needed_width); |
815 | 791 |
816 // We reverse the order of the location entry and suggested text if: | 792 // We reverse the order of the location entry and suggested text if: |
817 // - Chrome is RTL but the text is fully LTR, or | 793 // - Chrome is RTL but the text is fully LTR, or |
818 // - Chrome is LTR but the text is fully RTL. | 794 // - 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 | 824 // 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. | 825 // RTL so far. In other words, no testable RTL environment so far. |
850 int x = location_needed_width; | 826 int x = location_needed_width; |
851 if (width > entry_width) | 827 if (width > entry_width) |
852 x = 0; | 828 x = 0; |
853 else if (location_needed_width + width > entry_width) | 829 else if (location_needed_width + width > entry_width) |
854 x = entry_width - width; | 830 x = entry_width - width; |
855 location_bounds.set_width(x); | 831 location_bounds.set_width(x); |
856 ime_inline_autocomplete_view_->SetBounds( | 832 ime_inline_autocomplete_view_->SetBounds( |
857 location_bounds.right(), location_bounds.y(), | 833 location_bounds.right(), location_bounds.y(), |
858 std::min(width, entry_width), | 834 std::min(width, entry_width), location_bounds.height()); |
859 ime_inline_autocomplete_view_->GetPreferredSize().height()); | |
860 } | 835 } |
861 | 836 |
862 location_entry_view_->SetBoundsRect(location_bounds); | 837 location_entry_view_->SetBoundsRect(location_bounds); |
863 } | 838 } |
864 | 839 |
865 void LocationBarView::OnPaint(gfx::Canvas* canvas) { | 840 void LocationBarView::OnPaint(gfx::Canvas* canvas) { |
866 View::OnPaint(canvas); | 841 View::OnPaint(canvas); |
867 | 842 |
868 // Fill the location bar background color behind the border. Parts of the | 843 // 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 | 844 // 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); | 1474 bounds.Inset(-(horizontal_padding + 1) / 2, 0); |
1500 location_bar_util::PaintExtensionActionBackground( | 1475 location_bar_util::PaintExtensionActionBackground( |
1501 *(*page_action_view)->image_view()->page_action(), | 1476 *(*page_action_view)->image_view()->page_action(), |
1502 tab_id, canvas, bounds, text_color, background_color); | 1477 tab_id, canvas, bounds, text_color, background_color); |
1503 } | 1478 } |
1504 } | 1479 } |
1505 | 1480 |
1506 void LocationBarView::AccessibilitySetValue(const string16& new_value) { | 1481 void LocationBarView::AccessibilitySetValue(const string16& new_value) { |
1507 location_entry_->SetUserText(new_value); | 1482 location_entry_->SetUserText(new_value); |
1508 } | 1483 } |
OLD | NEW |