Chromium Code Reviews| 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 "grit/ui_resources.h" | |
| 8 #include "ui/app_list/app_list_constants.h" | 7 #include "ui/app_list/app_list_constants.h" |
| 9 #include "ui/app_list/views/contents_view.h" | 8 #include "ui/app_list/views/contents_view.h" |
| 10 #include "ui/base/resource/resource_bundle.h" | |
| 11 #include "ui/views/controls/button/custom_button.h" | 9 #include "ui/views/controls/button/custom_button.h" |
| 12 #include "ui/views/controls/button/image_button.h" | 10 #include "ui/views/controls/button/image_button.h" |
| 13 #include "ui/views/layout/box_layout.h" | 11 #include "ui/views/layout/box_layout.h" |
| 14 | 12 |
| 15 namespace app_list { | 13 namespace app_list { |
| 16 | 14 |
| 17 namespace { | 15 namespace { |
| 18 | 16 |
| 19 const int kPreferredHeight = 32; | 17 const int kPreferredHeight = 32; |
| 20 const int kButtonSpacing = 4; | 18 const int kButtonSpacing = 4; |
| 21 | 19 |
| 22 } // namespace | 20 } // namespace |
| 23 | 21 |
| 24 ContentsSwitcherView::ContentsSwitcherView(ContentsView* contents_view) | 22 ContentsSwitcherView::ContentsSwitcherView() |
| 25 : contents_view_(contents_view), buttons_(new views::View) { | 23 : contents_view_(NULL), buttons_(new views::View) { |
| 26 AddChildView(buttons_); | 24 AddChildView(buttons_); |
| 27 | 25 |
| 28 buttons_->SetLayoutManager(new views::BoxLayout( | 26 buttons_->SetLayoutManager(new views::BoxLayout( |
| 29 views::BoxLayout::kHorizontal, 0, 0, kButtonSpacing)); | 27 views::BoxLayout::kHorizontal, 0, 0, kButtonSpacing)); |
| 30 // TODO(mgiuca): Dynamically generate these buttons from the subviews of | |
| 31 // |contents_view|. | |
| 32 AddSwitcherButton( | |
| 33 IDR_APP_LIST_SEARCH_ICON, | |
| 34 contents_view->GetPageIndexForNamedPage(ContentsView::NAMED_PAGE_START)); | |
| 35 AddSwitcherButton( | |
| 36 IDR_APP_LIST_APPS_ICON, | |
| 37 contents_view->GetPageIndexForNamedPage(ContentsView::NAMED_PAGE_APPS)); | |
| 38 } | 28 } |
| 39 | 29 |
| 40 ContentsSwitcherView::~ContentsSwitcherView() {} | 30 ContentsSwitcherView::~ContentsSwitcherView() {} |
| 41 | 31 |
| 32 void ContentsSwitcherView::Init(ContentsView* contents_view) { | |
| 33 contents_view_ = contents_view; | |
| 34 } | |
| 35 | |
| 42 void ContentsSwitcherView::AddSwitcherButton(int resource_id, int page_index) { | 36 void ContentsSwitcherView::AddSwitcherButton(int resource_id, int page_index) { |
| 37 // Must not be NULL at this point, because it might trigger ButtonPressed(). | |
| 38 DCHECK(contents_view_); | |
| 39 | |
| 43 views::ImageButton* button = new views::ImageButton(this); | 40 views::ImageButton* button = new views::ImageButton(this); |
| 44 button->SetImage( | 41 if (resource_id) |
|
calamity
2014/06/18 05:22:29
nit: Braces around multiline statement.
Matt Giuca
2014/06/18 06:47:02
Done.
| |
| 45 views::CustomButton::STATE_NORMAL, | 42 button->SetImage( |
| 46 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id)); | 43 views::CustomButton::STATE_NORMAL, |
| 44 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id)); | |
| 47 button->set_tag(page_index); | 45 button->set_tag(page_index); |
| 48 buttons_->AddChildView(button); | 46 buttons_->AddChildView(button); |
| 49 } | 47 } |
| 50 | 48 |
| 51 gfx::Size ContentsSwitcherView::GetPreferredSize() const { | 49 gfx::Size ContentsSwitcherView::GetPreferredSize() const { |
| 52 return gfx::Size(buttons_->GetPreferredSize().width(), kPreferredHeight); | 50 return gfx::Size(buttons_->GetPreferredSize().width(), kPreferredHeight); |
| 53 } | 51 } |
| 54 | 52 |
| 55 void ContentsSwitcherView::Layout() { | 53 void ContentsSwitcherView::Layout() { |
| 56 gfx::Rect rect(GetContentsBounds()); | 54 gfx::Rect rect(GetContentsBounds()); |
| 57 | 55 |
| 58 // Makes |buttons_| horizontally center and vertically fill. | 56 // Makes |buttons_| horizontally center and vertically fill. |
| 59 gfx::Size buttons_size(buttons_->GetPreferredSize()); | 57 gfx::Size buttons_size(buttons_->GetPreferredSize()); |
| 60 gfx::Rect buttons_bounds(rect.CenterPoint().x() - buttons_size.width() / 2, | 58 gfx::Rect buttons_bounds(rect.CenterPoint().x() - buttons_size.width() / 2, |
| 61 rect.y(), | 59 rect.y(), |
| 62 buttons_size.width(), | 60 buttons_size.width(), |
| 63 rect.height()); | 61 rect.height()); |
| 64 buttons_->SetBoundsRect(gfx::IntersectRects(rect, buttons_bounds)); | 62 buttons_->SetBoundsRect(gfx::IntersectRects(rect, buttons_bounds)); |
| 65 } | 63 } |
| 66 | 64 |
| 67 void ContentsSwitcherView::ButtonPressed(views::Button* sender, | 65 void ContentsSwitcherView::ButtonPressed(views::Button* sender, |
| 68 const ui::Event& event) { | 66 const ui::Event& event) { |
| 69 contents_view_->SetActivePage(sender->tag()); | 67 contents_view_->SetActivePage(sender->tag()); |
| 70 } | 68 } |
| 71 | 69 |
| 72 } // namespace app_list | 70 } // namespace app_list |
| OLD | NEW |