1 // Copyright (c) 2013 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/folder_header_view.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "grit/ui_resources.h"
9 #include "grit/ui_strings.h"
10 #include "ui/app_list/app_list_constants.h"
11 #include "ui/app_list/app_list_folder_item.h"
12 #include "ui/app_list/views/app_list_folder_view.h"
13 #include "ui/base/resource/resource_bundle.h"
14 #include "ui/gfx/canvas.h"
15 #include "ui/views/border.h"
16 #include "ui/views/controls/button/image_button.h"
17 #include "ui/views/controls/textfield/textfield.h"
18 #include "ui/views/painter.h"
24 const int kPreferredWidth
= 360;
25 const int kPreferredHeight
= 48;
26 const int kIconDimension
= 24;
27 const int kPadding
= 14;
28 const int kFolderNameWidth
= 150;
29 const int kFolderNameHeight
= 30;
30 const int kBottomSeparatorWidth
= 380;
31 const int kBottomSeparatorHeight
= 1;
33 const SkColor kHintTextColor
= SkColorSetRGB(0xA0, 0xA0, 0xA0);
37 class FolderHeaderView::FolderNameView
: public views::Textfield
{
40 SetBorder(views::Border::CreateEmptyBorder(1, 1, 1, 1));
41 const SkColor kFocusBorderColor
= SkColorSetRGB(64, 128, 250);
42 SetFocusPainter(views::Painter::CreateSolidFocusPainter(
44 gfx::Insets(0, 0, 1, 1)));
47 virtual ~FolderNameView() {
50 // Overridden from views::View:
51 virtual gfx::Size
GetPreferredSize() OVERRIDE
{
52 return gfx::Size(kFolderNameWidth
, kFolderNameHeight
);
56 DISALLOW_COPY_AND_ASSIGN(FolderNameView
);
59 FolderHeaderView::FolderHeaderView(FolderHeaderViewDelegate
* delegate
)
61 back_button_(new views::ImageButton(this)),
62 folder_name_view_(new FolderNameView
),
64 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
65 back_button_
->SetImage(views::ImageButton::STATE_NORMAL
,
66 rb
.GetImageSkiaNamed(IDR_APP_LIST_FOLDER_BACK_NORMAL
));
67 back_button_
->SetImageAlignment(views::ImageButton::ALIGN_CENTER
,
68 views::ImageButton::ALIGN_MIDDLE
);
69 AddChildView(back_button_
);
71 folder_name_view_
->SetFontList(
72 rb
.GetFontList(ui::ResourceBundle::MediumFont
));
73 folder_name_view_
->set_placeholder_text_color(kHintTextColor
);
74 folder_name_view_
->set_placeholder_text(
75 rb
.GetLocalizedString(IDS_APP_LIST_FOLDER_NAME_PLACEHOLDER
));
76 folder_name_view_
->SetBorder(views::Border::NullBorder());
77 folder_name_view_
->SetBackgroundColor(kContentsBackgroundColor
);
78 folder_name_view_
->set_controller(this);
79 AddChildView(folder_name_view_
);
82 FolderHeaderView::~FolderHeaderView() {
84 folder_item_
->RemoveObserver(this);
87 void FolderHeaderView::SetFolderItem(AppListFolderItem
* folder_item
) {
89 folder_item_
->RemoveObserver(this);
91 folder_item_
= folder_item
;
94 folder_item_
->AddObserver(this);
99 void FolderHeaderView::Update() {
103 folder_name_view_
->SetText(base::UTF8ToUTF16(folder_item_
->title()));
106 gfx::Size
FolderHeaderView::GetPreferredSize() {
107 return gfx::Size(kPreferredWidth
, kPreferredHeight
);
110 void FolderHeaderView::Layout() {
111 gfx::Rect
rect(GetContentsBounds());
115 gfx::Rect
back_bounds(rect
);
116 back_bounds
.set_width(kIconDimension
+ 2 * kPadding
);
117 back_button_
->SetBoundsRect(back_bounds
);
119 gfx::Rect
text_bounds(rect
);
120 int text_width
= folder_name_view_
->GetPreferredSize().width();
121 text_bounds
.set_x(back_bounds
.x() + (rect
.width() - text_width
) / 2);
122 text_bounds
.set_width(text_width
);
123 text_bounds
.ClampToCenteredSize(gfx::Size(text_bounds
.width(),
124 folder_name_view_
->GetPreferredSize().height()));
125 folder_name_view_
->SetBoundsRect(text_bounds
);
128 void FolderHeaderView::OnPaint(gfx::Canvas
* canvas
) {
129 views::View::OnPaint(canvas
);
131 gfx::Rect
rect(GetContentsBounds());
135 // Draw bottom separator line.
136 rect
.set_x((rect
.width() - kBottomSeparatorWidth
) / 2 + rect
.x());
137 rect
.set_y(rect
.y() + rect
.height() - kBottomSeparatorHeight
);
138 rect
.set_width(kBottomSeparatorWidth
);
139 rect
.set_height(kBottomSeparatorHeight
);
140 canvas
->FillRect(rect
, kTopSeparatorColor
);
143 void FolderHeaderView::ContentsChanged(views::Textfield
* sender
,
144 const base::string16
& new_contents
) {
145 // Temporarily remove from observer to ignore data change caused by us.
149 folder_item_
->RemoveObserver(this);
150 std::string name
= base::UTF16ToUTF8(folder_name_view_
->text());
151 folder_item_
->SetTitleAndFullName(name
, name
);
152 folder_item_
->AddObserver(this);
155 void FolderHeaderView::ButtonPressed(views::Button
* sender
,
156 const ui::Event
& event
) {
157 delegate_
->NavigateBack(folder_item_
, event
);
160 void FolderHeaderView::ItemIconChanged() {
163 void FolderHeaderView::ItemTitleChanged() {
167 void FolderHeaderView::ItemHighlightedChanged() {
170 void FolderHeaderView::ItemIsInstallingChanged() {
173 void FolderHeaderView::ItemPercentDownloadedChanged() {
176 } // namespace app_list