| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/page_switcher.h" | 5 #include "ui/app_list/page_switcher.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "third_party/skia/include/core/SkPath.h" | 9 #include "third_party/skia/include/core/SkPath.h" |
| 10 #include "ui/app_list/pagination_model.h" | 10 #include "ui/app_list/pagination_model.h" |
| 11 #include "ui/base/animation/throb_animation.h" | 11 #include "ui/base/animation/throb_animation.h" |
| 12 #include "ui/gfx/canvas.h" | 12 #include "ui/gfx/canvas.h" |
| 13 #include "ui/gfx/skia_util.h" | 13 #include "ui/gfx/skia_util.h" |
| 14 #include "ui/views/controls/button/custom_button.h" | 14 #include "ui/views/controls/button/custom_button.h" |
| 15 #include "ui/views/layout/box_layout.h" | 15 #include "ui/views/layout/box_layout.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 const int kPreferredHeight = 57; | 19 const int kPreferredHeight = 57; |
| 20 | 20 |
| 21 const int kButtonSpacing = 18; | 21 const int kMaxButtonSpacing = 18; |
| 22 const int kMinButtonSpacing = 4; |
| 22 const int kMaxButtonWidth = 68; | 23 const int kMaxButtonWidth = 68; |
| 23 const int kMinButtonWidth = 44; | 24 const int kMinButtonWidth = 28; |
| 24 const int kButtonHeight = 6; | 25 const int kButtonHeight = 6; |
| 25 const int kButtonCornerRadius = 2; | 26 const int kButtonCornerRadius = 2; |
| 27 const int kButtonStripPadding = 20; |
| 26 | 28 |
| 27 const SkColor kHoverColor = SkColorSetRGB(0xB4, 0xB4, 0xB4); | 29 const SkColor kHoverColor = SkColorSetRGB(0xB4, 0xB4, 0xB4); |
| 28 | 30 |
| 29 const SkColor kNormalColor = SkColorSetRGB(0xE2, 0xE2, 0xE2); | 31 const SkColor kNormalColor = SkColorSetRGB(0xE2, 0xE2, 0xE2); |
| 30 | 32 |
| 31 const SkColor kSelectedColor = SkColorSetRGB(0x46, 0x8F, 0xFC); | 33 const SkColor kSelectedColor = SkColorSetRGB(0x46, 0x8F, 0xFC); |
| 32 | 34 |
| 33 class PageSwitcherButton : public views::CustomButton { | 35 class PageSwitcherButton : public views::CustomButton { |
| 34 public: | 36 public: |
| 35 explicit PageSwitcherButton(views::ButtonListener* listener) | 37 explicit PageSwitcherButton(views::ButtonListener* listener) |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 return static_cast<PageSwitcherButton*>(buttons->child_at(index)); | 119 return static_cast<PageSwitcherButton*>(buttons->child_at(index)); |
| 118 } | 120 } |
| 119 | 121 |
| 120 } // namespace | 122 } // namespace |
| 121 | 123 |
| 122 namespace app_list { | 124 namespace app_list { |
| 123 | 125 |
| 124 PageSwitcher::PageSwitcher(PaginationModel* model) | 126 PageSwitcher::PageSwitcher(PaginationModel* model) |
| 125 : model_(model), | 127 : model_(model), |
| 126 buttons_(new views::View) { | 128 buttons_(new views::View) { |
| 127 buttons_->SetLayoutManager(new views::BoxLayout( | |
| 128 views::BoxLayout::kHorizontal, 0, 0, kButtonSpacing)); | |
| 129 AddChildView(buttons_); | 129 AddChildView(buttons_); |
| 130 | 130 |
| 131 TotalPagesChanged(); | 131 TotalPagesChanged(); |
| 132 SelectedPageChanged(-1, model->selected_page()); | 132 SelectedPageChanged(-1, model->selected_page()); |
| 133 model_->AddObserver(this); | 133 model_->AddObserver(this); |
| 134 } | 134 } |
| 135 | 135 |
| 136 PageSwitcher::~PageSwitcher() { | 136 PageSwitcher::~PageSwitcher() { |
| 137 model_->RemoveObserver(this); | 137 model_->RemoveObserver(this); |
| 138 } | 138 } |
| 139 | 139 |
| 140 gfx::Size PageSwitcher::GetPreferredSize() { | 140 gfx::Size PageSwitcher::GetPreferredSize() { |
| 141 // Always return a size with correct height so that container resize is not | 141 // Always return a size with correct height so that container resize is not |
| 142 // needed when more pages are added. | 142 // needed when more pages are added. |
| 143 return gfx::Size(buttons_->GetPreferredSize().width(), | 143 return gfx::Size(buttons_->GetPreferredSize().width(), |
| 144 kPreferredHeight); | 144 kPreferredHeight); |
| 145 } | 145 } |
| 146 | 146 |
| 147 void PageSwitcher::Layout() { | 147 void PageSwitcher::Layout() { |
| 148 gfx::Rect rect(GetContentsBounds()); | 148 gfx::Rect rect(GetContentsBounds()); |
| 149 | 149 |
| 150 CalculateButtonWidth(rect.width()); | 150 CalculateButtonWidthAndSpacing(rect.width()); |
| 151 | 151 |
| 152 // Makes |buttons_| horizontally center and vertically fill. | 152 // Makes |buttons_| horizontally center and vertically fill. |
| 153 gfx::Size buttons_size(buttons_->GetPreferredSize()); | 153 gfx::Size buttons_size(buttons_->GetPreferredSize()); |
| 154 gfx::Rect buttons_bounds(rect.CenterPoint().x() - buttons_size.width() / 2, | 154 gfx::Rect buttons_bounds(rect.CenterPoint().x() - buttons_size.width() / 2, |
| 155 rect.y(), | 155 rect.y(), |
| 156 buttons_size.width(), | 156 buttons_size.width(), |
| 157 rect.height()); | 157 rect.height()); |
| 158 buttons_->SetBoundsRect(rect.Intersect(buttons_bounds)); | 158 buttons_->SetBoundsRect(rect.Intersect(buttons_bounds)); |
| 159 } | 159 } |
| 160 | 160 |
| 161 void PageSwitcher::CalculateButtonWidth(int contents_width) { | 161 void PageSwitcher::CalculateButtonWidthAndSpacing(int contents_width) { |
| 162 int button_count = buttons_->child_count(); | 162 int button_count = buttons_->child_count(); |
| 163 if (!button_count) | 163 if (!button_count) |
| 164 return; | 164 return; |
| 165 | 165 |
| 166 int button_width = contents_width / button_count - kButtonSpacing; | 166 contents_width -= 2 * kButtonStripPadding; |
| 167 |
| 168 int button_width = kMinButtonWidth; |
| 169 int button_spacing = kMinButtonSpacing; |
| 170 if (button_count > 1) { |
| 171 button_spacing = (contents_width - button_width * button_count) / |
| 172 (button_count - 1); |
| 173 button_spacing = std::min(kMaxButtonSpacing, |
| 174 std::max(kMinButtonSpacing, button_spacing)); |
| 175 } |
| 176 |
| 177 button_width = (contents_width - (button_count - 1) * button_spacing) / |
| 178 button_count; |
| 167 button_width = std::min(kMaxButtonWidth, | 179 button_width = std::min(kMaxButtonWidth, |
| 168 std::max(kMinButtonWidth, button_width)); | 180 std::max(kMinButtonWidth, button_width)); |
| 169 | 181 |
| 182 buttons_->SetLayoutManager(new views::BoxLayout( |
| 183 views::BoxLayout::kHorizontal, kButtonStripPadding, 0, button_spacing)); |
| 170 for (int i = 0; i < button_count; ++i) { | 184 for (int i = 0; i < button_count; ++i) { |
| 171 PageSwitcherButton* button = | 185 PageSwitcherButton* button = |
| 172 static_cast<PageSwitcherButton*>(buttons_->child_at(i)); | 186 static_cast<PageSwitcherButton*>(buttons_->child_at(i)); |
| 173 button->set_button_width(button_width); | 187 button->set_button_width(button_width); |
| 174 } | 188 } |
| 175 } | 189 } |
| 176 | 190 |
| 177 void PageSwitcher::ButtonPressed(views::Button* sender, | 191 void PageSwitcher::ButtonPressed(views::Button* sender, |
| 178 const ui::Event& event) { | 192 const ui::Event& event) { |
| 179 for (int i = 0; i < buttons_->child_count(); ++i) { | 193 for (int i = 0; i < buttons_->child_count(); ++i) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 remaining = -remaining; | 227 remaining = -remaining; |
| 214 progress = -progress; | 228 progress = -progress; |
| 215 } | 229 } |
| 216 | 230 |
| 217 GetButtonByIndex(buttons_, current_page)->SetSelectedRange(remaining); | 231 GetButtonByIndex(buttons_, current_page)->SetSelectedRange(remaining); |
| 218 if (model_->is_valid_page(target_page)) | 232 if (model_->is_valid_page(target_page)) |
| 219 GetButtonByIndex(buttons_, target_page)->SetSelectedRange(progress); | 233 GetButtonByIndex(buttons_, target_page)->SetSelectedRange(progress); |
| 220 } | 234 } |
| 221 | 235 |
| 222 } // namespace app_list | 236 } // namespace app_list |
| OLD | NEW |