| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/app_list/app_list_view.h" | 5 #include "ui/app_list/app_list_view.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "ui/app_list/app_list_bubble_border.h" | 7 #include "ui/app_list/app_list_bubble_border.h" |
| 10 #include "ui/app_list/app_list_item_view.h" | 8 #include "ui/app_list/app_list_item_view.h" |
| 11 #include "ui/app_list/app_list_model.h" | 9 #include "ui/app_list/app_list_model.h" |
| 12 #include "ui/app_list/app_list_view_delegate.h" | 10 #include "ui/app_list/app_list_view_delegate.h" |
| 13 #include "ui/app_list/apps_grid_view.h" | 11 #include "ui/app_list/contents_view.h" |
| 14 #include "ui/app_list/page_switcher.h" | |
| 15 #include "ui/app_list/pagination_model.h" | 12 #include "ui/app_list/pagination_model.h" |
| 16 #include "ui/app_list/search_box_model.h" | 13 #include "ui/app_list/search_box_model.h" |
| 17 #include "ui/app_list/search_box_view.h" | 14 #include "ui/app_list/search_box_view.h" |
| 18 #include "ui/app_list/search_result_list_view.h" | |
| 19 #include "ui/gfx/screen.h" | 15 #include "ui/gfx/screen.h" |
| 20 #include "ui/gfx/transform_util.h" | |
| 21 #include "ui/views/bubble/bubble_frame_view.h" | 16 #include "ui/views/bubble/bubble_frame_view.h" |
| 22 #include "ui/views/controls/textfield/textfield.h" | 17 #include "ui/views/controls/textfield/textfield.h" |
| 18 #include "ui/views/layout/box_layout.h" |
| 23 #include "ui/views/widget/widget.h" | 19 #include "ui/views/widget/widget.h" |
| 24 | 20 |
| 25 namespace app_list { | 21 namespace app_list { |
| 26 | 22 |
| 27 namespace { | 23 namespace { |
| 28 | 24 |
| 29 // 0.2 black | |
| 30 const SkColor kWidgetBackgroundColor = SkColorSetARGB(0x33, 0, 0, 0); | |
| 31 | |
| 32 const float kModelViewAnimationScaleFactor = 0.9f; | |
| 33 | |
| 34 const int kPreferredIconDimension = 48; | |
| 35 const int kPreferredCols = 4; | |
| 36 const int kPreferredRows = 4; | |
| 37 | |
| 38 // Inner padding space in pixels of bubble contents. | 25 // Inner padding space in pixels of bubble contents. |
| 39 const int kInnerPadding = 1; | 26 const int kInnerPadding = 1; |
| 40 | 27 |
| 41 ui::Transform GetScaleTransform(AppsGridView* model_view) { | |
| 42 gfx::Rect pixel_bounds = model_view->GetLayerBoundsInPixel(); | |
| 43 gfx::Point center(pixel_bounds.width() / 2, pixel_bounds.height() / 2); | |
| 44 return ui::GetScaleTransform(center, kModelViewAnimationScaleFactor); | |
| 45 } | |
| 46 | |
| 47 } // namespace | 28 } // namespace |
| 48 | 29 |
| 49 //////////////////////////////////////////////////////////////////////////////// | 30 //////////////////////////////////////////////////////////////////////////////// |
| 50 // AppListView: | 31 // AppListView: |
| 51 | 32 |
| 52 AppListView::AppListView(AppListViewDelegate* delegate) | 33 AppListView::AppListView(AppListViewDelegate* delegate) |
| 53 : delegate_(delegate), | 34 : delegate_(delegate), |
| 54 pagination_model_(new PaginationModel), | 35 pagination_model_(new PaginationModel), |
| 55 bubble_border_(NULL), | 36 bubble_border_(NULL), |
| 56 apps_grid_view_(NULL), | |
| 57 page_switcher_view_(NULL), | |
| 58 search_box_view_(NULL), | 37 search_box_view_(NULL), |
| 59 search_results_view_(NULL) { | 38 contents_view_(NULL) { |
| 60 } | 39 } |
| 61 | 40 |
| 62 AppListView::~AppListView() { | 41 AppListView::~AppListView() { |
| 63 // Deletes all child views while the models are still valid. | 42 // Deletes all child views while the models are still valid. |
| 64 RemoveAllChildViews(true); | 43 RemoveAllChildViews(true); |
| 65 } | 44 } |
| 66 | 45 |
| 67 void AppListView::InitAsBubble( | 46 void AppListView::InitAsBubble( |
| 68 gfx::NativeView parent, | 47 gfx::NativeView parent, |
| 69 views::View* anchor, | 48 views::View* anchor, |
| 70 views::BubbleBorder::ArrowLocation arrow_location) { | 49 views::BubbleBorder::ArrowLocation arrow_location) { |
| 71 set_background(NULL); | 50 set_background(NULL); |
| 72 | 51 |
| 52 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, |
| 53 kInnerPadding, |
| 54 kInnerPadding, |
| 55 kInnerPadding)); |
| 56 |
| 73 search_box_view_ = new SearchBoxView(this); | 57 search_box_view_ = new SearchBoxView(this); |
| 74 AddChildView(search_box_view_); | 58 AddChildView(search_box_view_); |
| 75 | 59 |
| 76 apps_grid_view_ = new AppsGridView(this, pagination_model_.get()); | 60 contents_view_ = new ContentsView(this, pagination_model_.get()); |
| 77 apps_grid_view_->SetLayout(kPreferredIconDimension, | 61 AddChildView(contents_view_); |
| 78 kPreferredCols, | |
| 79 kPreferredRows); | |
| 80 AddChildView(apps_grid_view_); | |
| 81 | 62 |
| 82 search_results_view_ = new SearchResultListView(this); | 63 search_box_view_->set_contents_view(contents_view_); |
| 83 search_results_view_->SetVisible(false); | |
| 84 AddChildView(search_results_view_); | |
| 85 | |
| 86 page_switcher_view_ = new PageSwitcher(pagination_model_.get()); | |
| 87 AddChildView(page_switcher_view_); | |
| 88 | |
| 89 search_box_view_->set_grid_view(apps_grid_view_); | |
| 90 search_box_view_->set_results_view(search_results_view_); | |
| 91 | 64 |
| 92 set_anchor_view(anchor); | 65 set_anchor_view(anchor); |
| 93 set_margin(0); | 66 set_margin(0); |
| 94 set_parent_window(parent); | 67 set_parent_window(parent); |
| 95 set_close_on_deactivate(false); | 68 set_close_on_deactivate(false); |
| 96 views::BubbleDelegateView::CreateBubble(this); | 69 views::BubbleDelegateView::CreateBubble(this); |
| 97 | 70 |
| 98 // Resets default background since AppListBubbleBorder paints background. | 71 // Resets default background since AppListBubbleBorder paints background. |
| 99 GetBubbleFrameView()->set_background(NULL); | 72 GetBubbleFrameView()->set_background(NULL); |
| 100 | 73 |
| 101 // Overrides border with AppListBubbleBorder. | 74 // Overrides border with AppListBubbleBorder. |
| 102 bubble_border_ = new AppListBubbleBorder(this, | 75 bubble_border_ = new AppListBubbleBorder(this, search_box_view_); |
| 103 search_box_view_, | |
| 104 apps_grid_view_, | |
| 105 search_results_view_); | |
| 106 GetBubbleFrameView()->SetBubbleBorder(bubble_border_); | 76 GetBubbleFrameView()->SetBubbleBorder(bubble_border_); |
| 107 SetBubbleArrowLocation(arrow_location); | 77 SetBubbleArrowLocation(arrow_location); |
| 108 | 78 |
| 109 CreateModel(); | 79 CreateModel(); |
| 110 } | 80 } |
| 111 | 81 |
| 112 void AppListView::SetBubbleArrowLocation( | 82 void AppListView::SetBubbleArrowLocation( |
| 113 views::BubbleBorder::ArrowLocation arrow_location) { | 83 views::BubbleBorder::ArrowLocation arrow_location) { |
| 114 DCHECK(bubble_border_); | 84 DCHECK(bubble_border_); |
| 115 bubble_border_->set_arrow_location(arrow_location); | 85 bubble_border_->set_arrow_location(arrow_location); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 126 void AppListView::UpdateBounds() { | 96 void AppListView::UpdateBounds() { |
| 127 SizeToContents(); | 97 SizeToContents(); |
| 128 } | 98 } |
| 129 | 99 |
| 130 void AppListView::CreateModel() { | 100 void AppListView::CreateModel() { |
| 131 if (delegate_.get()) { | 101 if (delegate_.get()) { |
| 132 // Creates a new model and update all references before releasing old one. | 102 // Creates a new model and update all references before releasing old one. |
| 133 scoped_ptr<AppListModel> new_model(new AppListModel); | 103 scoped_ptr<AppListModel> new_model(new AppListModel); |
| 134 | 104 |
| 135 delegate_->SetModel(new_model.get()); | 105 delegate_->SetModel(new_model.get()); |
| 136 apps_grid_view_->SetModel(new_model->apps()); | 106 search_box_view_->SetModel(new_model->search_box()); |
| 137 | 107 contents_view_->SetModel(new_model.get()); |
| 138 // search_box_view_ etc are not created for v1. | |
| 139 // TODO(xiyuan): Update this after v2 is ready. | |
| 140 if (search_box_view_) | |
| 141 search_box_view_->SetModel(new_model->search_box()); | |
| 142 if (search_results_view_) | |
| 143 search_results_view_->SetResults(new_model->results()); | |
| 144 | 108 |
| 145 model_.reset(new_model.release()); | 109 model_.reset(new_model.release()); |
| 146 } | 110 } |
| 147 } | 111 } |
| 148 | 112 |
| 149 views::View* AppListView::GetInitiallyFocusedView() { | 113 views::View* AppListView::GetInitiallyFocusedView() { |
| 150 return search_box_view_->search_box(); | 114 return search_box_view_->search_box(); |
| 151 } | 115 } |
| 152 | 116 |
| 153 gfx::Size AppListView::GetPreferredSize() { | |
| 154 const gfx::Size search_box_size = search_box_view_->GetPreferredSize(); | |
| 155 const gfx::Size grid_size = apps_grid_view_->GetPreferredSize(); | |
| 156 const gfx::Size page_switcher_size = page_switcher_view_->GetPreferredSize(); | |
| 157 const gfx::Size results_size = search_results_view_->GetPreferredSize(); | |
| 158 | |
| 159 int width = std::max( | |
| 160 std::max(search_box_size.width(), results_size.width()), | |
| 161 std::max(grid_size.width(), page_switcher_size.width())); | |
| 162 int height = search_box_size.height() + | |
| 163 std::max(grid_size.height() + page_switcher_size.height(), | |
| 164 results_size.height()); | |
| 165 return gfx::Size(width + 2 * kInnerPadding, | |
| 166 height + 2 * kInnerPadding); | |
| 167 } | |
| 168 | |
| 169 void AppListView::Layout() { | |
| 170 gfx::Rect rect(GetContentsBounds()); | |
| 171 if (rect.IsEmpty()) | |
| 172 return; | |
| 173 | |
| 174 rect.Inset(kInnerPadding, kInnerPadding); | |
| 175 | |
| 176 const int x = rect.x(); | |
| 177 const int width = rect.width(); | |
| 178 | |
| 179 // SeachBoxView, AppsGridView and PageSwitcher uses a vertical box layout. | |
| 180 int y = rect.y(); | |
| 181 const int search_box_height = search_box_view_->GetPreferredSize().height(); | |
| 182 gfx::Rect search_box_frame(gfx::Point(x, y), | |
| 183 gfx::Size(width, search_box_height)); | |
| 184 search_box_view_->SetBoundsRect(rect.Intersect(search_box_frame)); | |
| 185 | |
| 186 y = search_box_view_->bounds().bottom(); | |
| 187 const int grid_height = apps_grid_view_->GetPreferredSize().height(); | |
| 188 gfx::Rect grid_frame(gfx::Point(x, y), gfx::Size(width, grid_height)); | |
| 189 apps_grid_view_->SetBoundsRect(rect.Intersect(grid_frame)); | |
| 190 | |
| 191 y = apps_grid_view_->bounds().bottom(); | |
| 192 const int page_switcher_height = rect.bottom() - y; | |
| 193 gfx::Rect page_switcher_frame(gfx::Point(x, y), | |
| 194 gfx::Size(width, page_switcher_height)); | |
| 195 page_switcher_view_->SetBoundsRect(rect.Intersect(page_switcher_frame)); | |
| 196 | |
| 197 // Results view is mutually exclusive with AppsGridView and PageSwitcher. | |
| 198 // It occupies the same space as those two views. | |
| 199 gfx::Rect results_frame(grid_frame.Union(page_switcher_frame)); | |
| 200 search_results_view_->SetBoundsRect(rect.Intersect(results_frame)); | |
| 201 } | |
| 202 | |
| 203 bool AppListView::OnKeyPressed(const views::KeyEvent& event) { | 117 bool AppListView::OnKeyPressed(const views::KeyEvent& event) { |
| 204 if (event.key_code() == ui::VKEY_ESCAPE) { | 118 if (event.key_code() == ui::VKEY_ESCAPE) { |
| 205 Close(); | 119 Close(); |
| 206 return true; | 120 return true; |
| 207 } | 121 } |
| 208 | 122 |
| 209 return false; | 123 return false; |
| 210 } | 124 } |
| 211 | 125 |
| 212 void AppListView::ButtonPressed(views::Button* sender, | 126 void AppListView::ButtonPressed(views::Button* sender, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 bubble_border_->set_offset(offset); | 173 bubble_border_->set_offset(offset); |
| 260 | 174 |
| 261 // Repaints border if arrow offset is changed. | 175 // Repaints border if arrow offset is changed. |
| 262 if (bubble_border_->offset() != old_offset) | 176 if (bubble_border_->offset() != old_offset) |
| 263 GetBubbleFrameView()->SchedulePaint(); | 177 GetBubbleFrameView()->SchedulePaint(); |
| 264 | 178 |
| 265 return bubble_rect; | 179 return bubble_rect; |
| 266 } | 180 } |
| 267 | 181 |
| 268 void AppListView::QueryChanged(SearchBoxView* sender) { | 182 void AppListView::QueryChanged(SearchBoxView* sender) { |
| 269 bool showing_search = search_results_view_->visible(); | |
| 270 bool should_show_search = !model_->search_box()->text().empty(); | 183 bool should_show_search = !model_->search_box()->text().empty(); |
| 184 contents_view_->ShowSearchResults(should_show_search); |
| 271 | 185 |
| 272 if (delegate_.get()) { | 186 if (delegate_.get()) { |
| 273 if (should_show_search) | 187 if (should_show_search) |
| 274 delegate_->StartSearch(); | 188 delegate_->StartSearch(); |
| 275 else | 189 else |
| 276 delegate_->StopSearch(); | 190 delegate_->StopSearch(); |
| 277 } | 191 } |
| 278 | |
| 279 if (showing_search != should_show_search) { | |
| 280 // TODO(xiyuan): Animate this transition. | |
| 281 apps_grid_view_->SetVisible(!should_show_search); | |
| 282 page_switcher_view_->SetVisible(!should_show_search); | |
| 283 search_results_view_->SetVisible(should_show_search); | |
| 284 | |
| 285 // TODO(xiyuan): Highlight default match instead of the first. | |
| 286 if (search_results_view_->visible()) | |
| 287 search_results_view_->SetSelectedIndex(0); | |
| 288 | |
| 289 // Needs to repaint frame as well. | |
| 290 GetBubbleFrameView()->SchedulePaint(); | |
| 291 } | |
| 292 } | 192 } |
| 293 | 193 |
| 294 void AppListView::OpenResult(const SearchResult& result, int event_flags) { | 194 void AppListView::OpenResult(const SearchResult& result, int event_flags) { |
| 295 if (delegate_.get()) | 195 if (delegate_.get()) |
| 296 delegate_->OpenSearchResult(result, event_flags); | 196 delegate_->OpenSearchResult(result, event_flags); |
| 297 Close(); | 197 Close(); |
| 298 } | 198 } |
| 299 | 199 |
| 300 } // namespace app_list | 200 } // namespace app_list |
| OLD | NEW |