OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/app_list/views/contents_switcher_view.h" | 5 #include "ui/app_list/views/contents_switcher_view.h" |
6 | 6 |
7 #include "ui/app_list/app_list_constants.h" | |
8 #include "ui/app_list/views/contents_view.h" | 7 #include "ui/app_list/views/contents_view.h" |
9 #include "ui/views/background.h" | 8 #include "ui/base/resource/resource_bundle.h" |
10 #include "ui/views/controls/button/custom_button.h" | 9 #include "ui/views/controls/button/custom_button.h" |
11 #include "ui/views/controls/button/image_button.h" | 10 #include "ui/views/controls/button/image_button.h" |
12 #include "ui/views/layout/box_layout.h" | 11 #include "ui/views/layout/box_layout.h" |
13 #include "ui/views/layout/fill_layout.h" | |
14 | 12 |
15 namespace app_list { | 13 namespace app_list { |
16 | 14 |
17 namespace { | 15 namespace { |
18 | 16 |
19 const int kButtonSpacing = 4; | 17 const int kButtonSpacing = 4; |
20 const int kMinimumHeight = 39; | 18 const int kMinimumHeight = 39; |
21 | 19 |
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 | 20 } // namespace |
37 | 21 |
38 ContentsSwitcherView::ContentsSwitcherView(ContentsView* contents_view) | 22 ContentsSwitcherView::ContentsSwitcherView(ContentsView* contents_view) |
39 : contents_view_(contents_view) { | 23 : contents_view_(contents_view) { |
40 views::BoxLayout* layout = new views::BoxLayout( | 24 views::BoxLayout* layout = new views::BoxLayout( |
41 views::BoxLayout::kHorizontal, 0, 0, kButtonSpacing); | 25 views::BoxLayout::kHorizontal, 0, 0, kButtonSpacing); |
42 layout->set_main_axis_alignment(views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); | 26 layout->set_main_axis_alignment(views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); |
43 layout->set_minimum_cross_axis_size(kMinimumHeight); | 27 layout->set_minimum_cross_axis_size(kMinimumHeight); |
44 SetLayoutManager(layout); | 28 SetLayoutManager(layout); |
45 } | 29 } |
46 | 30 |
47 ContentsSwitcherView::~ContentsSwitcherView() {} | 31 ContentsSwitcherView::~ContentsSwitcherView() {} |
48 | 32 |
49 void ContentsSwitcherView::AddSwitcherButton(int resource_id, int page_index) { | 33 void ContentsSwitcherView::AddSwitcherButton(int resource_id, int page_index) { |
50 views::ImageButton* button = new views::ImageButton(this); | 34 views::ImageButton* button = new views::ImageButton(this); |
51 button->SetImage( | 35 button->SetImage( |
52 views::CustomButton::STATE_NORMAL, | 36 views::CustomButton::STATE_NORMAL, |
53 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id)); | 37 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id)); |
54 button->set_tag(page_index); | 38 button->set_tag(page_index); |
55 | 39 |
56 // Add an indicator for the current launcher page. | 40 AddChildView(button); |
57 app_list::ContentsPageIndicatorView* indicator = | |
58 new app_list::ContentsPageIndicatorView(); | |
59 indicator->set_background( | |
60 views::Background::CreateSolidBackground(app_list::kPagerSelectedColor)); | |
61 indicator->SetVisible(false); | |
62 page_active_indicators_[page_index] = indicator; | |
63 | |
64 // A container view that will consume space when its child is not visible. | |
65 // TODO(calamity): Remove this once BoxLayout supports space-consuming | |
66 // invisible views. | |
67 views::View* indicator_container = new views::View(); | |
68 indicator_container->SetLayoutManager(new views::FillLayout()); | |
69 indicator_container->AddChildView(indicator); | |
70 | |
71 // View containing the indicator view and image button. | |
72 views::View* button_container = new views::View(); | |
73 button_container->SetLayoutManager( | |
74 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0)); | |
75 button_container->AddChildView(indicator_container); | |
76 button_container->AddChildView(button); | |
77 | |
78 AddChildView(button_container); | |
79 } | 41 } |
80 | 42 |
81 void ContentsSwitcherView::ButtonPressed(views::Button* sender, | 43 void ContentsSwitcherView::ButtonPressed(views::Button* sender, |
82 const ui::Event& event) { | 44 const ui::Event& event) { |
83 contents_view_->SetActivePage(sender->tag()); | 45 contents_view_->SetActivePage(sender->tag()); |
84 } | 46 } |
85 | 47 |
86 void ContentsSwitcherView::TotalPagesChanged() { | 48 void ContentsSwitcherView::TotalPagesChanged() { |
87 } | 49 } |
88 | 50 |
89 void ContentsSwitcherView::SelectedPageChanged(int old_selected, | 51 void ContentsSwitcherView::SelectedPageChanged(int old_selected, |
90 int new_selected) { | 52 int new_selected) { |
91 // Makes the indicator visible when it is first drawn and when the | 53 // TODO(mgiuca): Visually indicate which page is now selected. |
92 // selected page is changed. | |
93 std::map<int, views::View*>::const_iterator it = | |
94 page_active_indicators_.find(old_selected); | |
95 if (it != page_active_indicators_.end()) | |
96 it->second->SetVisible(false); | |
97 | |
98 it = page_active_indicators_.find(new_selected); | |
99 if (it != page_active_indicators_.end()) | |
100 it->second->SetVisible(true); | |
101 } | 54 } |
102 | 55 |
103 void ContentsSwitcherView::TransitionStarted() { | 56 void ContentsSwitcherView::TransitionStarted() { |
104 } | 57 } |
105 | 58 |
106 void ContentsSwitcherView::TransitionChanged() { | 59 void ContentsSwitcherView::TransitionChanged() { |
107 // Change the indicator during a launcher page transition. | 60 // Change the indicator during a launcher page transition. |
108 const PaginationModel& pm = contents_view_->pagination_model(); | 61 const PaginationModel& pm = contents_view_->pagination_model(); |
109 int old_selected = pm.selected_page(); | 62 int old_selected = pm.selected_page(); |
110 int new_selected = pm.transition().target_page; | 63 int new_selected = pm.transition().target_page; |
111 if (pm.IsRevertingCurrentTransition()) { | 64 if (pm.IsRevertingCurrentTransition()) { |
112 // Swap the direction if the transition is reversed. | 65 // Swap the direction if the transition is reversed. |
113 old_selected = pm.transition().target_page; | 66 old_selected = pm.transition().target_page; |
114 new_selected = pm.selected_page(); | 67 new_selected = pm.selected_page(); |
115 } | 68 } |
116 | 69 |
117 SelectedPageChanged(old_selected, new_selected); | 70 SelectedPageChanged(old_selected, new_selected); |
118 } | 71 } |
119 | 72 |
120 } // namespace app_list | 73 } // namespace app_list |
OLD | NEW |