Adds full-functional search results for Athena on Chrome.
[chromium-blink-merge.git] / athena / home / home_card_impl.cc
blob1ffbfbf607ebfa79480b52e1f4b0aa0cb39696d7
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"
7 #include <cmath>
8 #include <limits>
10 #include "athena/env/public/athena_env.h"
11 #include "athena/home/app_list_view_delegate.h"
12 #include "athena/home/athena_start_page_view.h"
13 #include "athena/home/home_card_constants.h"
14 #include "athena/home/minimized_home.h"
15 #include "athena/home/public/app_model_builder.h"
16 #include "athena/screen/public/screen_manager.h"
17 #include "athena/util/container_priorities.h"
18 #include "athena/wm/public/window_manager.h"
19 #include "ui/app_list/views/app_list_main_view.h"
20 #include "ui/app_list/views/contents_view.h"
21 #include "ui/aura/layout_manager.h"
22 #include "ui/aura/window.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/layout/fill_layout.h"
27 #include "ui/views/widget/widget.h"
28 #include "ui/views/widget/widget_delegate.h"
29 #include "ui/wm/core/shadow_types.h"
30 #include "ui/wm/core/visibility_controller.h"
31 #include "ui/wm/public/activation_client.h"
33 namespace athena {
34 namespace {
36 HomeCard* instance = NULL;
38 gfx::Rect GetBoundsForState(const gfx::Rect& screen_bounds,
39 HomeCard::State state) {
40 switch (state) {
41 case HomeCard::HIDDEN:
42 break;
44 case HomeCard::VISIBLE_CENTERED:
45 return screen_bounds;
47 // Do not change the home_card's size, only changes the top position
48 // instead, because size change causes unnecessary re-layouts.
49 case HomeCard::VISIBLE_BOTTOM:
50 return gfx::Rect(0,
51 screen_bounds.bottom() - kHomeCardHeight,
52 screen_bounds.width(),
53 screen_bounds.height());
54 case HomeCard::VISIBLE_MINIMIZED:
55 return gfx::Rect(0,
56 screen_bounds.bottom() - kHomeCardMinimizedHeight,
57 screen_bounds.width(),
58 screen_bounds.height());
61 NOTREACHED();
62 return gfx::Rect();
65 } // namespace
67 // Makes sure the homecard is center-aligned horizontally and bottom-aligned
68 // vertically.
69 class HomeCardLayoutManager : public aura::LayoutManager {
70 public:
71 HomeCardLayoutManager()
72 : home_card_(NULL),
73 minimized_layer_(NULL) {}
75 virtual ~HomeCardLayoutManager() {}
77 void Layout(bool animate, gfx::Tween::Type tween_type) {
78 // |home_card| could be detached from the root window (e.g. when it is being
79 // destroyed).
80 if (!home_card_ || !home_card_->IsVisible() || !home_card_->GetRootWindow())
81 return;
83 scoped_ptr<ui::ScopedLayerAnimationSettings> settings;
84 if (animate) {
85 settings.reset(new ui::ScopedLayerAnimationSettings(
86 home_card_->layer()->GetAnimator()));
87 settings->SetTweenType(tween_type);
89 SetChildBoundsDirect(home_card_, GetBoundsForState(
90 home_card_->GetRootWindow()->bounds(), HomeCard::Get()->GetState()));
93 void SetMinimizedLayer(ui::Layer* minimized_layer) {
94 minimized_layer_ = minimized_layer;
95 UpdateMinimizedHomeBounds();
98 private:
99 void UpdateMinimizedHomeBounds() {
100 gfx::Rect minimized_bounds = minimized_layer_->parent()->bounds();
101 minimized_bounds.set_y(
102 minimized_bounds.bottom() - kHomeCardMinimizedHeight);
103 minimized_bounds.set_height(kHomeCardMinimizedHeight);
104 minimized_layer_->SetBounds(minimized_bounds);
107 // aura::LayoutManager:
108 virtual void OnWindowResized() override {
109 Layout(false, gfx::Tween::LINEAR);
110 UpdateMinimizedHomeBounds();
112 virtual void OnWindowAddedToLayout(aura::Window* child) override {
113 if (!home_card_) {
114 home_card_ = child;
115 Layout(false, gfx::Tween::LINEAR);
118 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) override {
119 if (home_card_ == child)
120 home_card_ = NULL;
122 virtual void OnWindowRemovedFromLayout(aura::Window* child) override {
124 virtual void OnChildWindowVisibilityChanged(aura::Window* child,
125 bool visible) override {
126 if (home_card_ == child)
127 Layout(false, gfx::Tween::LINEAR);
129 virtual void SetChildBounds(aura::Window* child,
130 const gfx::Rect& requested_bounds) override {
131 SetChildBoundsDirect(child, requested_bounds);
134 aura::Window* home_card_;
135 ui::Layer* minimized_layer_;
137 DISALLOW_COPY_AND_ASSIGN(HomeCardLayoutManager);
140 // The container view of home card contents of each state.
141 class HomeCardView : public views::WidgetDelegateView {
142 public:
143 HomeCardView(app_list::AppListViewDelegate* view_delegate,
144 aura::Window* container,
145 HomeCardGestureManager::Delegate* gesture_delegate)
146 : gesture_delegate_(gesture_delegate) {
147 SetLayoutManager(new views::FillLayout());
148 // Ideally AppListMainView should be used here and have AthenaStartPageView
149 // as its child view, so that custom pages and apps grid are available in
150 // the home card.
151 // TODO(mukai): make it so after the detailed UI has been fixed.
152 main_view_ = new AthenaStartPageView(view_delegate);
153 AddChildView(main_view_);
156 void SetStateProgress(HomeCard::State from_state,
157 HomeCard::State to_state,
158 float progress) {
159 // TODO(mukai): not clear the focus, but simply close the virtual keyboard.
160 GetFocusManager()->ClearFocus();
161 if (from_state == HomeCard::VISIBLE_CENTERED)
162 main_view_->SetLayoutState(1.0f - progress);
163 else if (to_state == HomeCard::VISIBLE_CENTERED)
164 main_view_->SetLayoutState(progress);
165 UpdateShadow(true);
168 void SetStateWithAnimation(HomeCard::State state,
169 gfx::Tween::Type tween_type) {
170 UpdateShadow(state != HomeCard::VISIBLE_MINIMIZED);
171 if (state == HomeCard::VISIBLE_CENTERED)
172 main_view_->RequestFocusOnSearchBox();
173 else
174 GetWidget()->GetFocusManager()->ClearFocus();
176 main_view_->SetLayoutStateWithAnimation(
177 (state == HomeCard::VISIBLE_CENTERED) ? 1.0f : 0.0f, tween_type);
180 void ClearGesture() {
181 gesture_manager_.reset();
184 // views::View:
185 virtual void OnGestureEvent(ui::GestureEvent* event) override {
186 if (!gesture_manager_ &&
187 event->type() == ui::ET_GESTURE_SCROLL_BEGIN) {
188 gesture_manager_.reset(new HomeCardGestureManager(
189 gesture_delegate_,
190 GetWidget()->GetNativeWindow()->GetRootWindow()->bounds()));
193 if (gesture_manager_)
194 gesture_manager_->ProcessGestureEvent(event);
196 virtual bool OnMousePressed(const ui::MouseEvent& event) override {
197 if (HomeCard::Get()->GetState() == HomeCard::VISIBLE_MINIMIZED &&
198 event.IsLeftMouseButton() && event.GetClickCount() == 1) {
199 athena::WindowManager::Get()->ToggleOverview();
200 return true;
202 return false;
205 private:
206 void UpdateShadow(bool should_show) {
207 wm::SetShadowType(
208 GetWidget()->GetNativeWindow(),
209 should_show ? wm::SHADOW_TYPE_RECTANGULAR : wm::SHADOW_TYPE_NONE);
212 // views::WidgetDelegate:
213 virtual views::View* GetContentsView() override {
214 return this;
217 AthenaStartPageView* main_view_;
218 scoped_ptr<HomeCardGestureManager> gesture_manager_;
219 HomeCardGestureManager::Delegate* gesture_delegate_;
221 DISALLOW_COPY_AND_ASSIGN(HomeCardView);
224 HomeCardImpl::HomeCardImpl(scoped_ptr<AppModelBuilder> model_builder,
225 scoped_ptr<SearchControllerFactory> search_factory)
226 : model_builder_(model_builder.Pass()),
227 search_factory_(search_factory.Pass()),
228 state_(HIDDEN),
229 original_state_(VISIBLE_MINIMIZED),
230 home_card_widget_(NULL),
231 home_card_view_(NULL),
232 layout_manager_(NULL),
233 activation_client_(NULL) {
234 DCHECK(!instance);
235 instance = this;
236 WindowManager::Get()->AddObserver(this);
239 HomeCardImpl::~HomeCardImpl() {
240 DCHECK(instance);
241 WindowManager::Get()->RemoveObserver(this);
242 if (activation_client_)
243 activation_client_->RemoveObserver(this);
244 home_card_widget_->CloseNow();
246 // Reset the view delegate first as it access search provider during
247 // shutdown.
248 view_delegate_.reset();
249 instance = NULL;
252 void HomeCardImpl::Init() {
253 InstallAccelerators();
254 ScreenManager::ContainerParams params("HomeCardContainer", CP_HOME_CARD);
255 params.can_activate_children = true;
256 aura::Window* container = ScreenManager::Get()->CreateContainer(params);
257 layout_manager_ = new HomeCardLayoutManager();
259 container->SetLayoutManager(layout_manager_);
260 wm::SetChildWindowVisibilityChangesAnimated(container);
262 view_delegate_.reset(
263 new AppListViewDelegate(model_builder_.get(), search_factory_.get()));
265 home_card_view_ = new HomeCardView(view_delegate_.get(), container, this);
266 home_card_widget_ = new views::Widget();
267 views::Widget::InitParams widget_params(
268 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
269 widget_params.parent = container;
270 widget_params.delegate = home_card_view_;
271 widget_params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
272 home_card_widget_->Init(widget_params);
274 minimized_home_ = CreateMinimizedHome();
275 container->layer()->Add(minimized_home_->layer());
276 container->layer()->StackAtTop(minimized_home_->layer());
277 layout_manager_->SetMinimizedLayer(minimized_home_->layer());
279 SetState(VISIBLE_MINIMIZED);
280 home_card_view_->Layout();
282 activation_client_ =
283 aura::client::GetActivationClient(container->GetRootWindow());
284 if (activation_client_)
285 activation_client_->AddObserver(this);
287 AthenaEnv::Get()->SetDisplayWorkAreaInsets(
288 gfx::Insets(0, 0, kHomeCardMinimizedHeight, 0));
291 aura::Window* HomeCardImpl::GetHomeCardWindowForTest() const {
292 return home_card_widget_ ? home_card_widget_->GetNativeWindow() : NULL;
295 void HomeCardImpl::InstallAccelerators() {
296 const AcceleratorData accelerator_data[] = {
297 {TRIGGER_ON_PRESS, ui::VKEY_L, ui::EF_CONTROL_DOWN,
298 COMMAND_SHOW_HOME_CARD, AF_NONE},
300 AcceleratorManager::Get()->RegisterAccelerators(
301 accelerator_data, arraysize(accelerator_data), this);
304 void HomeCardImpl::SetState(HomeCard::State state) {
305 if (state_ == state)
306 return;
308 // Update |state_| before changing the visibility of the widgets, so that
309 // LayoutManager callbacks get the correct state.
310 HomeCard::State old_state = state_;
311 state_ = state;
312 original_state_ = state;
314 if (old_state == VISIBLE_MINIMIZED ||
315 state_ == VISIBLE_MINIMIZED) {
316 minimized_home_->layer()->SetVisible(true);
318 ui::ScopedLayerAnimationSettings settings(
319 minimized_home_->layer()->GetAnimator());
320 minimized_home_->layer()->SetVisible(state_ == VISIBLE_MINIMIZED);
321 minimized_home_->layer()->SetOpacity(
322 state_ == VISIBLE_MINIMIZED ? 1.0f : 0.0f);
325 if (state_ == HIDDEN) {
326 home_card_widget_->Hide();
327 } else {
328 if (state_ == VISIBLE_MINIMIZED)
329 home_card_widget_->ShowInactive();
330 else
331 home_card_widget_->Show();
332 home_card_view_->SetStateWithAnimation(state, gfx::Tween::EASE_IN_OUT);
333 layout_manager_->Layout(true, gfx::Tween::EASE_IN_OUT);
337 HomeCard::State HomeCardImpl::GetState() {
338 return state_;
341 void HomeCardImpl::UpdateVirtualKeyboardBounds(
342 const gfx::Rect& bounds) {
343 if (state_ == VISIBLE_MINIMIZED && !bounds.IsEmpty()) {
344 SetState(HIDDEN);
345 original_state_ = VISIBLE_MINIMIZED;
346 } else if (state_ == VISIBLE_BOTTOM && !bounds.IsEmpty()) {
347 SetState(VISIBLE_CENTERED);
348 original_state_ = VISIBLE_BOTTOM;
349 } else if (state_ != original_state_ && bounds.IsEmpty()) {
350 SetState(original_state_);
354 bool HomeCardImpl::IsCommandEnabled(int command_id) const {
355 return true;
358 bool HomeCardImpl::OnAcceleratorFired(int command_id,
359 const ui::Accelerator& accelerator) {
360 DCHECK_EQ(COMMAND_SHOW_HOME_CARD, command_id);
362 if (state_ == VISIBLE_CENTERED && original_state_ != VISIBLE_BOTTOM)
363 SetState(VISIBLE_MINIMIZED);
364 else if (state_ == VISIBLE_MINIMIZED)
365 SetState(VISIBLE_CENTERED);
366 return true;
369 void HomeCardImpl::OnGestureEnded(State final_state, bool is_fling) {
370 home_card_view_->ClearGesture();
371 if (state_ != final_state &&
372 (state_ == VISIBLE_MINIMIZED || final_state == VISIBLE_MINIMIZED)) {
373 SetState(final_state);
374 WindowManager::Get()->ToggleOverview();
375 } else {
376 state_ = final_state;
377 // When the animation happens after a fling, EASE_IN_OUT would cause weird
378 // slow-down right after the finger release because of slow-in. Therefore
379 // EASE_OUT is better.
380 gfx::Tween::Type tween_type =
381 is_fling ? gfx::Tween::EASE_OUT : gfx::Tween::EASE_IN_OUT;
382 home_card_view_->SetStateWithAnimation(state_, tween_type);
383 layout_manager_->Layout(true, tween_type);
387 void HomeCardImpl::OnGestureProgressed(
388 State from_state, State to_state, float progress) {
389 if (from_state == VISIBLE_MINIMIZED || to_state == VISIBLE_MINIMIZED) {
390 minimized_home_->layer()->SetVisible(true);
391 float opacity =
392 (from_state == VISIBLE_MINIMIZED) ? 1.0f - progress : progress;
393 minimized_home_->layer()->SetOpacity(opacity);
395 gfx::Rect screen_bounds =
396 home_card_widget_->GetNativeWindow()->GetRootWindow()->bounds();
397 home_card_widget_->SetBounds(gfx::Tween::RectValueBetween(
398 progress,
399 GetBoundsForState(screen_bounds, from_state),
400 GetBoundsForState(screen_bounds, to_state)));
402 home_card_view_->SetStateProgress(from_state, to_state, progress);
404 // TODO(mukai): signals the update to the window manager so that it shows the
405 // intermediate visual state of overview mode.
408 void HomeCardImpl::OnOverviewModeEnter() {
409 if (state_ == HIDDEN || state_ == VISIBLE_MINIMIZED)
410 SetState(VISIBLE_BOTTOM);
413 void HomeCardImpl::OnOverviewModeExit() {
414 SetState(VISIBLE_MINIMIZED);
417 void HomeCardImpl::OnSplitViewModeEnter() {
420 void HomeCardImpl::OnSplitViewModeExit() {
423 void HomeCardImpl::OnWindowActivated(aura::Window* gained_active,
424 aura::Window* lost_active) {
425 if (state_ != HIDDEN &&
426 gained_active != home_card_widget_->GetNativeWindow()) {
427 SetState(VISIBLE_MINIMIZED);
431 // static
432 HomeCard* HomeCard::Create(scoped_ptr<AppModelBuilder> model_builder,
433 scoped_ptr<SearchControllerFactory> search_factory) {
434 (new HomeCardImpl(model_builder.Pass(), search_factory.Pass()))->Init();
435 DCHECK(instance);
436 return instance;
439 // static
440 void HomeCard::Shutdown() {
441 DCHECK(instance);
442 delete instance;
443 instance = NULL;
446 // static
447 HomeCard* HomeCard::Get() {
448 DCHECK(instance);
449 return instance;
452 } // namespace athena