Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ui/app_list/search_result_view.h" | |
| 6 | |
| 7 #include "ui/app_list/search_result.h" | |
| 8 #include "ui/gfx/canvas.h" | |
| 9 #include "ui/gfx/font.h" | |
| 10 #include "ui/gfx/render_text.h" | |
| 11 #include "ui/views/controls/image_view.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 const int kPreferredWidth = 300; | |
| 16 const int kPreferredHeight = 48; | |
| 17 const int kIconViewWidth = 48; | |
| 18 const int kIconPadding = 8; | |
| 19 const int kBorderSize = 1; | |
| 20 | |
| 21 const SkColor kBorderColor = SkColorSetRGB(0xF1, 0xF1, 0xF1); | |
| 22 | |
| 23 const SkColor kDefaultTextColor = SkColorSetRGB(0x33, 0x33, 0x33); | |
| 24 const SkColor kDimmedTextColor = SK_ColorGRAY; | |
| 25 const SkColor kURLTextColor = SkColorSetRGB(0x00, 0x99, 0x33); | |
| 26 | |
| 27 // rgba(77,144,254,0.2) | |
| 28 const SkColor kSelectedBorderColor = SkColorSetARGB(51, 77, 144, 254); | |
| 29 // rgba(77,144,254,0.33) | |
| 30 const SkColor kSelectedBackgroundColor = SkColorSetARGB(84, 77, 144, 254); | |
| 31 const SkColor kHoverAndPushedColor = SkColorSetARGB(51, 77, 144, 254); | |
| 32 | |
| 33 // A non-interactive image view to display result icon. | |
| 34 class IconView : public views::ImageView { | |
| 35 public: | |
| 36 IconView() : ImageView() {} | |
| 37 virtual ~IconView() {} | |
| 38 | |
| 39 private: | |
| 40 // views::View overrides: | |
| 41 virtual bool HitTest(const gfx::Point& l) const OVERRIDE { | |
|
sky
2012/05/22 22:16:06
Why do you need this?
xiyuan
2012/05/23 21:25:22
If we don't do this, the parent SearchResultView l
| |
| 42 return false; | |
| 43 } | |
| 44 | |
| 45 DISALLOW_COPY_AND_ASSIGN(IconView); | |
| 46 }; | |
| 47 | |
| 48 // Creates a RenderText of given |text| and |styles|. Callers takes ownership | |
| 49 // of returned RenderText. | |
| 50 gfx::RenderText* CreateRenderText(const string16& text, | |
| 51 const app_list::SearchResult::Tags& tags) { | |
| 52 gfx::RenderText* render_text = gfx::RenderText::CreateRenderText(); | |
| 53 render_text->SetText(text); | |
| 54 | |
| 55 gfx::StyleRange default_style; | |
| 56 default_style.foreground = kDefaultTextColor; | |
| 57 render_text->set_default_style(default_style); | |
| 58 render_text->ApplyDefaultStyle(); | |
| 59 | |
| 60 for (app_list::SearchResult::Tags::const_iterator it = tags.begin(); | |
| 61 it != tags.end(); | |
| 62 ++it) { | |
| 63 // NONE means default style so do nothing. | |
| 64 if (it->styles == app_list::SearchResult::Tag::NONE) | |
| 65 continue; | |
| 66 | |
| 67 gfx::StyleRange style; | |
| 68 style.range = it->range; | |
| 69 | |
| 70 if (it->styles & app_list::SearchResult::Tag::MATCH) | |
| 71 style.font_style = gfx::Font::BOLD; | |
| 72 if (it->styles & app_list::SearchResult::Tag::URL) | |
| 73 style.foreground = kURLTextColor; | |
| 74 if (it->styles & app_list::SearchResult::Tag::DIM) | |
| 75 style.foreground = kDimmedTextColor; | |
| 76 | |
| 77 render_text->ApplyStyleRange(style); | |
| 78 } | |
| 79 | |
| 80 return render_text; | |
| 81 } | |
| 82 | |
| 83 } // namespace | |
| 84 | |
| 85 namespace app_list { | |
| 86 | |
| 87 // static | |
| 88 const char SearchResultView::kViewClassName[] = "ui/app_list/SearchResultView"; | |
| 89 | |
| 90 SearchResultView::SearchResultView(views::ButtonListener* listener) | |
| 91 : views::CustomButton(listener), | |
| 92 result_(NULL), | |
| 93 selected_(false), | |
| 94 icon_(NULL) { | |
| 95 icon_ = new IconView; | |
| 96 AddChildView(icon_); | |
| 97 } | |
| 98 | |
| 99 SearchResultView::~SearchResultView() { | |
| 100 } | |
| 101 | |
| 102 void SearchResultView::SetResult(const SearchResult* result) { | |
| 103 result_ = result; | |
| 104 | |
| 105 icon_->SetImage(result_ ? result_->icon() : SkBitmap()); | |
| 106 UpdateTitleText(); | |
| 107 UpdateDetailsText(); | |
| 108 SchedulePaint(); | |
| 109 } | |
| 110 | |
| 111 void SearchResultView::SetSelected(bool selected) { | |
| 112 if (selected_ == selected) | |
| 113 return; | |
| 114 | |
| 115 selected_ = selected; | |
| 116 SchedulePaint(); | |
| 117 } | |
| 118 | |
| 119 void SearchResultView::UpdateTitleText() { | |
| 120 if (!result_ || result_->title().empty()) { | |
| 121 title_text_.reset(); | |
| 122 } else { | |
| 123 title_text_.reset(CreateRenderText(result_->title(), | |
| 124 result_->title_tags())); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 void SearchResultView::UpdateDetailsText() { | |
| 129 if (!result_ || result_->details().empty()) { | |
| 130 details_text_.reset(); | |
| 131 } else { | |
| 132 details_text_.reset(CreateRenderText(result_->details(), | |
| 133 result_->details_tags())); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 std::string SearchResultView::GetClassName() const { | |
| 138 return kViewClassName; | |
| 139 } | |
| 140 | |
| 141 gfx::Size SearchResultView::GetPreferredSize() { | |
| 142 return gfx::Size(kPreferredWidth, kPreferredHeight); | |
| 143 } | |
| 144 | |
| 145 void SearchResultView::Layout() { | |
| 146 gfx::Rect rect(GetContentsBounds()); | |
| 147 if (rect.IsEmpty()) | |
| 148 return; | |
| 149 | |
| 150 gfx::Rect icon_bounds(rect); | |
| 151 icon_bounds.set_width(kIconViewWidth); | |
| 152 icon_bounds.Inset(kIconPadding, kIconPadding); | |
| 153 icon_->SetBoundsRect(icon_bounds.Intersect(rect)); | |
| 154 } | |
| 155 | |
| 156 void SearchResultView::OnPaint(gfx::Canvas* canvas) { | |
| 157 gfx::Rect rect(GetContentsBounds()); | |
| 158 if (rect.IsEmpty()) | |
| 159 return; | |
| 160 | |
| 161 gfx::Rect content_rect(rect); | |
| 162 content_rect.set_height(rect.height() - kBorderSize); | |
| 163 | |
| 164 if (selected_) { | |
| 165 canvas->FillRect(content_rect, kSelectedBackgroundColor); | |
| 166 } else if (state() == BS_HOT || state() == BS_PUSHED) { | |
| 167 canvas->FillRect(content_rect, kHoverAndPushedColor); | |
| 168 } | |
| 169 | |
| 170 gfx::Rect border_bottom = rect.Subtract(content_rect); | |
| 171 canvas->FillRect(border_bottom, | |
| 172 selected_ ? kSelectedBorderColor : kBorderColor); | |
| 173 | |
| 174 gfx::Rect text_bounds(rect); | |
| 175 text_bounds.set_x(kIconViewWidth); | |
| 176 text_bounds.set_width(rect.width() - kIconViewWidth); | |
| 177 | |
| 178 if (title_text_.get() && details_text_.get()) { | |
| 179 gfx::Size title_size(text_bounds.width(), | |
| 180 title_text_->GetStringSize().height()); | |
| 181 gfx::Size details_size(text_bounds.width(), | |
| 182 details_text_->GetStringSize().height()); | |
| 183 int total_height = title_size.height() + + details_size.height(); | |
| 184 int y = text_bounds.y() + (text_bounds.height() - total_height) / 2; | |
| 185 | |
| 186 title_text_->SetDisplayRect(gfx::Rect(gfx::Point(text_bounds.x(), y), | |
| 187 title_size)); | |
| 188 title_text_->Draw(canvas); | |
| 189 | |
| 190 y += title_size.height(); | |
| 191 details_text_->SetDisplayRect(gfx::Rect(gfx::Point(text_bounds.x(), y), | |
| 192 details_size)); | |
| 193 details_text_->Draw(canvas); | |
| 194 } else if (title_text_.get()) { | |
| 195 gfx::Size title_size(text_bounds.width(), | |
|
sky
2012/05/22 22:16:06
Don't you want the title to always get the same bo
xiyuan
2012/05/23 21:25:22
Per mock, we should vertically center title text.
| |
| 196 title_text_->GetStringSize().height()); | |
| 197 title_text_->SetDisplayRect(text_bounds.Center(title_size)); | |
| 198 title_text_->Draw(canvas); | |
| 199 } | |
| 200 } | |
| 201 | |
| 202 } // namespace app_list | |
| OLD | NEW |