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"
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
) {
21 const int kIconSizes
[] = { 64, 48, 32, 16 };
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
)
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
);
53 AppListModelView::AppListModelView(views::ButtonListener
* listener
)
58 AppListModelView::~AppListModelView() {
60 model_
->RemoveObserver(this);
63 void AppListModelView::SetModel(AppListModel
* model
) {
67 model_
->RemoveObserver(this);
70 model_
->AddObserver(this);
74 void AppListModelView::Update() {
75 RemoveAllChildViews(true);
76 if (!model_
|| model_
->item_count() == 0)
79 for (int i
= 0; i
< model_
->item_count(); ++i
)
80 AddChildView(new AppListItemView(model_
->GetItem(i
), listener_
));
86 void AppListModelView::Layout() {
87 gfx::Rect
rect(GetContentsBounds());
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
) {
112 void AppListModelView::ListItemsRemoved(int start
, int count
) {
116 void AppListModelView::ListItemsChanged(int start
, int count
) {