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
3 // found in the LICENSE file.
5 #include "ui/app_list/views/contents_switcher_view.h"
7 #include "grit/ui_resources.h"
8 #include "ui/app_list/app_list_constants.h"
9 #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"
12 #include "ui/views/controls/button/image_button.h"
13 #include "ui/views/layout/box_layout.h"
19 const int kPreferredHeight = 32;
20 const int kButtonSpacing = 4;
24 ContentsSwitcherView::ContentsSwitcherView(ContentsView* contents_view)
25 : contents_view_(contents_view), buttons_(new views::View) {
26 AddChildView(buttons_);
28 buttons_->SetLayoutManager(new views::BoxLayout(
29 views::BoxLayout::kHorizontal, 0, 0, kButtonSpacing));
30 AddSwitcherButton(IDR_APP_LIST_SEARCH_ICON,
31 ContentsView::SHOW_SEARCH_RESULTS);
32 AddSwitcherButton(IDR_APP_LIST_APPS_ICON, ContentsView::SHOW_APPS);
35 ContentsSwitcherView::~ContentsSwitcherView() {}
37 void ContentsSwitcherView::AddSwitcherButton(int resource_id, int tag) {
38 views::ImageButton* button = new views::ImageButton(this);
40 views::CustomButton::STATE_NORMAL,
41 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id));
43 buttons_->AddChildView(button);
46 gfx::Size ContentsSwitcherView::GetPreferredSize() {
47 return gfx::Size(buttons_->GetPreferredSize().width(), kPreferredHeight);
50 void ContentsSwitcherView::Layout() {
51 gfx::Rect rect(GetContentsBounds());
53 // Makes |buttons_| horizontally center and vertically fill.
54 gfx::Size buttons_size(buttons_->GetPreferredSize());
55 gfx::Rect buttons_bounds(rect.CenterPoint().x() - buttons_size.width() / 2,
59 buttons_->SetBoundsRect(gfx::IntersectRects(rect, buttons_bounds));
62 void ContentsSwitcherView::ButtonPressed(views::Button* sender,
63 const ui::Event& event) {
64 contents_view_->SetShowState(
65 static_cast<ContentsView::ShowState>(sender->tag()));
68 } // namespace app_list