50dee4a516a825d2b34987305894ee15cc4b5725
[platform/framework/web/crosswalk.git] / src / ui / app_list / views / folder_header_view.cc
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.
4
5 #include "ui/app_list/views/folder_header_view.h"
6
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"
19
20 namespace app_list {
21
22 namespace {
23
24 const int kPreferredWidth = 360;
25 const int kPreferredHeight = 48;
26 const int kIconDimension = 24;
27 const int kPadding = 14;
28 const int kBottomSeparatorWidth = 380;
29 const int kBottomSeparatorHeight = 1;
30 const int kMaxFolderNameWidth = 300;
31 const int kFolderNameLeftRightPaddingChars = 4;
32
33 const SkColor kHintTextColor = SkColorSetRGB(0xA0, 0xA0, 0xA0);
34
35 }  // namespace
36
37 class FolderHeaderView::FolderNameView : public views::Textfield {
38  public:
39   FolderNameView() {
40     SetBorder(views::Border::CreateEmptyBorder(1, 1, 1, 1));
41     const SkColor kFocusBorderColor = SkColorSetRGB(64, 128, 250);
42     SetFocusPainter(views::Painter::CreateSolidFocusPainter(
43           kFocusBorderColor,
44           gfx::Insets(0, 0, 1, 1)));
45   }
46
47   virtual ~FolderNameView() {
48   }
49
50   void Update() {
51     SetBackgroundColor(text().size() <= kMaxFolderNameChars
52                            ? kContentsBackgroundColor
53                            : SK_ColorRED);
54   }
55
56  private:
57   DISALLOW_COPY_AND_ASSIGN(FolderNameView);
58 };
59
60 FolderHeaderView::FolderHeaderView(FolderHeaderViewDelegate* delegate)
61     : folder_item_(NULL),
62       back_button_(new views::ImageButton(this)),
63       folder_name_view_(new FolderNameView),
64       folder_name_placeholder_text_(
65           ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
66               IDS_APP_LIST_FOLDER_NAME_PLACEHOLDER)),
67       delegate_(delegate),
68       folder_name_visible_(true) {
69   ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
70   back_button_->SetImage(views::ImageButton::STATE_NORMAL,
71       rb.GetImageSkiaNamed(IDR_APP_LIST_FOLDER_BACK_NORMAL));
72   back_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER,
73       views::ImageButton::ALIGN_MIDDLE);
74   AddChildView(back_button_);
75
76   folder_name_view_->SetFontList(
77       rb.GetFontList(ui::ResourceBundle::MediumFont));
78   folder_name_view_->set_placeholder_text_color(kHintTextColor);
79   folder_name_view_->set_placeholder_text(folder_name_placeholder_text_);
80   folder_name_view_->SetBorder(views::Border::NullBorder());
81   folder_name_view_->SetBackgroundColor(kContentsBackgroundColor);
82   folder_name_view_->set_controller(this);
83   AddChildView(folder_name_view_);
84 }
85
86 FolderHeaderView::~FolderHeaderView() {
87   if (folder_item_)
88     folder_item_->RemoveObserver(this);
89 }
90
91 void FolderHeaderView::SetFolderItem(AppListFolderItem* folder_item) {
92   if (folder_item_)
93     folder_item_->RemoveObserver(this);
94
95   folder_item_ = folder_item;
96   if (!folder_item_)
97     return;
98   folder_item_->AddObserver(this);
99
100   folder_name_view_->SetEnabled(folder_item_->folder_type() !=
101                                 AppListFolderItem::FOLDER_TYPE_OEM);
102
103   Update();
104 }
105
106 void FolderHeaderView::UpdateFolderNameVisibility(bool visible) {
107   folder_name_visible_ = visible;
108   Update();
109   SchedulePaint();
110 }
111
112 void FolderHeaderView::OnFolderItemRemoved() {
113   folder_item_ = NULL;
114 }
115
116 void FolderHeaderView::Update() {
117   if (!folder_item_)
118     return;
119
120   folder_name_view_->SetVisible(folder_name_visible_);
121   if (folder_name_visible_) {
122     folder_name_view_->SetText(base::UTF8ToUTF16(folder_item_->name()));
123     folder_name_view_->Update();
124   }
125
126   Layout();
127 }
128
129 gfx::Size FolderHeaderView::GetPreferredSize() {
130   return gfx::Size(kPreferredWidth, kPreferredHeight);
131 }
132
133 void FolderHeaderView::Layout() {
134   gfx::Rect rect(GetContentsBounds());
135   if (rect.IsEmpty())
136     return;
137
138   gfx::Rect back_bounds(rect);
139   back_bounds.set_width(kIconDimension + 2 * kPadding);
140   back_button_->SetBoundsRect(back_bounds);
141
142   gfx::Rect text_bounds(rect);
143   int text_char_num = folder_item_->name().size()
144                           ? folder_item_->name().size()
145                           : folder_name_placeholder_text_.size();
146   int text_width = folder_name_view_->GetFontList().GetExpectedTextWidth(
147       text_char_num + kFolderNameLeftRightPaddingChars);
148   text_width = std::min(text_width, kMaxFolderNameWidth);
149   text_bounds.set_x(back_bounds.x() + (rect.width() - text_width) / 2);
150   text_bounds.set_width(text_width);
151   text_bounds.ClampToCenteredSize(gfx::Size(text_bounds.width(),
152       folder_name_view_->GetPreferredSize().height()));
153   folder_name_view_->SetBoundsRect(text_bounds);
154 }
155
156 bool FolderHeaderView::OnKeyPressed(const ui::KeyEvent& event) {
157   if (event.key_code() == ui::VKEY_RETURN)
158     delegate_->GiveBackFocusToSearchBox();
159
160   return false;
161 }
162
163 void FolderHeaderView::OnPaint(gfx::Canvas* canvas) {
164   views::View::OnPaint(canvas);
165
166   gfx::Rect rect(GetContentsBounds());
167   if (rect.IsEmpty() || !folder_name_visible_)
168     return;
169
170   // Draw bottom separator line.
171   rect.set_x((rect.width() - kBottomSeparatorWidth) / 2 + rect.x());
172   rect.set_y(rect.y() + rect.height() - kBottomSeparatorHeight);
173   rect.set_width(kBottomSeparatorWidth);
174   rect.set_height(kBottomSeparatorHeight);
175   canvas->FillRect(rect, kTopSeparatorColor);
176 }
177
178 void FolderHeaderView::ContentsChanged(views::Textfield* sender,
179                                        const base::string16& new_contents) {
180   // Temporarily remove from observer to ignore data change caused by us.
181   if (!folder_item_)
182     return;
183
184   folder_name_view_->Update();
185   folder_item_->RemoveObserver(this);
186   // Enforce the maximum folder name length in UI.
187   std::string name = base::UTF16ToUTF8(
188       folder_name_view_->text().substr(0, kMaxFolderNameChars));
189   if (name != folder_item_->name())
190     delegate_->SetItemName(folder_item_, name);
191   folder_item_->AddObserver(this);
192
193   Layout();
194 }
195
196 void FolderHeaderView::ButtonPressed(views::Button* sender,
197                                      const ui::Event& event) {
198   delegate_->GiveBackFocusToSearchBox();
199   delegate_->NavigateBack(folder_item_, event);
200 }
201
202 void FolderHeaderView::ItemIconChanged() {
203 }
204
205 void FolderHeaderView::ItemNameChanged() {
206   Update();
207 }
208
209 void FolderHeaderView::ItemHighlightedChanged() {
210 }
211
212 void FolderHeaderView::ItemIsInstallingChanged() {
213 }
214
215 void FolderHeaderView::ItemPercentDownloadedChanged() {
216 }
217
218 }  // namespace app_list