| 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/views/page_switcher.h" | 5 #include "ui/app_list/views/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/app_list_constants.h" | 10 #include "ui/app_list/app_list_constants.h" |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 : model_(model), | 121 : model_(model), |
| 122 buttons_(new views::View) { | 122 buttons_(new views::View) { |
| 123 AddChildView(buttons_); | 123 AddChildView(buttons_); |
| 124 | 124 |
| 125 TotalPagesChanged(); | 125 TotalPagesChanged(); |
| 126 SelectedPageChanged(-1, model->selected_page()); | 126 SelectedPageChanged(-1, model->selected_page()); |
| 127 model_->AddObserver(this); | 127 model_->AddObserver(this); |
| 128 } | 128 } |
| 129 | 129 |
| 130 PageSwitcher::~PageSwitcher() { | 130 PageSwitcher::~PageSwitcher() { |
| 131 model_->RemoveObserver(this); | 131 if (model_) |
| 132 model_->RemoveObserver(this); |
| 132 } | 133 } |
| 133 | 134 |
| 134 int PageSwitcher::GetPageForPoint(const gfx::Point& point) const { | 135 int PageSwitcher::GetPageForPoint(const gfx::Point& point) const { |
| 135 if (!buttons_->bounds().Contains(point)) | 136 if (!buttons_->bounds().Contains(point)) |
| 136 return -1; | 137 return -1; |
| 137 | 138 |
| 138 gfx::Point buttons_point(point); | 139 gfx::Point buttons_point(point); |
| 139 views::View::ConvertPointToTarget(this, buttons_, &buttons_point); | 140 views::View::ConvertPointToTarget(this, buttons_, &buttons_point); |
| 140 | 141 |
| 141 for (int i = 0; i < buttons_->child_count(); ++i) { | 142 for (int i = 0; i < buttons_->child_count(); ++i) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 158 return; | 159 return; |
| 159 } | 160 } |
| 160 | 161 |
| 161 for (int i = 0; i < button_count; ++i) { | 162 for (int i = 0; i < button_count; ++i) { |
| 162 PageSwitcherButton* button = | 163 PageSwitcherButton* button = |
| 163 static_cast<PageSwitcherButton*>(buttons_->child_at(i)); | 164 static_cast<PageSwitcherButton*>(buttons_->child_at(i)); |
| 164 button->SetState(views::CustomButton::STATE_NORMAL); | 165 button->SetState(views::CustomButton::STATE_NORMAL); |
| 165 } | 166 } |
| 166 } | 167 } |
| 167 | 168 |
| 169 void PageSwitcher::ReleasePaginationModel() { |
| 170 if (!model_) |
| 171 return; |
| 172 model_->RemoveObserver(this); |
| 173 model_ = NULL; |
| 174 } |
| 175 |
| 168 gfx::Size PageSwitcher::GetPreferredSize() const { | 176 gfx::Size PageSwitcher::GetPreferredSize() const { |
| 169 // Always return a size with correct height so that container resize is not | 177 // Always return a size with correct height so that container resize is not |
| 170 // needed when more pages are added. | 178 // needed when more pages are added. |
| 171 return gfx::Size(buttons_->GetPreferredSize().width(), | 179 return gfx::Size(buttons_->GetPreferredSize().width(), |
| 172 kPreferredHeight); | 180 kPreferredHeight); |
| 173 } | 181 } |
| 174 | 182 |
| 175 void PageSwitcher::Layout() { | 183 void PageSwitcher::Layout() { |
| 176 gfx::Rect rect(GetContentsBounds()); | 184 gfx::Rect rect(GetContentsBounds()); |
| 177 | 185 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 views::BoxLayout::kHorizontal, kButtonStripPadding, 0, button_spacing)); | 219 views::BoxLayout::kHorizontal, kButtonStripPadding, 0, button_spacing)); |
| 212 for (int i = 0; i < button_count; ++i) { | 220 for (int i = 0; i < button_count; ++i) { |
| 213 PageSwitcherButton* button = | 221 PageSwitcherButton* button = |
| 214 static_cast<PageSwitcherButton*>(buttons_->child_at(i)); | 222 static_cast<PageSwitcherButton*>(buttons_->child_at(i)); |
| 215 button->set_button_width(button_width); | 223 button->set_button_width(button_width); |
| 216 } | 224 } |
| 217 } | 225 } |
| 218 | 226 |
| 219 void PageSwitcher::ButtonPressed(views::Button* sender, | 227 void PageSwitcher::ButtonPressed(views::Button* sender, |
| 220 const ui::Event& event) { | 228 const ui::Event& event) { |
| 229 if (!model_) |
| 230 return; |
| 231 |
| 221 for (int i = 0; i < buttons_->child_count(); ++i) { | 232 for (int i = 0; i < buttons_->child_count(); ++i) { |
| 222 if (sender == static_cast<views::Button*>(buttons_->child_at(i))) { | 233 if (sender == static_cast<views::Button*>(buttons_->child_at(i))) { |
| 223 model_->SelectPage(i, true /* animate */); | 234 model_->SelectPage(i, true /* animate */); |
| 224 break; | 235 break; |
| 225 } | 236 } |
| 226 } | 237 } |
| 227 } | 238 } |
| 228 | 239 |
| 229 void PageSwitcher::TotalPagesChanged() { | 240 void PageSwitcher::TotalPagesChanged() { |
| 241 DCHECK(model_); |
| 230 buttons_->RemoveAllChildViews(true); | 242 buttons_->RemoveAllChildViews(true); |
| 231 for (int i = 0; i < model_->total_pages(); ++i) { | 243 for (int i = 0; i < model_->total_pages(); ++i) { |
| 232 PageSwitcherButton* button = new PageSwitcherButton(this); | 244 PageSwitcherButton* button = new PageSwitcherButton(this); |
| 233 button->SetSelectedRange(i == model_->selected_page() ? 1 : 0); | 245 button->SetSelectedRange(i == model_->selected_page() ? 1 : 0); |
| 234 buttons_->AddChildView(button); | 246 buttons_->AddChildView(button); |
| 235 } | 247 } |
| 236 buttons_->SetVisible(model_->total_pages() > 1); | 248 buttons_->SetVisible(model_->total_pages() > 1); |
| 237 Layout(); | 249 Layout(); |
| 238 } | 250 } |
| 239 | 251 |
| 240 void PageSwitcher::SelectedPageChanged(int old_selected, int new_selected) { | 252 void PageSwitcher::SelectedPageChanged(int old_selected, int new_selected) { |
| 241 if (old_selected >= 0 && old_selected < buttons_->child_count()) | 253 if (old_selected >= 0 && old_selected < buttons_->child_count()) |
| 242 GetButtonByIndex(buttons_, old_selected)->SetSelectedRange(0); | 254 GetButtonByIndex(buttons_, old_selected)->SetSelectedRange(0); |
| 243 if (new_selected >= 0 && new_selected < buttons_->child_count()) | 255 if (new_selected >= 0 && new_selected < buttons_->child_count()) |
| 244 GetButtonByIndex(buttons_, new_selected)->SetSelectedRange(1); | 256 GetButtonByIndex(buttons_, new_selected)->SetSelectedRange(1); |
| 245 } | 257 } |
| 246 | 258 |
| 247 void PageSwitcher::TransitionStarted() { | 259 void PageSwitcher::TransitionStarted() { |
| 248 } | 260 } |
| 249 | 261 |
| 250 void PageSwitcher::TransitionChanged() { | 262 void PageSwitcher::TransitionChanged() { |
| 263 DCHECK(model_); |
| 251 const int current_page = model_->selected_page(); | 264 const int current_page = model_->selected_page(); |
| 252 const int target_page = model_->transition().target_page; | 265 const int target_page = model_->transition().target_page; |
| 253 | 266 |
| 254 double progress = model_->transition().progress; | 267 double progress = model_->transition().progress; |
| 255 double remaining = progress - 1; | 268 double remaining = progress - 1; |
| 256 | 269 |
| 257 if (current_page > target_page) { | 270 if (current_page > target_page) { |
| 258 remaining = -remaining; | 271 remaining = -remaining; |
| 259 progress = -progress; | 272 progress = -progress; |
| 260 } | 273 } |
| 261 | 274 |
| 262 GetButtonByIndex(buttons_, current_page)->SetSelectedRange(remaining); | 275 GetButtonByIndex(buttons_, current_page)->SetSelectedRange(remaining); |
| 263 if (model_->is_valid_page(target_page)) | 276 if (model_->is_valid_page(target_page)) |
| 264 GetButtonByIndex(buttons_, target_page)->SetSelectedRange(progress); | 277 GetButtonByIndex(buttons_, target_page)->SetSelectedRange(progress); |
| 265 } | 278 } |
| 266 | 279 |
| 267 } // namespace app_list | 280 } // namespace app_list |
| OLD | NEW |