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/home_card_view.h"
14 #include "athena/home/public/app_model_builder.h"
15 #include "athena/screen/public/screen_manager.h"
16 #include "athena/util/container_priorities.h"
17 #include "athena/wm/public/window_manager.h"
18 #include "ui/app_list/search_box_model.h"
19 #include "ui/aura/layout_manager.h"
20 #include "ui/aura/window.h"
21 #include "ui/compositor/layer.h"
22 #include "ui/compositor/scoped_layer_animation_settings.h"
23 #include "ui/gfx/animation/tween.h"
24 #include "ui/views/widget/widget.h"
25 #include "ui/wm/core/visibility_controller.h"
30 HomeCard
* instance
= nullptr;
32 gfx::Rect
GetBoundsForState(const gfx::Rect
& screen_bounds
,
33 HomeCard::State state
) {
35 case HomeCard::HIDDEN
:
38 case HomeCard::VISIBLE_CENTERED
:
41 // Do not change the home_card's size, only changes the top position
42 // instead, because size change causes unnecessary re-layouts.
43 case HomeCard::VISIBLE_BOTTOM
:
45 screen_bounds
.bottom() - kHomeCardHeight
,
46 screen_bounds
.width(),
47 screen_bounds
.height());
48 case HomeCard::VISIBLE_MINIMIZED
:
50 screen_bounds
.bottom() - kHomeCardMinimizedHeight
,
51 screen_bounds
.width(),
52 screen_bounds
.height());
61 // Makes sure the homecard is center-aligned horizontally and bottom-aligned
63 class HomeCardLayoutManager
: public aura::LayoutManager
{
65 HomeCardLayoutManager() : home_card_(nullptr) {}
67 ~HomeCardLayoutManager() override
{}
69 void Layout(bool animate
, gfx::Tween::Type tween_type
) {
70 // |home_card| could be detached from the root window (e.g. when it is being
72 if (!home_card_
|| !home_card_
->IsVisible() || !home_card_
->GetRootWindow())
75 scoped_ptr
<ui::ScopedLayerAnimationSettings
> settings
;
77 settings
.reset(new ui::ScopedLayerAnimationSettings(
78 home_card_
->layer()->GetAnimator()));
79 settings
->SetTweenType(tween_type
);
81 SetChildBoundsDirect(home_card_
, GetBoundsForState(
82 home_card_
->GetRootWindow()->bounds(), HomeCard::Get()->GetState()));
86 // aura::LayoutManager:
87 void OnWindowResized() override
{
88 Layout(false, gfx::Tween::LINEAR
);
90 void OnWindowAddedToLayout(aura::Window
* child
) override
{
93 Layout(false, gfx::Tween::LINEAR
);
96 void OnWillRemoveWindowFromLayout(aura::Window
* child
) override
{
97 if (home_card_
== child
)
100 void OnWindowRemovedFromLayout(aura::Window
* child
) override
{}
101 void OnChildWindowVisibilityChanged(aura::Window
* child
,
102 bool visible
) override
{
103 if (home_card_
== child
)
104 Layout(false, gfx::Tween::LINEAR
);
106 void SetChildBounds(aura::Window
* child
,
107 const gfx::Rect
& requested_bounds
) override
{
108 SetChildBoundsDirect(child
, requested_bounds
);
111 aura::Window
* home_card_
;
113 DISALLOW_COPY_AND_ASSIGN(HomeCardLayoutManager
);
116 HomeCardImpl::HomeCardImpl(scoped_ptr
<AppModelBuilder
> model_builder
,
117 scoped_ptr
<SearchControllerFactory
> search_factory
)
118 : model_builder_(model_builder
.Pass()),
119 search_factory_(search_factory
.Pass()),
121 original_state_(VISIBLE_MINIMIZED
),
122 home_card_widget_(nullptr),
123 home_card_view_(nullptr),
124 layout_manager_(nullptr) {
127 WindowManager::Get()->AddObserver(this);
130 HomeCardImpl::~HomeCardImpl() {
132 WindowManager::Get()->RemoveObserver(this);
133 home_card_widget_
->CloseNow();
135 // Reset the view delegate first as it access search provider during
137 view_delegate_
->GetModel()->RemoveObserver(this);
138 view_delegate_
.reset();
142 void HomeCardImpl::Init() {
143 InstallAccelerators();
144 ScreenManager::ContainerParams
params("HomeCardContainer", CP_HOME_CARD
);
145 params
.can_activate_children
= true;
146 aura::Window
* container
= ScreenManager::Get()->CreateContainer(params
);
147 layout_manager_
= new HomeCardLayoutManager();
149 container
->SetLayoutManager(layout_manager_
);
150 wm::SetChildWindowVisibilityChangesAnimated(container
);
152 view_delegate_
.reset(
153 new AppListViewDelegate(model_builder_
.get(), search_factory_
.get()));
155 view_delegate_
->GetModel()->AddObserver(this);
156 home_card_view_
= new HomeCardView(view_delegate_
.get(), this);
157 home_card_widget_
= new views::Widget();
158 views::Widget::InitParams
widget_params(
159 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS
);
160 widget_params
.parent
= container
;
161 widget_params
.delegate
= home_card_view_
;
162 widget_params
.opacity
= views::Widget::InitParams::TRANSLUCENT_WINDOW
;
163 home_card_widget_
->Init(widget_params
);
164 // AppListMainView in home card may move outside of home card layer partially
165 // in an transition animation. This flag is set to clip the parts outside of
167 home_card_widget_
->GetNativeWindow()->layer()->SetMasksToBounds(true);
169 home_card_view_
->Init();
170 SetState(VISIBLE_MINIMIZED
);
171 home_card_view_
->Layout();
173 AthenaEnv::Get()->SetDisplayWorkAreaInsets(
174 gfx::Insets(0, 0, kHomeCardMinimizedHeight
, 0));
177 aura::Window
* HomeCardImpl::GetHomeCardWindowForTest() const {
178 return home_card_widget_
? home_card_widget_
->GetNativeWindow() : nullptr;
181 void HomeCardImpl::ResetQuery() {
182 view_delegate_
->GetModel()->search_box()->SetText(base::string16());
185 void HomeCardImpl::InstallAccelerators() {
186 const AcceleratorData accelerator_data
[] = {
187 {TRIGGER_ON_PRESS
, ui::VKEY_L
, ui::EF_CONTROL_DOWN
,
188 COMMAND_SHOW_HOME_CARD
, AF_NONE
},
190 AcceleratorManager::Get()->RegisterAccelerators(
191 accelerator_data
, arraysize(accelerator_data
), this);
194 void HomeCardImpl::SetState(HomeCard::State state
) {
198 // Update |state_| before changing the visibility of the widgets, so that
199 // LayoutManager callbacks get the correct state.
201 original_state_
= state
;
203 if (state_
== HIDDEN
) {
204 home_card_widget_
->Hide();
206 if (state_
== VISIBLE_MINIMIZED
)
207 home_card_widget_
->ShowInactive();
209 home_card_widget_
->Show();
211 // Query should be reset on state change to reset the main_view. Also it's
212 // not possible to invoke ResetQuery() here, it causes a crash on search.
213 home_card_view_
->SetStateWithAnimation(
215 gfx::Tween::EASE_IN_OUT
,
216 base::Bind(&HomeCardImpl::ResetQuery
, base::Unretained(this)));
217 layout_manager_
->Layout(true, gfx::Tween::EASE_IN_OUT
);
221 HomeCard::State
HomeCardImpl::GetState() {
225 void HomeCardImpl::UpdateVirtualKeyboardBounds(
226 const gfx::Rect
& bounds
) {
227 if (state_
== VISIBLE_MINIMIZED
&& !bounds
.IsEmpty()) {
229 original_state_
= VISIBLE_MINIMIZED
;
230 } else if (state_
== VISIBLE_BOTTOM
&& !bounds
.IsEmpty()) {
231 SetState(VISIBLE_CENTERED
);
232 original_state_
= VISIBLE_BOTTOM
;
233 } else if (state_
!= original_state_
&& bounds
.IsEmpty()) {
234 SetState(original_state_
);
238 bool HomeCardImpl::IsCommandEnabled(int command_id
) const {
242 bool HomeCardImpl::OnAcceleratorFired(int command_id
,
243 const ui::Accelerator
& accelerator
) {
244 DCHECK_EQ(COMMAND_SHOW_HOME_CARD
, command_id
);
246 if (state_
== VISIBLE_CENTERED
&& original_state_
!= VISIBLE_BOTTOM
) {
247 SetState(VISIBLE_MINIMIZED
);
248 WindowManager::Get()->ExitOverview();
249 } else if (state_
== VISIBLE_MINIMIZED
) {
250 SetState(VISIBLE_CENTERED
);
251 WindowManager::Get()->EnterOverview();
256 void HomeCardImpl::OnGestureEnded(State final_state
, bool is_fling
) {
257 home_card_view_
->ClearGesture();
258 if (state_
!= final_state
&&
259 (state_
== VISIBLE_MINIMIZED
|| final_state
== VISIBLE_MINIMIZED
)) {
260 SetState(final_state
);
261 if (WindowManager::Get()->IsOverviewModeActive())
262 WindowManager::Get()->ExitOverview();
264 WindowManager::Get()->EnterOverview();
266 state_
= final_state
;
267 // When the animation happens after a fling, EASE_IN_OUT would cause weird
268 // slow-down right after the finger release because of slow-in. Therefore
269 // EASE_OUT is better.
270 gfx::Tween::Type tween_type
=
271 is_fling
? gfx::Tween::EASE_OUT
: gfx::Tween::EASE_IN_OUT
;
272 home_card_view_
->SetStateWithAnimation(
275 base::Bind(&HomeCardImpl::ResetQuery
, base::Unretained(this)));
276 layout_manager_
->Layout(true, tween_type
);
280 void HomeCardImpl::OnGestureProgressed(
281 State from_state
, State to_state
, float progress
) {
282 gfx::Rect screen_bounds
=
283 home_card_widget_
->GetNativeWindow()->GetRootWindow()->bounds();
284 home_card_widget_
->SetBounds(gfx::Tween::RectValueBetween(
286 GetBoundsForState(screen_bounds
, from_state
),
287 GetBoundsForState(screen_bounds
, to_state
)));
289 home_card_view_
->SetStateProgress(from_state
, to_state
, progress
);
291 // TODO(mukai): signals the update to the window manager so that it shows the
292 // intermediate visual state of overview mode.
295 void HomeCardImpl::OnOverviewModeEnter() {
296 if (state_
== HIDDEN
|| state_
== VISIBLE_MINIMIZED
)
297 SetState(VISIBLE_BOTTOM
);
300 void HomeCardImpl::OnOverviewModeExit() {
301 SetState(VISIBLE_MINIMIZED
);
304 void HomeCardImpl::OnSplitViewModeEnter() {
307 void HomeCardImpl::OnSplitViewModeExit() {
310 void HomeCardImpl::OnAppListModelStateChanged(
311 app_list::AppListModel::State old_state
,
312 app_list::AppListModel::State new_state
) {
313 // State change should not happen in minimized mode.
314 DCHECK_NE(VISIBLE_MINIMIZED
, state_
);
316 if (state_
== VISIBLE_BOTTOM
) {
317 if (old_state
== app_list::AppListModel::STATE_START
)
318 SetState(VISIBLE_CENTERED
);
320 DCHECK_EQ(app_list::AppListModel::STATE_START
, new_state
);
325 HomeCard
* HomeCard::Create(scoped_ptr
<AppModelBuilder
> model_builder
,
326 scoped_ptr
<SearchControllerFactory
> search_factory
) {
327 (new HomeCardImpl(model_builder
.Pass(), search_factory
.Pass()))->Init();
333 void HomeCard::Shutdown() {
340 HomeCard
* HomeCard::Get() {
345 } // namespace athena