Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "touch_autocomplete_popup_contents_view.h" | |
| 6 | |
| 7 #include "chrome/browser/autocomplete/autocomplete_edit_view.h" | |
| 8 #include "chrome/browser/profiles/profile.h" | |
| 9 #include "ui/gfx/canvas.h" | |
| 10 #include "ui/gfx/canvas_skia.h" | |
| 11 #include "ui/gfx/font.h" | |
| 12 #include "ui/gfx/path.h" | |
| 13 #include "ui/gfx/rect.h" | |
| 14 #include "ui/gfx/size.h" | |
| 15 #include "third_party/skia/include/core/SkPaint.h" | |
| 16 #include "views/view.h" | |
| 17 | |
| 18 | |
| 19 // TouchAutocompleteResultView ------------------------------------------------ | |
| 20 | |
| 21 TouchAutocompleteResultView::TouchAutocompleteResultView( | |
| 22 AutocompleteResultViewModel* model, | |
| 23 int model_index, | |
| 24 const gfx::Font& font, | |
| 25 const gfx::Font& bold_font) | |
| 26 : AutocompleteResultView(model, model_index, font, bold_font) { | |
| 27 } | |
| 28 | |
| 29 TouchAutocompleteResultView::~TouchAutocompleteResultView() { | |
| 30 } | |
| 31 | |
| 32 void TouchAutocompleteResultView::Layout() { | |
| 33 AutocompleteResultView::Layout(); | |
| 34 | |
| 35 // In touch version of autocomplete popup, the text is displayed in two lines: | |
| 36 // First line is the title of the suggestion and second is the description. | |
| 37 // Hence, the total text height is 2 times the height of one line. | |
| 38 int text_height = 2 * GetFontHeight(); | |
| 39 set_text_bounds(gfx::Rect(text_bounds().x(), | |
| 40 std::max(0, (height() - text_height) / 2), | |
| 41 text_bounds().width(), text_height)); | |
| 42 } | |
| 43 | |
| 44 gfx::Size TouchAutocompleteResultView::GetPreferredSize() { | |
| 45 return gfx::Size(0, std::max( | |
| 46 AutocompleteResultView::icon_size() + (icon_vertical_padding_ * 2), | |
| 47 2 * (GetFontHeight() + text_vertical_padding_))); | |
| 48 } | |
| 49 | |
| 50 void TouchAutocompleteResultView::PaintMatch(gfx::Canvas* canvas, | |
| 51 const AutocompleteMatch& match, | |
| 52 int x) { | |
| 53 DrawString(canvas, match.contents, match.contents_class, false, x, | |
| 54 text_bounds().y()); | |
| 55 | |
| 56 if (!match.description.empty()) { | |
| 57 DrawString(canvas, match.description, match.description_class, true, x, | |
| 58 text_bounds().y() + GetFontHeight()); | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 int TouchAutocompleteResultView::GetFontHeight() const { | |
| 63 return std::max(normal_font().GetHeight(), bold_font().GetHeight()); | |
| 64 } | |
| 65 | |
| 66 | |
| 67 // TouchAutocompletePopupContentsView ----------------------------------------- | |
| 68 | |
| 69 TouchAutocompletePopupContentsView::TouchAutocompletePopupContentsView( | |
| 70 const gfx::Font& font, | |
| 71 AutocompleteEditView* edit_view, | |
| 72 AutocompleteEditModel* edit_model, | |
| 73 Profile* profile, | |
| 74 const views::View* location_bar) | |
| 75 : AutocompletePopupContentsView(font, edit_view, edit_model, profile, | |
| 76 location_bar) { | |
| 77 } | |
| 78 | |
| 79 TouchAutocompletePopupContentsView::~TouchAutocompletePopupContentsView() { | |
| 80 } | |
| 81 | |
| 82 void TouchAutocompletePopupContentsView::UpdatePopupAppearance() { | |
| 83 AutocompletePopupContentsView::UpdatePopupAppearance(); | |
| 84 Layout(); | |
| 85 } | |
| 86 | |
| 87 void TouchAutocompletePopupContentsView::PaintChildren( | |
| 88 gfx::CanvasSkia* canvas) { | |
| 89 AutocompletePopupContentsView::PaintChildren(canvas); | |
| 90 | |
| 91 // Draw divider lines. | |
| 92 std::vector<View*> visible_children(GetVisibleChildren()); | |
| 93 if (visible_children.size() < 2) | |
| 94 return; | |
| 95 SkColor color = AutocompleteResultView::GetColor( | |
| 96 AutocompleteResultView::NORMAL, AutocompleteResultView::DIMMED_TEXT); | |
| 97 gfx::Rect bounds(GetContentsBounds()); | |
| 98 for (std::vector<View*>::const_iterator i(visible_children.begin() + 1); | |
| 99 i != visible_children.end(); ++i) { | |
| 100 canvas->DrawLineInt(color, (*i)->x(), bounds.y(), (*i)->x(), | |
| 101 bounds.bottom()); | |
|
Peter Kasting
2011/02/18 18:18:08
Nit: Line this up with the (, since you have the r
varunjain
2011/02/18 19:13:04
Done.
| |
| 102 } | |
| 103 } | |
| 104 | |
| 105 void TouchAutocompletePopupContentsView::LayoutChildren() { | |
| 106 std::vector<View*> visible_children(GetVisibleChildren()); | |
| 107 gfx::Rect bounds(GetContentsBounds()); | |
| 108 double child_width = | |
| 109 static_cast<double>(bounds.width()) / visible_children.size(); | |
| 110 int x = bounds.x(); | |
| 111 for (size_t i = 0; i < visible_children.size(); ++i) { | |
| 112 int next_x = bounds.x() + static_cast<int>(((i + 1) * child_width) + 0.5); | |
| 113 visible_children[i]->SetBounds(x, bounds.y(), next_x - x, bounds.height()); | |
| 114 x = next_x; | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 int TouchAutocompletePopupContentsView::CalculatePopupHeight() { | |
| 119 DCHECK_GE(static_cast<size_t>(child_count()), model_->result().size()); | |
| 120 int popup_height = 0; | |
| 121 for (size_t i = 0; i < model_->result().size(); ++i) { | |
| 122 popup_height = std::max(popup_height, | |
| 123 GetChildViewAt(i)->GetPreferredSize().height()); | |
| 124 } | |
| 125 popup_height = std::max(popup_height, opt_in_view_ ? | |
| 126 opt_in_view_->GetPreferredSize().height() : 0); | |
| 127 return popup_height; | |
| 128 } | |
| 129 | |
| 130 AutocompleteResultView* TouchAutocompletePopupContentsView::CreateResultView( | |
| 131 AutocompleteResultViewModel* model, | |
| 132 int model_index, | |
| 133 const gfx::Font& font, | |
| 134 const gfx::Font& bold_font) { | |
| 135 return new TouchAutocompleteResultView(model, model_index, font, bold_font); | |
| 136 } | |
| 137 | |
| 138 std::vector<views::View*> | |
| 139 TouchAutocompletePopupContentsView::GetVisibleChildren() { | |
| 140 std::vector<View*> visible_children; | |
| 141 for (int i = 0; i < child_count(); ++i) { | |
| 142 View* v = GetChildViewAt(i); | |
| 143 if (GetChildViewAt(i)->IsVisible()) | |
| 144 visible_children.push_back(v); | |
| 145 } | |
| 146 return visible_children; | |
| 147 } | |
| OLD | NEW |