| 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_move_with_anchor(true); | 67 set_move_with_anchor(true); |
| 95 set_parent_window(parent); | 68 set_parent_window(parent); |
| 96 set_close_on_deactivate(false); | 69 set_close_on_deactivate(false); |
| 97 views::BubbleDelegateView::CreateBubble(this); | 70 views::BubbleDelegateView::CreateBubble(this); |
| 98 | 71 |
| 99 // Resets default background since AppListBubbleBorder paints background. | 72 // Resets default background since AppListBubbleBorder paints background. |
| 100 GetBubbleFrameView()->set_background(NULL); | 73 GetBubbleFrameView()->set_background(NULL); |
| 101 | 74 |
| 102 // Overrides border with AppListBubbleBorder. | 75 // Overrides border with AppListBubbleBorder. |
| 103 bubble_border_ = new AppListBubbleBorder(this, | 76 bubble_border_ = new AppListBubbleBorder(this, search_box_view_); |
| 104 search_box_view_, | |
| 105 apps_grid_view_, | |
| 106 search_results_view_); | |
| 107 GetBubbleFrameView()->SetBubbleBorder(bubble_border_); | 77 GetBubbleFrameView()->SetBubbleBorder(bubble_border_); |
| 108 SetBubbleArrowLocation(arrow_location); | 78 SetBubbleArrowLocation(arrow_location); |
| 109 | 79 |
| 110 CreateModel(); | 80 CreateModel(); |
| 111 } | 81 } |
| 112 | 82 |
| 113 void AppListView::SetBubbleArrowLocation( | 83 void AppListView::SetBubbleArrowLocation( |
| 114 views::BubbleBorder::ArrowLocation arrow_location) { | 84 views::BubbleBorder::ArrowLocation arrow_location) { |
| 115 DCHECK(bubble_border_); | 85 DCHECK(bubble_border_); |
| 116 bubble_border_->set_arrow_location(arrow_location); | 86 bubble_border_->set_arrow_location(arrow_location); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 127 void AppListView::UpdateBounds() { | 97 void AppListView::UpdateBounds() { |
| 128 SizeToContents(); | 98 SizeToContents(); |
| 129 } | 99 } |
| 130 | 100 |
| 131 void AppListView::CreateModel() { | 101 void AppListView::CreateModel() { |
| 132 if (delegate_.get()) { | 102 if (delegate_.get()) { |
| 133 // Creates a new model and update all references before releasing old one. | 103 // Creates a new model and update all references before releasing old one. |
| 134 scoped_ptr<AppListModel> new_model(new AppListModel); | 104 scoped_ptr<AppListModel> new_model(new AppListModel); |
| 135 | 105 |
| 136 delegate_->SetModel(new_model.get()); | 106 delegate_->SetModel(new_model.get()); |
| 137 apps_grid_view_->SetModel(new_model->apps()); | 107 search_box_view_->SetModel(new_model->search_box()); |
| 138 | 108 contents_view_->SetModel(new_model.get()); |
| 139 // search_box_view_ etc are not created for v1. | |
| 140 // TODO(xiyuan): Update this after v2 is ready. | |
| 141 if (search_box_view_) | |
| 142 search_box_view_->SetModel(new_model->search_box()); | |
| 143 if (search_results_view_) | |
| 144 search_results_view_->SetResults(new_model->results()); | |
| 145 | 109 |
| 146 model_.reset(new_model.release()); | 110 model_.reset(new_model.release()); |
| 147 } | 111 } |
| 148 } | 112 } |
| 149 | 113 |
| 150 views::View* AppListView::GetInitiallyFocusedView() { | 114 views::View* AppListView::GetInitiallyFocusedView() { |
| 151 return search_box_view_->search_box(); | 115 return search_box_view_->search_box(); |
| 152 } | 116 } |
| 153 | 117 |
| 154 gfx::Size AppListView::GetPreferredSize() { | |
| 155 const gfx::Size search_box_size = search_box_view_->GetPreferredSize(); | |
| 156 const gfx::Size grid_size = apps_grid_view_->GetPreferredSize(); | |
| 157 const gfx::Size page_switcher_size = page_switcher_view_->GetPreferredSize(); | |
| 158 const gfx::Size results_size = search_results_view_->GetPreferredSize(); | |
| 159 | |
| 160 int width = std::max( | |
| 161 std::max(search_box_size.width(), results_size.width()), | |
| 162 std::max(grid_size.width(), page_switcher_size.width())); | |
| 163 int height = search_box_size.height() + | |
| 164 std::max(grid_size.height() + page_switcher_size.height(), | |
| 165 results_size.height()); | |
| 166 return gfx::Size(width + 2 * kInnerPadding, | |
| 167 height + 2 * kInnerPadding); | |
| 168 } | |
| 169 | |
| 170 void AppListView::Layout() { | |
| 171 gfx::Rect rect(GetContentsBounds()); | |
| 172 if (rect.IsEmpty()) | |
| 173 return; | |
| 174 | |
| 175 rect.Inset(kInnerPadding, kInnerPadding); | |
| 176 | |
| 177 const int x = rect.x(); | |
| 178 const int width = rect.width(); | |
| 179 | |
| 180 // SeachBoxView, AppsGridView and PageSwitcher uses a vertical box layout. | |
| 181 int y = rect.y(); | |
| 182 const int search_box_height = search_box_view_->GetPreferredSize().height(); | |
| 183 gfx::Rect search_box_frame(gfx::Point(x, y), | |
| 184 gfx::Size(width, search_box_height)); | |
| 185 search_box_view_->SetBoundsRect(rect.Intersect(search_box_frame)); | |
| 186 | |
| 187 y = search_box_view_->bounds().bottom(); | |
| 188 const int grid_height = apps_grid_view_->GetPreferredSize().height(); | |
| 189 gfx::Rect grid_frame(gfx::Point(x, y), gfx::Size(width, grid_height)); | |
| 190 apps_grid_view_->SetBoundsRect(rect.Intersect(grid_frame)); | |
| 191 | |
| 192 y = apps_grid_view_->bounds().bottom(); | |
| 193 const int page_switcher_height = rect.bottom() - y; | |
| 194 gfx::Rect page_switcher_frame(gfx::Point(x, y), | |
| 195 gfx::Size(width, page_switcher_height)); | |
| 196 page_switcher_view_->SetBoundsRect(rect.Intersect(page_switcher_frame)); | |
| 197 | |
| 198 // Results view is mutually exclusive with AppsGridView and PageSwitcher. | |
| 199 // It occupies the same space as those two views. | |
| 200 gfx::Rect results_frame(grid_frame.Union(page_switcher_frame)); | |
| 201 search_results_view_->SetBoundsRect(rect.Intersect(results_frame)); | |
| 202 } | |
| 203 | |
| 204 bool AppListView::OnKeyPressed(const views::KeyEvent& event) { | 118 bool AppListView::OnKeyPressed(const views::KeyEvent& event) { |
| 205 if (event.key_code() == ui::VKEY_ESCAPE) { | 119 if (event.key_code() == ui::VKEY_ESCAPE) { |
| 206 Close(); | 120 Close(); |
| 207 return true; | 121 return true; |
| 208 } | 122 } |
| 209 | 123 |
| 210 return false; | 124 return false; |
| 211 } | 125 } |
| 212 | 126 |
| 213 void AppListView::ButtonPressed(views::Button* sender, | 127 void AppListView::ButtonPressed(views::Button* sender, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 bubble_border_->set_offset(offset); | 174 bubble_border_->set_offset(offset); |
| 261 | 175 |
| 262 // Repaints border if arrow offset is changed. | 176 // Repaints border if arrow offset is changed. |
| 263 if (bubble_border_->offset() != old_offset) | 177 if (bubble_border_->offset() != old_offset) |
| 264 GetBubbleFrameView()->SchedulePaint(); | 178 GetBubbleFrameView()->SchedulePaint(); |
| 265 | 179 |
| 266 return bubble_rect; | 180 return bubble_rect; |
| 267 } | 181 } |
| 268 | 182 |
| 269 void AppListView::QueryChanged(SearchBoxView* sender) { | 183 void AppListView::QueryChanged(SearchBoxView* sender) { |
| 270 bool showing_search = search_results_view_->visible(); | |
| 271 bool should_show_search = !model_->search_box()->text().empty(); | 184 bool should_show_search = !model_->search_box()->text().empty(); |
| 185 contents_view_->ShowSearchResults(should_show_search); |
| 272 | 186 |
| 273 if (delegate_.get()) { | 187 if (delegate_.get()) { |
| 274 if (should_show_search) | 188 if (should_show_search) |
| 275 delegate_->StartSearch(); | 189 delegate_->StartSearch(); |
| 276 else | 190 else |
| 277 delegate_->StopSearch(); | 191 delegate_->StopSearch(); |
| 278 } | 192 } |
| 279 | |
| 280 if (showing_search != should_show_search) { | |
| 281 // TODO(xiyuan): Animate this transition. | |
| 282 apps_grid_view_->SetVisible(!should_show_search); | |
| 283 page_switcher_view_->SetVisible(!should_show_search); | |
| 284 search_results_view_->SetVisible(should_show_search); | |
| 285 | |
| 286 // TODO(xiyuan): Highlight default match instead of the first. | |
| 287 if (search_results_view_->visible()) | |
| 288 search_results_view_->SetSelectedIndex(0); | |
| 289 | |
| 290 // Needs to repaint frame as well. | |
| 291 GetBubbleFrameView()->SchedulePaint(); | |
| 292 } | |
| 293 } | 193 } |
| 294 | 194 |
| 295 void AppListView::OpenResult(const SearchResult& result, int event_flags) { | 195 void AppListView::OpenResult(const SearchResult& result, int event_flags) { |
| 296 if (delegate_.get()) | 196 if (delegate_.get()) |
| 297 delegate_->OpenSearchResult(result, event_flags); | 197 delegate_->OpenSearchResult(result, event_flags); |
| 298 Close(); | 198 Close(); |
| 299 } | 199 } |
| 300 | 200 |
| 301 } // namespace app_list | 201 } // namespace app_list |
| OLD | NEW |