aura: Implement app list M19 mock.
[chromium-blink-merge.git] / ash / app_list / app_list_model_view.cc
blob8667c66e0b1b79a0c613e1f6dc97b2ff93618f76
1 // Copyright (c) 2012 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 "ash/app_list/app_list_model_view.h"
7 #include "ash/app_list/app_list_item_view.h"
8 #include "ash/app_list/app_list_model.h"
9 #include "base/utf_string_conversions.h"
11 namespace ash {
13 namespace {
15 // Minimum label width.
16 const int kMinLabelWidth = 150;
18 // Calculate preferred tile size for given |content_size| and |num_of_tiles|.
19 gfx::Size CalculateTileSize(const gfx::Size& content_size, int num_of_tiles) {
20 // Icon sizes to try.
21 const int kIconSizes[] = { 64, 48, 32, 16 };
23 int tile_height = 0;
24 int tile_width = 0;
25 int rows = 0;
26 int cols = 0;
28 // Chooses the biggest icon size that could fit all tiles.
29 for (size_t i = 0; i < arraysize(kIconSizes); ++i) {
30 int icon_size = kIconSizes[i];
31 tile_height = icon_size + 2 * AppListItemView::kPadding;
32 tile_width = icon_size + std::min(kMinLabelWidth, icon_size * 2) +
33 2 * AppListItemView::kPadding;
35 rows = content_size.height() / tile_height;
36 cols = std::min(content_size.width() / tile_width,
37 (num_of_tiles - 1) / rows + 1);
38 if (rows * cols >= num_of_tiles)
39 break;
42 if (rows && cols) {
43 // Adjusts tile width to fit |content_size| as much as possible.
44 tile_width = std::max(tile_width, content_size.width() / cols);
45 return gfx::Size(tile_width, tile_height);
48 return gfx::Size();
51 } // namespace
53 AppListModelView::AppListModelView(views::ButtonListener* listener)
54 : model_(NULL),
55 listener_(listener) {
58 AppListModelView::~AppListModelView() {
59 if (model_)
60 model_->RemoveObserver(this);
63 void AppListModelView::SetModel(AppListModel* model) {
64 DCHECK(model);
66 if (model_)
67 model_->RemoveObserver(this);
69 model_ = model;
70 model_->AddObserver(this);
71 Update();
74 void AppListModelView::Update() {
75 RemoveAllChildViews(true);
76 if (!model_ || model_->item_count() == 0)
77 return;
79 for (int i = 0; i < model_->item_count(); ++i)
80 AddChildView(new AppListItemView(model_->GetItem(i), listener_));
82 Layout();
83 SchedulePaint();
86 void AppListModelView::Layout() {
87 gfx::Rect rect(GetContentsBounds());
88 if (rect.IsEmpty())
89 return;
91 gfx::Size tile_size = CalculateTileSize(rect.size(), child_count());
93 int col_bottom = rect.bottom();
94 gfx::Rect current_tile(rect.origin(), tile_size);
96 for (int i = 0; i < child_count(); ++i) {
97 views::View* view = child_at(i);
98 view->SetBoundsRect(current_tile);
100 current_tile.Offset(0, tile_size.height());
101 if (current_tile.bottom() >= col_bottom) {
102 current_tile.set_x(current_tile.x() + tile_size.width());
103 current_tile.set_y(rect.y());
108 void AppListModelView::ListItemsAdded(int start, int count) {
109 Update();
112 void AppListModelView::ListItemsRemoved(int start, int count) {
113 Update();
116 void AppListModelView::ListItemsChanged(int start, int count) {
117 NOTREACHED();
120 } // namespace ash