OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "ui/aura_shell/app_list/app_list.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/command_line.h" | |
9 #include "ui/aura/event.h" | |
10 #include "ui/aura/window.h" | |
11 #include "ui/aura_shell/app_list/app_list_model.h" | |
12 #include "ui/aura_shell/app_list/app_list_view.h" | |
13 #include "ui/aura_shell/aura_shell_switches.h" | |
14 #include "ui/aura_shell/shell_delegate.h" | |
15 #include "ui/aura_shell/shell.h" | |
16 #include "ui/aura_shell/shell_window_ids.h" | |
17 #include "ui/gfx/screen.h" | |
18 | |
19 namespace aura_shell { | |
20 namespace internal { | |
21 | |
22 namespace { | |
23 | |
24 // Gets preferred bounds of app list window in show/hide state. | |
25 gfx::Rect GetPreferredBounds(bool show) { | |
26 // The y-axis offset used at the beginning of showing animation. | |
27 static const int kMoveUpAnimationOffset = 50; | |
28 | |
29 gfx::Point cursor = gfx::Screen::GetCursorScreenPoint(); | |
30 gfx::Rect work_area = gfx::Screen::GetMonitorWorkAreaNearestPoint(cursor); | |
31 gfx::Rect widget_bounds(work_area); | |
32 widget_bounds.Inset(100, 100); | |
33 if (!show) | |
34 widget_bounds.Offset(0, kMoveUpAnimationOffset); | |
35 | |
36 return widget_bounds; | |
37 } | |
38 | |
39 ui::Layer* GetLayer(views::Widget* widget) { | |
40 return widget->GetNativeView()->layer(); | |
41 } | |
42 | |
43 } // namespace | |
44 | |
45 //////////////////////////////////////////////////////////////////////////////// | |
46 // AppList, public: | |
47 | |
48 AppList::AppList() | |
49 : aura::EventFilter(NULL), | |
50 is_visible_(false), | |
51 widget_(NULL), | |
52 ALLOW_THIS_IN_INITIALIZER_LIST(set_widget_factory_(this)) { | |
53 } | |
54 | |
55 AppList::~AppList() { | |
56 ResetWidget(); | |
57 } | |
58 | |
59 void AppList::SetVisible(bool visible) { | |
60 if (visible == is_visible_) | |
61 return; | |
62 | |
63 is_visible_ = visible; | |
64 | |
65 if (widget_) { | |
66 ScheduleAnimation(); | |
67 } else if (is_visible_ && !set_widget_factory_.HasWeakPtrs()) { | |
68 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
69 switches::kAuraViewsAppList)) { | |
70 scoped_ptr<AppListModel> model(new AppListModel); | |
71 Shell::GetInstance()->delegate()->BuildAppListModel(model.get()); | |
72 | |
73 // AppListModel and AppListViewDelegate are owned by AppListView. They | |
74 // will be released with AppListView on close. | |
75 new AppListView( | |
76 model.release(), | |
77 Shell::GetInstance()->delegate()->CreateAppListViewDelegate(), | |
78 GetPreferredBounds(false), | |
79 base::Bind(&AppList::SetWidget, set_widget_factory_.GetWeakPtr())); | |
80 } else { | |
81 Shell::GetInstance()->delegate()->RequestAppListWidget( | |
82 GetPreferredBounds(false), | |
83 base::Bind(&AppList::SetWidget, set_widget_factory_.GetWeakPtr())); | |
84 } | |
85 } | |
86 } | |
87 | |
88 bool AppList::IsVisible() { | |
89 return widget_ && widget_->IsVisible(); | |
90 } | |
91 | |
92 //////////////////////////////////////////////////////////////////////////////// | |
93 // AppList, private: | |
94 | |
95 void AppList::SetWidget(views::Widget* widget) { | |
96 DCHECK(widget_ == NULL); | |
97 set_widget_factory_.InvalidateWeakPtrs(); | |
98 | |
99 if (is_visible_) { | |
100 widget_ = widget; | |
101 widget_->AddObserver(this); | |
102 GetLayer(widget_)->GetAnimator()->AddObserver(this); | |
103 Shell::GetInstance()->AddRootWindowEventFilter(this); | |
104 | |
105 widget_->SetBounds(GetPreferredBounds(false)); | |
106 widget_->SetOpacity(0); | |
107 ScheduleAnimation(); | |
108 | |
109 widget_->Show(); | |
110 } else { | |
111 widget->Close(); | |
112 } | |
113 } | |
114 | |
115 void AppList::ResetWidget() { | |
116 if (!widget_) | |
117 return; | |
118 | |
119 widget_->RemoveObserver(this); | |
120 GetLayer(widget_)->GetAnimator()->RemoveObserver(this); | |
121 Shell::GetInstance()->RemoveRootWindowEventFilter(this); | |
122 widget_ = NULL; | |
123 } | |
124 | |
125 void AppList::ScheduleAnimation() { | |
126 ui::Layer* layer = GetLayer(widget_); | |
127 ui::LayerAnimator::ScopedSettings app_list_animation(layer->GetAnimator()); | |
128 layer->SetBounds(GetPreferredBounds(is_visible_)); | |
129 layer->SetOpacity(is_visible_ ? 1.0 : 0.0); | |
130 | |
131 ui::Layer* default_container_layer = Shell::GetInstance()->GetContainer( | |
132 internal::kShellWindowId_DefaultContainer)->layer(); | |
133 ui::LayerAnimator::ScopedSettings default_container_animation( | |
134 default_container_layer->GetAnimator()); | |
135 default_container_layer->SetOpacity(is_visible_ ? 0.0 : 1.0); | |
136 } | |
137 | |
138 //////////////////////////////////////////////////////////////////////////////// | |
139 // AppList, aura::EventFilter implementation: | |
140 | |
141 bool AppList::PreHandleKeyEvent(aura::Window* target, | |
142 aura::KeyEvent* event) { | |
143 return false; | |
144 } | |
145 | |
146 bool AppList::PreHandleMouseEvent(aura::Window* target, | |
147 aura::MouseEvent* event) { | |
148 if (widget_ && is_visible_ && event->type() == ui::ET_MOUSE_PRESSED) { | |
149 aura::MouseEvent translated(*event, target, widget_->GetNativeView()); | |
150 if (!widget_->GetNativeView()->ContainsPoint(translated.location())) | |
151 SetVisible(false); | |
152 } | |
153 return false; | |
154 } | |
155 | |
156 ui::TouchStatus AppList::PreHandleTouchEvent(aura::Window* target, | |
157 aura::TouchEvent* event) { | |
158 return ui::TOUCH_STATUS_UNKNOWN; | |
159 } | |
160 | |
161 //////////////////////////////////////////////////////////////////////////////// | |
162 // AppList, ui::LayerAnimationObserver implementation: | |
163 | |
164 void AppList::OnLayerAnimationEnded( | |
165 const ui::LayerAnimationSequence* sequence) { | |
166 if (!is_visible_ ) | |
167 widget_->Close(); | |
168 } | |
169 | |
170 void AppList::OnLayerAnimationAborted( | |
171 const ui::LayerAnimationSequence* sequence) { | |
172 } | |
173 | |
174 void AppList::OnLayerAnimationScheduled( | |
175 const ui::LayerAnimationSequence* sequence) { | |
176 } | |
177 | |
178 //////////////////////////////////////////////////////////////////////////////// | |
179 // AppList, views::Widget::Observer implementation: | |
180 | |
181 void AppList::OnWidgetClosing(views::Widget* widget) { | |
182 DCHECK(widget_ == widget); | |
183 if (is_visible_) | |
184 SetVisible(false); | |
185 ResetWidget(); | |
186 } | |
187 | |
188 void AppList::OnWidgetActivationChanged(views::Widget* widget, bool active) { | |
189 DCHECK(widget_ == widget); | |
190 if (widget_ && is_visible_ && !active) | |
191 SetVisible(false); | |
192 } | |
193 | |
194 } // namespace internal | |
195 } // namespace aura_shell | |
OLD | NEW |