OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/launcher/launcher.h" | 5 #include "ash/launcher/launcher.h" |
6 | 6 |
7 #include "ash/focus_cycler.h" | 7 #include "ash/focus_cycler.h" |
8 #include "ash/launcher/launcher_delegate.h" | 8 #include "ash/launcher/launcher_delegate.h" |
9 #include "ash/launcher/launcher_model.h" | 9 #include "ash/launcher/launcher_model.h" |
10 #include "ash/launcher/launcher_view.h" | 10 #include "ash/launcher/launcher_view.h" |
11 #include "ash/shell.h" | 11 #include "ash/shell.h" |
12 #include "ash/shell_delegate.h" | 12 #include "ash/shell_delegate.h" |
13 #include "ash/shell_window_ids.h" | 13 #include "ash/shell_window_ids.h" |
14 #include "ash/wm/shelf_layout_manager.h" | |
15 #include "base/timer.h" | |
14 #include "ui/aura/window.h" | 16 #include "ui/aura/window.h" |
15 #include "ui/gfx/canvas.h" | 17 #include "ui/gfx/canvas.h" |
16 #include "ui/gfx/compositor/layer.h" | 18 #include "ui/gfx/compositor/layer.h" |
17 #include "ui/gfx/image/image.h" | 19 #include "ui/gfx/image/image.h" |
18 #include "ui/views/accessible_pane_view.h" | 20 #include "ui/views/accessible_pane_view.h" |
19 #include "ui/views/painter.h" | 21 #include "ui/views/background.h" |
20 #include "ui/views/widget/widget.h" | 22 #include "ui/views/widget/widget.h" |
21 #include "ui/views/widget/widget_delegate.h" | 23 #include "ui/views/widget/widget_delegate.h" |
22 | 24 |
23 namespace ash { | 25 namespace ash { |
24 | 26 |
27 namespace { | |
28 | |
29 // Duration of the background animation. | |
30 const int kBackgroundDurationMS = 1000; | |
31 | |
32 // Delay before showing the launcher after the mouse enters the view. | |
33 const int kShowDelayMS = 300; | |
34 | |
35 } | |
36 | |
25 // The contents view of the Widget. This view contains LauncherView and | 37 // The contents view of the Widget. This view contains LauncherView and |
26 // sizes it to the width of the widget minus the size of the status area. | 38 // sizes it to the width of the widget minus the size of the status area. |
27 class Launcher::DelegateView : public views::WidgetDelegate, | 39 class Launcher::DelegateView : public views::WidgetDelegate, |
28 public views::AccessiblePaneView { | 40 public views::AccessiblePaneView{ |
Ben Goodger (Google)
2012/03/21 05:16:50
you lost a space here
| |
29 public: | 41 public: |
30 explicit DelegateView(); | 42 explicit DelegateView(Launcher* launcher); |
31 virtual ~DelegateView(); | 43 virtual ~DelegateView(); |
32 | 44 |
33 void SetStatusWidth(int width); | 45 void SetStatusWidth(int width); |
34 int status_width() const { return status_width_; } | 46 int status_width() const { return status_width_; } |
35 | 47 |
36 void set_focus_cycler(const internal::FocusCycler* focus_cycler) { | 48 void set_focus_cycler(const internal::FocusCycler* focus_cycler) { |
37 focus_cycler_ = focus_cycler; | 49 focus_cycler_ = focus_cycler; |
38 } | 50 } |
39 | 51 |
40 // views::View overrides | 52 // views::View overrides |
41 virtual gfx::Size GetPreferredSize() OVERRIDE; | 53 virtual gfx::Size GetPreferredSize() OVERRIDE; |
42 virtual void Layout() OVERRIDE; | 54 virtual void Layout() OVERRIDE; |
55 virtual void OnMouseEntered(const views::MouseEvent& event) OVERRIDE; | |
56 virtual void OnMouseExited(const views::MouseEvent& event) OVERRIDE; | |
43 | 57 |
58 // views::WidgetDelegateView overrides: | |
44 virtual views::Widget* GetWidget() OVERRIDE { | 59 virtual views::Widget* GetWidget() OVERRIDE { |
45 return View::GetWidget(); | 60 return View::GetWidget(); |
46 } | 61 } |
47 | |
48 virtual const views::Widget* GetWidget() const OVERRIDE { | 62 virtual const views::Widget* GetWidget() const OVERRIDE { |
49 return View::GetWidget(); | 63 return View::GetWidget(); |
50 } | 64 } |
51 | |
52 // views::WidgetDelegateView overrides: | |
53 virtual bool CanActivate() const OVERRIDE { | 65 virtual bool CanActivate() const OVERRIDE { |
54 // We don't want mouse clicks to activate us, but we need to allow | 66 // We don't want mouse clicks to activate us, but we need to allow |
55 // activation when the user is using the keyboard (FocusCycler). | 67 // activation when the user is using the keyboard (FocusCycler). |
56 return focus_cycler_ && focus_cycler_->widget_activating() == GetWidget(); | 68 return focus_cycler_ && focus_cycler_->widget_activating() == GetWidget(); |
57 } | 69 } |
58 | 70 |
59 private: | 71 private: |
72 // Shows the launcher. | |
73 void ShowLauncher(); | |
74 | |
75 Launcher* launcher_; | |
76 | |
77 // Width of the status area. | |
60 int status_width_; | 78 int status_width_; |
79 | |
61 const internal::FocusCycler* focus_cycler_; | 80 const internal::FocusCycler* focus_cycler_; |
62 | 81 |
82 base::OneShotTimer<DelegateView> show_timer_; | |
83 | |
63 DISALLOW_COPY_AND_ASSIGN(DelegateView); | 84 DISALLOW_COPY_AND_ASSIGN(DelegateView); |
64 }; | 85 }; |
65 | 86 |
66 Launcher::DelegateView::DelegateView() | 87 Launcher::DelegateView::DelegateView(Launcher* launcher) |
67 : status_width_(0), | 88 : launcher_(launcher), |
89 status_width_(0), | |
68 focus_cycler_(NULL) { | 90 focus_cycler_(NULL) { |
91 set_notify_enter_exit_on_child(true); | |
69 } | 92 } |
70 | 93 |
71 Launcher::DelegateView::~DelegateView() { | 94 Launcher::DelegateView::~DelegateView() { |
72 } | 95 } |
73 | 96 |
74 void Launcher::SetFocusCycler(const internal::FocusCycler* focus_cycler) { | |
75 delegate_view_->set_focus_cycler(focus_cycler); | |
76 } | |
77 | |
78 void Launcher::DelegateView::SetStatusWidth(int width) { | 97 void Launcher::DelegateView::SetStatusWidth(int width) { |
79 if (status_width_ == width) | 98 if (status_width_ == width) |
80 return; | 99 return; |
81 | 100 |
82 status_width_ = width; | 101 status_width_ = width; |
83 Layout(); | 102 Layout(); |
84 } | 103 } |
85 | 104 |
86 gfx::Size Launcher::DelegateView::GetPreferredSize() { | 105 gfx::Size Launcher::DelegateView::GetPreferredSize() { |
87 return child_count() > 0 ? child_at(0)->GetPreferredSize() : gfx::Size(); | 106 return child_count() > 0 ? child_at(0)->GetPreferredSize() : gfx::Size(); |
88 } | 107 } |
89 | 108 |
90 void Launcher::DelegateView::Layout() { | 109 void Launcher::DelegateView::Layout() { |
91 if (child_count() == 0) | 110 if (child_count() == 0) |
92 return; | 111 return; |
93 child_at(0)->SetBounds(0, 0, std::max(0, width() - status_width_), height()); | 112 child_at(0)->SetBounds(0, 0, std::max(0, width() - status_width_), height()); |
94 } | 113 } |
95 | 114 |
115 void Launcher::DelegateView::OnMouseEntered(const views::MouseEvent& event) { | |
116 if (!show_timer_.IsRunning()) { | |
117 // The user may be trying to target a button near the bottom of the screen | |
118 // and accidentally moved into the launcher area. Delay showing. | |
119 show_timer_.Start(FROM_HERE, | |
120 base::TimeDelta::FromMilliseconds(kShowDelayMS), | |
121 this, &DelegateView::ShowLauncher); | |
122 } | |
123 } | |
124 | |
125 void Launcher::DelegateView::OnMouseExited(const views::MouseEvent& event) { | |
126 show_timer_.Stop(); | |
127 internal::ShelfLayoutManager* shelf = Shell::GetInstance()->shelf(); | |
128 shelf->SetState(shelf->visibility_state(), | |
129 internal::ShelfLayoutManager::AUTO_HIDE_HIDDEN); | |
130 } | |
131 | |
132 void Launcher::DelegateView::ShowLauncher() { | |
133 show_timer_.Stop(); | |
134 internal::ShelfLayoutManager* shelf = Shell::GetInstance()->shelf(); | |
135 shelf->SetState(shelf->visibility_state(), | |
136 internal::ShelfLayoutManager::AUTO_HIDE_SHOWN); | |
137 } | |
138 | |
139 | |
140 // Launcher -------------------------------------------------------------------- | |
141 | |
96 Launcher::Launcher(aura::Window* window_container) | 142 Launcher::Launcher(aura::Window* window_container) |
97 : widget_(NULL), | 143 : widget_(NULL), |
98 window_container_(window_container), | 144 window_container_(window_container), |
99 delegate_view_(NULL), | 145 delegate_view_(NULL), |
100 launcher_view_(NULL) { | 146 launcher_view_(NULL), |
147 ALLOW_THIS_IN_INITIALIZER_LIST(background_animation_(this)), | |
148 renders_background_(false), | |
149 background_alpha_(0) { | |
101 model_.reset(new LauncherModel); | 150 model_.reset(new LauncherModel); |
102 if (Shell::GetInstance()->delegate()) { | 151 if (Shell::GetInstance()->delegate()) { |
103 delegate_.reset( | 152 delegate_.reset( |
104 Shell::GetInstance()->delegate()->CreateLauncherDelegate(model_.get())); | 153 Shell::GetInstance()->delegate()->CreateLauncherDelegate(model_.get())); |
105 } | 154 } |
106 | 155 |
107 widget_.reset(new views::Widget); | 156 widget_.reset(new views::Widget); |
108 views::Widget::InitParams params( | 157 views::Widget::InitParams params( |
109 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | 158 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
110 params.create_texture_for_layer = true; | 159 params.create_texture_for_layer = true; |
111 params.transparent = true; | 160 params.transparent = true; |
112 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 161 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
113 params.parent = Shell::GetInstance()->GetContainer( | 162 params.parent = Shell::GetInstance()->GetContainer( |
114 ash::internal::kShellWindowId_LauncherContainer); | 163 ash::internal::kShellWindowId_LauncherContainer); |
115 launcher_view_ = new internal::LauncherView(model_.get(), delegate_.get()); | 164 launcher_view_ = new internal::LauncherView(model_.get(), delegate_.get()); |
116 launcher_view_->Init(); | 165 launcher_view_->Init(); |
117 delegate_view_ = new DelegateView; | 166 delegate_view_ = new DelegateView(this); |
118 delegate_view_->AddChildView(launcher_view_); | 167 delegate_view_->AddChildView(launcher_view_); |
119 params.delegate = delegate_view_; | 168 params.delegate = delegate_view_; |
120 widget_->Init(params); | 169 widget_->Init(params); |
121 widget_->GetNativeWindow()->SetName("LauncherWindow"); | 170 widget_->GetNativeWindow()->SetName("LauncherWindow"); |
122 gfx::Size pref = | 171 gfx::Size pref = |
123 static_cast<views::View*>(launcher_view_)->GetPreferredSize(); | 172 static_cast<views::View*>(launcher_view_)->GetPreferredSize(); |
124 widget_->SetBounds(gfx::Rect(0, 0, pref.width(), pref.height())); | 173 widget_->SetBounds(gfx::Rect(0, 0, pref.width(), pref.height())); |
125 // The launcher should not take focus when it is initially shown. | 174 // The launcher should not take focus when it is initially shown. |
126 widget_->set_focus_on_creation(false); | 175 widget_->set_focus_on_creation(false); |
127 widget_->SetContentsView(delegate_view_); | 176 widget_->SetContentsView(delegate_view_); |
128 widget_->Show(); | 177 widget_->Show(); |
129 widget_->GetNativeView()->SetName("LauncherView"); | 178 widget_->GetNativeView()->SetName("LauncherView"); |
179 background_animation_.SetSlideDuration(kBackgroundDurationMS); | |
130 } | 180 } |
131 | 181 |
132 Launcher::~Launcher() { | 182 Launcher::~Launcher() { |
133 } | 183 } |
134 | 184 |
185 void Launcher::SetFocusCycler(const internal::FocusCycler* focus_cycler) { | |
186 delegate_view_->set_focus_cycler(focus_cycler); | |
187 } | |
188 | |
189 void Launcher::SetRendersBackground(bool value, BackgroundChangeSpeed speed) { | |
190 if (renders_background_ == value) | |
191 return; | |
192 renders_background_ = value; | |
193 if (speed == CHANGE_IMMEDIATE && !background_animation_.is_animating()) { | |
194 background_animation_.Reset(value ? 1.0f : 0.0f); | |
195 AnimationProgressed(&background_animation_); | |
196 return; | |
197 } | |
198 if (renders_background_) | |
199 background_animation_.Show(); | |
200 else | |
201 background_animation_.Hide(); | |
202 } | |
203 | |
135 void Launcher::SetStatusWidth(int width) { | 204 void Launcher::SetStatusWidth(int width) { |
136 delegate_view_->SetStatusWidth(width); | 205 delegate_view_->SetStatusWidth(width); |
137 } | 206 } |
138 | 207 |
139 int Launcher::GetStatusWidth() { | 208 int Launcher::GetStatusWidth() { |
140 return delegate_view_->status_width(); | 209 return delegate_view_->status_width(); |
141 } | 210 } |
142 | 211 |
143 gfx::Rect Launcher::GetScreenBoundsOfItemIconForWindow(aura::Window* window) { | 212 gfx::Rect Launcher::GetScreenBoundsOfItemIconForWindow(aura::Window* window) { |
144 if (!delegate_.get()) | 213 if (!delegate_.get()) |
145 return gfx::Rect(); | 214 return gfx::Rect(); |
146 | 215 |
147 LauncherID id = delegate_->GetIDByWindow(window); | 216 LauncherID id = delegate_->GetIDByWindow(window); |
148 gfx::Rect bounds(launcher_view_->GetIdealBoundsOfItemIcon(id)); | 217 gfx::Rect bounds(launcher_view_->GetIdealBoundsOfItemIcon(id)); |
149 if (bounds.IsEmpty()) | 218 if (bounds.IsEmpty()) |
150 return bounds; | 219 return bounds; |
151 | 220 |
152 gfx::Point screen_origin; | 221 gfx::Point screen_origin; |
153 views::View::ConvertPointToScreen(launcher_view_, &screen_origin); | 222 views::View::ConvertPointToScreen(launcher_view_, &screen_origin); |
154 return gfx::Rect(screen_origin.x() + bounds.x(), | 223 return gfx::Rect(screen_origin.x() + bounds.x(), |
155 screen_origin.y() + bounds.y(), | 224 screen_origin.y() + bounds.y(), |
156 bounds.width(), | 225 bounds.width(), |
157 bounds.height()); | 226 bounds.height()); |
158 } | 227 } |
159 | 228 |
160 internal::LauncherView* Launcher::GetLauncherViewForTest() { | 229 internal::LauncherView* Launcher::GetLauncherViewForTest() { |
161 return static_cast<internal::LauncherView*>( | 230 return static_cast<internal::LauncherView*>( |
162 widget_->GetContentsView()->child_at(0)); | 231 widget_->GetContentsView()->child_at(0)); |
163 } | 232 } |
233 | |
234 void Launcher::AnimationProgressed(const ui::Animation* animation) { | |
235 int alpha = animation->CurrentValueBetween(0, 128); | |
Ben Goodger (Google)
2012/03/21 05:16:50
kOnstant
| |
236 if (background_alpha_ == alpha) | |
237 return; | |
238 background_alpha_ = alpha; | |
239 if (alpha == 0) { | |
240 delegate_view_->set_background(NULL); | |
241 } else { | |
242 delegate_view_->set_background( | |
243 views::Background::CreateSolidBackground(0, 0, 0, alpha)); | |
244 } | |
245 delegate_view_->SchedulePaint(); | |
246 } | |
247 | |
164 } // namespace ash | 248 } // namespace ash |
OLD | NEW |