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 #include "ui/views/controls/button/text_button.h" |
| 11 #include "ui/views/layout/grid_layout.h" |
| 12 |
| 13 namespace ash { |
| 14 |
| 15 namespace { |
| 16 |
| 17 // Minimum label width |
| 18 const int kMinLabelWidth = 150; |
| 19 |
| 20 // Calculate preferred tile size for given |content_size| and |num_of_tiles|. |
| 21 gfx::Size CalculateTileSize(const gfx::Size& content_size, int num_of_tiles) { |
| 22 const int kIconSizes[] = { 64, 48, 32, 16 }; |
| 23 |
| 24 int tile_height = 0; |
| 25 int tile_width = 0; |
| 26 for (size_t i = 0; i < arraysize(kIconSizes); ++i) { |
| 27 int icon_size = kIconSizes[i]; |
| 28 tile_height = icon_size + 2 * AppListItemView::kPadding; |
| 29 tile_width = icon_size + std::min(kMinLabelWidth, icon_size * 2) + |
| 30 2 * AppListItemView::kPadding; |
| 31 |
| 32 int rows = content_size.height() / tile_height; |
| 33 int cols = content_size.width() / tile_width; |
| 34 if (rows * cols >= num_of_tiles) |
| 35 break; |
| 36 } |
| 37 |
| 38 return gfx::Size(tile_width, tile_height); |
| 39 } |
| 40 |
| 41 } // namespace |
| 42 |
| 43 AppListModelView::AppListModelView(views::ButtonListener* listener) |
| 44 : model_(NULL), |
| 45 listener_(listener) { |
| 46 } |
| 47 |
| 48 AppListModelView::~AppListModelView() { |
| 49 if (model_) |
| 50 model_->RemoveObserver(this); |
| 51 } |
| 52 |
| 53 void AppListModelView::SetModel(AppListModel* model) { |
| 54 DCHECK(model); |
| 55 |
| 56 if (model_) |
| 57 model_->RemoveObserver(this); |
| 58 |
| 59 model_ = model; |
| 60 model_->AddObserver(this); |
| 61 Update(); |
| 62 } |
| 63 |
| 64 void AppListModelView::Update() { |
| 65 RemoveAllChildViews(true); |
| 66 if (!model_ || model_->item_count() == 0) |
| 67 return; |
| 68 |
| 69 for (int i = 0; i < model_->item_count(); ++i) |
| 70 AddChildView(new AppListItemView(model_->GetItem(i), listener_)); |
| 71 |
| 72 Layout(); |
| 73 SchedulePaint(); |
| 74 } |
| 75 |
| 76 void AppListModelView::Layout() { |
| 77 gfx::Rect rect(GetContentsBounds()); |
| 78 |
| 79 gfx::Size tile_size = CalculateTileSize(rect.size(), child_count()); |
| 80 |
| 81 int col_bottom = rect.bottom(); |
| 82 gfx::Rect current_tile(rect.origin(), tile_size); |
| 83 |
| 84 for (int i = 0; i < child_count(); ++i) { |
| 85 views::View* view = child_at(i); |
| 86 view->SetBoundsRect(current_tile); |
| 87 |
| 88 current_tile.Offset(0, tile_size.height()); |
| 89 if (current_tile.bottom() >= col_bottom) { |
| 90 current_tile.set_x(current_tile.x() + tile_size.width()); |
| 91 current_tile.set_y(rect.y()); |
| 92 } |
| 93 } |
| 94 } |
| 95 |
| 96 void AppListModelView::ListItemsAdded(int start, int count) { |
| 97 Update(); |
| 98 } |
| 99 |
| 100 void AppListModelView::ListItemsRemoved(int start, int count) { |
| 101 Update(); |
| 102 } |
| 103 |
| 104 void AppListModelView::ListItemsChanged(int start, int count) { |
| 105 NOTREACHED(); |
| 106 } |
| 107 |
| 108 } // namespace ash |
OLD | NEW |