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 "ui/app_list/views/start_page_view.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "content/public/browser/web_contents.h"
9 #include "ui/app_list/app_list_constants.h"
10 #include "ui/app_list/app_list_item.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/app_list_main_view.h"
14 #include "ui/app_list/views/tile_item_view.h"
15 #include "ui/gfx/canvas.h"
16 #include "ui/views/controls/button/custom_button.h"
17 #include "ui/views/controls/image_view.h"
18 #include "ui/views/controls/label.h"
19 #include "ui/views/controls/webview/webview.h"
20 #include "ui/views/layout/box_layout.h"
26 const int kTopMargin
= 30;
28 const int kWebViewWidth
= 200;
29 const int kWebViewHeight
= 105;
31 const int kInstantContainerSpacing
= 20;
32 const int kBarPlaceholderWidth
= 490;
33 const int kBarPlaceholderHeight
= 30;
35 const size_t kNumStartPageTiles
= 5;
36 const int kTileSpacing
= 10;
38 // A button that is the placeholder for the search bar in the start page view.
39 class BarPlaceholderButton
: public views::CustomButton
{
41 explicit BarPlaceholderButton(views::ButtonListener
* listener
)
42 : views::CustomButton(listener
) {}
44 virtual ~BarPlaceholderButton() {}
46 // Overridden from views::View:
47 virtual gfx::Size
GetPreferredSize() const OVERRIDE
{
48 return gfx::Size(kBarPlaceholderWidth
, kBarPlaceholderHeight
);
51 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
{
54 state() == STATE_HOVERED
? kPagerHoverColor
: kPagerNormalColor
);
58 // Paints a rectangular button.
59 void PaintButton(gfx::Canvas
* canvas
, SkColor base_color
) {
60 gfx::Rect
rect(GetContentsBounds());
61 rect
.ClampToCenteredSize(
62 gfx::Size(kBarPlaceholderWidth
, kBarPlaceholderHeight
));
65 paint
.setAntiAlias(true);
66 paint
.setStyle(SkPaint::kFill_Style
);
67 paint
.setColor(base_color
);
68 canvas
->DrawRect(rect
, paint
);
71 DISALLOW_COPY_AND_ASSIGN(BarPlaceholderButton
);
76 StartPageView::StartPageView(AppListMainView
* app_list_main_view
,
77 AppListViewDelegate
* view_delegate
)
78 : app_list_main_view_(app_list_main_view
),
80 view_delegate_(view_delegate
),
81 instant_container_(new views::View
),
82 tiles_container_(new views::View
) {
83 SetLayoutManager(new views::BoxLayout(
84 views::BoxLayout::kVertical
, 0, kTopMargin
, kInstantContainerSpacing
));
86 // The view containing the start page WebContents and the BarPlaceholder.
87 AddChildView(instant_container_
);
88 views::BoxLayout
* instant_layout_manager
= new views::BoxLayout(
89 views::BoxLayout::kVertical
, 0, 0, kInstantContainerSpacing
);
90 instant_layout_manager
->set_main_axis_alignment(
91 views::BoxLayout::MAIN_AXIS_ALIGNMENT_END
);
92 instant_container_
->SetLayoutManager(instant_layout_manager
);
94 content::WebContents
* start_page_web_contents
=
95 view_delegate
->GetStartPageContents();
96 views::WebView
* web_view
= new views::WebView(
97 start_page_web_contents
? start_page_web_contents
->GetBrowserContext()
99 web_view
->SetPreferredSize(gfx::Size(kWebViewWidth
, kWebViewHeight
));
100 web_view
->SetWebContents(start_page_web_contents
);
102 instant_container_
->AddChildView(web_view
);
103 instant_container_
->AddChildView(new BarPlaceholderButton(this));
105 // The view containing the start page tiles.
106 AddChildView(tiles_container_
);
107 views::BoxLayout
* tiles_layout_manager
=
108 new views::BoxLayout(views::BoxLayout::kHorizontal
, 0, 0, kTileSpacing
);
109 tiles_layout_manager
->set_main_axis_alignment(
110 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER
);
111 tiles_container_
->SetLayoutManager(tiles_layout_manager
);
112 for (size_t i
= 0; i
< kNumStartPageTiles
; ++i
) {
113 TileItemView
* tile_item
= new TileItemView();
114 tiles_container_
->AddChildView(tile_item
);
115 tile_views_
.push_back(tile_item
);
118 SetModel(view_delegate_
->GetModel());
119 view_delegate_
->AddObserver(this);
122 StartPageView::~StartPageView() {
123 view_delegate_
->RemoveObserver(this);
126 void StartPageView::SetModel(AppListModel
* model
) {
129 model_
->RemoveObserver(this);
131 model_
->AddObserver(this);
135 void StartPageView::Reset() {
136 instant_container_
->SetVisible(true);
137 if (!model_
|| !model_
->top_level_item_list())
140 for (size_t i
= 0; i
< kNumStartPageTiles
; ++i
) {
141 AppListItem
* item
= NULL
;
142 if (i
< model_
->top_level_item_list()->item_count())
143 item
= model_
->top_level_item_list()->item_at(i
);
144 tile_views_
[i
]->SetAppListItem(item
);
150 void StartPageView::ButtonPressed(views::Button
* sender
,
151 const ui::Event
& event
) {
152 app_list_main_view_
->OnStartPageSearchButtonPressed();
153 instant_container_
->SetVisible(false);
156 void StartPageView::OnProfilesChanged() {
157 SetModel(view_delegate_
->GetModel());
160 void StartPageView::OnAppListModelStatusChanged() {
164 void StartPageView::OnAppListItemAdded(AppListItem
* item
) {
168 void StartPageView::OnAppListItemDeleted() {
172 void StartPageView::OnAppListItemUpdated(AppListItem
* item
) {
176 } // namespace app_list