| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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_item_group_view.h" | |
| 6 | |
| 7 #include "ash/app_list/app_list_item_group_model.h" | |
| 8 #include "ash/app_list/app_list_item_view.h" | |
| 9 #include "ui/views/layout/grid_layout.h" | |
| 10 | |
| 11 namespace ash { | |
| 12 | |
| 13 AppListItemGroupView::AppListItemGroupView(AppListItemGroupModel* model, | |
| 14 AppListItemViewListener* listener) | |
| 15 : model_(model), | |
| 16 listener_(listener), | |
| 17 tiles_per_row_(0), | |
| 18 focused_index_(0) { | |
| 19 Update(); | |
| 20 } | |
| 21 | |
| 22 AppListItemGroupView::~AppListItemGroupView() { | |
| 23 } | |
| 24 | |
| 25 void AppListItemGroupView::SetTilesPerRow(int tiles_per_row) { | |
| 26 if (tiles_per_row_ == tiles_per_row) | |
| 27 return; | |
| 28 | |
| 29 tiles_per_row_ = tiles_per_row; | |
| 30 Update(); | |
| 31 } | |
| 32 | |
| 33 void AppListItemGroupView::Update() { | |
| 34 RemoveAllChildViews(true); | |
| 35 if (model_->item_count() == 0 || tiles_per_row_ == 0) | |
| 36 return; | |
| 37 | |
| 38 views::GridLayout* layout = new views::GridLayout(this); | |
| 39 SetLayoutManager(layout); | |
| 40 | |
| 41 const int kTileColumnSetId = 0; | |
| 42 views::ColumnSet* column_set = layout->AddColumnSet(kTileColumnSetId); | |
| 43 for (int i = 0; i < tiles_per_row_; ++i) { | |
| 44 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, | |
| 45 views::GridLayout::USE_PREF, 0, 0); | |
| 46 } | |
| 47 | |
| 48 for (int i = 0; i < model_->item_count(); ++i) { | |
| 49 if (i % tiles_per_row_ == 0) | |
| 50 layout->StartRow(0, kTileColumnSetId); | |
| 51 | |
| 52 layout->AddView(new AppListItemView(model_->GetItem(i), listener_)); | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 views::View* AppListItemGroupView::GetFocusedTile() { | |
| 57 return focused_index_ < child_count() ? child_at(focused_index_) : NULL; | |
| 58 } | |
| 59 | |
| 60 void AppListItemGroupView::UpdateFocusedTile(views::View* tile) { | |
| 61 for (int i = 0; i < child_count(); ++i) { | |
| 62 if (child_at(i) == tile) { | |
| 63 focused_index_ = i; | |
| 64 break; | |
| 65 } | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 void AppListItemGroupView::SetFocusedTileByIndex(int index) { | |
| 70 index = std::max(0, std::min(child_count() - 1, index)); | |
| 71 if (index != focused_index_) | |
| 72 child_at(index)->RequestFocus(); | |
| 73 } | |
| 74 | |
| 75 bool AppListItemGroupView::OnKeyPressed(const views::KeyEvent& event) { | |
| 76 if (!event.IsControlDown() && !event.IsShiftDown() && !event.IsAltDown()) { | |
| 77 // Arrow keys navigates in tile grid. | |
| 78 switch (event.key_code()) { | |
| 79 case ui::VKEY_LEFT: | |
| 80 if (focused_index_ > 0) | |
| 81 SetFocusedTileByIndex(focused_index_ - 1); | |
| 82 return true; | |
| 83 case ui::VKEY_RIGHT: | |
| 84 if (focused_index_ + 1 < child_count()) | |
| 85 SetFocusedTileByIndex(focused_index_ + 1); | |
| 86 return true; | |
| 87 case ui::VKEY_UP: | |
| 88 if (focused_index_ - tiles_per_row_ >= 0) | |
| 89 SetFocusedTileByIndex(focused_index_ - tiles_per_row_); | |
| 90 return true; | |
| 91 case ui::VKEY_DOWN: | |
| 92 if (focused_index_ + tiles_per_row_ < child_count()) | |
| 93 SetFocusedTileByIndex(focused_index_ + tiles_per_row_); | |
| 94 return true; | |
| 95 default: | |
| 96 break; | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 return false; | |
| 101 } | |
| 102 | |
| 103 void AppListItemGroupView::ListItemsAdded(int start, int count) { | |
| 104 Update(); | |
| 105 } | |
| 106 | |
| 107 void AppListItemGroupView::ListItemsRemoved(int start, int count) { | |
| 108 Update(); | |
| 109 } | |
| 110 | |
| 111 void AppListItemGroupView::ListItemsChanged(int start, int count) { | |
| 112 NOTREACHED(); | |
| 113 } | |
| 114 | |
| 115 } // namespace ash | |
| OLD | NEW |