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.
5 #include "athena/home/home_card_impl.h"
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"
36 HomeCard* instance = nullptr;
38 const float kMinimizedHomeOpacity = 0.65f;
39 const int kIndicatorOffset = 24;
40 const int kAppListOffset = -128;
42 gfx::Rect GetBoundsForState(const gfx::Rect& screen_bounds,
43 HomeCard::State state) {
45 case HomeCard::HIDDEN:
48 case HomeCard::VISIBLE_CENTERED:
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:
55 screen_bounds.bottom() - kHomeCardHeight,
56 screen_bounds.width(),
57 screen_bounds.height());
58 case HomeCard::VISIBLE_MINIMIZED:
60 screen_bounds.bottom() - kHomeCardMinimizedHeight,
61 screen_bounds.width(),
62 screen_bounds.height());
71 // Makes sure the homecard is center-aligned horizontally and bottom-aligned
73 class HomeCardLayoutManager : public aura::LayoutManager {
75 HomeCardLayoutManager() : home_card_(nullptr) {}
77 ~HomeCardLayoutManager() override {}
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
82 if (!home_card_ || !home_card_->IsVisible() || !home_card_->GetRootWindow())
85 scoped_ptr<ui::ScopedLayerAnimationSettings> settings;
87 settings.reset(new ui::ScopedLayerAnimationSettings(
88 home_card_->layer()->GetAnimator()));
89 settings->SetTweenType(tween_type);
91 SetChildBoundsDirect(home_card_, GetBoundsForState(
92 home_card_->GetRootWindow()->bounds(), HomeCard::Get()->GetState()));
96 // aura::LayoutManager:
97 void OnWindowResized() override {
98 Layout(false, gfx::Tween::LINEAR);
100 void OnWindowAddedToLayout(aura::Window* child) override {
103 Layout(false, gfx::Tween::LINEAR);
106 void OnWillRemoveWindowFromLayout(aura::Window* child) override {
107 if (home_card_ == child)
108 home_card_ = nullptr;
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);
116 void SetChildBounds(aura::Window* child,
117 const gfx::Rect& requested_bounds) override {
118 SetChildBoundsDirect(child, requested_bounds);
121 aura::Window* home_card_;
123 DISALLOW_COPY_AND_ASSIGN(HomeCardLayoutManager);
126 // The container view of home card contents of each state.
127 class HomeCardView : public views::WidgetDelegateView {
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)),
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,
143 background_->SetPaintToLayer(true);
144 background_->SetFillsBoundsOpaquely(false);
145 AddChildView(background_);
147 main_view_->SetPaintToLayer(true);
148 main_view_->SetFillsBoundsOpaquely(false);
149 main_view_->layer()->SetMasksToBounds(true);
150 AddChildView(main_view_);
152 search_box_view_->SetPaintToLayer(true);
153 search_box_view_->SetFillsBoundsOpaquely(false);
154 search_box_view_->layer()->SetMasksToBounds(true);
155 AddChildView(search_box_view_);
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_);
165 drag_indicator_->set_background(
166 views::Background::CreateSolidBackground(SK_ColorWHITE));
167 drag_indicator_->SetPaintToLayer(true);
168 AddChildView(drag_indicator_);
172 main_view_->Init(GetWidget()->GetNativeView(),
173 -1, /* inital apps page: -1 means default */
177 void SetStateProgress(HomeCard::State from_state,
178 HomeCard::State to_state,
180 // TODO(mukai): not clear the focus, but simply close the virtual keyboard.
181 GetFocusManager()->ClearFocus();
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);
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)
203 background_->layer()->SetOpacity(background_opacity);
204 minimized_background_->layer()->SetOpacity(1.0f - background_opacity);
205 UpdateMinimizedBackgroundVisibility();
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();
213 gfx::Transform background_transform;
214 background_transform.Scale(
216 SkIntToMScalar(background_height) / SkIntToMScalar(height()));
217 background_->layer()->SetTransform(background_transform);
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));
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);
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);
248 gfx::Transform background_transform;
249 if (state != HomeCard::VISIBLE_CENTERED) {
250 background_transform.Scale(
252 SkIntToMScalar(kHomeCardHeight) / SkIntToMScalar(height()));
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);
265 ui::ScopedLayerAnimationSettings settings(
266 drag_indicator_->layer()->GetAnimator());
267 settings.SetTweenType(tween_type);
268 drag_indicator_->SetBoundsRect(GetDragIndicatorBounds(state));
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);
284 main_view_->UpdateSearchBoxVisibility();
287 void ClearGesture() {
288 gesture_manager_.reset();
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(
297 GetWidget()->GetNativeWindow()->GetRootWindow()->bounds()));
300 if (gesture_manager_)
301 gesture_manager_->ProcessGestureEvent(event);
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();
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);
320 gfx::Rect main_bounds(GetMainViewBounds(HomeCard::Get()->GetState()));
321 main_view_->SetBoundsRect(main_bounds);
323 main_bounds.set_height(
324 search_box_view_->GetHeightForWidth(main_bounds.width()));
325 search_box_view_->SetBoundsRect(main_bounds);
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;
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,
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);
356 void UpdateMinimizedBackgroundVisibility() {
357 minimized_background_->SetVisible(
358 minimized_background_->layer()->GetTargetOpacity() != 0.0f);
361 // views::WidgetDelegate:
362 views::View* GetContentsView() override { return this; }
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_;
373 base::WeakPtrFactory<HomeCardView> weak_factory_;
375 DISALLOW_COPY_AND_ASSIGN(HomeCardView);
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()),
383 original_state_(VISIBLE_MINIMIZED),
384 home_card_widget_(nullptr),
385 home_card_view_(nullptr),
386 layout_manager_(nullptr) {
389 WindowManager::Get()->AddObserver(this);
392 HomeCardImpl::~HomeCardImpl() {
394 WindowManager::Get()->RemoveObserver(this);
395 home_card_widget_->CloseNow();
397 // Reset the view delegate first as it access search provider during
399 view_delegate_.reset();
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();
410 container->SetLayoutManager(layout_manager_);
411 wm::SetChildWindowVisibilityChangesAnimated(container);
413 view_delegate_.reset(
414 new AppListViewDelegate(model_builder_.get(), search_factory_.get()));
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
427 home_card_widget_->GetNativeWindow()->layer()->SetMasksToBounds(true);
429 home_card_view_->Init();
430 SetState(VISIBLE_MINIMIZED);
431 home_card_view_->Layout();
433 AthenaEnv::Get()->SetDisplayWorkAreaInsets(
434 gfx::Insets(0, 0, kHomeCardMinimizedHeight, 0));
437 aura::Window* HomeCardImpl::GetHomeCardWindowForTest() const {
438 return home_card_widget_ ? home_card_widget_->GetNativeWindow() : nullptr;
441 void HomeCardImpl::ResetQuery() {
442 view_delegate_->GetModel()->search_box()->SetText(base::string16());
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},
450 AcceleratorManager::Get()->RegisterAccelerators(
451 accelerator_data, arraysize(accelerator_data), this);
454 void HomeCardImpl::SetState(HomeCard::State state) {
458 // Update |state_| before changing the visibility of the widgets, so that
459 // LayoutManager callbacks get the correct state.
461 original_state_ = state;
463 if (state_ == HIDDEN) {
464 home_card_widget_->Hide();
466 if (state_ == VISIBLE_MINIMIZED)
467 home_card_widget_->ShowInactive();
469 home_card_widget_->Show();
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(
475 gfx::Tween::EASE_IN_OUT,
476 base::Bind(&HomeCardImpl::ResetQuery, base::Unretained(this)));
477 layout_manager_->Layout(true, gfx::Tween::EASE_IN_OUT);
481 HomeCard::State HomeCardImpl::GetState() {
485 void HomeCardImpl::UpdateVirtualKeyboardBounds(
486 const gfx::Rect& bounds) {
487 if (state_ == VISIBLE_MINIMIZED && !bounds.IsEmpty()) {
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_);
498 bool HomeCardImpl::IsCommandEnabled(int command_id) const {
502 bool HomeCardImpl::OnAcceleratorFired(int command_id,
503 const ui::Accelerator& accelerator) {
504 DCHECK_EQ(COMMAND_SHOW_HOME_CARD, command_id);
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();
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();
524 WindowManager::Get()->EnterOverview();
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(
535 base::Bind(&HomeCardImpl::ResetQuery, base::Unretained(this)));
536 layout_manager_->Layout(true, tween_type);
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(
546 GetBoundsForState(screen_bounds, from_state),
547 GetBoundsForState(screen_bounds, to_state)));
549 home_card_view_->SetStateProgress(from_state, to_state, progress);
551 // TODO(mukai): signals the update to the window manager so that it shows the
552 // intermediate visual state of overview mode.
555 void HomeCardImpl::OnOverviewModeEnter() {
556 if (state_ == HIDDEN || state_ == VISIBLE_MINIMIZED)
557 SetState(VISIBLE_BOTTOM);
560 void HomeCardImpl::OnOverviewModeExit() {
561 SetState(VISIBLE_MINIMIZED);
564 void HomeCardImpl::OnSplitViewModeEnter() {
567 void HomeCardImpl::OnSplitViewModeExit() {
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();
579 void HomeCard::Shutdown() {
586 HomeCard* HomeCard::Get() {
591 } // namespace athena