Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / athena / home / athena_start_page_view.cc
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.
4
5 #include "athena/home/athena_start_page_view.h"
6
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"
20
21 namespace {
22
23 const size_t kMaxIconNum = 3;
24 const int kIconSize = 50;
25 const int kIconMargin = 25;
26
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;
32
33 // The preferred height for VISIBLE_BOTTOM state.
34 const int kPreferredHeightBottom = 100;
35
36 class PlaceHolderButton : public views::ImageButton,
37                           public views::ButtonListener {
38  public:
39   PlaceHolderButton()
40       : ImageButton(this) {
41     gfx::Canvas canvas(gfx::Size(kIconSize, kIconSize), 1.0f, true);
42     SkPaint paint;
43     paint.setStyle(SkPaint::kFill_Style);
44     paint.setColor(SkColorSetRGB(86, 119, 252));
45     paint.setFlags(SkPaint::kAntiAlias_Flag);
46     canvas.DrawCircle(
47         gfx::Point(kIconSize / 2, kIconSize / 2), kIconSize / 2, paint);
48
49     scoped_ptr<gfx::ImageSkia> image(
50         new gfx::ImageSkia(canvas.ExtractImageRep()));
51     SetImage(STATE_NORMAL, image.get());
52   }
53
54  private:
55   // views::ButtonListener:
56   virtual void ButtonPressed(views::Button* sender,
57                              const ui::Event& event) OVERRIDE {
58     // Do nothing: remove these place holders.
59   }
60
61   DISALLOW_COPY_AND_ASSIGN(PlaceHolderButton);
62 };
63
64 class AppIconButton : public views::ImageButton,
65                       public views::ButtonListener {
66  public:
67   explicit AppIconButton(app_list::AppListItem* item)
68       : ImageButton(this),
69         item_(item) {
70     // TODO(mukai): icon should be resized.
71     SetImage(STATE_NORMAL, &item->icon());
72   }
73
74  private:
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());
80   }
81
82   app_list::AppListItem* item_;
83
84   DISALLOW_COPY_AND_ASSIGN(AppIconButton);
85 };
86
87 // The background to paint the round rectangle of the view area.
88 class RoundRectBackground : public views::Background {
89  public:
90   RoundRectBackground(SkColor color, int corner_radius)
91       : color_(color),
92         corner_radius_(corner_radius) {}
93   virtual ~RoundRectBackground() {}
94
95  private:
96   // views::Background:
97   virtual void Paint(gfx::Canvas* canvas, views::View* view) const OVERRIDE {
98     SkPaint paint;
99     paint.setStyle(SkPaint::kFill_Style);
100     paint.setColor(color_);
101     canvas->DrawRoundRect(view->GetContentsBounds(), corner_radius_, paint);
102   }
103
104   SkColor color_;
105   int corner_radius_;
106
107   DISALLOW_COPY_AND_ASSIGN(RoundRectBackground);
108 };
109
110 class SearchBoxContainer : public views::View {
111  public:
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_);
121   }
122   virtual ~SearchBoxContainer() {}
123
124  private:
125   // views::View:
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);
130   }
131   virtual gfx::Size GetPreferredSize() const OVERRIDE {
132     return gfx::Size(kSearchBoxWidth, kSearchBoxHeight);
133   }
134
135   // Owned by the views hierarchy.
136   app_list::SearchBoxView* search_box_;
137
138   DISALLOW_COPY_AND_ASSIGN(SearchBoxContainer);
139 };
140
141 }  // namespace
142
143 namespace athena {
144
145 AthenaStartPageView::AthenaStartPageView(
146     app_list::AppListViewDelegate* view_delegate) {
147   app_list::AppListItemList* top_level =
148       view_delegate->GetModel()->top_level_item_list();
149
150   container_ = new views::View();
151   AddChildView(container_);
152
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)));
162
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);
167
168   for (size_t i = 0; i < kMaxIconNum; ++i)
169     container_->AddChildView(new PlaceHolderButton());
170
171   set_background(views::Background::CreateSolidBackground(
172       255, 255, 255, 255 * 0.9));
173 }
174
175 AthenaStartPageView::~AthenaStartPageView() {}
176
177 void AthenaStartPageView::Layout() {
178   gfx::Rect container_bounds = bounds();
179   container_bounds.set_height(kPreferredHeightBottom);
180   container_->SetBoundsRect(container_bounds);
181 }
182
183 void AthenaStartPageView::QueryChanged(app_list::SearchBoxView* sender) {
184   // Nothing needs to be done.
185 }
186
187 }  // namespace athena