OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "ash/shelf/shelf_widget.h" | |
6 | |
7 #include "ash/focus_cycler.h" | |
8 #include "ash/launcher/launcher_delegate.h" | |
9 #include "ash/launcher/launcher_model.h" | |
10 #include "ash/launcher/launcher_navigator.h" | |
11 #include "ash/launcher/launcher_view.h" | |
12 #include "ash/root_window_controller.h" | |
13 #include "ash/shelf/shelf_layout_manager.h" | |
14 #include "ash/shelf/shelf_widget.h" | |
15 #include "ash/shell.h" | |
16 #include "ash/shell_delegate.h" | |
17 #include "ash/shell_window_ids.h" | |
18 #include "ash/wm/property_util.h" | |
19 #include "ash/wm/status_area_layout_manager.h" | |
20 #include "ash/wm/window_properties.h" | |
21 #include "ash/wm/workspace_controller.h" | |
22 #include "grit/ash_resources.h" | |
23 #include "ui/aura/client/activation_client.h" | |
24 #include "ui/aura/root_window.h" | |
25 #include "ui/aura/window.h" | |
26 #include "ui/aura/window_observer.h" | |
27 #include "ui/base/resource/resource_bundle.h" | |
28 #include "ui/compositor/layer.h" | |
29 #include "ui/gfx/canvas.h" | |
30 #include "ui/gfx/image/image.h" | |
31 #include "ui/gfx/image/image_skia_operations.h" | |
32 #include "ui/gfx/skbitmap_operations.h" | |
33 #include "ui/views/accessible_pane_view.h" | |
34 #include "ui/views/widget/widget.h" | |
35 #include "ui/views/widget/widget_delegate.h" | |
36 | |
37 namespace { | |
38 // Size of black border at bottom (or side) of launcher. | |
39 const int kNumBlackPixels = 3; | |
40 // Alpha to paint dimming image with. | |
41 const int kDimAlpha = 96; | |
42 } | |
43 | |
44 namespace ash { | |
45 | |
46 // The contents view of the Shelf. This view contains LauncherView and | |
47 // sizes it to the width of the shelf minus the size of the status area. | |
48 class ShelfWidget::DelegateView : public views::WidgetDelegate, | |
49 public views::AccessiblePaneView, | |
50 public internal::BackgroundAnimatorDelegate { | |
51 public: | |
52 explicit DelegateView(ShelfWidget* shelf); | |
53 virtual ~DelegateView(); | |
54 | |
55 void set_focus_cycler(internal::FocusCycler* focus_cycler) { | |
56 focus_cycler_ = focus_cycler; | |
57 } | |
58 internal::FocusCycler* focus_cycler() { | |
59 return focus_cycler_; | |
60 } | |
61 | |
62 // Set if the shelf area is dimmed (eg when a window is maximized). | |
63 void SetDimmed(bool dimmed) { | |
64 if (dimmed_ != dimmed) { | |
65 dimmed_ = dimmed; | |
66 SchedulePaint(); | |
67 } | |
68 } | |
69 bool GetDimmed() const { return dimmed_; } | |
70 | |
71 // views::View overrides: | |
72 void OnPaintBackground(gfx::Canvas* canvas) OVERRIDE; | |
73 | |
74 // views::WidgetDelegateView overrides: | |
75 views::Widget* GetWidget() OVERRIDE { | |
76 return View::GetWidget(); | |
77 } | |
78 const views::Widget* GetWidget() const OVERRIDE { | |
79 return View::GetWidget(); | |
80 } | |
81 | |
82 bool CanActivate() const OVERRIDE; | |
83 void Layout() OVERRIDE; | |
84 | |
85 // BackgroundAnimatorDelegate overrides: | |
86 void UpdateBackground(int alpha) OVERRIDE; | |
87 | |
88 private: | |
89 ShelfWidget* shelf_; | |
90 internal::FocusCycler* focus_cycler_; | |
91 int alpha_; | |
92 bool dimmed_; | |
93 | |
94 DISALLOW_COPY_AND_ASSIGN(DelegateView); | |
95 }; | |
96 | |
97 ShelfWidget::DelegateView::DelegateView(ShelfWidget* shelf) | |
98 : shelf_(shelf), | |
99 focus_cycler_(NULL), | |
100 alpha_(0), | |
101 dimmed_(false) { | |
102 } | |
103 | |
104 ShelfWidget::DelegateView::~DelegateView() { | |
105 } | |
106 | |
107 void ShelfWidget::DelegateView::OnPaintBackground(gfx::Canvas* canvas) { | |
108 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
109 gfx::ImageSkia launcher_background = | |
110 *rb.GetImageSkiaNamed(IDR_AURA_LAUNCHER_BACKGROUND); | |
111 if (SHELF_ALIGNMENT_BOTTOM != shelf_->GetAlignment()) | |
112 launcher_background = gfx::ImageSkiaOperations::CreateRotatedImage( | |
113 launcher_background, | |
114 shelf_->shelf_layout_manager()->SelectValueForShelfAlignment( | |
115 SkBitmapOperations::ROTATION_90_CW, | |
116 SkBitmapOperations::ROTATION_90_CW, | |
117 SkBitmapOperations::ROTATION_270_CW, | |
118 SkBitmapOperations::ROTATION_180_CW)); | |
119 | |
120 gfx::Rect black_rect = | |
121 shelf_->shelf_layout_manager()->SelectValueForShelfAlignment( | |
122 gfx::Rect(0, height() - kNumBlackPixels, width(), kNumBlackPixels), | |
123 gfx::Rect(0, 0, kNumBlackPixels, height()), | |
124 gfx::Rect(width() - kNumBlackPixels, 0, kNumBlackPixels, height()), | |
125 gfx::Rect(0, 0, width(), kNumBlackPixels)); | |
126 | |
127 SkPaint paint; | |
128 paint.setAlpha(alpha_); | |
129 canvas->DrawImageInt( | |
130 launcher_background, | |
131 0, 0, launcher_background.width(), launcher_background.height(), | |
132 0, 0, width(), height(), | |
133 false, | |
134 paint); | |
135 canvas->FillRect(black_rect, SK_ColorBLACK); | |
136 | |
137 if (dimmed_) { | |
138 gfx::ImageSkia background_image = | |
139 *rb.GetImageSkiaNamed(IDR_AURA_LAUNCHER_DIMMING); | |
140 if (SHELF_ALIGNMENT_BOTTOM != shelf_->GetAlignment()) | |
141 background_image = gfx::ImageSkiaOperations::CreateRotatedImage( | |
142 background_image, | |
143 shelf_->shelf_layout_manager()->SelectValueForShelfAlignment( | |
144 SkBitmapOperations::ROTATION_90_CW, | |
145 SkBitmapOperations::ROTATION_90_CW, | |
146 SkBitmapOperations::ROTATION_270_CW, | |
147 SkBitmapOperations::ROTATION_180_CW)); | |
148 | |
149 SkPaint paint; | |
150 paint.setAlpha(kDimAlpha); | |
151 canvas->DrawImageInt( | |
152 background_image, | |
153 0, 0, background_image.width(), background_image.height(), | |
154 0, 0, width(), height(), | |
155 false, | |
156 paint); | |
157 } | |
158 } | |
159 | |
160 bool ShelfWidget::DelegateView::CanActivate() const { | |
161 // Allow to activate as fallback. | |
162 if (shelf_->activating_as_fallback_) | |
163 return true; | |
164 // Allow to activate from the focus cycler. | |
165 if (focus_cycler_ && focus_cycler_->widget_activating() == GetWidget()) | |
166 return true; | |
167 // Disallow activating in other cases, especially when using mouse. | |
168 return false; | |
169 } | |
170 | |
171 void ShelfWidget::DelegateView::Layout() { | |
172 for(int i = 0; i < this->child_count(); ++i) { | |
James Cook
2013/03/06 02:39:03
nit: Don't need "this", just child_count()
| |
173 if (shelf_->shelf_layout_manager()->IsHorizontalAlignment()) { | |
174 child_at(i)->SetBounds(child_at(i)->x(), child_at(i)->y(), | |
175 child_at(i)->width(), height()); | |
176 } else { | |
177 child_at(i)->SetBounds(child_at(i)->x(), child_at(i)->y(), | |
178 width(), child_at(i)->height()); | |
179 } | |
180 } | |
181 } | |
182 | |
183 void ShelfWidget::DelegateView::UpdateBackground(int alpha) { | |
184 alpha_ = alpha; | |
185 SchedulePaint(); | |
186 } | |
187 | |
188 ShelfWidget::ShelfWidget( | |
189 aura::Window* shelf_container, | |
190 aura::Window* status_container, | |
191 internal::WorkspaceController* workspace_controller) : | |
192 launcher_(NULL), | |
193 delegate_view_(new DelegateView(this)), | |
194 background_animator_(delegate_view_, 0, kLauncherBackgroundAlpha), | |
195 activating_as_fallback_(false), | |
196 window_container_(shelf_container) { | |
197 views::Widget::InitParams params( | |
198 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | |
199 params.transparent = true; | |
200 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
201 params.parent = shelf_container; | |
202 params.delegate = delegate_view_; | |
203 Init(params); | |
204 | |
205 // The shelf should not take focus when initially shown. | |
206 set_focus_on_creation(false); | |
207 SetContentsView(delegate_view_); | |
208 | |
209 status_area_widget_ = new internal::StatusAreaWidget(status_container); | |
210 status_area_widget_->CreateTrayViews(); | |
211 if (Shell::GetInstance()->delegate()->IsSessionStarted()) | |
212 status_area_widget_->Show(); | |
213 Shell::GetInstance()->focus_cycler()->AddWidget(status_area_widget_); | |
214 | |
215 shelf_layout_manager_ = new internal::ShelfLayoutManager(this); | |
216 shelf_container->SetLayoutManager(shelf_layout_manager_); | |
217 shelf_layout_manager_->set_workspace_controller(workspace_controller); | |
218 workspace_controller->SetShelf(shelf_layout_manager_); | |
219 | |
220 status_container->SetLayoutManager( | |
221 new internal::StatusAreaLayoutManager(this)); | |
222 | |
223 views::Widget::AddObserver(this); | |
224 } | |
225 | |
226 ShelfWidget::~ShelfWidget() { | |
227 RemoveObserver(this); | |
228 } | |
229 | |
230 void ShelfWidget::SetPaintsBackground( | |
231 bool value, | |
232 internal::BackgroundAnimator::ChangeType change_type) { | |
233 background_animator_.SetPaintsBackground(value, change_type); | |
234 } | |
235 | |
236 ShelfAlignment ShelfWidget::GetAlignment() const { | |
237 return shelf_layout_manager_->GetAlignment(); | |
238 } | |
239 | |
240 void ShelfWidget::SetAlignment(ShelfAlignment alignment) { | |
241 shelf_layout_manager_->SetAlignment(alignment); | |
242 shelf_layout_manager_->LayoutShelf(); | |
243 delegate_view_->SchedulePaint(); | |
244 } | |
245 | |
246 void ShelfWidget::SetDimsShelf(bool dimming) { | |
247 delegate_view_->SetDimmed(dimming); | |
248 } | |
249 | |
250 bool ShelfWidget::GetDimsShelf() const { | |
251 return delegate_view_->GetDimmed(); | |
252 } | |
253 | |
254 void ShelfWidget::CreateLauncher() { | |
255 if (!launcher_.get()) { | |
256 Shell* shell = Shell::GetInstance(); | |
257 // This needs to be called before launcher_model(). | |
258 shell->GetLauncherDelegate(); | |
259 launcher_.reset(new Launcher(shell->launcher_model(), | |
260 shell->GetLauncherDelegate(), | |
261 this)); | |
262 | |
263 SetFocusCycler(shell->focus_cycler()); | |
264 | |
265 // Inform the root window controller. | |
266 internal::RootWindowController::ForWindow(window_container_)-> | |
267 OnLauncherCreated(); | |
268 | |
269 ShellDelegate* delegate = shell->delegate(); | |
270 if (delegate) | |
271 launcher_->SetVisible(delegate->IsSessionStarted()); | |
272 | |
273 Show(); | |
274 } | |
275 } | |
276 | |
277 bool ShelfWidget::IsLauncherVisible() const { | |
278 return launcher_.get() && launcher_->IsVisible(); | |
279 } | |
280 | |
281 void ShelfWidget::SetLauncherVisibility(bool visible) { | |
282 if (launcher_.get()) | |
283 launcher_->SetVisible(visible); | |
284 } | |
285 | |
286 void ShelfWidget::SetFocusCycler(internal::FocusCycler* focus_cycler) { | |
287 delegate_view_->set_focus_cycler(focus_cycler); | |
288 if (focus_cycler) | |
289 focus_cycler->AddWidget(this); | |
290 } | |
291 | |
292 internal::FocusCycler* ShelfWidget::GetFocusCycler() { | |
293 return delegate_view_->focus_cycler(); | |
294 } | |
295 | |
296 void ShelfWidget::OnWidgetActivationChanged(views::Widget* widget, | |
297 bool active) { | |
298 activating_as_fallback_ = false; | |
299 if (active) | |
300 delegate_view_->SetPaneFocusAndFocusDefault(); | |
301 else | |
302 delegate_view_->GetFocusManager()->ClearFocus(); | |
303 } | |
304 | |
305 } // namespace ash | |
306 | |
OLD | NEW |