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/controls/button/image_button.h"
16 #include "ui/views/controls/textfield/textfield.h"
22 const int kPreferredWidth = 360;
23 const int kPreferredHeight = 48;
24 const int kIconDimension = 32;
25 const int kPadding = 14;
26 const int kFolderNameWidth = 150;
27 const int kFolderNameHeight = 30;
28 const int kBottomSeparatorWidth = 380;
29 const int kBottomSeparatorHeight = 1;
31 const SkColor kHintTextColor = SkColorSetRGB(0xA0, 0xA0, 0xA0);
35 class FolderHeaderView::FolderNameView : public views::Textfield {
38 set_border(views::Border::CreateEmptyBorder(1, 1, 1, 1));
41 virtual ~FolderNameView() {
44 // Overridden from views::View:
45 virtual gfx::Size GetPreferredSize() OVERRIDE {
46 return gfx::Size(kFolderNameWidth, kFolderNameHeight);
49 virtual void OnPaintFocusBorder(gfx::Canvas* canvas) OVERRIDE {
50 const SkColor kFocusBorderColor = SkColorSetRGB(64, 128, 250);
51 if (HasFocus() && focusable()) {
52 gfx::Rect rect = GetLocalBounds();
53 rect.Inset(0, 0, 1, 1);
54 canvas->DrawRect(rect, kFocusBorderColor);
59 DISALLOW_COPY_AND_ASSIGN(FolderNameView);
62 FolderHeaderView::FolderHeaderView(FolderHeaderViewDelegate* delegate)
64 back_button_(new views::ImageButton(this)),
65 folder_name_view_(new FolderNameView),
67 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
68 back_button_->SetImage(views::ImageButton::STATE_NORMAL,
69 rb.GetImageSkiaNamed(IDR_APP_LIST_FOLDER_BACK_NORMAL));
70 back_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER,
71 views::ImageButton::ALIGN_MIDDLE);
72 AddChildView(back_button_);
74 folder_name_view_->SetFont(rb.GetFont(ui::ResourceBundle::MediumFont));
75 folder_name_view_->set_placeholder_text_color(kHintTextColor);
76 folder_name_view_->set_placeholder_text(
77 rb.GetLocalizedString(IDS_APP_LIST_FOLDER_NAME_PLACEHOLDER));
78 folder_name_view_->RemoveBorder();
79 folder_name_view_->SetBackgroundColor(kContentsBackgroundColor);
80 folder_name_view_->SetController(this);
81 AddChildView(folder_name_view_);
84 FolderHeaderView::~FolderHeaderView() {
86 folder_item_->RemoveObserver(this);
89 void FolderHeaderView::SetFolderItem(AppListFolderItem* folder_item) {
91 folder_item_->RemoveObserver(this);
93 folder_item_ = folder_item;
96 folder_item_->AddObserver(this);
101 void FolderHeaderView::Update() {
105 folder_name_view_->SetText(UTF8ToUTF16(folder_item_->title()));
108 gfx::Size FolderHeaderView::GetPreferredSize() {
109 return gfx::Size(kPreferredWidth, kPreferredHeight);
112 void FolderHeaderView::Layout() {
113 gfx::Rect rect(GetContentsBounds());
117 gfx::Rect back_bounds(rect);
118 back_bounds.set_width(kIconDimension + 2 * kPadding);
119 back_button_->SetBoundsRect(back_bounds);
121 gfx::Rect text_bounds(rect);
122 int text_width = folder_name_view_->GetPreferredSize().width();
123 text_bounds.set_x(back_bounds.x() + (rect.width() - text_width) / 2);
124 text_bounds.set_width(text_width);
125 text_bounds.ClampToCenteredSize(gfx::Size(text_bounds.width(),
126 folder_name_view_->GetPreferredSize().height()));
127 folder_name_view_->SetBoundsRect(text_bounds);
130 void FolderHeaderView::OnPaint(gfx::Canvas* canvas) {
131 views::View::OnPaint(canvas);
133 gfx::Rect rect(GetContentsBounds());
137 // Draw bottom separator line.
138 rect.set_x((rect.width() - kBottomSeparatorWidth) / 2 + rect.x());
139 rect.set_y(rect.y() + rect.height() - kBottomSeparatorHeight);
140 rect.set_width(kBottomSeparatorWidth);
141 rect.set_height(kBottomSeparatorHeight);
142 canvas->FillRect(rect, kTopSeparatorColor);
145 void FolderHeaderView::ContentsChanged(views::Textfield* sender,
146 const base::string16& new_contents) {
147 // Temporarily remove from observer to ignore data change caused by us.
151 folder_item_->RemoveObserver(this);
152 std::string name = UTF16ToUTF8(folder_name_view_->text());
153 folder_item_->SetTitleAndFullName(name, name);
154 folder_item_->AddObserver(this);
157 void FolderHeaderView::ButtonPressed(views::Button* sender,
158 const ui::Event& event) {
159 delegate_->NavigateBack(folder_item_, event);
162 void FolderHeaderView::ItemIconChanged() {
165 void FolderHeaderView::ItemTitleChanged() {
169 void FolderHeaderView::ItemHighlightedChanged() {
172 void FolderHeaderView::ItemIsInstallingChanged() {
175 void FolderHeaderView::ItemPercentDownloadedChanged() {
178 } // namespace app_list