| 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 "third_party/skia/include/core/SkPath.h" | 7 #include "third_party/skia/include/core/SkPath.h" |
| 8 #include "ui/app_list/pagination_model.h" | 8 #include "ui/app_list/pagination_model.h" |
| 9 #include "ui/base/animation/throb_animation.h" | 9 #include "ui/base/animation/throb_animation.h" |
| 10 #include "ui/gfx/canvas.h" | 10 #include "ui/gfx/canvas.h" |
| 11 #include "ui/gfx/skia_util.h" |
| 11 #include "ui/views/controls/button/custom_button.h" | 12 #include "ui/views/controls/button/custom_button.h" |
| 12 #include "ui/views/layout/box_layout.h" | 13 #include "ui/views/layout/box_layout.h" |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 17 const int kPreferredHeight = 36; |
| 18 |
| 16 const int kButtonSpacing = 10; | 19 const int kButtonSpacing = 10; |
| 17 const int kButtonWidth = 60; | 20 const int kButtonWidth = 60; |
| 18 const int kButtonHeight = 6; | 21 const int kButtonHeight = 6; |
| 19 const int kButtonHeightPadding = 10; | |
| 20 const int kButtonCornerRadius = 2; | 22 const int kButtonCornerRadius = 2; |
| 21 | 23 |
| 22 const SkColor kHoverColor = SkColorSetRGB(0x6E, 0x6E, 0x6E); | 24 const SkColor kHoverColor = SkColorSetRGB(0x6E, 0x6E, 0x6E); |
| 23 | 25 |
| 24 const SkColor kNormalColor = SkColorSetRGB(0xBB, 0xBB, 0xBB); | 26 const SkColor kNormalColor = SkColorSetRGB(0xBB, 0xBB, 0xBB); |
| 25 | 27 |
| 26 const SkColor kSelectedColor = kHoverColor; | 28 const SkColor kSelectedColor = kHoverColor; |
| 27 | 29 |
| 28 class PageSwitcherButton : public views::CustomButton { | 30 class PageSwitcherButton : public views::CustomButton { |
| 29 public: | 31 public: |
| 30 explicit PageSwitcherButton(views::ButtonListener* listener) | 32 explicit PageSwitcherButton(views::ButtonListener* listener) |
| 31 : views::CustomButton(listener), | 33 : views::CustomButton(listener), |
| 32 selected_(false) { | 34 selected_(false) { |
| 33 } | 35 } |
| 34 virtual ~PageSwitcherButton() {} | 36 virtual ~PageSwitcherButton() {} |
| 35 | 37 |
| 36 void SetSelected(bool selected) { | 38 void SetSelected(bool selected) { |
| 37 if (selected == selected_) | 39 if (selected == selected_) |
| 38 return; | 40 return; |
| 39 | 41 |
| 40 selected_ = selected; | 42 selected_ = selected; |
| 41 SchedulePaint(); | 43 SchedulePaint(); |
| 42 } | 44 } |
| 43 | 45 |
| 44 // Overridden from views::View: | 46 // Overridden from views::View: |
| 45 virtual gfx::Size GetPreferredSize() OVERRIDE { | 47 virtual gfx::Size GetPreferredSize() OVERRIDE { |
| 46 return gfx::Size(kButtonWidth, kButtonHeight + kButtonHeightPadding); | 48 return gfx::Size(kButtonWidth, kButtonHeight); |
| 47 } | 49 } |
| 48 | 50 |
| 49 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | 51 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { |
| 50 if (selected_ || state() == BS_PUSHED) { | 52 if (selected_ || state() == BS_PUSHED) { |
| 51 PaintButton(canvas, kSelectedColor); | 53 PaintButton(canvas, kSelectedColor); |
| 52 } else if (state() == BS_HOT) { | 54 } else if (state() == BS_HOT) { |
| 53 PaintButton(canvas, kHoverColor); | 55 PaintButton(canvas, kHoverColor); |
| 54 } else { | 56 } else { |
| 55 PaintButton(canvas, kNormalColor); | 57 PaintButton(canvas, kNormalColor); |
| 56 } | 58 } |
| 57 } | 59 } |
| 58 | 60 |
| 59 private: | 61 private: |
| 60 // Paints a button that has two rounded corner at bottom. | 62 // Paints a button that has two rounded corner at bottom. |
| 61 void PaintButton(gfx::Canvas* canvas, SkColor color) { | 63 void PaintButton(gfx::Canvas* canvas, SkColor color) { |
| 62 gfx::Rect rect(GetContentsBounds()); | 64 gfx::Rect rect(GetContentsBounds().Center( |
| 63 rect.set_height(kButtonHeight); | 65 gfx::Size(kButtonWidth, kButtonHeight))); |
| 64 | |
| 65 gfx::Point center = rect.CenterPoint(); | |
| 66 | 66 |
| 67 SkPath path; | 67 SkPath path; |
| 68 path.incReserve(12); | 68 path.addRoundRect(gfx::RectToSkRect(rect), |
| 69 path.moveTo(SkIntToScalar(rect.x()), SkIntToScalar(rect.y())); | 69 SkIntToScalar(kButtonCornerRadius), |
| 70 path.arcTo(SkIntToScalar(rect.x()), SkIntToScalar(rect.bottom()), | 70 SkIntToScalar(kButtonCornerRadius)); |
| 71 SkIntToScalar(center.x()), SkIntToScalar(rect.bottom()), | |
| 72 SkIntToScalar(kButtonCornerRadius)); | |
| 73 path.arcTo(SkIntToScalar(rect.right()), SkIntToScalar(rect.bottom()), | |
| 74 SkIntToScalar(rect.right()), SkIntToScalar(rect.y()), | |
| 75 SkIntToScalar(kButtonCornerRadius)); | |
| 76 path.lineTo(SkIntToScalar(rect.right()), SkIntToScalar(rect.y())); | |
| 77 path.close(); | |
| 78 | 71 |
| 79 SkPaint paint; | 72 SkPaint paint; |
| 80 paint.setStyle(SkPaint::kFill_Style); | 73 paint.setStyle(SkPaint::kFill_Style); |
| 81 paint.setColor(color); | 74 paint.setColor(color); |
| 82 canvas->DrawPath(path, paint); | 75 canvas->DrawPath(path, paint); |
| 83 } | 76 } |
| 84 | 77 |
| 85 bool selected_; | 78 bool selected_; |
| 86 | 79 |
| 87 DISALLOW_COPY_AND_ASSIGN(PageSwitcherButton); | 80 DISALLOW_COPY_AND_ASSIGN(PageSwitcherButton); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 109 model_->AddObserver(this); | 102 model_->AddObserver(this); |
| 110 } | 103 } |
| 111 | 104 |
| 112 PageSwitcher::~PageSwitcher() { | 105 PageSwitcher::~PageSwitcher() { |
| 113 model_->RemoveObserver(this); | 106 model_->RemoveObserver(this); |
| 114 } | 107 } |
| 115 | 108 |
| 116 gfx::Size PageSwitcher::GetPreferredSize() { | 109 gfx::Size PageSwitcher::GetPreferredSize() { |
| 117 // Always return a size with correct height so that container resize is not | 110 // Always return a size with correct height so that container resize is not |
| 118 // needed when more pages are added. | 111 // needed when more pages are added. |
| 119 return gfx::Size(kButtonWidth, kButtonHeight + kButtonHeightPadding); | 112 return gfx::Size(buttons_->GetPreferredSize().width(), |
| 113 kPreferredHeight); |
| 120 } | 114 } |
| 121 | 115 |
| 122 void PageSwitcher::Layout() { | 116 void PageSwitcher::Layout() { |
| 123 gfx::Rect rect(GetContentsBounds()); | 117 gfx::Rect rect(GetContentsBounds()); |
| 124 buttons_->SetBoundsRect(rect.Center(buttons_->GetPreferredSize())); | 118 // Makes |buttons_| horizontally center and vertically fill. |
| 119 gfx::Size buttons_size(buttons_->GetPreferredSize()); |
| 120 gfx::Rect buttons_bounds(rect.CenterPoint().x() - buttons_size.width() / 2, |
| 121 rect.y(), |
| 122 buttons_size.width(), |
| 123 rect.height()); |
| 124 buttons_->SetBoundsRect(rect.Intersect(buttons_bounds)); |
| 125 } | 125 } |
| 126 | 126 |
| 127 void PageSwitcher::ButtonPressed(views::Button* sender, | 127 void PageSwitcher::ButtonPressed(views::Button* sender, |
| 128 const views::Event& event) { | 128 const views::Event& event) { |
| 129 for (int i = 0; i < buttons_->child_count(); ++i) { | 129 for (int i = 0; i < buttons_->child_count(); ++i) { |
| 130 if (sender == static_cast<views::Button*>(buttons_->child_at(i))) { | 130 if (sender == static_cast<views::Button*>(buttons_->child_at(i))) { |
| 131 model_->SelectPage(i); | 131 model_->SelectPage(i); |
| 132 break; | 132 break; |
| 133 } | 133 } |
| 134 } | 134 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 146 } | 146 } |
| 147 | 147 |
| 148 void PageSwitcher::SelectedPageChanged(int old_selected, int new_selected) { | 148 void PageSwitcher::SelectedPageChanged(int old_selected, int new_selected) { |
| 149 if (old_selected >= 0 && old_selected < buttons_->child_count()) | 149 if (old_selected >= 0 && old_selected < buttons_->child_count()) |
| 150 GetButtonByIndex(buttons_, old_selected)->SetSelected(false); | 150 GetButtonByIndex(buttons_, old_selected)->SetSelected(false); |
| 151 if (new_selected >= 0 && new_selected < buttons_->child_count()) | 151 if (new_selected >= 0 && new_selected < buttons_->child_count()) |
| 152 GetButtonByIndex(buttons_, new_selected)->SetSelected(true); | 152 GetButtonByIndex(buttons_, new_selected)->SetSelected(true); |
| 153 } | 153 } |
| 154 | 154 |
| 155 } // namespace app_list | 155 } // namespace app_list |
| OLD | NEW |