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/contents_switcher_view.h"
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"
19 const int kButtonImageSize
= 32;
20 const int kButtonSpacing
= 4;
22 class ContentsPageIndicatorView
: public views::View
{
24 ContentsPageIndicatorView() {};
25 virtual ~ContentsPageIndicatorView() {};
27 // Overridden from views::View:
28 virtual gfx::Size
GetPreferredSize() const OVERRIDE
{
29 return gfx::Size(0, kContentsSwitcherSeparatorHeight
);
33 DISALLOW_COPY_AND_ASSIGN(ContentsPageIndicatorView
);
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
);
46 ContentsSwitcherView::~ContentsSwitcherView() {}
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
));
53 views::CustomButton::STATE_NORMAL
,
54 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id
));
56 button
->set_tag(page_index
);
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
);
66 // A container view that will consume space when its child is not visible.
67 // TODO(calamity): Remove this once BoxLayout supports space-consuming
69 views::View
* indicator_container
= new views::View();
70 indicator_container
->SetLayoutManager(new views::FillLayout());
71 indicator_container
->AddChildView(indicator
);
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
);
80 AddChildView(button_container
);
83 void ContentsSwitcherView::ButtonPressed(views::Button
* sender
,
84 const ui::Event
& event
) {
85 contents_view_
->SetActivePage(sender
->tag());
88 void ContentsSwitcherView::TotalPagesChanged() {
91 void ContentsSwitcherView::SelectedPageChanged(int old_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);
99 if (new_selected
>= 0 && new_selected
< num_indicators
)
100 page_active_indicators_
[new_selected
]->SetVisible(true);
103 void ContentsSwitcherView::TransitionStarted() {
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();
117 SelectedPageChanged(old_selected
, new_selected
);
120 } // namespace app_list