1 // Copyright 2014 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.
5 #include "athena/home/athena_start_page_view.h"
7 #include "third_party/skia/include/core/SkPaint.h"
8 #include "third_party/skia/include/core/SkPath.h"
9 #include "ui/app_list/app_list_item.h"
10 #include "ui/app_list/app_list_item_list.h"
11 #include "ui/app_list/app_list_model.h"
12 #include "ui/app_list/app_list_view_delegate.h"
13 #include "ui/app_list/views/search_box_view.h"
14 #include "ui/gfx/canvas.h"
15 #include "ui/views/background.h"
16 #include "ui/views/border.h"
17 #include "ui/views/layout/box_layout.h"
18 #include "ui/views/layout/fill_layout.h"
19 #include "ui/views/round_rect_painter.h"
23 const size_t kMaxIconNum = 3;
24 const int kIconSize = 50;
25 const int kIconMargin = 25;
27 // Copied from ui/app_list/views/start_page_view.cc
28 const int kSearchBoxBorderWidth = 1;
29 const int kSearchBoxCornerRadius = 2;
30 const int kSearchBoxWidth = 490;
31 const int kSearchBoxHeight = 40;
33 // The preferred height for VISIBLE_BOTTOM state.
34 const int kPreferredHeightBottom = 100;
36 class PlaceHolderButton : public views::ImageButton,
37 public views::ButtonListener {
41 gfx::Canvas canvas(gfx::Size(kIconSize, kIconSize), 1.0f, true);
43 paint.setStyle(SkPaint::kFill_Style);
44 paint.setColor(SkColorSetRGB(86, 119, 252));
45 paint.setFlags(SkPaint::kAntiAlias_Flag);
47 gfx::Point(kIconSize / 2, kIconSize / 2), kIconSize / 2, paint);
49 scoped_ptr<gfx::ImageSkia> image(
50 new gfx::ImageSkia(canvas.ExtractImageRep()));
51 SetImage(STATE_NORMAL, image.get());
55 // views::ButtonListener:
56 virtual void ButtonPressed(views::Button* sender,
57 const ui::Event& event) OVERRIDE {
58 // Do nothing: remove these place holders.
61 DISALLOW_COPY_AND_ASSIGN(PlaceHolderButton);
64 class AppIconButton : public views::ImageButton,
65 public views::ButtonListener {
67 explicit AppIconButton(app_list::AppListItem* item)
70 // TODO(mukai): icon should be resized.
71 SetImage(STATE_NORMAL, &item->icon());
75 // views::ButtonListener:
76 virtual void ButtonPressed(views::Button* sender,
77 const ui::Event& event) OVERRIDE {
78 DCHECK_EQ(sender, this);
79 item_->Activate(event.flags());
82 app_list::AppListItem* item_;
84 DISALLOW_COPY_AND_ASSIGN(AppIconButton);
87 // The background to paint the round rectangle of the view area.
88 class RoundRectBackground : public views::Background {
90 RoundRectBackground(SkColor color, int corner_radius)
92 corner_radius_(corner_radius) {}
93 virtual ~RoundRectBackground() {}
97 virtual void Paint(gfx::Canvas* canvas, views::View* view) const OVERRIDE {
99 paint.setStyle(SkPaint::kFill_Style);
100 paint.setColor(color_);
101 canvas->DrawRoundRect(view->GetContentsBounds(), corner_radius_, paint);
107 DISALLOW_COPY_AND_ASSIGN(RoundRectBackground);
110 class SearchBoxContainer : public views::View {
112 explicit SearchBoxContainer(app_list::SearchBoxView* search_box)
113 : search_box_(search_box) {
114 search_box->set_background(
115 new RoundRectBackground(SK_ColorWHITE, kSearchBoxCornerRadius));
116 search_box->SetBorder(views::Border::CreateBorderPainter(
117 new views::RoundRectPainter(SK_ColorGRAY, kSearchBoxCornerRadius),
118 gfx::Insets(kSearchBoxBorderWidth, kSearchBoxBorderWidth,
119 kSearchBoxBorderWidth, kSearchBoxBorderWidth)));
120 AddChildView(search_box_);
122 virtual ~SearchBoxContainer() {}
126 virtual void Layout() OVERRIDE {
127 gfx::Rect search_box_bounds = GetContentsBounds();
128 search_box_bounds.ClampToCenteredSize(GetPreferredSize());
129 search_box_->SetBoundsRect(search_box_bounds);
131 virtual gfx::Size GetPreferredSize() const OVERRIDE {
132 return gfx::Size(kSearchBoxWidth, kSearchBoxHeight);
135 // Owned by the views hierarchy.
136 app_list::SearchBoxView* search_box_;
138 DISALLOW_COPY_AND_ASSIGN(SearchBoxContainer);
145 AthenaStartPageView::AthenaStartPageView(
146 app_list::AppListViewDelegate* view_delegate) {
147 app_list::AppListItemList* top_level =
148 view_delegate->GetModel()->top_level_item_list();
150 container_ = new views::View();
151 AddChildView(container_);
153 views::BoxLayout* box_layout = new views::BoxLayout(
154 views::BoxLayout::kHorizontal, kIconMargin, kIconMargin, kIconMargin);
155 box_layout->set_main_axis_alignment(
156 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
157 box_layout->set_cross_axis_alignment(
158 views::BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER);
159 container_->SetLayoutManager(box_layout);
160 for (size_t i = 0; i < std::min(top_level->item_count(), kMaxIconNum); ++i)
161 container_->AddChildView(new AppIconButton(top_level->item_at(i)));
163 views::View* search_box_container = new SearchBoxContainer(
164 new app_list::SearchBoxView(this, view_delegate));
165 container_->AddChildView(search_box_container);
166 box_layout->SetFlexForView(search_box_container, 1);
168 for (size_t i = 0; i < kMaxIconNum; ++i)
169 container_->AddChildView(new PlaceHolderButton());
171 set_background(views::Background::CreateSolidBackground(
172 255, 255, 255, 255 * 0.9));
175 AthenaStartPageView::~AthenaStartPageView() {}
177 void AthenaStartPageView::Layout() {
178 gfx::Rect container_bounds = bounds();
179 container_bounds.set_height(kPreferredHeightBottom);
180 container_->SetBoundsRect(container_bounds);
183 void AthenaStartPageView::QueryChanged(app_list::SearchBoxView* sender) {
184 // Nothing needs to be done.
187 } // namespace athena