OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "ash/app_list/app_list_model_view.h" |
| 6 |
| 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" |
| 10 |
| 11 namespace ash { |
| 12 |
| 13 namespace { |
| 14 |
| 15 // Minimum label width. |
| 16 const int kMinLabelWidth = 150; |
| 17 |
| 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 }; |
| 22 |
| 23 int tile_height = 0; |
| 24 int tile_width = 0; |
| 25 int rows = 0; |
| 26 int cols = 0; |
| 27 |
| 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; |
| 34 |
| 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; |
| 40 } |
| 41 |
| 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); |
| 46 } |
| 47 |
| 48 return gfx::Size(); |
| 49 } |
| 50 |
| 51 } // namespace |
| 52 |
| 53 AppListModelView::AppListModelView(views::ButtonListener* listener) |
| 54 : model_(NULL), |
| 55 listener_(listener) { |
| 56 } |
| 57 |
| 58 AppListModelView::~AppListModelView() { |
| 59 if (model_) |
| 60 model_->RemoveObserver(this); |
| 61 } |
| 62 |
| 63 void AppListModelView::SetModel(AppListModel* model) { |
| 64 DCHECK(model); |
| 65 |
| 66 if (model_) |
| 67 model_->RemoveObserver(this); |
| 68 |
| 69 model_ = model; |
| 70 model_->AddObserver(this); |
| 71 Update(); |
| 72 } |
| 73 |
| 74 void AppListModelView::Update() { |
| 75 RemoveAllChildViews(true); |
| 76 if (!model_ || model_->item_count() == 0) |
| 77 return; |
| 78 |
| 79 for (int i = 0; i < model_->item_count(); ++i) |
| 80 AddChildView(new AppListItemView(model_->GetItem(i), listener_)); |
| 81 |
| 82 Layout(); |
| 83 SchedulePaint(); |
| 84 } |
| 85 |
| 86 void AppListModelView::Layout() { |
| 87 gfx::Rect rect(GetContentsBounds()); |
| 88 if (rect.IsEmpty()) |
| 89 return; |
| 90 |
| 91 gfx::Size tile_size = CalculateTileSize(rect.size(), child_count()); |
| 92 |
| 93 int col_bottom = rect.bottom(); |
| 94 gfx::Rect current_tile(rect.origin(), tile_size); |
| 95 |
| 96 for (int i = 0; i < child_count(); ++i) { |
| 97 views::View* view = child_at(i); |
| 98 view->SetBoundsRect(current_tile); |
| 99 |
| 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()); |
| 104 } |
| 105 } |
| 106 } |
| 107 |
| 108 void AppListModelView::ListItemsAdded(int start, int count) { |
| 109 Update(); |
| 110 } |
| 111 |
| 112 void AppListModelView::ListItemsRemoved(int start, int count) { |
| 113 Update(); |
| 114 } |
| 115 |
| 116 void AppListModelView::ListItemsChanged(int start, int count) { |
| 117 NOTREACHED(); |
| 118 } |
| 119 |
| 120 } // namespace ash |
OLD | NEW |