Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / athena / home / home_card_impl.cc
1 // Copyright 2014 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 "athena/home/home_card_impl.h"
6
7 #include <cmath>
8 #include <limits>
9
10 #include "athena/env/public/athena_env.h"
11 #include "athena/home/app_list_view_delegate.h"
12 #include "athena/home/home_card_constants.h"
13 #include "athena/home/public/app_model_builder.h"
14 #include "athena/screen/public/screen_manager.h"
15 #include "athena/util/container_priorities.h"
16 #include "athena/wm/public/window_manager.h"
17 #include "ui/app_list/search_box_model.h"
18 #include "ui/app_list/views/app_list_main_view.h"
19 #include "ui/app_list/views/search_box_view.h"
20 #include "ui/aura/layout_manager.h"
21 #include "ui/aura/window.h"
22 #include "ui/compositor/closure_animation_observer.h"
23 #include "ui/compositor/layer.h"
24 #include "ui/compositor/scoped_layer_animation_settings.h"
25 #include "ui/gfx/animation/tween.h"
26 #include "ui/views/background.h"
27 #include "ui/views/layout/fill_layout.h"
28 #include "ui/views/widget/widget.h"
29 #include "ui/views/widget/widget_delegate.h"
30 #include "ui/wm/core/shadow_types.h"
31 #include "ui/wm/core/visibility_controller.h"
32
33 namespace athena {
34 namespace {
35
36 HomeCard* instance = nullptr;
37
38 const float kMinimizedHomeOpacity = 0.65f;
39 const int kIndicatorOffset = 24;
40 const int kAppListOffset = -128;
41
42 gfx::Rect GetBoundsForState(const gfx::Rect& screen_bounds,
43                             HomeCard::State state) {
44   switch (state) {
45     case HomeCard::HIDDEN:
46       break;
47
48     case HomeCard::VISIBLE_CENTERED:
49       return screen_bounds;
50
51     // Do not change the home_card's size, only changes the top position
52     // instead, because size change causes unnecessary re-layouts.
53     case HomeCard::VISIBLE_BOTTOM:
54       return gfx::Rect(0,
55                        screen_bounds.bottom() - kHomeCardHeight,
56                        screen_bounds.width(),
57                        screen_bounds.height());
58     case HomeCard::VISIBLE_MINIMIZED:
59       return gfx::Rect(0,
60                        screen_bounds.bottom() - kHomeCardMinimizedHeight,
61                        screen_bounds.width(),
62                        screen_bounds.height());
63   }
64
65   NOTREACHED();
66   return gfx::Rect();
67 }
68
69 }  // namespace
70
71 // Makes sure the homecard is center-aligned horizontally and bottom-aligned
72 // vertically.
73 class HomeCardLayoutManager : public aura::LayoutManager {
74  public:
75   HomeCardLayoutManager() : home_card_(nullptr) {}
76
77   ~HomeCardLayoutManager() override {}
78
79   void Layout(bool animate, gfx::Tween::Type tween_type) {
80     // |home_card| could be detached from the root window (e.g. when it is being
81     // destroyed).
82     if (!home_card_ || !home_card_->IsVisible() || !home_card_->GetRootWindow())
83       return;
84
85     scoped_ptr<ui::ScopedLayerAnimationSettings> settings;
86     if (animate) {
87       settings.reset(new ui::ScopedLayerAnimationSettings(
88           home_card_->layer()->GetAnimator()));
89       settings->SetTweenType(tween_type);
90     }
91     SetChildBoundsDirect(home_card_, GetBoundsForState(
92         home_card_->GetRootWindow()->bounds(), HomeCard::Get()->GetState()));
93   }
94
95  private:
96   // aura::LayoutManager:
97   void OnWindowResized() override {
98     Layout(false, gfx::Tween::LINEAR);
99   }
100   void OnWindowAddedToLayout(aura::Window* child) override {
101     if (!home_card_) {
102       home_card_ = child;
103       Layout(false, gfx::Tween::LINEAR);
104     }
105   }
106   void OnWillRemoveWindowFromLayout(aura::Window* child) override {
107     if (home_card_ == child)
108       home_card_ = nullptr;
109   }
110   void OnWindowRemovedFromLayout(aura::Window* child) override {}
111   void OnChildWindowVisibilityChanged(aura::Window* child,
112                                       bool visible) override {
113     if (home_card_ == child)
114       Layout(false, gfx::Tween::LINEAR);
115   }
116   void SetChildBounds(aura::Window* child,
117                       const gfx::Rect& requested_bounds) override {
118     SetChildBoundsDirect(child, requested_bounds);
119   }
120
121   aura::Window* home_card_;
122
123   DISALLOW_COPY_AND_ASSIGN(HomeCardLayoutManager);
124 };
125
126 // The container view of home card contents of each state.
127 class HomeCardView : public views::WidgetDelegateView {
128  public:
129   HomeCardView(app_list::AppListViewDelegate* view_delegate,
130                aura::Window* container,
131                HomeCardGestureManager::Delegate* gesture_delegate)
132       : background_(new views::View),
133         main_view_(new app_list::AppListMainView(view_delegate)),
134         search_box_view_(
135             new app_list::SearchBoxView(main_view_, view_delegate)),
136         minimized_background_(new views::View()),
137         drag_indicator_(new views::View()),
138         gesture_delegate_(gesture_delegate),
139         weak_factory_(this) {
140     background_->set_background(
141         views::Background::CreateVerticalGradientBackground(SK_ColorLTGRAY,
142                                                             SK_ColorWHITE));
143     background_->SetPaintToLayer(true);
144     background_->SetFillsBoundsOpaquely(false);
145     AddChildView(background_);
146
147     main_view_->SetPaintToLayer(true);
148     main_view_->SetFillsBoundsOpaquely(false);
149     main_view_->layer()->SetMasksToBounds(true);
150     AddChildView(main_view_);
151
152     search_box_view_->SetPaintToLayer(true);
153     search_box_view_->SetFillsBoundsOpaquely(false);
154     search_box_view_->layer()->SetMasksToBounds(true);
155     AddChildView(search_box_view_);
156
157     minimized_background_->set_background(
158         views::Background::CreateSolidBackground(
159             SkColorSetA(SK_ColorBLACK, 256 * kMinimizedHomeOpacity)));
160     minimized_background_->SetPaintToLayer(true);
161     minimized_background_->SetFillsBoundsOpaquely(false);
162     minimized_background_->layer()->set_name("MinimizedBackground");
163     AddChildView(minimized_background_);
164
165     drag_indicator_->set_background(
166         views::Background::CreateSolidBackground(SK_ColorWHITE));
167     drag_indicator_->SetPaintToLayer(true);
168     AddChildView(drag_indicator_);
169   }
170
171   void Init() {
172     main_view_->Init(GetWidget()->GetNativeView(),
173                      -1, /* inital apps page: -1 means default */
174                      search_box_view_);
175   }
176
177   void SetStateProgress(HomeCard::State from_state,
178                         HomeCard::State to_state,
179                         float progress) {
180     // TODO(mukai): not clear the focus, but simply close the virtual keyboard.
181     GetFocusManager()->ClearFocus();
182
183     gfx::Rect from_main_bounds = GetMainViewBounds(from_state);
184     gfx::Rect to_main_bounds = GetMainViewBounds(to_state);
185     if (from_main_bounds != to_main_bounds) {
186       DCHECK_EQ(from_main_bounds.size().ToString(),
187                 to_main_bounds.size().ToString());
188       gfx::Rect main_bounds = gfx::Tween::RectValueBetween(
189           progress, from_main_bounds, to_main_bounds);
190       main_view_->SetBoundsRect(main_bounds);
191       main_bounds.set_height(
192           search_box_view_->GetHeightForWidth(main_bounds.width()));
193       search_box_view_->SetBoundsRect(main_bounds);
194     }
195
196     float background_opacity = 1.0f;
197     if (from_state == HomeCard::VISIBLE_MINIMIZED ||
198         to_state == HomeCard::VISIBLE_MINIMIZED) {
199       background_opacity = (from_state == HomeCard::VISIBLE_MINIMIZED)
200                                ? progress
201                                : (1.0f - progress);
202     }
203     background_->layer()->SetOpacity(background_opacity);
204     minimized_background_->layer()->SetOpacity(1.0f - background_opacity);
205     UpdateMinimizedBackgroundVisibility();
206
207     int background_height = kHomeCardHeight;
208     if (from_state == HomeCard::VISIBLE_CENTERED ||
209         to_state == HomeCard::VISIBLE_CENTERED) {
210       gfx::Rect window_bounds = GetWidget()->GetWindowBoundsInScreen();
211       background_height = window_bounds.height() - window_bounds.y();
212     }
213     gfx::Transform background_transform;
214     background_transform.Scale(
215         SK_MScalar1,
216         SkIntToMScalar(background_height) / SkIntToMScalar(height()));
217     background_->layer()->SetTransform(background_transform);
218
219     gfx::Rect from_bounds = GetDragIndicatorBounds(from_state);
220     gfx::Rect to_bounds = GetDragIndicatorBounds(to_state);
221     if (from_bounds != to_bounds) {
222       DCHECK_EQ(from_bounds.size().ToString(), to_bounds.size().ToString());
223       drag_indicator_->SetBoundsRect(
224           gfx::Tween::RectValueBetween(progress, from_bounds, to_bounds));
225     }
226   }
227
228   void SetStateWithAnimation(HomeCard::State state,
229                              gfx::Tween::Type tween_type,
230                              const base::Closure& on_animation_ended) {
231     float minimized_opacity =
232         (state == HomeCard::VISIBLE_MINIMIZED) ? 1.0f : 0.0f;
233     // |minimized_background_| needs to be visible before scheduling animation.
234     if (state == HomeCard::VISIBLE_MINIMIZED)
235       minimized_background_->SetVisible(true);
236
237     if (minimized_opacity !=
238         minimized_background_->layer()->GetTargetOpacity()) {
239       ui::ScopedLayerAnimationSettings settings(
240           minimized_background_->layer()->GetAnimator());
241       settings.SetTweenType(gfx::Tween::EASE_IN);
242       settings.AddObserver(new ui::ClosureAnimationObserver(
243           base::Bind(&HomeCardView::UpdateMinimizedBackgroundVisibility,
244                      weak_factory_.GetWeakPtr())));
245       minimized_background_->layer()->SetOpacity(minimized_opacity);
246     }
247
248     gfx::Transform background_transform;
249     if (state != HomeCard::VISIBLE_CENTERED) {
250       background_transform.Scale(
251           SK_MScalar1,
252           SkIntToMScalar(kHomeCardHeight) / SkIntToMScalar(height()));
253     }
254     float background_opacity = 1.0f - minimized_opacity;
255     if (background_->layer()->GetTargetTransform() != background_transform ||
256         background_->layer()->GetTargetOpacity() != background_opacity) {
257       ui::ScopedLayerAnimationSettings settings(
258           background_->layer()->GetAnimator());
259       settings.SetTweenType(tween_type);
260       background_->layer()->SetTransform(background_transform);
261       background_->layer()->SetOpacity(background_opacity);
262     }
263
264     {
265       ui::ScopedLayerAnimationSettings settings(
266           drag_indicator_->layer()->GetAnimator());
267       settings.SetTweenType(tween_type);
268       drag_indicator_->SetBoundsRect(GetDragIndicatorBounds(state));
269     }
270
271     {
272       ui::ScopedLayerAnimationSettings settings(
273           main_view_->layer()->GetAnimator());
274       settings.SetTweenType(tween_type);
275       settings.AddObserver(
276           new ui::ClosureAnimationObserver(on_animation_ended));
277       gfx::Rect main_bounds = GetMainViewBounds(state);
278       main_view_->SetBoundsRect(main_bounds);
279       main_bounds.set_height(
280           search_box_view_->GetHeightForWidth(main_bounds.width()));
281       search_box_view_->SetBoundsRect(main_bounds);
282     }
283
284     main_view_->UpdateSearchBoxVisibility();
285   }
286
287   void ClearGesture() {
288     gesture_manager_.reset();
289   }
290
291   // views::View:
292   void OnGestureEvent(ui::GestureEvent* event) override {
293     if (!gesture_manager_ &&
294         event->type() == ui::ET_GESTURE_SCROLL_BEGIN) {
295       gesture_manager_.reset(new HomeCardGestureManager(
296           gesture_delegate_,
297           GetWidget()->GetNativeWindow()->GetRootWindow()->bounds()));
298     }
299
300     if (gesture_manager_)
301       gesture_manager_->ProcessGestureEvent(event);
302   }
303   bool OnMousePressed(const ui::MouseEvent& event) override {
304     if (HomeCard::Get()->GetState() == HomeCard::VISIBLE_MINIMIZED &&
305         event.IsLeftMouseButton() && event.GetClickCount() == 1) {
306       athena::WindowManager::Get()->EnterOverview();
307       return true;
308     }
309     return false;
310   }
311
312   void Layout() override {
313     const gfx::Rect contents_bounds = GetContentsBounds();
314     background_->SetBoundsRect(contents_bounds);
315     minimized_background_->SetBoundsRect(contents_bounds);
316     const gfx::Rect drag_indicator_bounds =
317         GetDragIndicatorBounds(HomeCard::Get()->GetState());
318     drag_indicator_->SetBoundsRect(drag_indicator_bounds);
319
320     gfx::Rect main_bounds(GetMainViewBounds(HomeCard::Get()->GetState()));
321     main_view_->SetBoundsRect(main_bounds);
322
323     main_bounds.set_height(
324         search_box_view_->GetHeightForWidth(main_bounds.width()));
325     search_box_view_->SetBoundsRect(main_bounds);
326   }
327
328  private:
329   gfx::Rect GetDragIndicatorBounds(HomeCard::State state) {
330     gfx::Rect drag_indicator_bounds(
331         GetContentsBounds().CenterPoint().x() - kHomeCardDragIndicatorWidth / 2,
332         kHomeCardDragIndicatorMarginHeight,
333         kHomeCardDragIndicatorWidth,
334         kHomeCardDragIndicatorHeight);
335     if (state == HomeCard::VISIBLE_CENTERED)
336       drag_indicator_bounds.Offset(0, kSystemUIHeight);
337     return drag_indicator_bounds;
338   }
339
340   gfx::Rect GetMainViewBounds(HomeCard::State state) {
341     const gfx::Rect contents_bounds = GetContentsBounds();
342     const int main_width = main_view_->GetPreferredSize().width();
343     gfx::Rect main_bounds(
344         contents_bounds.CenterPoint().x() - main_width / 2,
345         GetDragIndicatorBounds(state).bottom() + kIndicatorOffset,
346         main_width,
347         contents_bounds.height());
348     // This is a bit hacky but slightly shifting up the main_view to fit
349     // the search box and app icons in the home card.
350     if (state != HomeCard::VISIBLE_CENTERED)
351       main_bounds.set_y(kAppListOffset);
352     return main_bounds;
353   }
354
355  private:
356   void UpdateMinimizedBackgroundVisibility() {
357     minimized_background_->SetVisible(
358         minimized_background_->layer()->GetTargetOpacity() != 0.0f);
359   }
360
361   // views::WidgetDelegate:
362   views::View* GetContentsView() override { return this; }
363
364   views::View* background_;
365   app_list::AppListMainView* main_view_;
366   app_list::SearchBoxView* search_box_view_;
367   views::View* minimized_background_;
368   views::View* drag_indicator_;
369   HomeCard::State state_;
370   scoped_ptr<HomeCardGestureManager> gesture_manager_;
371   HomeCardGestureManager::Delegate* gesture_delegate_;
372
373   base::WeakPtrFactory<HomeCardView> weak_factory_;
374
375   DISALLOW_COPY_AND_ASSIGN(HomeCardView);
376 };
377
378 HomeCardImpl::HomeCardImpl(scoped_ptr<AppModelBuilder> model_builder,
379                            scoped_ptr<SearchControllerFactory> search_factory)
380     : model_builder_(model_builder.Pass()),
381       search_factory_(search_factory.Pass()),
382       state_(HIDDEN),
383       original_state_(VISIBLE_MINIMIZED),
384       home_card_widget_(nullptr),
385       home_card_view_(nullptr),
386       layout_manager_(nullptr) {
387   DCHECK(!instance);
388   instance = this;
389   WindowManager::Get()->AddObserver(this);
390 }
391
392 HomeCardImpl::~HomeCardImpl() {
393   DCHECK(instance);
394   WindowManager::Get()->RemoveObserver(this);
395   home_card_widget_->CloseNow();
396
397   // Reset the view delegate first as it access search provider during
398   // shutdown.
399   view_delegate_.reset();
400   instance = nullptr;
401 }
402
403 void HomeCardImpl::Init() {
404   InstallAccelerators();
405   ScreenManager::ContainerParams params("HomeCardContainer", CP_HOME_CARD);
406   params.can_activate_children = true;
407   aura::Window* container = ScreenManager::Get()->CreateContainer(params);
408   layout_manager_ = new HomeCardLayoutManager();
409
410   container->SetLayoutManager(layout_manager_);
411   wm::SetChildWindowVisibilityChangesAnimated(container);
412
413   view_delegate_.reset(
414       new AppListViewDelegate(model_builder_.get(), search_factory_.get()));
415
416   home_card_view_ = new HomeCardView(view_delegate_.get(), container, this);
417   home_card_widget_ = new views::Widget();
418   views::Widget::InitParams widget_params(
419       views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
420   widget_params.parent = container;
421   widget_params.delegate = home_card_view_;
422   widget_params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
423   home_card_widget_->Init(widget_params);
424   // AppListMainView in home card may move outside of home card layer partially
425   // in an transition animation. This flag is set to clip the parts outside of
426   // home card.
427   home_card_widget_->GetNativeWindow()->layer()->SetMasksToBounds(true);
428
429   home_card_view_->Init();
430   SetState(VISIBLE_MINIMIZED);
431   home_card_view_->Layout();
432
433   AthenaEnv::Get()->SetDisplayWorkAreaInsets(
434       gfx::Insets(0, 0, kHomeCardMinimizedHeight, 0));
435 }
436
437 aura::Window* HomeCardImpl::GetHomeCardWindowForTest() const {
438   return home_card_widget_ ? home_card_widget_->GetNativeWindow() : nullptr;
439 }
440
441 void HomeCardImpl::ResetQuery() {
442   view_delegate_->GetModel()->search_box()->SetText(base::string16());
443 }
444
445 void HomeCardImpl::InstallAccelerators() {
446   const AcceleratorData accelerator_data[] = {
447       {TRIGGER_ON_PRESS, ui::VKEY_L, ui::EF_CONTROL_DOWN,
448        COMMAND_SHOW_HOME_CARD, AF_NONE},
449   };
450   AcceleratorManager::Get()->RegisterAccelerators(
451       accelerator_data, arraysize(accelerator_data), this);
452 }
453
454 void HomeCardImpl::SetState(HomeCard::State state) {
455   if (state_ == state)
456     return;
457
458   // Update |state_| before changing the visibility of the widgets, so that
459   // LayoutManager callbacks get the correct state.
460   state_ = state;
461   original_state_ = state;
462
463   if (state_ == HIDDEN) {
464     home_card_widget_->Hide();
465   } else {
466     if (state_ == VISIBLE_MINIMIZED)
467       home_card_widget_->ShowInactive();
468     else
469       home_card_widget_->Show();
470
471     // Query should be reset on state change to reset the main_view. Also it's
472     // not possible to invoke ResetQuery() here, it causes a crash on search.
473     home_card_view_->SetStateWithAnimation(
474         state,
475         gfx::Tween::EASE_IN_OUT,
476         base::Bind(&HomeCardImpl::ResetQuery, base::Unretained(this)));
477     layout_manager_->Layout(true, gfx::Tween::EASE_IN_OUT);
478   }
479 }
480
481 HomeCard::State HomeCardImpl::GetState() {
482   return state_;
483 }
484
485 void HomeCardImpl::UpdateVirtualKeyboardBounds(
486     const gfx::Rect& bounds) {
487   if (state_ == VISIBLE_MINIMIZED && !bounds.IsEmpty()) {
488     SetState(HIDDEN);
489     original_state_ = VISIBLE_MINIMIZED;
490   } else if (state_ == VISIBLE_BOTTOM && !bounds.IsEmpty()) {
491     SetState(VISIBLE_CENTERED);
492     original_state_ = VISIBLE_BOTTOM;
493   } else if (state_ != original_state_ && bounds.IsEmpty()) {
494     SetState(original_state_);
495   }
496 }
497
498 bool HomeCardImpl::IsCommandEnabled(int command_id) const {
499   return true;
500 }
501
502 bool HomeCardImpl::OnAcceleratorFired(int command_id,
503                                       const ui::Accelerator& accelerator) {
504   DCHECK_EQ(COMMAND_SHOW_HOME_CARD, command_id);
505
506   if (state_ == VISIBLE_CENTERED && original_state_ != VISIBLE_BOTTOM) {
507     SetState(VISIBLE_MINIMIZED);
508     WindowManager::Get()->ExitOverview();
509   } else if (state_ == VISIBLE_MINIMIZED) {
510     SetState(VISIBLE_CENTERED);
511     WindowManager::Get()->EnterOverview();
512   }
513   return true;
514 }
515
516 void HomeCardImpl::OnGestureEnded(State final_state, bool is_fling) {
517   home_card_view_->ClearGesture();
518   if (state_ != final_state &&
519       (state_ == VISIBLE_MINIMIZED || final_state == VISIBLE_MINIMIZED)) {
520     SetState(final_state);
521     if (WindowManager::Get()->IsOverviewModeActive())
522       WindowManager::Get()->ExitOverview();
523     else
524       WindowManager::Get()->EnterOverview();
525   } else {
526     state_ = final_state;
527     // When the animation happens after a fling, EASE_IN_OUT would cause weird
528     // slow-down right after the finger release because of slow-in. Therefore
529     // EASE_OUT is better.
530     gfx::Tween::Type tween_type =
531         is_fling ? gfx::Tween::EASE_OUT : gfx::Tween::EASE_IN_OUT;
532     home_card_view_->SetStateWithAnimation(
533         state_,
534         tween_type,
535         base::Bind(&HomeCardImpl::ResetQuery, base::Unretained(this)));
536     layout_manager_->Layout(true, tween_type);
537   }
538 }
539
540 void HomeCardImpl::OnGestureProgressed(
541     State from_state, State to_state, float progress) {
542   gfx::Rect screen_bounds =
543       home_card_widget_->GetNativeWindow()->GetRootWindow()->bounds();
544   home_card_widget_->SetBounds(gfx::Tween::RectValueBetween(
545       progress,
546       GetBoundsForState(screen_bounds, from_state),
547       GetBoundsForState(screen_bounds, to_state)));
548
549   home_card_view_->SetStateProgress(from_state, to_state, progress);
550
551   // TODO(mukai): signals the update to the window manager so that it shows the
552   // intermediate visual state of overview mode.
553 }
554
555 void HomeCardImpl::OnOverviewModeEnter() {
556   if (state_ == HIDDEN || state_ == VISIBLE_MINIMIZED)
557     SetState(VISIBLE_BOTTOM);
558 }
559
560 void HomeCardImpl::OnOverviewModeExit() {
561   SetState(VISIBLE_MINIMIZED);
562 }
563
564 void HomeCardImpl::OnSplitViewModeEnter() {
565 }
566
567 void HomeCardImpl::OnSplitViewModeExit() {
568 }
569
570 // static
571 HomeCard* HomeCard::Create(scoped_ptr<AppModelBuilder> model_builder,
572                            scoped_ptr<SearchControllerFactory> search_factory) {
573   (new HomeCardImpl(model_builder.Pass(), search_factory.Pass()))->Init();
574   DCHECK(instance);
575   return instance;
576 }
577
578 // static
579 void HomeCard::Shutdown() {
580   DCHECK(instance);
581   delete instance;
582   instance = nullptr;
583 }
584
585 // static
586 HomeCard* HomeCard::Get() {
587   DCHECK(instance);
588   return instance;
589 }
590
591 }  // namespace athena