| 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 <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "ui/app_list/app_list_bubble_border.h" | 9 #include "ui/app_list/app_list_bubble_border.h" |
| 10 #include "ui/app_list/app_list_item_view.h" | 10 #include "ui/app_list/app_list_item_view.h" |
| 11 #include "ui/app_list/app_list_model.h" | 11 #include "ui/app_list/app_list_model.h" |
| 12 #include "ui/app_list/app_list_model_view.h" | |
| 13 #include "ui/app_list/app_list_view_delegate.h" | 12 #include "ui/app_list/app_list_view_delegate.h" |
| 13 #include "ui/app_list/apps_grid_view.h" |
| 14 #include "ui/app_list/page_switcher.h" | 14 #include "ui/app_list/page_switcher.h" |
| 15 #include "ui/app_list/pagination_model.h" | 15 #include "ui/app_list/pagination_model.h" |
| 16 #include "ui/app_list/search_box_model.h" |
| 17 #include "ui/app_list/search_box_view.h" |
| 18 #include "ui/app_list/search_result_list_view.h" |
| 16 #include "ui/compositor/layer.h" | 19 #include "ui/compositor/layer.h" |
| 17 #include "ui/compositor/scoped_layer_animation_settings.h" | 20 #include "ui/compositor/scoped_layer_animation_settings.h" |
| 18 #include "ui/gfx/screen.h" | 21 #include "ui/gfx/screen.h" |
| 19 #include "ui/gfx/transform_util.h" | 22 #include "ui/gfx/transform_util.h" |
| 20 #include "ui/views/background.h" | 23 #include "ui/views/background.h" |
| 21 #include "ui/views/bubble/bubble_frame_view.h" | 24 #include "ui/views/bubble/bubble_frame_view.h" |
| 25 #include "ui/views/controls/textfield/textfield.h" |
| 22 #include "ui/views/layout/box_layout.h" | 26 #include "ui/views/layout/box_layout.h" |
| 23 #include "ui/views/widget/widget.h" | 27 #include "ui/views/widget/widget.h" |
| 24 | 28 |
| 25 namespace app_list { | 29 namespace app_list { |
| 26 | 30 |
| 27 namespace { | 31 namespace { |
| 28 | 32 |
| 29 // 0.2 black | 33 // 0.2 black |
| 30 const SkColor kWidgetBackgroundColor = SkColorSetARGB(0x33, 0, 0, 0); | 34 const SkColor kWidgetBackgroundColor = SkColorSetARGB(0x33, 0, 0, 0); |
| 31 | 35 |
| 32 const float kModelViewAnimationScaleFactor = 0.9f; | 36 const float kModelViewAnimationScaleFactor = 0.9f; |
| 33 | 37 |
| 34 const int kPreferredIconDimension = 48; | 38 const int kPreferredIconDimension = 48; |
| 35 const int kPreferredCols = 4; | 39 const int kPreferredCols = 4; |
| 36 const int kPreferredRows = 4; | 40 const int kPreferredRows = 4; |
| 37 | 41 |
| 38 ui::Transform GetScaleTransform(AppListModelView* model_view) { | 42 ui::Transform GetScaleTransform(AppsGridView* model_view) { |
| 39 gfx::Rect pixel_bounds = model_view->GetLayerBoundsInPixel(); | 43 gfx::Rect pixel_bounds = model_view->GetLayerBoundsInPixel(); |
| 40 gfx::Point center(pixel_bounds.width() / 2, pixel_bounds.height() / 2); | 44 gfx::Point center(pixel_bounds.width() / 2, pixel_bounds.height() / 2); |
| 41 return ui::GetScaleTransform(center, kModelViewAnimationScaleFactor); | 45 return ui::GetScaleTransform(center, kModelViewAnimationScaleFactor); |
| 42 } | 46 } |
| 43 | 47 |
| 44 } // namespace | 48 } // namespace |
| 45 | 49 |
| 46 //////////////////////////////////////////////////////////////////////////////// | 50 //////////////////////////////////////////////////////////////////////////////// |
| 47 // AppListView: | 51 // AppListView: |
| 48 | 52 |
| 49 AppListView::AppListView(AppListViewDelegate* delegate) | 53 AppListView::AppListView(AppListViewDelegate* delegate) |
| 50 : delegate_(delegate), | 54 : delegate_(delegate), |
| 51 pagination_model_(new PaginationModel), | 55 pagination_model_(new PaginationModel), |
| 52 bubble_style_(false), | 56 bubble_style_(false), |
| 53 bubble_border_(NULL), | 57 bubble_border_(NULL), |
| 54 model_view_(NULL) { | 58 apps_view_(NULL), |
| 59 page_switcher_view_(NULL), |
| 60 search_box_view_(NULL), |
| 61 search_results_view_(NULL) { |
| 55 } | 62 } |
| 56 | 63 |
| 57 AppListView::~AppListView() { | 64 AppListView::~AppListView() { |
| 58 // Deletes all child views while the models are still valid. | 65 // Deletes all child views while the models are still valid. |
| 59 RemoveAllChildViews(true); | 66 RemoveAllChildViews(true); |
| 60 } | 67 } |
| 61 | 68 |
| 62 void AppListView::InitAsFullscreenWidget(gfx::NativeView parent, | 69 void AppListView::InitAsFullscreenWidget(gfx::NativeView parent, |
| 63 const gfx::Rect& screen_bounds, | 70 const gfx::Rect& screen_bounds, |
| 64 const gfx::Rect& work_area) { | 71 const gfx::Rect& work_area) { |
| 65 bubble_style_ = false; | 72 bubble_style_ = false; |
| 66 set_background(views::Background::CreateSolidBackground( | 73 set_background(views::Background::CreateSolidBackground( |
| 67 kWidgetBackgroundColor)); | 74 kWidgetBackgroundColor)); |
| 68 work_area_ = work_area; | 75 work_area_ = work_area; |
| 69 | 76 |
| 70 model_view_ = new AppListModelView(this, pagination_model_.get()); | 77 apps_view_ = new AppsGridView(this, pagination_model_.get()); |
| 71 model_view_->SetPaintToLayer(true); | 78 apps_view_->SetPaintToLayer(true); |
| 72 model_view_->layer()->SetFillsBoundsOpaquely(false); | 79 apps_view_->layer()->SetFillsBoundsOpaquely(false); |
| 73 AddChildView(model_view_); | 80 AddChildView(apps_view_); |
| 74 | 81 |
| 75 views::Widget::InitParams widget_params( | 82 views::Widget::InitParams widget_params( |
| 76 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | 83 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 77 widget_params.delegate = this; | 84 widget_params.delegate = this; |
| 78 widget_params.transparent = true; | 85 widget_params.transparent = true; |
| 79 widget_params.parent = parent; | 86 widget_params.parent = parent; |
| 80 | 87 |
| 81 views::Widget* widget = new views::Widget; | 88 views::Widget* widget = new views::Widget; |
| 82 widget->Init(widget_params); | 89 widget->Init(widget_params); |
| 83 widget->SetContentsView(this); | 90 widget->SetContentsView(this); |
| 84 widget->SetBounds(screen_bounds); | 91 widget->SetBounds(screen_bounds); |
| 85 | 92 |
| 86 // Turns off default animation. | 93 // Turns off default animation. |
| 87 widget->SetVisibilityChangedAnimationsEnabled(false); | 94 widget->SetVisibilityChangedAnimationsEnabled(false); |
| 88 | 95 |
| 89 // Sets initial transform. AnimateShow changes it back to identity transform. | 96 // Sets initial transform. AnimateShow changes it back to identity transform. |
| 90 model_view_->SetTransform(GetScaleTransform(model_view_)); | 97 apps_view_->SetTransform(GetScaleTransform(apps_view_)); |
| 91 UpdateModel(); | 98 CreateModel(); |
| 92 } | 99 } |
| 93 | 100 |
| 94 void AppListView::InitAsBubble(gfx::NativeView parent, views::View* anchor) { | 101 void AppListView::InitAsBubble(gfx::NativeView parent, views::View* anchor) { |
| 95 bubble_style_ = true; | 102 bubble_style_ = true; |
| 96 set_background(NULL); | 103 set_background(NULL); |
| 97 | 104 |
| 98 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0)); | 105 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 1, 1, 0)); |
| 99 | 106 |
| 100 model_view_ = new AppListModelView(this, pagination_model_.get()); | 107 search_box_view_ = new SearchBoxView(this); |
| 101 model_view_->SetLayout(kPreferredIconDimension, | 108 AddChildView(search_box_view_); |
| 109 |
| 110 apps_view_ = new AppsGridView(this, pagination_model_.get()); |
| 111 apps_view_->SetLayout(kPreferredIconDimension, |
| 102 kPreferredCols, | 112 kPreferredCols, |
| 103 kPreferredRows); | 113 kPreferredRows); |
| 104 AddChildView(model_view_); | 114 AddChildView(apps_view_); |
| 105 | 115 |
| 106 PageSwitcher* page_switcher = new PageSwitcher(pagination_model_.get()); | 116 search_results_view_ = new SearchResultListView(this); |
| 107 AddChildView(page_switcher); | 117 search_results_view_->SetVisible(false); |
| 118 AddChildView(search_results_view_); |
| 119 |
| 120 page_switcher_view_ = new PageSwitcher(pagination_model_.get()); |
| 121 AddChildView(page_switcher_view_); |
| 108 | 122 |
| 109 set_anchor_view(anchor); | 123 set_anchor_view(anchor); |
| 124 set_margin(0); |
| 110 set_parent_window(parent); | 125 set_parent_window(parent); |
| 111 set_close_on_deactivate(false); | 126 set_close_on_deactivate(false); |
| 112 views::BubbleDelegateView::CreateBubble(this); | 127 views::BubbleDelegateView::CreateBubble(this); |
| 113 | 128 |
| 114 // Resets default background since AppListBubbleBorder paints background. | 129 // Resets default background since AppListBubbleBorder paints background. |
| 115 GetBubbleFrameView()->set_background(NULL); | 130 GetBubbleFrameView()->set_background(NULL); |
| 116 | 131 |
| 117 // Overrides border with AppListBubbleBorder. | 132 // Overrides border with AppListBubbleBorder. |
| 118 bubble_border_ = new AppListBubbleBorder(this); | 133 bubble_border_ = new AppListBubbleBorder(this); |
| 119 GetBubbleFrameView()->SetBubbleBorder(bubble_border_); | 134 GetBubbleFrameView()->SetBubbleBorder(bubble_border_); |
| 120 SizeToContents(); // Recalcuates with new border. | 135 SizeToContents(); // Recalcuates with new border. |
| 121 | 136 |
| 122 UpdateModel(); | 137 CreateModel(); |
| 123 } | 138 } |
| 124 | 139 |
| 125 void AppListView::AnimateShow(int duration_ms) { | 140 void AppListView::AnimateShow(int duration_ms) { |
| 126 if (bubble_style_) | 141 if (bubble_style_) |
| 127 return; | 142 return; |
| 128 | 143 |
| 129 ui::Layer* layer = model_view_->layer(); | 144 ui::Layer* layer = apps_view_->layer(); |
| 130 ui::ScopedLayerAnimationSettings animation(layer->GetAnimator()); | 145 ui::ScopedLayerAnimationSettings animation(layer->GetAnimator()); |
| 131 animation.SetTransitionDuration( | 146 animation.SetTransitionDuration( |
| 132 base::TimeDelta::FromMilliseconds(duration_ms)); | 147 base::TimeDelta::FromMilliseconds(duration_ms)); |
| 133 animation.SetTweenType(ui::Tween::EASE_OUT); | 148 animation.SetTweenType(ui::Tween::EASE_OUT); |
| 134 model_view_->SetTransform(ui::Transform()); | 149 apps_view_->SetTransform(ui::Transform()); |
| 135 } | 150 } |
| 136 | 151 |
| 137 void AppListView::AnimateHide(int duration_ms) { | 152 void AppListView::AnimateHide(int duration_ms) { |
| 138 if (bubble_style_) | 153 if (bubble_style_) |
| 139 return; | 154 return; |
| 140 | 155 |
| 141 ui::Layer* layer = model_view_->layer(); | 156 ui::Layer* layer = apps_view_->layer(); |
| 142 ui::ScopedLayerAnimationSettings animation(layer->GetAnimator()); | 157 ui::ScopedLayerAnimationSettings animation(layer->GetAnimator()); |
| 143 animation.SetTransitionDuration( | 158 animation.SetTransitionDuration( |
| 144 base::TimeDelta::FromMilliseconds(duration_ms)); | 159 base::TimeDelta::FromMilliseconds(duration_ms)); |
| 145 animation.SetTweenType(ui::Tween::EASE_IN); | 160 animation.SetTweenType(ui::Tween::EASE_IN); |
| 146 model_view_->SetTransform(GetScaleTransform(model_view_)); | 161 apps_view_->SetTransform(GetScaleTransform(apps_view_)); |
| 147 } | 162 } |
| 148 | 163 |
| 149 void AppListView::Close() { | 164 void AppListView::Close() { |
| 150 if (delegate_.get()) | 165 if (delegate_.get()) |
| 151 delegate_->Close(); | 166 delegate_->Close(); |
| 152 else | 167 else |
| 153 GetWidget()->Close(); | 168 GetWidget()->Close(); |
| 154 } | 169 } |
| 155 | 170 |
| 156 void AppListView::UpdateBounds(const gfx::Rect& screen_bounds, | 171 void AppListView::UpdateBounds(const gfx::Rect& screen_bounds, |
| 157 const gfx::Rect& work_area) { | 172 const gfx::Rect& work_area) { |
| 158 if (bubble_style_) { | 173 if (bubble_style_) { |
| 159 SizeToContents(); | 174 SizeToContents(); |
| 160 } else { | 175 } else { |
| 161 work_area_ = work_area; | 176 work_area_ = work_area; |
| 162 GetWidget()->SetBounds(screen_bounds); | 177 GetWidget()->SetBounds(screen_bounds); |
| 163 } | 178 } |
| 164 } | 179 } |
| 165 | 180 |
| 166 void AppListView::UpdateModel() { | 181 void AppListView::CreateModel() { |
| 167 if (delegate_.get()) { | 182 if (delegate_.get()) { |
| 183 // Creates a new model and update all references before releasing old one. |
| 168 scoped_ptr<AppListModel> new_model(new AppListModel); | 184 scoped_ptr<AppListModel> new_model(new AppListModel); |
| 185 |
| 169 delegate_->SetModel(new_model.get()); | 186 delegate_->SetModel(new_model.get()); |
| 170 delegate_->UpdateModel(std::string()); | 187 apps_view_->SetModel(new_model->apps()); |
| 171 model_view_->SetModel(new_model.get()); | 188 |
| 189 // search_box_view_ etc are not created for v1. |
| 190 // TODO(xiyuan): Update this after v2 is ready. |
| 191 if (search_box_view_) |
| 192 search_box_view_->SetModel(new_model->search_box()); |
| 193 if (search_results_view_) |
| 194 search_results_view_->SetResults(new_model->results()); |
| 195 |
| 172 model_.reset(new_model.release()); | 196 model_.reset(new_model.release()); |
| 173 } | 197 } |
| 174 } | 198 } |
| 175 | 199 |
| 176 views::View* AppListView::GetInitiallyFocusedView() { | 200 views::View* AppListView::GetInitiallyFocusedView() { |
| 177 return model_view_; | 201 if (bubble_style_) |
| 202 return search_box_view_->search_box(); |
| 203 else |
| 204 return apps_view_; |
| 178 } | 205 } |
| 179 | 206 |
| 180 void AppListView::Layout() { | 207 void AppListView::Layout() { |
| 181 gfx::Rect rect(GetContentsBounds()); | 208 gfx::Rect rect(GetContentsBounds()); |
| 182 if (rect.IsEmpty()) | 209 if (rect.IsEmpty()) |
| 183 return; | 210 return; |
| 184 | 211 |
| 185 if (bubble_style_) { | 212 if (bubble_style_) { |
| 186 views::View::Layout(); | 213 views::View::Layout(); |
| 187 } else { | 214 } else { |
| 188 // Gets work area rect, which is in screen coordinates. | 215 // Gets work area rect, which is in screen coordinates. |
| 189 gfx::Rect workarea(work_area_); | 216 gfx::Rect workarea(work_area_); |
| 190 | 217 |
| 191 // Converts |workarea| into view's coordinates. | 218 // Converts |workarea| into view's coordinates. |
| 192 gfx::Point origin(workarea.origin()); | 219 gfx::Point origin(workarea.origin()); |
| 193 views::View::ConvertPointFromScreen(this, &origin); | 220 views::View::ConvertPointFromScreen(this, &origin); |
| 194 workarea.Offset(-origin.x(), -origin.y()); | 221 workarea.Offset(-origin.x(), -origin.y()); |
| 195 | 222 |
| 196 rect = rect.Intersect(workarea); | 223 rect = rect.Intersect(workarea); |
| 197 model_view_->SetBoundsRect(rect); | 224 apps_view_->SetBoundsRect(rect); |
| 198 } | 225 } |
| 199 } | 226 } |
| 200 | 227 |
| 201 bool AppListView::OnKeyPressed(const views::KeyEvent& event) { | 228 bool AppListView::OnKeyPressed(const views::KeyEvent& event) { |
| 202 if (event.key_code() == ui::VKEY_ESCAPE) { | 229 if (event.key_code() == ui::VKEY_ESCAPE) { |
| 203 Close(); | 230 Close(); |
| 204 return true; | 231 return true; |
| 205 } | 232 } |
| 206 | 233 |
| 207 return false; | 234 return false; |
| 208 } | 235 } |
| 209 | 236 |
| 210 bool AppListView::OnMousePressed(const views::MouseEvent& event) { | 237 bool AppListView::OnMousePressed(const views::MouseEvent& event) { |
| 211 // For full screen mode, if mouse click reaches us, this means user clicks | 238 // For full screen mode, if mouse click reaches us, this means user clicks |
| 212 // on blank area. So close. | 239 // on blank area. So close. |
| 213 if (!bubble_style_) | 240 if (!bubble_style_) |
| 214 Close(); | 241 Close(); |
| 215 | 242 |
| 216 return true; | 243 return true; |
| 217 } | 244 } |
| 218 | 245 |
| 219 void AppListView::ButtonPressed(views::Button* sender, | 246 void AppListView::ButtonPressed(views::Button* sender, |
| 220 const views::Event& event) { | 247 const views::Event& event) { |
| 221 if (sender->GetClassName() != AppListItemView::kViewClassName) | 248 if (sender->GetClassName() != AppListItemView::kViewClassName) |
| 222 return; | 249 return; |
| 223 | 250 |
| 224 if (delegate_.get()) { | 251 if (delegate_.get()) { |
| 225 delegate_->OnAppListItemActivated( | 252 delegate_->ActivateAppListItem( |
| 226 static_cast<AppListItemView*>(sender)->model(), | 253 static_cast<AppListItemView*>(sender)->model(), |
| 227 event.flags()); | 254 event.flags()); |
| 228 } | 255 } |
| 229 Close(); | 256 Close(); |
| 230 } | 257 } |
| 231 | 258 |
| 232 gfx::Rect AppListView::GetBubbleBounds() { | 259 gfx::Rect AppListView::GetBubbleBounds() { |
| 233 // This happens before replacing the default border. | 260 // This happens before replacing the default border. |
| 234 if (!bubble_border_) | 261 if (!bubble_border_) |
| 235 return views::BubbleDelegateView::GetBubbleBounds(); | 262 return views::BubbleDelegateView::GetBubbleBounds(); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 261 // it points to the same position before the move. | 288 // it points to the same position before the move. |
| 262 bubble_border_->set_arrow_offset(-offset); | 289 bubble_border_->set_arrow_offset(-offset); |
| 263 | 290 |
| 264 // Repaints border if arrow offset is changed. | 291 // Repaints border if arrow offset is changed. |
| 265 if (bubble_border_->arrow_offset() != old_arrow_offset) | 292 if (bubble_border_->arrow_offset() != old_arrow_offset) |
| 266 GetBubbleFrameView()->SchedulePaint(); | 293 GetBubbleFrameView()->SchedulePaint(); |
| 267 | 294 |
| 268 return bubble_rect; | 295 return bubble_rect; |
| 269 } | 296 } |
| 270 | 297 |
| 298 void AppListView::QueryChanged(SearchBoxView* sender) { |
| 299 bool showing_search = search_results_view_->visible(); |
| 300 bool should_show_search = !model_->search_box()->text().empty(); |
| 301 |
| 302 if (delegate_.get()) { |
| 303 if (should_show_search) |
| 304 delegate_->StartSearch(); |
| 305 else |
| 306 delegate_->StopSearch(); |
| 307 } |
| 308 |
| 309 if (showing_search != should_show_search) { |
| 310 // TODO(xiyuan): Animate this transition. |
| 311 apps_view_->SetVisible(!should_show_search); |
| 312 page_switcher_view_->SetVisible(!should_show_search); |
| 313 search_results_view_->SetVisible(should_show_search); |
| 314 |
| 315 // TODO(xiyuan): Highlight default match instead of the first. |
| 316 if (search_results_view_->visible()) |
| 317 search_results_view_->SetSelectedIndex(0); |
| 318 |
| 319 Layout(); |
| 320 SchedulePaint(); |
| 321 // Needs to repaint frame as well. |
| 322 GetBubbleFrameView()->SchedulePaint(); |
| 323 } |
| 324 } |
| 325 |
| 326 void AppListView::OpenResult(const SearchResult* result, int event_flags) { |
| 327 if (delegate_.get()) |
| 328 delegate_->OpenSearchResult(result, event_flags); |
| 329 Close(); |
| 330 } |
| 331 |
| 271 } // namespace app_list | 332 } // namespace app_list |
| OLD | NEW |