| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ash/app_list/page_switcher.h" | |
| 6 | |
| 7 #include "ash/app_list/pagination_model.h" | |
| 8 #include "third_party/skia/include/core/SkPath.h" | |
| 9 #include "ui/base/animation/throb_animation.h" | |
| 10 #include "ui/gfx/canvas.h" | |
| 11 #include "ui/views/controls/button/custom_button.h" | |
| 12 #include "ui/views/layout/box_layout.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 const int kButtonSpacing = 10; | |
| 17 const int kButtonWidth = 60; | |
| 18 const int kButtonHeight = 6; | |
| 19 const int kButtonHeightPadding = 10; | |
| 20 const int kButtonCornerRadius = 2; | |
| 21 | |
| 22 // 0.16 black | |
| 23 const SkColor kHoverColor = SkColorSetARGB(0x2A, 0x00, 0x00, 0x00); | |
| 24 | |
| 25 // 0.2 black | |
| 26 const SkColor kNormalColor = SkColorSetARGB(0x33, 0x00, 0x00, 0x00); | |
| 27 | |
| 28 // 0.33 black | |
| 29 const SkColor kSelectedColor = SkColorSetARGB(0x55, 0x00, 0x00, 0x00); | |
| 30 | |
| 31 class PageSwitcherButton : public views::CustomButton { | |
| 32 public: | |
| 33 PageSwitcherButton(views::ButtonListener* listener) | |
| 34 : views::CustomButton(listener), | |
| 35 selected_(false) { | |
| 36 } | |
| 37 virtual ~PageSwitcherButton() {} | |
| 38 | |
| 39 void SetSelected(bool selected) { | |
| 40 if (selected == selected_) | |
| 41 return; | |
| 42 | |
| 43 selected_ = selected; | |
| 44 SchedulePaint(); | |
| 45 } | |
| 46 | |
| 47 // Overridden from views::View: | |
| 48 virtual gfx::Size GetPreferredSize() OVERRIDE { | |
| 49 return gfx::Size(kButtonWidth, kButtonHeight + kButtonHeightPadding); | |
| 50 } | |
| 51 | |
| 52 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | |
| 53 if (selected_ || state() == BS_PUSHED) { | |
| 54 PaintButton(canvas, kSelectedColor); | |
| 55 } else if (state() == BS_HOT) { | |
| 56 PaintButton(canvas, kHoverColor); | |
| 57 } else { | |
| 58 PaintButton(canvas, kNormalColor); | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 private: | |
| 63 // Paints a button that has two rounded corner at bottom. | |
| 64 void PaintButton(gfx::Canvas* canvas, SkColor color) { | |
| 65 gfx::Rect rect(GetContentsBounds()); | |
| 66 rect.set_height(kButtonHeight); | |
| 67 | |
| 68 gfx::Point center = rect.CenterPoint(); | |
| 69 | |
| 70 SkPath path; | |
| 71 path.incReserve(12); | |
| 72 path.moveTo(SkIntToScalar(rect.x()), SkIntToScalar(rect.y())); | |
| 73 path.arcTo(SkIntToScalar(rect.x()), SkIntToScalar(rect.bottom()), | |
| 74 SkIntToScalar(center.x()), SkIntToScalar(rect.bottom()), | |
| 75 SkIntToScalar(kButtonCornerRadius)); | |
| 76 path.arcTo(SkIntToScalar(rect.right()), SkIntToScalar(rect.bottom()), | |
| 77 SkIntToScalar(rect.right()), SkIntToScalar(rect.y()), | |
| 78 SkIntToScalar(kButtonCornerRadius)); | |
| 79 path.lineTo(SkIntToScalar(rect.right()), SkIntToScalar(rect.y())); | |
| 80 path.close(); | |
| 81 | |
| 82 SkPaint paint; | |
| 83 paint.setStyle(SkPaint::kFill_Style); | |
| 84 paint.setColor(color); | |
| 85 canvas->DrawPath(path, paint); | |
| 86 } | |
| 87 | |
| 88 bool selected_; | |
| 89 | |
| 90 DISALLOW_COPY_AND_ASSIGN(PageSwitcherButton); | |
| 91 }; | |
| 92 | |
| 93 // Gets PageSwitcherButton at |index| in |buttons|. | |
| 94 PageSwitcherButton* GetButtonByIndex(views::View* buttons, int index) { | |
| 95 return static_cast<PageSwitcherButton*>(buttons->child_at(index)); | |
| 96 } | |
| 97 | |
| 98 } // namespace | |
| 99 | |
| 100 namespace ash { | |
| 101 | |
| 102 PageSwitcher::PageSwitcher(PaginationModel* model) | |
| 103 : model_(model), | |
| 104 buttons_(NULL) { | |
| 105 buttons_ = new views::View; | |
| 106 buttons_->SetLayoutManager(new views::BoxLayout( | |
| 107 views::BoxLayout::kHorizontal, 0, 0, kButtonSpacing)); | |
| 108 AddChildView(buttons_); | |
| 109 | |
| 110 TotalPagesChanged(); | |
| 111 SelectedPageChanged(-1, model->selected_page()); | |
| 112 model_->AddObserver(this); | |
| 113 } | |
| 114 | |
| 115 PageSwitcher::~PageSwitcher() { | |
| 116 model_->RemoveObserver(this); | |
| 117 } | |
| 118 | |
| 119 gfx::Size PageSwitcher::GetPreferredSize() { | |
| 120 // Always return a size with correct height so that container resize is not | |
| 121 // needed when more pages are added. | |
| 122 return gfx::Size(kButtonWidth, kButtonHeight + kButtonHeightPadding); | |
| 123 } | |
| 124 | |
| 125 void PageSwitcher::Layout() { | |
| 126 gfx::Rect rect(GetContentsBounds()); | |
| 127 buttons_->SetBoundsRect(rect.Center(buttons_->GetPreferredSize())); | |
| 128 } | |
| 129 | |
| 130 void PageSwitcher::ButtonPressed(views::Button* sender, | |
| 131 const views::Event& event) { | |
| 132 for (int i = 0; i < buttons_->child_count(); ++i) { | |
| 133 if (sender == static_cast<views::Button*>(buttons_->child_at(i))) { | |
| 134 model_->SelectPage(i); | |
| 135 break; | |
| 136 } | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 void PageSwitcher::TotalPagesChanged() { | |
| 141 buttons_->RemoveAllChildViews(true); | |
| 142 for (int i = 0; i < model_->total_pages(); ++i) { | |
| 143 PageSwitcherButton* button = new PageSwitcherButton(this); | |
| 144 button->SetSelected(i == model_->selected_page()); | |
| 145 buttons_->AddChildView(button); | |
| 146 } | |
| 147 buttons_->SetVisible(model_->total_pages() > 1); | |
| 148 Layout(); | |
| 149 } | |
| 150 | |
| 151 void PageSwitcher::SelectedPageChanged(int old_selected, int new_selected) { | |
| 152 if (old_selected >= 0 && old_selected < buttons_->child_count()) | |
| 153 GetButtonByIndex(buttons_, old_selected)->SetSelected(false); | |
| 154 if (new_selected >= 0 && new_selected < buttons_->child_count()) | |
| 155 GetButtonByIndex(buttons_, new_selected)->SetSelected(true); | |
| 156 } | |
| 157 | |
| 158 } // namespace ash | |
| OLD | NEW |