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