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