| OLD | NEW |
| (Empty) |
| 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/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" | |
| 26 | |
| 27 namespace athena { | |
| 28 namespace { | |
| 29 | |
| 30 HomeCard* instance = nullptr; | |
| 31 | |
| 32 gfx::Rect GetBoundsForState(const gfx::Rect& screen_bounds, | |
| 33 HomeCard::State state) { | |
| 34 switch (state) { | |
| 35 case HomeCard::HIDDEN: | |
| 36 break; | |
| 37 | |
| 38 case HomeCard::VISIBLE_CENTERED: | |
| 39 return screen_bounds; | |
| 40 | |
| 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: | |
| 44 return gfx::Rect(0, | |
| 45 screen_bounds.bottom() - kHomeCardHeight, | |
| 46 screen_bounds.width(), | |
| 47 screen_bounds.height()); | |
| 48 case HomeCard::VISIBLE_MINIMIZED: | |
| 49 return gfx::Rect(0, | |
| 50 screen_bounds.bottom() - kHomeCardMinimizedHeight, | |
| 51 screen_bounds.width(), | |
| 52 screen_bounds.height()); | |
| 53 } | |
| 54 | |
| 55 NOTREACHED(); | |
| 56 return gfx::Rect(); | |
| 57 } | |
| 58 | |
| 59 } // namespace | |
| 60 | |
| 61 // Makes sure the homecard is center-aligned horizontally and bottom-aligned | |
| 62 // vertically. | |
| 63 class HomeCardLayoutManager : public aura::LayoutManager { | |
| 64 public: | |
| 65 HomeCardLayoutManager() : home_card_(nullptr) {} | |
| 66 | |
| 67 ~HomeCardLayoutManager() override {} | |
| 68 | |
| 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 | |
| 71 // destroyed). | |
| 72 if (!home_card_ || !home_card_->IsVisible() || !home_card_->GetRootWindow()) | |
| 73 return; | |
| 74 | |
| 75 scoped_ptr<ui::ScopedLayerAnimationSettings> settings; | |
| 76 if (animate) { | |
| 77 settings.reset(new ui::ScopedLayerAnimationSettings( | |
| 78 home_card_->layer()->GetAnimator())); | |
| 79 settings->SetTweenType(tween_type); | |
| 80 } | |
| 81 SetChildBoundsDirect(home_card_, GetBoundsForState( | |
| 82 home_card_->GetRootWindow()->bounds(), HomeCard::Get()->GetState())); | |
| 83 } | |
| 84 | |
| 85 private: | |
| 86 // aura::LayoutManager: | |
| 87 void OnWindowResized() override { | |
| 88 Layout(false, gfx::Tween::LINEAR); | |
| 89 } | |
| 90 void OnWindowAddedToLayout(aura::Window* child) override { | |
| 91 if (!home_card_) { | |
| 92 home_card_ = child; | |
| 93 Layout(false, gfx::Tween::LINEAR); | |
| 94 } | |
| 95 } | |
| 96 void OnWillRemoveWindowFromLayout(aura::Window* child) override { | |
| 97 if (home_card_ == child) | |
| 98 home_card_ = nullptr; | |
| 99 } | |
| 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); | |
| 105 } | |
| 106 void SetChildBounds(aura::Window* child, | |
| 107 const gfx::Rect& requested_bounds) override { | |
| 108 SetChildBoundsDirect(child, requested_bounds); | |
| 109 } | |
| 110 | |
| 111 aura::Window* home_card_; | |
| 112 | |
| 113 DISALLOW_COPY_AND_ASSIGN(HomeCardLayoutManager); | |
| 114 }; | |
| 115 | |
| 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()), | |
| 120 state_(HIDDEN), | |
| 121 original_state_(VISIBLE_MINIMIZED), | |
| 122 home_card_widget_(nullptr), | |
| 123 home_card_view_(nullptr), | |
| 124 layout_manager_(nullptr) { | |
| 125 DCHECK(!instance); | |
| 126 instance = this; | |
| 127 WindowManager::Get()->AddObserver(this); | |
| 128 } | |
| 129 | |
| 130 HomeCardImpl::~HomeCardImpl() { | |
| 131 DCHECK(instance); | |
| 132 WindowManager::Get()->RemoveObserver(this); | |
| 133 home_card_widget_->CloseNow(); | |
| 134 | |
| 135 // Reset the view delegate first as it access search provider during | |
| 136 // shutdown. | |
| 137 view_delegate_->GetModel()->RemoveObserver(this); | |
| 138 view_delegate_.reset(); | |
| 139 instance = nullptr; | |
| 140 } | |
| 141 | |
| 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(); | |
| 148 | |
| 149 container->SetLayoutManager(layout_manager_); | |
| 150 wm::SetChildWindowVisibilityChangesAnimated(container); | |
| 151 | |
| 152 view_delegate_.reset( | |
| 153 new AppListViewDelegate(model_builder_.get(), search_factory_.get())); | |
| 154 | |
| 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 | |
| 166 // home card. | |
| 167 home_card_widget_->GetNativeWindow()->layer()->SetMasksToBounds(true); | |
| 168 | |
| 169 home_card_view_->Init(); | |
| 170 SetState(VISIBLE_MINIMIZED); | |
| 171 home_card_view_->Layout(); | |
| 172 | |
| 173 AthenaEnv::Get()->SetDisplayWorkAreaInsets( | |
| 174 gfx::Insets(0, 0, kHomeCardMinimizedHeight, 0)); | |
| 175 } | |
| 176 | |
| 177 aura::Window* HomeCardImpl::GetHomeCardWindowForTest() const { | |
| 178 return home_card_widget_ ? home_card_widget_->GetNativeWindow() : nullptr; | |
| 179 } | |
| 180 | |
| 181 void HomeCardImpl::ResetQuery() { | |
| 182 view_delegate_->GetModel()->search_box()->SetText(base::string16()); | |
| 183 } | |
| 184 | |
| 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}, | |
| 189 }; | |
| 190 AcceleratorManager::Get()->RegisterAccelerators( | |
| 191 accelerator_data, arraysize(accelerator_data), this); | |
| 192 } | |
| 193 | |
| 194 void HomeCardImpl::SetState(HomeCard::State state) { | |
| 195 if (state_ == state) | |
| 196 return; | |
| 197 | |
| 198 // Update |state_| before changing the visibility of the widgets, so that | |
| 199 // LayoutManager callbacks get the correct state. | |
| 200 state_ = state; | |
| 201 original_state_ = state; | |
| 202 | |
| 203 if (state_ == HIDDEN) { | |
| 204 home_card_widget_->Hide(); | |
| 205 } else { | |
| 206 if (state_ == VISIBLE_MINIMIZED) | |
| 207 home_card_widget_->ShowInactive(); | |
| 208 else | |
| 209 home_card_widget_->Show(); | |
| 210 | |
| 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( | |
| 214 state, | |
| 215 gfx::Tween::EASE_IN_OUT, | |
| 216 base::Bind(&HomeCardImpl::ResetQuery, base::Unretained(this))); | |
| 217 layout_manager_->Layout(true, gfx::Tween::EASE_IN_OUT); | |
| 218 } | |
| 219 } | |
| 220 | |
| 221 HomeCard::State HomeCardImpl::GetState() { | |
| 222 return state_; | |
| 223 } | |
| 224 | |
| 225 void HomeCardImpl::UpdateVirtualKeyboardBounds( | |
| 226 const gfx::Rect& bounds) { | |
| 227 if (state_ == VISIBLE_MINIMIZED && !bounds.IsEmpty()) { | |
| 228 SetState(HIDDEN); | |
| 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_); | |
| 235 } | |
| 236 } | |
| 237 | |
| 238 bool HomeCardImpl::IsCommandEnabled(int command_id) const { | |
| 239 return true; | |
| 240 } | |
| 241 | |
| 242 bool HomeCardImpl::OnAcceleratorFired(int command_id, | |
| 243 const ui::Accelerator& accelerator) { | |
| 244 DCHECK_EQ(COMMAND_SHOW_HOME_CARD, command_id); | |
| 245 | |
| 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(); | |
| 252 } | |
| 253 return true; | |
| 254 } | |
| 255 | |
| 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(); | |
| 263 else | |
| 264 WindowManager::Get()->EnterOverview(); | |
| 265 } else { | |
| 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( | |
| 273 state_, | |
| 274 tween_type, | |
| 275 base::Bind(&HomeCardImpl::ResetQuery, base::Unretained(this))); | |
| 276 layout_manager_->Layout(true, tween_type); | |
| 277 } | |
| 278 } | |
| 279 | |
| 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( | |
| 285 progress, | |
| 286 GetBoundsForState(screen_bounds, from_state), | |
| 287 GetBoundsForState(screen_bounds, to_state))); | |
| 288 | |
| 289 home_card_view_->SetStateProgress(from_state, to_state, progress); | |
| 290 | |
| 291 // TODO(mukai): signals the update to the window manager so that it shows the | |
| 292 // intermediate visual state of overview mode. | |
| 293 } | |
| 294 | |
| 295 void HomeCardImpl::OnOverviewModeEnter() { | |
| 296 if (state_ == HIDDEN || state_ == VISIBLE_MINIMIZED) | |
| 297 SetState(VISIBLE_BOTTOM); | |
| 298 } | |
| 299 | |
| 300 void HomeCardImpl::OnOverviewModeExit() { | |
| 301 SetState(VISIBLE_MINIMIZED); | |
| 302 } | |
| 303 | |
| 304 void HomeCardImpl::OnSplitViewModeEnter() { | |
| 305 } | |
| 306 | |
| 307 void HomeCardImpl::OnSplitViewModeExit() { | |
| 308 } | |
| 309 | |
| 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_); | |
| 315 | |
| 316 if (state_ == VISIBLE_BOTTOM) { | |
| 317 if (old_state == app_list::AppListModel::STATE_START) | |
| 318 SetState(VISIBLE_CENTERED); | |
| 319 else | |
| 320 DCHECK_EQ(app_list::AppListModel::STATE_START, new_state); | |
| 321 } | |
| 322 } | |
| 323 | |
| 324 // static | |
| 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(); | |
| 328 DCHECK(instance); | |
| 329 return instance; | |
| 330 } | |
| 331 | |
| 332 // static | |
| 333 void HomeCard::Shutdown() { | |
| 334 DCHECK(instance); | |
| 335 delete instance; | |
| 336 instance = nullptr; | |
| 337 } | |
| 338 | |
| 339 // static | |
| 340 HomeCard* HomeCard::Get() { | |
| 341 DCHECK(instance); | |
| 342 return instance; | |
| 343 } | |
| 344 | |
| 345 } // namespace athena | |
| OLD | NEW |