| 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/autocomplete/touch_autocomplete_popup_contents
_view.h" | 5 #include "chrome/browser/ui/views/autocomplete/touch_autocomplete_popup_contents
_view.h" |
| 6 | 6 |
| 7 #include "chrome/browser/ui/omnibox/omnibox_view.h" | 7 #include "chrome/browser/ui/omnibox/omnibox_view.h" |
| 8 #include "third_party/skia/include/core/SkPaint.h" | 8 #include "third_party/skia/include/core/SkPaint.h" |
| 9 #include "ui/gfx/canvas.h" | 9 #include "ui/gfx/canvas.h" |
| 10 #include "ui/gfx/font.h" | 10 #include "ui/gfx/font.h" |
| 11 #include "ui/gfx/path.h" | 11 #include "ui/gfx/path.h" |
| 12 #include "ui/gfx/rect.h" | 12 #include "ui/gfx/rect.h" |
| 13 #include "ui/gfx/size.h" | 13 #include "ui/gfx/size.h" |
| 14 #include "ui/views/view.h" | 14 #include "ui/views/view.h" |
| 15 | 15 |
| 16 | |
| 17 // TouchAutocompleteResultView ------------------------------------------------ | 16 // TouchAutocompleteResultView ------------------------------------------------ |
| 18 | 17 |
| 19 TouchAutocompleteResultView::TouchAutocompleteResultView( | 18 TouchAutocompleteResultView::TouchAutocompleteResultView( |
| 20 AutocompleteResultViewModel* model, | 19 AutocompleteResultViewModel* model, |
| 21 int model_index, | 20 int model_index, |
| 22 const gfx::Font& font, | 21 const gfx::Font& font, |
| 23 const gfx::Font& bold_font) | 22 const gfx::Font& bold_font) |
| 24 : AutocompleteResultView(model, model_index, font, bold_font) { | 23 : AutocompleteResultView(model, model_index, font, bold_font) { |
| 24 set_edge_item_padding(8); |
| 25 set_item_padding(8); |
| 26 set_minimum_text_vertical_padding(10); |
| 25 } | 27 } |
| 26 | 28 |
| 27 TouchAutocompleteResultView::~TouchAutocompleteResultView() { | 29 TouchAutocompleteResultView::~TouchAutocompleteResultView() { |
| 28 } | 30 } |
| 29 | 31 |
| 30 void TouchAutocompleteResultView::PaintMatch(gfx::Canvas* canvas, | 32 void TouchAutocompleteResultView::PaintMatch(gfx::Canvas* canvas, |
| 31 const AutocompleteMatch& match, | 33 const AutocompleteMatch& match, |
| 32 int x) { | 34 int x) { |
| 33 DrawString(canvas, match.contents, match.contents_class, false, x, | 35 int y = text_bounds().y(); |
| 34 text_bounds().y()); | |
| 35 | 36 |
| 36 if (!match.description.empty()) { | 37 if (!match.description.empty()) { |
| 37 // We use our base class's GetTextHeight below because we need the height | 38 // We use our base class's GetTextHeight below because we need the height |
| 38 // of a single line of text. | 39 // of a single line of text. |
| 39 DrawString(canvas, match.description, match.description_class, true, x, | 40 DrawString(canvas, match.description, match.description_class, true, x, y); |
| 40 text_bounds().y() + AutocompleteResultView::GetTextHeight()); | 41 y += AutocompleteResultView::GetTextHeight(); |
| 42 } else { |
| 43 // When we have only one line of content (no description), we center the |
| 44 // single line vertically on our two-lines-tall results box. |
| 45 y += AutocompleteResultView::GetTextHeight() / 2; |
| 41 } | 46 } |
| 47 |
| 48 DrawString(canvas, match.contents, match.contents_class, false, x, y); |
| 42 } | 49 } |
| 43 | 50 |
| 44 int TouchAutocompleteResultView::GetTextHeight() const { | 51 int TouchAutocompleteResultView::GetTextHeight() const { |
| 45 // In the touch version of the autocomplete popup, the text is displayed in | |
| 46 // two lines: First line is the title of the suggestion and second is the | |
| 47 // description. Hence, the total text height is twice the height of one line. | |
| 48 return AutocompleteResultView::GetTextHeight() * 2; | 52 return AutocompleteResultView::GetTextHeight() * 2; |
| 49 } | 53 } |
| 50 | 54 |
| 51 | |
| 52 // TouchAutocompletePopupContentsView ----------------------------------------- | 55 // TouchAutocompletePopupContentsView ----------------------------------------- |
| 53 | 56 |
| 54 TouchAutocompletePopupContentsView::TouchAutocompletePopupContentsView( | 57 TouchAutocompletePopupContentsView::TouchAutocompletePopupContentsView( |
| 55 const gfx::Font& font, | 58 const gfx::Font& font, |
| 56 OmniboxView* omnibox_view, | 59 OmniboxView* omnibox_view, |
| 57 AutocompleteEditModel* edit_model, | 60 AutocompleteEditModel* edit_model, |
| 58 views::View* location_bar) | 61 views::View* location_bar) |
| 59 : AutocompletePopupContentsView(font, omnibox_view, edit_model, | 62 : AutocompletePopupContentsView(font, omnibox_view, edit_model, |
| 60 location_bar) { | 63 location_bar) { |
| 61 } | 64 } |
| 62 | 65 |
| 63 TouchAutocompletePopupContentsView::~TouchAutocompletePopupContentsView() { | 66 TouchAutocompletePopupContentsView::~TouchAutocompletePopupContentsView() { |
| 64 } | 67 } |
| 65 | 68 |
| 66 void TouchAutocompletePopupContentsView::UpdatePopupAppearance() { | 69 void TouchAutocompletePopupContentsView::UpdatePopupAppearance() { |
| 67 AutocompletePopupContentsView::UpdatePopupAppearance(); | 70 AutocompletePopupContentsView::UpdatePopupAppearance(); |
| 68 Layout(); | 71 Layout(); |
| 69 } | 72 } |
| 70 | 73 |
| 71 void TouchAutocompletePopupContentsView::LayoutChildren() { | |
| 72 std::vector<View*> visible_children(GetVisibleChildren()); | |
| 73 gfx::Rect bounds(GetContentsBounds()); | |
| 74 double child_width = | |
| 75 static_cast<double>(bounds.width()) / visible_children.size(); | |
| 76 int x = bounds.x(); | |
| 77 for (size_t i = 0; i < visible_children.size(); ++i) { | |
| 78 int next_x = bounds.x() + static_cast<int>(((i + 1) * child_width) + 0.5); | |
| 79 visible_children[i]->SetBounds(x, bounds.y(), next_x - x, bounds.height()); | |
| 80 x = next_x; | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 void TouchAutocompletePopupContentsView::PaintResultViews(gfx::Canvas* canvas) { | 74 void TouchAutocompletePopupContentsView::PaintResultViews(gfx::Canvas* canvas) { |
| 85 AutocompletePopupContentsView::PaintResultViews(canvas); | 75 AutocompletePopupContentsView::PaintResultViews(canvas); |
| 86 | 76 |
| 87 // Draw divider lines. | 77 // Draw divider lines. |
| 88 std::vector<View*> visible_children(GetVisibleChildren()); | 78 std::vector<View*> visible_children(GetVisibleChildren()); |
| 89 if (visible_children.size() < 2) | 79 if (visible_children.size() < 2) |
| 90 return; | 80 return; |
| 91 SkColor color = AutocompleteResultView::GetColor( | |
| 92 AutocompleteResultView::NORMAL, AutocompleteResultView::DIMMED_TEXT); | |
| 93 gfx::Rect bounds(GetContentsBounds()); | 81 gfx::Rect bounds(GetContentsBounds()); |
| 94 for (std::vector<View*>::const_iterator i(visible_children.begin() + 1); | 82 |
| 95 i != visible_children.end(); ++i) { | 83 // Draw a line at the bottom of each child except the last. The |
| 96 canvas->DrawLine(gfx::Point((*i)->x(), bounds.y()), | 84 // color of the line is determined to blend appropriately with the |
| 97 gfx::Point((*i)->x(), bounds.bottom()), color); | 85 // most dominant of the two surrounding cells, in precedence order, |
| 86 // i.e. selected > hovered > normal. |
| 87 for (std::vector<View*>::const_iterator i(visible_children.begin()); |
| 88 i + 1 != visible_children.end(); ++i) { |
| 89 TouchAutocompleteResultView* child = |
| 90 static_cast<TouchAutocompleteResultView*>(*i); |
| 91 TouchAutocompleteResultView* next_child = |
| 92 static_cast<TouchAutocompleteResultView*>(*(i + 1)); |
| 93 SkColor divider_color = AutocompleteResultView::GetColor( |
| 94 std::max(child->GetState(), next_child->GetState()), |
| 95 AutocompleteResultView::DIVIDER); |
| 96 int line_y = child->y() + child->height() - 1; |
| 97 canvas->DrawLine(gfx::Point(bounds.x(), line_y), |
| 98 gfx::Point(bounds.right(), line_y), divider_color); |
| 98 } | 99 } |
| 99 } | 100 } |
| 100 | 101 |
| 101 int TouchAutocompletePopupContentsView::CalculatePopupHeight() { | |
| 102 DCHECK_GE(static_cast<size_t>(child_count()), model_->result().size()); | |
| 103 int popup_height = 0; | |
| 104 for (size_t i = 0; i < model_->result().size(); ++i) { | |
| 105 popup_height = std::max(popup_height, | |
| 106 child_at(i)->GetPreferredSize().height()); | |
| 107 } | |
| 108 return popup_height; | |
| 109 } | |
| 110 | |
| 111 AutocompleteResultView* TouchAutocompletePopupContentsView::CreateResultView( | 102 AutocompleteResultView* TouchAutocompletePopupContentsView::CreateResultView( |
| 112 AutocompleteResultViewModel* model, | 103 AutocompleteResultViewModel* model, |
| 113 int model_index, | 104 int model_index, |
| 114 const gfx::Font& font, | 105 const gfx::Font& font, |
| 115 const gfx::Font& bold_font) { | 106 const gfx::Font& bold_font) { |
| 116 return new TouchAutocompleteResultView(model, model_index, font, bold_font); | 107 return new TouchAutocompleteResultView(model, model_index, font, bold_font); |
| 117 } | 108 } |
| 118 | 109 |
| 119 std::vector<views::View*> | 110 std::vector<views::View*> |
| 120 TouchAutocompletePopupContentsView::GetVisibleChildren() { | 111 TouchAutocompletePopupContentsView::GetVisibleChildren() { |
| 121 std::vector<View*> visible_children; | 112 std::vector<View*> visible_children; |
| 122 for (int i = 0; i < child_count(); ++i) { | 113 for (int i = 0; i < child_count(); ++i) { |
| 123 View* v = child_at(i); | 114 View* v = child_at(i); |
| 124 if (child_at(i)->visible()) | 115 if (child_at(i)->visible()) |
| 125 visible_children.push_back(v); | 116 visible_children.push_back(v); |
| 126 } | 117 } |
| 127 return visible_children; | 118 return visible_children; |
| 128 } | 119 } |
| OLD | NEW |