aura: Implement app list M19 mock.
[chromium-blink-merge.git] / ash / app_list / app_list.cc
blobda9384aae13d1b8b3864d842fd0eb403dbc158ce
1 // Copyright (c) 2012 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 "ash/app_list/app_list.h"
7 #include "ash/app_list/app_list_view.h"
8 #include "ash/shell_delegate.h"
9 #include "ash/shell.h"
10 #include "ash/shell_window_ids.h"
11 #include "ui/aura/event.h"
12 #include "ui/aura/root_window.h"
13 #include "ui/aura/window.h"
14 #include "ui/gfx/compositor/scoped_layer_animation_settings.h"
15 #include "ui/gfx/screen.h"
17 namespace ash {
18 namespace internal {
20 namespace {
22 // Gets preferred bounds of app list window.
23 gfx::Rect GetPreferredBounds() {
24 gfx::Point cursor = gfx::Screen::GetCursorScreenPoint();
25 // Use full monitor rect so that the app list shade goes behind the launcher.
26 return gfx::Screen::GetMonitorAreaNearestPoint(cursor);
29 ui::Layer* GetLayer(views::Widget* widget) {
30 return widget->GetNativeView()->layer();
33 } // namespace
35 ////////////////////////////////////////////////////////////////////////////////
36 // AppList, public:
38 AppList::AppList() : is_visible_(false), widget_(NULL) {
41 AppList::~AppList() {
42 ResetWidget();
45 void AppList::SetVisible(bool visible) {
46 if (visible == is_visible_)
47 return;
49 is_visible_ = visible;
51 if (widget_) {
52 ScheduleAnimation();
53 } else if (is_visible_) {
54 // AppListModel and AppListViewDelegate are owned by AppListView. They
55 // will be released with AppListView on close.
56 AppListView* app_list_view = new AppListView(
57 Shell::GetInstance()->delegate()->CreateAppListViewDelegate(),
58 GetPreferredBounds());
59 SetWidget(app_list_view->GetWidget());
63 bool AppList::IsVisible() {
64 return widget_ && widget_->IsVisible();
67 ////////////////////////////////////////////////////////////////////////////////
68 // AppList, private:
70 void AppList::SetWidget(views::Widget* widget) {
71 DCHECK(widget_ == NULL);
73 if (is_visible_) {
74 widget_ = widget;
75 widget_->AddObserver(this);
76 Shell::GetInstance()->AddRootWindowEventFilter(this);
77 Shell::GetRootWindow()->AddRootWindowObserver(this);
79 widget_->SetOpacity(0);
80 ScheduleAnimation();
82 widget_->Show();
83 widget_->Activate();
84 } else {
85 widget->Close();
89 void AppList::ResetWidget() {
90 if (!widget_)
91 return;
93 widget_->RemoveObserver(this);
94 GetLayer(widget_)->GetAnimator()->RemoveObserver(this);
95 Shell::GetInstance()->RemoveRootWindowEventFilter(this);
96 Shell::GetRootWindow()->RemoveRootWindowObserver(this);
97 widget_ = NULL;
100 void AppList::ScheduleAnimation() {
101 aura::Window* default_container = Shell::GetInstance()->GetContainer(
102 internal::kShellWindowId_DefaultContainer);
103 // |default_container| could be NULL during Shell shutdown.
104 if (!default_container)
105 return;
107 ui::Layer* layer = GetLayer(widget_);
109 // Stop observing previous animation.
110 StopObservingImplicitAnimations();
112 ui::ScopedLayerAnimationSettings app_list_animation(layer->GetAnimator());
113 app_list_animation.AddObserver(this);
114 layer->SetOpacity(is_visible_ ? 1.0 : 0.0);
116 ui::Layer* default_container_layer = default_container->layer();
117 ui::ScopedLayerAnimationSettings default_container_animation(
118 default_container_layer->GetAnimator());
119 app_list_animation.AddObserver(this);
120 default_container_layer->SetOpacity(is_visible_ ? 0.0 : 1.0);
123 ////////////////////////////////////////////////////////////////////////////////
124 // AppList, aura::EventFilter implementation:
126 bool AppList::PreHandleKeyEvent(aura::Window* target,
127 aura::KeyEvent* event) {
128 return false;
131 bool AppList::PreHandleMouseEvent(aura::Window* target,
132 aura::MouseEvent* event) {
133 if (widget_ && is_visible_ && event->type() == ui::ET_MOUSE_PRESSED) {
134 aura::MouseEvent translated(*event, target, widget_->GetNativeView());
135 if (!widget_->GetNativeView()->ContainsPoint(translated.location()))
136 SetVisible(false);
138 return false;
141 ui::TouchStatus AppList::PreHandleTouchEvent(aura::Window* target,
142 aura::TouchEvent* event) {
143 return ui::TOUCH_STATUS_UNKNOWN;
146 ui::GestureStatus AppList::PreHandleGestureEvent(
147 aura::Window* target,
148 aura::GestureEvent* event) {
149 return ui::GESTURE_STATUS_UNKNOWN;
152 ////////////////////////////////////////////////////////////////////////////////
153 // AppList, ura::RootWindowObserver implementation:
154 void AppList::OnRootWindowResized(const gfx::Size& new_size) {
155 if (widget_ && is_visible_)
156 widget_->SetBounds(gfx::Rect(new_size));
159 ////////////////////////////////////////////////////////////////////////////////
160 // AppList, ui::ImplicitAnimationObserver implementation:
162 void AppList::OnImplicitAnimationsCompleted() {
163 if (!is_visible_ )
164 widget_->Close();
167 ////////////////////////////////////////////////////////////////////////////////
168 // AppList, views::Widget::Observer implementation:
170 void AppList::OnWidgetClosing(views::Widget* widget) {
171 DCHECK(widget_ == widget);
172 if (is_visible_)
173 SetVisible(false);
174 ResetWidget();
177 void AppList::OnWidgetActivationChanged(views::Widget* widget, bool active) {
178 DCHECK(widget_ == widget);
179 if (widget_ && is_visible_ && !active)
180 SetVisible(false);
183 } // namespace internal
184 } // namespace ash