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 "ui/app_list/views/contents_switcher_view.h"
6
7 #include "ui/app_list/app_list_constants.h"
8 #include "ui/app_list/views/contents_view.h"
9 #include "ui/views/controls/button/custom_button.h"
10 #include "ui/views/controls/button/image_button.h"
11 #include "ui/views/layout/box_layout.h"
12
13 namespace app_list {
14
15 namespace {
16
17 const int kPreferredHeight = 32;
18 const int kButtonSpacing = 4;
19
20 } // namespace
21
ContentsSwitcherView(ContentsView * contents_view)22 ContentsSwitcherView::ContentsSwitcherView(ContentsView* contents_view)
23 : contents_view_(contents_view), buttons_(new views::View) {
24 AddChildView(buttons_);
25
26 buttons_->SetLayoutManager(new views::BoxLayout(
27 views::BoxLayout::kHorizontal, 0, 0, kButtonSpacing));
28 }
29
~ContentsSwitcherView()30 ContentsSwitcherView::~ContentsSwitcherView() {}
31
AddSwitcherButton(int resource_id,int page_index)32 void ContentsSwitcherView::AddSwitcherButton(int resource_id, int page_index) {
33 views::ImageButton* button = new views::ImageButton(this);
34 if (resource_id) {
35 button->SetImage(
36 views::CustomButton::STATE_NORMAL,
37 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id));
38 }
39 button->set_tag(page_index);
40 buttons_->AddChildView(button);
41 }
42
GetPreferredSize() const43 gfx::Size ContentsSwitcherView::GetPreferredSize() const {
44 return gfx::Size(buttons_->GetPreferredSize().width(), kPreferredHeight);
45 }
46
Layout()47 void ContentsSwitcherView::Layout() {
48 gfx::Rect rect(GetContentsBounds());
49
50 // Makes |buttons_| horizontally center and vertically fill.
51 gfx::Size buttons_size(buttons_->GetPreferredSize());
52 gfx::Rect buttons_bounds(rect.CenterPoint().x() - buttons_size.width() / 2,
53 rect.y(),
54 buttons_size.width(),
55 rect.height());
56 buttons_->SetBoundsRect(gfx::IntersectRects(rect, buttons_bounds));
57 }
58
ButtonPressed(views::Button * sender,const ui::Event & event)59 void ContentsSwitcherView::ButtonPressed(views::Button* sender,
60 const ui::Event& event) {
61 contents_view_->SetActivePage(sender->tag());
62 }
63
64 } // namespace app_list
65