Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ui / app_list / views / contents_switcher_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 "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/background.h"
10 #include "ui/views/controls/button/custom_button.h"
11 #include "ui/views/controls/button/image_button.h"
12 #include "ui/views/layout/box_layout.h"
13 #include "ui/views/layout/fill_layout.h"
14
15 namespace app_list {
16
17 namespace {
18
19 const int kButtonImageSize = 32;
20 const int kButtonSpacing = 4;
21
22 class ContentsPageIndicatorView : public views::View {
23  public:
24   ContentsPageIndicatorView() {};
25   virtual ~ContentsPageIndicatorView() {};
26
27   // Overridden from views::View:
28   virtual gfx::Size GetPreferredSize() const OVERRIDE {
29     return gfx::Size(0, kContentsSwitcherSeparatorHeight);
30   };
31
32  private:
33   DISALLOW_COPY_AND_ASSIGN(ContentsPageIndicatorView);
34 };
35
36 }  // namespace
37
38 ContentsSwitcherView::ContentsSwitcherView(ContentsView* contents_view)
39     : contents_view_(contents_view) {
40   views::BoxLayout* layout = new views::BoxLayout(
41       views::BoxLayout::kHorizontal, 0, 0, kButtonSpacing);
42   layout->set_main_axis_alignment(views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
43   SetLayoutManager(layout);
44 }
45
46 ContentsSwitcherView::~ContentsSwitcherView() {}
47
48 void ContentsSwitcherView::AddSwitcherButton(int resource_id, int page_index) {
49   views::ImageButton* button = new views::ImageButton(this);
50   button->SetPreferredSize(gfx::Size(kButtonImageSize, kButtonImageSize));
51   if (resource_id) {
52     button->SetImage(
53         views::CustomButton::STATE_NORMAL,
54         ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id));
55   }
56   button->set_tag(page_index);
57
58   // Add an indicator for the current launcher page.
59   app_list::ContentsPageIndicatorView* indicator =
60       new app_list::ContentsPageIndicatorView();
61   indicator->set_background(
62       views::Background::CreateSolidBackground(app_list::kPagerSelectedColor));
63   indicator->SetVisible(false);
64   page_active_indicators_.push_back(indicator);
65
66   // A container view that will consume space when its child is not visible.
67   // TODO(calamity): Remove this once BoxLayout supports space-consuming
68   // invisible views.
69   views::View* indicator_container = new views::View();
70   indicator_container->SetLayoutManager(new views::FillLayout());
71   indicator_container->AddChildView(indicator);
72
73   // View containing the indicator view and image button.
74   views::View* button_container = new views::View();
75   button_container->SetLayoutManager(
76       new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0));
77   button_container->AddChildView(indicator_container);
78   button_container->AddChildView(button);
79
80   AddChildView(button_container);
81 }
82
83 void ContentsSwitcherView::ButtonPressed(views::Button* sender,
84                                          const ui::Event& event) {
85   contents_view_->SetActivePage(sender->tag());
86 }
87
88 void ContentsSwitcherView::TotalPagesChanged() {
89 }
90
91 void ContentsSwitcherView::SelectedPageChanged(int old_selected,
92                                                int new_selected) {
93   // Makes the indicator visible when it is first drawn and when the
94   // selected page is changed.
95   int num_indicators = static_cast<int>(page_active_indicators_.size());
96   if (old_selected >= 0 && old_selected < num_indicators)
97     page_active_indicators_[old_selected]->SetVisible(false);
98
99   if (new_selected >= 0 && new_selected < num_indicators)
100     page_active_indicators_[new_selected]->SetVisible(true);
101 }
102
103 void ContentsSwitcherView::TransitionStarted() {
104 }
105
106 void ContentsSwitcherView::TransitionChanged() {
107   // Change the indicator during a launcher page transition.
108   const PaginationModel& pm = contents_view_->pagination_model();
109   int old_selected = pm.selected_page();
110   int new_selected = pm.transition().target_page;
111   if (pm.IsRevertingCurrentTransition()) {
112     // Swap the direction if the transition is reversed.
113     old_selected = pm.transition().target_page;
114     new_selected = pm.selected_page();
115   }
116
117   SelectedPageChanged(old_selected, new_selected);
118 }
119
120 }  // namespace app_list