Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(416)

Side by Side Diff: chrome/browser/ui/views/location_bar/location_bar_view.cc

Issue 19666006: Supports FontList in Textfield. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixes examples and unittests. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 #if defined(OS_WIN) 87 #if defined(OS_WIN)
88 #include "base/win/scoped_hdc.h" 88 #include "base/win/scoped_hdc.h"
89 #include "base/win/scoped_select_object.h" 89 #include "base/win/scoped_select_object.h"
90 #include "ui/native_theme/native_theme_win.h" 90 #include "ui/native_theme/native_theme_win.h"
91 #endif 91 #endif
92 92
93 #if defined(OS_WIN) && !defined(USE_AURA) 93 #if defined(OS_WIN) && !defined(USE_AURA)
94 #include "chrome/browser/ui/views/omnibox/omnibox_view_win.h" 94 #include "chrome/browser/ui/views/omnibox/omnibox_view_win.h"
95 #endif 95 #endif
96 96
97 #if defined(OS_CHROMEOS)
98 #include "grit/app_locale_settings.h"
99 #endif
100
97 #if !defined(OS_CHROMEOS) 101 #if !defined(OS_CHROMEOS)
98 #include "chrome/browser/ui/views/first_run_bubble.h" 102 #include "chrome/browser/ui/views/first_run_bubble.h"
99 #endif 103 #endif
100 104
101 #if defined(USE_AURA) 105 #if defined(USE_AURA)
102 #include "ui/compositor/layer.h" 106 #include "ui/compositor/layer.h"
103 #include "ui/compositor/scoped_layer_animation_settings.h" 107 #include "ui/compositor/scoped_layer_animation_settings.h"
104 #endif 108 #endif
105 109
106 using content::WebContents; 110 using content::WebContents;
107 using views::View; 111 using views::View;
108 112
109 113
110 namespace { 114 namespace {
111 115
112 Browser* GetBrowserFromDelegate(LocationBarView::Delegate* delegate) { 116 Browser* GetBrowserFromDelegate(LocationBarView::Delegate* delegate) {
113 WebContents* contents = delegate->GetWebContents(); 117 WebContents* contents = delegate->GetWebContents();
114 return contents ? chrome::FindBrowserWithWebContents(contents) : NULL; 118 return contents ? chrome::FindBrowserWithWebContents(contents) : NULL;
115 } 119 }
116 120
117 // Given a containing |height| and a base |font|, shrinks the font until it will 121 // Given a containing |height| and a base |font_list|, shrinks the fonts until
118 // fit within |height| while having its cap height vertically centered. Returns 122 // it will fit within |height| while having its cap height vertically centered.
Peter Kasting 2013/07/26 20:54:27 Nit: it -> the primary font
Yuki 2013/07/29 15:27:21 Done.
119 // the |font_y_offset| needed to produce this centering. 123 // Returns the |font_y_offset| needed to produce this centering.
120 void CalculateFontAndOffsetForHeight(int height, 124 void CalculateFontAndOffsetForHeight(int height,
121 gfx::Font* font, 125 gfx::FontList* font_list,
122 int* font_y_offset) { 126 int* font_y_offset) {
123 #if defined(OS_WIN) 127 #if defined(OS_WIN)
124 base::win::ScopedGetDC screen_dc(NULL); 128 base::win::ScopedGetDC screen_dc(NULL);
125 #endif 129 #endif
126 130
127 while (true) { 131 while (true) {
128 // TODO(pkasting): Expand the gfx::Font metrics (and underlying Skia 132 // TODO(pkasting): Expand the gfx::Font metrics (and underlying Skia
129 // metrics) enough to expose the cap height directly. 133 // metrics) enough to expose the cap height directly.
130 #if defined(OS_WIN) 134 #if defined(OS_WIN)
131 base::win::ScopedSelectObject font_in_dc(screen_dc, font->GetNativeFont()); 135 const gfx::Font& font = font_list->GetPrimaryFont();
136 base::win::ScopedSelectObject font_in_dc(screen_dc, font.GetNativeFont());
132 TEXTMETRIC tm = {0}; 137 TEXTMETRIC tm = {0};
133 GetTextMetrics(screen_dc, &tm); 138 GetTextMetrics(screen_dc, &tm);
134 int cap_height = font->GetBaseline() - tm.tmInternalLeading; 139 int cap_height = font.GetBaseline() - tm.tmInternalLeading;
135 *font_y_offset = ((height - cap_height) / 2) - tm.tmInternalLeading; 140 *font_y_offset = ((height - cap_height) / 2) - tm.tmInternalLeading;
136 #else 141 #else
137 // Without cap height available, we fall back to centering the full height. 142 // Without cap height available, we fall back to centering the full height.
138 *font_y_offset = (height - font->GetHeight()) / 2; 143 *font_y_offset = (height - font_list->GetHeight()) / 2;
139 #endif 144 #endif
140 145
141 if (((*font_y_offset >= 0) && 146 if (((*font_y_offset >= 0) &&
142 ((*font_y_offset + font->GetHeight()) <= height)) || 147 ((*font_y_offset + font_list->GetHeight()) <= height)) ||
143 (font->GetFontSize() <= 1)) 148 (font_list->GetPrimaryFont().GetFontSize() <= 1))
Peter Kasting 2013/07/26 20:54:27 Nit: Pull font_list->GetPrimaryFont().GetFontSize(
msw 2013/07/26 21:19:57 Should FontList have a GetFontSize()?
Yuki 2013/07/29 15:27:21 Done.
Yuki 2013/07/29 15:27:21 Done.
144 return; 149 return;
145 *font = font->DeriveFont(-1); 150 *font_list = font_list->DeriveFontListWithSize(
151 font_list->GetPrimaryFont().GetFontSize() - 1);
146 } 152 }
147 } 153 }
148 154
149 } // namespace 155 } // namespace
150 156
151 157
152 // LocationBarView ----------------------------------------------------------- 158 // LocationBarView -----------------------------------------------------------
153 159
154 // static 160 // static
155 const int LocationBarView::kNormalEdgeThickness = 2; 161 const int LocationBarView::kNormalEdgeThickness = 2;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 void LocationBarView::Init() { 233 void LocationBarView::Init() {
228 // We need to be in a Widget, otherwise GetNativeTheme() may change and we're 234 // We need to be in a Widget, otherwise GetNativeTheme() may change and we're
229 // not prepared for that. 235 // not prepared for that.
230 DCHECK(GetWidget()); 236 DCHECK(GetWidget());
231 237
232 location_icon_view_ = new LocationIconView(this); 238 location_icon_view_ = new LocationIconView(this);
233 location_icon_view_->set_drag_controller(this); 239 location_icon_view_->set_drag_controller(this);
234 AddChildView(location_icon_view_); 240 AddChildView(location_icon_view_);
235 241
236 // Determine the main font. 242 // Determine the main font.
237 gfx::Font font(ui::ResourceBundle::GetSharedInstance().GetFont( 243 #if defined(OS_CHROMEOS)
244 gfx::FontList font_list(l10n_util::GetStringUTF8(IDS_UI_FONT_FAMILY_CROS));
245 #else
246 gfx::FontList font_list(ui::ResourceBundle::GetSharedInstance().GetFont(
Peter Kasting 2013/07/26 20:54:27 We make this sort of call many places in the front
msw 2013/07/26 21:19:57 I agree; this code should not choose/create the in
Peter Kasting 2013/07/26 22:17:43 I think (2) and (3) are both workable. I didn't d
Yuki 2013/07/29 15:27:21 I've introduced ResourceBundle::GetFontList(style)
238 ui::ResourceBundle::BaseFont)); 247 ui::ResourceBundle::BaseFont));
239 const int current_font_size = font.GetFontSize(); 248 #endif
249 const int current_font_size = font_list.GetPrimaryFont().GetFontSize();
240 const int desired_font_size = browser_defaults::kOmniboxFontPixelSize; 250 const int desired_font_size = browser_defaults::kOmniboxFontPixelSize;
241 if (current_font_size < desired_font_size) 251 if (current_font_size < desired_font_size)
242 font = font.DeriveFont(desired_font_size - current_font_size); 252 font_list = font_list.DeriveFontListWithSize(desired_font_size);
243 // Shrink large fonts to make them fit. 253 // Shrink large fonts to make them fit.
244 // TODO(pkasting): Stretch the location bar instead in this case. 254 // TODO(pkasting): Stretch the location bar instead in this case.
245 int location_height = GetInternalHeight(true); 255 int location_height = GetInternalHeight(true);
246 int font_y_offset; 256 int font_y_offset;
247 CalculateFontAndOffsetForHeight(location_height, &font, &font_y_offset); 257 CalculateFontAndOffsetForHeight(location_height, &font_list, &font_y_offset);
248 258
249 // Determine the font for use inside the bubbles. 259 // Determine the font for use inside the bubbles.
250 gfx::Font bubble_font(font); 260 gfx::FontList bubble_font_list(font_list);
251 int bubble_font_y_offset; 261 int bubble_font_y_offset;
252 // The bubble background images have 1 px thick edges, which we don't want to 262 // The bubble background images have 1 px thick edges, which we don't want to
253 // overlap. 263 // overlap.
254 const int kBubbleInteriorVerticalPadding = 1; 264 const int kBubbleInteriorVerticalPadding = 1;
255 CalculateFontAndOffsetForHeight( 265 CalculateFontAndOffsetForHeight(
256 location_height - ((kBubblePadding + kBubbleInteriorVerticalPadding) * 2), 266 location_height - ((kBubblePadding + kBubbleInteriorVerticalPadding) * 2),
257 &bubble_font, &bubble_font_y_offset); 267 &bubble_font_list, &bubble_font_y_offset);
258 bubble_font_y_offset += kBubbleInteriorVerticalPadding; 268 bubble_font_y_offset += kBubbleInteriorVerticalPadding;
259 269
260 const SkColor background_color = 270 const SkColor background_color =
261 GetColor(ToolbarModel::NONE, LocationBarView::BACKGROUND); 271 GetColor(ToolbarModel::NONE, LocationBarView::BACKGROUND);
262 ev_bubble_view_ = new EVBubbleView( 272 ev_bubble_view_ = new EVBubbleView(
263 bubble_font, bubble_font_y_offset, 273 bubble_font_list.GetPrimaryFont(), bubble_font_y_offset,
264 GetColor(ToolbarModel::EV_SECURE, SECURITY_TEXT), background_color, this); 274 GetColor(ToolbarModel::EV_SECURE, SECURITY_TEXT), background_color, this);
265 ev_bubble_view_->set_drag_controller(this); 275 ev_bubble_view_->set_drag_controller(this);
266 AddChildView(ev_bubble_view_); 276 AddChildView(ev_bubble_view_);
267 277
268 // Initialize the Omnibox view. 278 // Initialize the Omnibox view.
269 location_entry_.reset(CreateOmniboxView(this, model_, profile_, 279 location_entry_.reset(CreateOmniboxView(this, model_, profile_,
270 command_updater_, is_popup_mode_, this, font, font_y_offset)); 280 command_updater_, is_popup_mode_, this, font_list, font_y_offset));
271 SetLocationEntryFocusable(true); 281 SetLocationEntryFocusable(true);
272 location_entry_view_ = location_entry_->AddToView(this); 282 location_entry_view_ = location_entry_->AddToView(this);
273 283
274 // Initialize the inline autocomplete view which is visible only when IME is 284 // Initialize the inline autocomplete view which is visible only when IME is
275 // turned on. Use the same font with the omnibox and highlighted background. 285 // turned on. Use the same font with the omnibox and highlighted background.
276 ime_inline_autocomplete_view_ = new views::Label(string16(), font); 286 ime_inline_autocomplete_view_ = new views::Label(string16(),
287 font_list.GetPrimaryFont());
Peter Kasting 2013/07/26 20:54:27 Nit: We seem to call this repeatedly, consider cre
Yuki 2013/07/29 15:27:21 Done.
277 ime_inline_autocomplete_view_->set_border( 288 ime_inline_autocomplete_view_->set_border(
278 views::Border::CreateEmptyBorder(font_y_offset, 0, 0, 0)); 289 views::Border::CreateEmptyBorder(font_y_offset, 0, 0, 0));
279 ime_inline_autocomplete_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT); 290 ime_inline_autocomplete_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
280 ime_inline_autocomplete_view_->SetAutoColorReadabilityEnabled(false); 291 ime_inline_autocomplete_view_->SetAutoColorReadabilityEnabled(false);
281 ime_inline_autocomplete_view_->set_background( 292 ime_inline_autocomplete_view_->set_background(
282 views::Background::CreateSolidBackground(GetNativeTheme()->GetSystemColor( 293 views::Background::CreateSolidBackground(GetNativeTheme()->GetSystemColor(
283 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused))); 294 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused)));
284 ime_inline_autocomplete_view_->SetEnabledColor( 295 ime_inline_autocomplete_view_->SetEnabledColor(
285 GetNativeTheme()->GetSystemColor( 296 GetNativeTheme()->GetSystemColor(
286 ui::NativeTheme::kColorId_TextfieldSelectionColor)); 297 ui::NativeTheme::kColorId_TextfieldSelectionColor));
287 ime_inline_autocomplete_view_->SetVisible(false); 298 ime_inline_autocomplete_view_->SetVisible(false);
288 AddChildView(ime_inline_autocomplete_view_); 299 AddChildView(ime_inline_autocomplete_view_);
289 300
290 const SkColor text_color = GetColor(ToolbarModel::NONE, TEXT); 301 const SkColor text_color = GetColor(ToolbarModel::NONE, TEXT);
291 selected_keyword_view_ = new SelectedKeywordView( 302 selected_keyword_view_ = new SelectedKeywordView(
292 bubble_font, bubble_font_y_offset, text_color, background_color, 303 bubble_font_list.GetPrimaryFont(), bubble_font_y_offset, text_color,
293 profile_); 304 background_color, profile_);
294 AddChildView(selected_keyword_view_); 305 AddChildView(selected_keyword_view_);
295 306
296 suggested_text_view_ = new views::Label(string16(), font); 307 suggested_text_view_ = new views::Label(string16(),
308 font_list.GetPrimaryFont());
297 suggested_text_view_->set_border( 309 suggested_text_view_->set_border(
298 views::Border::CreateEmptyBorder(font_y_offset, 0, 0, 0)); 310 views::Border::CreateEmptyBorder(font_y_offset, 0, 0, 0));
299 suggested_text_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT); 311 suggested_text_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
300 suggested_text_view_->SetAutoColorReadabilityEnabled(false); 312 suggested_text_view_->SetAutoColorReadabilityEnabled(false);
301 suggested_text_view_->SetEnabledColor(GetColor( 313 suggested_text_view_->SetEnabledColor(GetColor(
302 ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT)); 314 ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT));
303 suggested_text_view_->SetVisible(false); 315 suggested_text_view_->SetVisible(false);
304 AddChildView(suggested_text_view_); 316 AddChildView(suggested_text_view_);
305 317
306 keyword_hint_view_ = new KeywordHintView( 318 keyword_hint_view_ = new KeywordHintView(
307 profile_, font, font_y_offset, 319 profile_, font_list.GetPrimaryFont(), font_y_offset,
308 GetColor(ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT), 320 GetColor(ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT),
309 background_color); 321 background_color);
310 AddChildView(keyword_hint_view_); 322 AddChildView(keyword_hint_view_);
311 323
312 mic_search_view_ = new MicSearchView(this); 324 mic_search_view_ = new MicSearchView(this);
313 mic_search_view_->SetVisible(false); 325 mic_search_view_->SetVisible(false);
314 AddChildView(mic_search_view_); 326 AddChildView(mic_search_view_);
315 327
316 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { 328 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
317 ContentSettingImageView* content_blocked_view = 329 ContentSettingImageView* content_blocked_view =
318 new ContentSettingImageView(static_cast<ContentSettingsType>(i), this, 330 new ContentSettingImageView(static_cast<ContentSettingsType>(i), this,
319 bubble_font, bubble_font_y_offset, 331 bubble_font_list.GetPrimaryFont(),
320 text_color, background_color); 332 bubble_font_y_offset, text_color,
333 background_color);
321 content_setting_views_.push_back(content_blocked_view); 334 content_setting_views_.push_back(content_blocked_view);
322 content_blocked_view->SetVisible(false); 335 content_blocked_view->SetVisible(false);
323 AddChildView(content_blocked_view); 336 AddChildView(content_blocked_view);
324 } 337 }
325 338
326 autofill_credit_card_view_ = new AutofillCreditCardView(model_, delegate_); 339 autofill_credit_card_view_ = new AutofillCreditCardView(model_, delegate_);
327 AddChildView(autofill_credit_card_view_); 340 AddChildView(autofill_credit_card_view_);
328 341
329 zoom_view_ = new ZoomView(model_, delegate_); 342 zoom_view_ = new ZoomView(model_, delegate_);
330 zoom_view_->set_id(VIEW_ID_ZOOM_BUTTON); 343 zoom_view_->set_id(VIEW_ID_ZOOM_BUTTON);
(...skipping 1170 matching lines...) Expand 10 before | Expand all | Expand 10 after
1501 int LocationBarView::GetInternalHeight(bool use_preferred_size) { 1514 int LocationBarView::GetInternalHeight(bool use_preferred_size) {
1502 int total_height = 1515 int total_height =
1503 use_preferred_size ? GetPreferredSize().height() : height(); 1516 use_preferred_size ? GetPreferredSize().height() : height();
1504 return std::max(total_height - (vertical_edge_thickness() * 2), 0); 1517 return std::max(total_height - (vertical_edge_thickness() * 2), 0);
1505 } 1518 }
1506 1519
1507 bool LocationBarView::HasValidSuggestText() const { 1520 bool LocationBarView::HasValidSuggestText() const {
1508 return suggested_text_view_->visible() && 1521 return suggested_text_view_->visible() &&
1509 !suggested_text_view_->size().IsEmpty(); 1522 !suggested_text_view_->size().IsEmpty();
1510 } 1523 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698