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" | 7 #include "ui/app_list/app_list_constants.h" |
8 #include "ui/app_list/views/contents_view.h" | 8 #include "ui/app_list/views/contents_view.h" |
9 #include "ui/views/background.h" | |
9 #include "ui/views/controls/button/custom_button.h" | 10 #include "ui/views/controls/button/custom_button.h" |
10 #include "ui/views/controls/button/image_button.h" | 11 #include "ui/views/controls/button/image_button.h" |
11 #include "ui/views/layout/box_layout.h" | 12 #include "ui/views/layout/box_layout.h" |
13 #include "ui/views/layout/fill_layout.h" | |
12 | 14 |
13 namespace app_list { | 15 namespace app_list { |
14 | 16 |
15 namespace { | 17 namespace { |
16 | 18 |
17 const int kPreferredHeight = 32; | 19 const int kPageIndicatorHeight = 1; |
20 const int kButtonImageSize = 32; | |
21 const int kPreferredHeight = kButtonImageSize + kPageIndicatorHeight; | |
18 const int kButtonSpacing = 4; | 22 const int kButtonSpacing = 4; |
19 | 23 |
24 class ContentsPageIndicatorView : public views::View { | |
25 public: | |
26 ContentsPageIndicatorView() {}; | |
27 virtual ~ContentsPageIndicatorView() {}; | |
28 | |
29 // Overridden from views::View: | |
30 virtual gfx::Size GetPreferredSize() const OVERRIDE { | |
31 return gfx::Size(0, kPageIndicatorHeight); | |
32 }; | |
33 | |
34 private: | |
35 DISALLOW_COPY_AND_ASSIGN(ContentsPageIndicatorView); | |
36 }; | |
37 | |
20 } // namespace | 38 } // namespace |
21 | 39 |
22 ContentsSwitcherView::ContentsSwitcherView(ContentsView* contents_view) | 40 ContentsSwitcherView::ContentsSwitcherView(ContentsView* contents_view) |
23 : contents_view_(contents_view), buttons_(new views::View) { | 41 : contents_view_(contents_view), buttons_(new views::View) { |
24 AddChildView(buttons_); | 42 AddChildView(buttons_); |
25 | 43 |
26 buttons_->SetLayoutManager(new views::BoxLayout( | 44 buttons_->SetLayoutManager(new views::BoxLayout( |
27 views::BoxLayout::kHorizontal, 0, 0, kButtonSpacing)); | 45 views::BoxLayout::kHorizontal, 0, 0, kButtonSpacing)); |
28 } | 46 } |
29 | 47 |
30 ContentsSwitcherView::~ContentsSwitcherView() {} | 48 ContentsSwitcherView::~ContentsSwitcherView() {} |
31 | 49 |
32 void ContentsSwitcherView::AddSwitcherButton(int resource_id, int page_index) { | 50 void ContentsSwitcherView::AddSwitcherButton(int resource_id, int page_index) { |
33 views::ImageButton* button = new views::ImageButton(this); | 51 views::ImageButton* button = new views::ImageButton(this); |
52 button->SetPreferredSize(gfx::Size(kButtonImageSize, kButtonImageSize)); | |
Matt Giuca
2014/07/09 07:01:20
What does setting this do? (Why is it needed now a
calamity
2014/07/09 08:02:54
It should have been there before. It has no effect
| |
34 if (resource_id) { | 53 if (resource_id) { |
35 button->SetImage( | 54 button->SetImage( |
36 views::CustomButton::STATE_NORMAL, | 55 views::CustomButton::STATE_NORMAL, |
37 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id)); | 56 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id)); |
38 } | 57 } |
39 button->set_tag(page_index); | 58 button->set_tag(page_index); |
40 buttons_->AddChildView(button); | 59 |
60 // Add an indicator for the current launcher page. | |
61 app_list::ContentsPageIndicatorView* indicator = | |
62 new app_list::ContentsPageIndicatorView(); | |
63 indicator->set_background( | |
64 views::Background::CreateSolidBackground(app_list::kPagerSelectedColor)); | |
65 indicator->SetVisible(false); | |
66 page_active_indicators_.push_back(indicator); | |
67 | |
68 // A container view that will consume space when its child is not visible. | |
69 // TODO(calamity): Remove this once BoxLayout supports space-consuming | |
70 // invisible views. | |
71 views::View* indicator_container = new views::View(); | |
72 indicator_container->SetLayoutManager(new views::FillLayout()); | |
73 indicator_container->AddChildView(indicator); | |
74 | |
75 // View containing the indicator view and image button. | |
76 views::View* button_container = new views::View(); | |
77 views::BoxLayout* layout_manager = | |
78 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); | |
79 layout_manager->set_main_axis_alignment( | |
80 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); | |
81 button_container->SetLayoutManager(layout_manager); | |
82 button_container->AddChildView(indicator_container); | |
83 button_container->AddChildView(button); | |
84 | |
85 buttons_->AddChildView(button_container); | |
41 } | 86 } |
42 | 87 |
43 gfx::Size ContentsSwitcherView::GetPreferredSize() const { | 88 gfx::Size ContentsSwitcherView::GetPreferredSize() const { |
44 return gfx::Size(buttons_->GetPreferredSize().width(), kPreferredHeight); | 89 return gfx::Size(buttons_->GetPreferredSize().width(), kPreferredHeight); |
45 } | 90 } |
46 | 91 |
47 void ContentsSwitcherView::Layout() { | 92 void ContentsSwitcherView::Layout() { |
48 gfx::Rect rect(GetContentsBounds()); | 93 gfx::Rect rect(GetContentsBounds()); |
49 | 94 |
50 // Makes |buttons_| horizontally center and vertically fill. | 95 // Makes |buttons_| horizontally center and vertically fill. |
51 gfx::Size buttons_size(buttons_->GetPreferredSize()); | 96 gfx::Size buttons_size(buttons_->GetPreferredSize()); |
52 gfx::Rect buttons_bounds(rect.CenterPoint().x() - buttons_size.width() / 2, | 97 gfx::Rect buttons_bounds(rect.CenterPoint().x() - buttons_size.width() / 2, |
53 rect.y(), | 98 rect.y(), |
54 buttons_size.width(), | 99 buttons_size.width(), |
55 rect.height()); | 100 rect.height()); |
56 buttons_->SetBoundsRect(gfx::IntersectRects(rect, buttons_bounds)); | 101 buttons_->SetBoundsRect(gfx::IntersectRects(rect, buttons_bounds)); |
57 } | 102 } |
58 | 103 |
59 void ContentsSwitcherView::ButtonPressed(views::Button* sender, | 104 void ContentsSwitcherView::ButtonPressed(views::Button* sender, |
60 const ui::Event& event) { | 105 const ui::Event& event) { |
61 contents_view_->SetActivePage(sender->tag()); | 106 contents_view_->SetActivePage(sender->tag()); |
62 } | 107 } |
63 | 108 |
109 void ContentsSwitcherView::SelectedPageChanged(int old_selected, | |
110 int new_selected) { | |
111 int num_indicators = static_cast<int>(page_active_indicators_.size()); | |
112 if (old_selected >= 0 && old_selected < num_indicators) | |
113 page_active_indicators_[old_selected]->SetVisible(false); | |
114 | |
115 if (new_selected >= 0 && new_selected < num_indicators) | |
116 page_active_indicators_[new_selected]->SetVisible(true); | |
117 } | |
118 | |
64 } // namespace app_list | 119 } // namespace app_list |
OLD | NEW |