Chromium Code Reviews| Index: chrome/browser/ui/views/autocomplete/touch_autocomplete_popup_contents_view.cc |
| diff --git a/chrome/browser/ui/views/autocomplete/touch_autocomplete_popup_contents_view.cc b/chrome/browser/ui/views/autocomplete/touch_autocomplete_popup_contents_view.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ac991d02df6a257741105b47c68347983e430945 |
| --- /dev/null |
| +++ b/chrome/browser/ui/views/autocomplete/touch_autocomplete_popup_contents_view.cc |
| @@ -0,0 +1,151 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "touch_autocomplete_popup_contents_view.h" |
| + |
| +#include "chrome/browser/autocomplete/autocomplete_edit_view.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "ui/gfx/canvas.h" |
| +#include "ui/gfx/canvas_skia.h" |
| +#include "ui/gfx/font.h" |
| +#include "ui/gfx/path.h" |
| +#include "ui/gfx/rect.h" |
| +#include "ui/gfx/size.h" |
| +#include "third_party/skia/include/core/SkPaint.h" |
| +#include "views/view.h" |
| + |
| + |
| +// TouchAutocompleteResultView ------------------------------------------------ |
| + |
| +TouchAutocompleteResultView::TouchAutocompleteResultView( |
| + AutocompleteResultViewModel* model, |
| + int model_index, |
| + const gfx::Font& font, |
| + const gfx::Font& bold_font) |
| + : AutocompleteResultView(model, model_index, font, bold_font) { |
| +} |
| + |
| +TouchAutocompleteResultView::~TouchAutocompleteResultView() { |
| +} |
| + |
| +void TouchAutocompleteResultView::Layout() { |
| + AutocompleteResultView::Layout(); |
| + |
| + // In touch version of autocomplete popup, the text is displayed in two lines: |
| + // First line is the title of the suggestion and second is the description. |
| + // Hence, the total text height is 2 times the height of one line. |
| + int text_height = 2 * GetFontHeight(); |
| + set_text_bounds(gfx::Rect(text_bounds().x(), |
| + std::max(0, (height() - text_height) / 2), |
| + text_bounds().width(), |
|
Peter Kasting
2011/02/17 23:08:57
Nit: Feel free to collapse arg 4 onto this line
varunjain
2011/02/18 00:16:34
Done.
|
| + text_height)); |
| +} |
| + |
| +gfx::Size TouchAutocompleteResultView::GetPreferredSize() { |
| + int text_height = 2 * (GetFontHeight() + text_vertical_padding_); |
| + int icon_height = AutocompleteResultView::icon_size() + |
| + (icon_vertical_padding_ * 2); |
| + return gfx::Size(0, std::max(icon_height, text_height)); |
|
Peter Kasting
2011/02/17 23:08:57
Nit: I'd just collapse the temps into this stateme
varunjain
2011/02/18 00:16:34
Done.
|
| +} |
| + |
| +void TouchAutocompleteResultView::PaintMatch(gfx::Canvas* canvas, |
| + const AutocompleteMatch& match, |
| + int x) { |
| + DrawString(canvas, match.contents, match.contents_class, false, x, |
| + text_bounds().y()); |
| + |
| + if (!match.description.empty()) { |
| + int y = text_bounds().y() + GetFontHeight(); |
| + DrawString(canvas, match.description, match.description_class, true, x, y); |
|
Peter Kasting
2011/02/17 23:08:57
Nit: I'd just collapse |y| into this statement
varunjain
2011/02/18 00:16:34
Done.
|
| + } |
| +} |
| + |
| +int TouchAutocompleteResultView::GetFontHeight() const { |
| + return std::max(normal_font().GetHeight(), bold_font().GetHeight()); |
| +} |
| + |
| + |
| +// TouchAutocompletePopupContentsView ----------------------------------------- |
| + |
| +TouchAutocompletePopupContentsView::TouchAutocompletePopupContentsView( |
| + const gfx::Font& font, |
| + AutocompleteEditView* edit_view, |
| + AutocompleteEditModel* edit_model, |
| + Profile* profile, |
| + const views::View* location_bar) |
| + : AutocompletePopupContentsView(font, edit_view, edit_model, profile, |
| + location_bar) { |
| +} |
| + |
| +TouchAutocompletePopupContentsView::~TouchAutocompletePopupContentsView() { |
| +} |
| + |
| +void TouchAutocompletePopupContentsView::UpdatePopupAppearance() { |
| + AutocompletePopupContentsView::UpdatePopupAppearance(); |
| + Layout(); |
| +} |
| + |
| +void TouchAutocompletePopupContentsView::PaintChildren( |
| + gfx::CanvasSkia* canvas) { |
| + AutocompletePopupContentsView::PaintChildren(canvas); |
| + |
| + // Draw divider lines. |
| + int visible_child_count = GetVisibleChildCount(); |
|
Peter Kasting
2011/02/17 23:08:57
Here's how to do this without GetVisibleChildCount
varunjain
2011/02/18 00:16:34
Done. This is fine but IMO, this only makes the co
Peter Kasting
2011/02/18 01:07:59
All right, then how about this:
Take what are cur
varunjain
2011/02/18 16:18:19
Done.
|
| + for (int i = 0, j = 0; i < child_count() && j < visible_child_count - 1; |
| + ++i) { |
| + View* v = GetChildViewAt(i); |
| + if (v->IsVisible()) { |
| + canvas->DrawLineInt(AutocompleteResultView::GetColor( |
| + AutocompleteResultView::NORMAL, AutocompleteResultView::DIMMED_TEXT), |
| + v->x() + v->width(), v->y(), |
| + v->x() + v->width(), v->y() + v->height()); |
| + ++j; |
| + } |
| + } |
| +} |
| + |
| +void TouchAutocompletePopupContentsView::LayoutChildren() { |
|
Peter Kasting
2011/02/17 23:08:57
This algorithm suffers from rounding error accumul
varunjain
2011/02/18 00:16:34
Done.
|
| + gfx::Rect contents_rect = GetContentsBounds(); |
| + int visible_child_count = GetVisibleChildCount(); |
| + int child_width = contents_rect.width() / visible_child_count; |
| + |
| + for (int i = 0, j = 0; i < child_count() && j < visible_child_count; ++i) { |
| + View* v = GetChildViewAt(i); |
| + if (v->IsVisible()) { |
| + v->SetBounds(contents_rect.x() + (j++) * child_width, contents_rect.y(), |
| + child_width, v->GetPreferredSize().height()); |
| + } |
| + } |
| +} |
| + |
| +int TouchAutocompletePopupContentsView::CalculatePopupHeight() { |
| + DCHECK_GE(static_cast<size_t>(child_count()), model_->result().size()); |
| + int popup_height = 0; |
| + for (size_t i = 0; i < model_->result().size(); ++i) { |
| + popup_height = std::max(popup_height, |
| + GetChildViewAt(i)->GetPreferredSize().height()); |
| + } |
| + if (opt_in_view_) { |
|
Peter Kasting
2011/02/17 23:08:57
Nit: Shorter:
popup_height = std::max(popup_hei
varunjain
2011/02/18 00:16:34
Done.
|
| + popup_height = std::max(popup_height, |
| + opt_in_view_->GetPreferredSize().height()); |
| + } |
| + return popup_height; |
| +} |
| + |
| +AutocompleteResultView* TouchAutocompletePopupContentsView::CreateResultView( |
| + AutocompleteResultViewModel* model, |
| + int model_index, |
| + const gfx::Font& font, |
| + const gfx::Font& bold_font) { |
| + return new TouchAutocompleteResultView(model, model_index, font, bold_font); |
| +} |
| + |
| +int TouchAutocompletePopupContentsView::GetVisibleChildCount() const { |
| + int visible_child_count = 0; |
| + for (int i = 0; i < child_count(); ++i) { |
| + if (GetChildViewAt(i)->IsVisible()) |
| + visible_child_count++; |
| + } |
| + return visible_child_count; |
| +} |