Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(836)

Side by Side Diff: ash/shelf/shelf_widget.cc

Issue 2268823002: mash: Move more shelf files to ash/common/shelf. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix rebase. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ash/shelf/shelf_widget.h ('k') | ash/shelf/shelf_widget_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 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/common/focus_cycler.h"
8 #include "ash/common/material_design/material_design_controller.h"
9 #include "ash/common/session/session_state_delegate.h"
10 #include "ash/common/shelf/shelf_background_animator_observer.h"
11 #include "ash/common/shelf/shelf_constants.h"
12 #include "ash/common/shelf/shelf_delegate.h"
13 #include "ash/common/shelf/shelf_view.h"
14 #include "ash/common/shelf/wm_dimmer_view.h"
15 #include "ash/common/shelf/wm_shelf.h"
16 #include "ash/common/shelf/wm_shelf_util.h"
17 #include "ash/common/system/status_area_widget.h"
18 #include "ash/common/system/tray/system_tray_delegate.h"
19 #include "ash/common/wm_root_window_controller.h"
20 #include "ash/common/wm_shell.h"
21 #include "ash/common/wm_window.h"
22 #include "ash/shelf/shelf_layout_manager.h"
23 #include "ash/wm/status_area_layout_manager.h"
24 #include "base/memory/ptr_util.h"
25 #include "grit/ash_resources.h"
26 #include "ui/base/resource/resource_bundle.h"
27 #include "ui/compositor/layer.h"
28 #include "ui/compositor/scoped_layer_animation_settings.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/layout/fill_layout.h"
35 #include "ui/views/widget/widget.h"
36 #include "ui/views/widget/widget_delegate.h"
37
38 namespace ash {
39
40 namespace {
41
42 // Size of black border at bottom (or side) of shelf.
43 const int kNumBlackPixels = 3;
44
45 } // namespace
46
47 // The contents view of the Shelf. This view contains ShelfView and
48 // sizes it to the width of the shelf minus the size of the status area.
49 class ShelfWidget::DelegateView : public views::WidgetDelegate,
50 public views::AccessiblePaneView,
51 public ShelfBackgroundAnimatorObserver {
52 public:
53 DelegateView(WmShelf* wm_shelf, ShelfWidget* shelf);
54 ~DelegateView() override;
55
56 void set_focus_cycler(FocusCycler* focus_cycler) {
57 focus_cycler_ = focus_cycler;
58 }
59 FocusCycler* focus_cycler() { return focus_cycler_; }
60
61 ui::Layer* opaque_background() { return &opaque_background_; }
62 ui::Layer* opaque_foreground() { return &opaque_foreground_; }
63
64 // Set if the shelf area is dimmed (eg when a window is maximized).
65 void SetDimmed(bool dimmed);
66 bool GetDimmed() const;
67
68 void SetParentLayer(ui::Layer* layer);
69
70 // views::View overrides:
71 void OnPaintBackground(gfx::Canvas* canvas) override;
72
73 // views::WidgetDelegateView overrides:
74 views::Widget* GetWidget() override { return View::GetWidget(); }
75 const views::Widget* GetWidget() const override { return View::GetWidget(); }
76
77 bool CanActivate() const override;
78 void ReorderChildLayers(ui::Layer* parent_layer) override;
79 // This will be called when the parent local bounds change.
80 void OnBoundsChanged(const gfx::Rect& old_bounds) override;
81
82 // ShelfBackgroundAnimatorObserver:
83 void UpdateShelfOpaqueBackground(int alpha) override;
84 void UpdateShelfAssetBackground(int alpha) override;
85
86 // Force the shelf to be presented in an undimmed state.
87 void ForceUndimming(bool force);
88
89 // A function to test the current alpha used by the dimming bar. If there is
90 // no dimmer active, the function will return -1.
91 int GetDimmingAlphaForTest();
92
93 // A function to test the bounds of the dimming bar. Returns gfx::Rect() if
94 // the dimmer is inactive.
95 gfx::Rect GetDimmerBoundsForTest();
96
97 // Disable dimming animations for running tests. This needs to be called
98 // prior to the creation of of the dimmer.
99 void disable_dimming_animations_for_test() {
100 disable_dimming_animations_for_test_ = true;
101 }
102
103 private:
104 WmShelf* wm_shelf_;
105 ShelfWidget* shelf_widget_;
106 FocusCycler* focus_cycler_;
107 int asset_background_alpha_;
108 // TODO(bruthig): Remove opaque_background_ (see https://crbug.com/621551).
109 // A black background layer which is shown when a maximized window is visible.
110 ui::Layer opaque_background_;
111 // A black foreground layer which is shown while transitioning between users.
112 // Note: Since the back- and foreground layers have different functions they
113 // can be used simultaneously - so no repurposing possible.
114 ui::Layer opaque_foreground_;
115
116 // The interface for the view which does the dimming. Null if the shelf is not
117 // being dimmed, or if dimming is not supported (e.g. for mus).
118 WmDimmerView* dimmer_view_;
119
120 // True if dimming animations should be turned off.
121 bool disable_dimming_animations_for_test_;
122
123 DISALLOW_COPY_AND_ASSIGN(DelegateView);
124 };
125
126 ShelfWidget::DelegateView::DelegateView(WmShelf* wm_shelf,
127 ShelfWidget* shelf_widget)
128 : wm_shelf_(wm_shelf),
129 shelf_widget_(shelf_widget),
130 focus_cycler_(nullptr),
131 asset_background_alpha_(0),
132 opaque_background_(ui::LAYER_SOLID_COLOR),
133 opaque_foreground_(ui::LAYER_SOLID_COLOR),
134 dimmer_view_(nullptr),
135 disable_dimming_animations_for_test_(false) {
136 DCHECK(wm_shelf_);
137 DCHECK(shelf_widget_);
138 SetLayoutManager(new views::FillLayout());
139 set_allow_deactivate_on_esc(true);
140 opaque_background_.SetColor(SK_ColorBLACK);
141 opaque_background_.SetBounds(GetLocalBounds());
142 opaque_background_.SetOpacity(0.0f);
143 opaque_foreground_.SetColor(SK_ColorBLACK);
144 opaque_foreground_.SetBounds(GetLocalBounds());
145 opaque_foreground_.SetOpacity(0.0f);
146 }
147
148 ShelfWidget::DelegateView::~DelegateView() {
149 // Make sure that the dimmer goes away since it might have set an observer.
150 SetDimmed(false);
151 }
152
153 void ShelfWidget::DelegateView::SetDimmed(bool dimmed) {
154 // When starting dimming, attempt to create a dimmer view.
155 if (dimmed) {
156 if (!dimmer_view_) {
157 dimmer_view_ =
158 wm_shelf_->CreateDimmerView(disable_dimming_animations_for_test_);
159 }
160 return;
161 }
162
163 // Close the dimmer widget when stopping dimming.
164 if (dimmer_view_) {
165 dimmer_view_->GetDimmerWidget()->CloseNow();
166 dimmer_view_ = nullptr;
167 }
168 }
169
170 bool ShelfWidget::DelegateView::GetDimmed() const {
171 return dimmer_view_ && dimmer_view_->GetDimmerWidget()->IsVisible();
172 }
173
174 void ShelfWidget::DelegateView::SetParentLayer(ui::Layer* layer) {
175 layer->Add(&opaque_background_);
176 layer->Add(&opaque_foreground_);
177 ReorderLayers();
178 }
179
180 void ShelfWidget::DelegateView::OnPaintBackground(gfx::Canvas* canvas) {
181 if (MaterialDesignController::IsShelfMaterial())
182 return;
183
184 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
185 gfx::ImageSkia shelf_background =
186 *rb->GetImageSkiaNamed(IDR_ASH_SHELF_BACKGROUND);
187 const bool horizontal = wm_shelf_->IsHorizontalAlignment();
188 if (!horizontal) {
189 shelf_background = gfx::ImageSkiaOperations::CreateRotatedImage(
190 shelf_background, wm_shelf_->GetAlignment() == SHELF_ALIGNMENT_LEFT
191 ? SkBitmapOperations::ROTATION_90_CW
192 : SkBitmapOperations::ROTATION_270_CW);
193 }
194 const gfx::Rect dock_bounds(
195 shelf_widget_->shelf_layout_manager()->dock_bounds());
196 SkPaint paint;
197 paint.setAlpha(asset_background_alpha_);
198 canvas->DrawImageInt(
199 shelf_background, 0, 0, shelf_background.width(),
200 shelf_background.height(),
201 (horizontal && dock_bounds.x() == 0 && dock_bounds.width() > 0)
202 ? dock_bounds.width()
203 : 0,
204 0, horizontal ? width() - dock_bounds.width() : width(), height(), false,
205 paint);
206 if (horizontal && dock_bounds.width() > 0) {
207 // The part of the shelf background that is in the corner below the docked
208 // windows close to the work area is an arched gradient that blends
209 // vertically oriented docked background and horizontal shelf.
210 gfx::ImageSkia shelf_corner = *rb->GetImageSkiaNamed(IDR_ASH_SHELF_CORNER);
211 if (dock_bounds.x() == 0) {
212 shelf_corner = gfx::ImageSkiaOperations::CreateRotatedImage(
213 shelf_corner, SkBitmapOperations::ROTATION_90_CW);
214 }
215 canvas->DrawImageInt(
216 shelf_corner, 0, 0, shelf_corner.width(), shelf_corner.height(),
217 dock_bounds.x() > 0 ? dock_bounds.x() : dock_bounds.width() - height(),
218 0, height(), height(), false, paint);
219 // The part of the shelf background that is just below the docked windows
220 // is drawn using the last (lowest) 1-pixel tall strip of the image asset.
221 // This avoids showing the border 3D shadow between the shelf and the
222 // dock.
223 canvas->DrawImageInt(shelf_background, 0, shelf_background.height() - 1,
224 shelf_background.width(), 1,
225 dock_bounds.x() > 0 ? dock_bounds.x() + height() : 0,
226 0, dock_bounds.width() - height(), height(), false,
227 paint);
228 }
229 gfx::Rect black_rect =
230 shelf_widget_->shelf_layout_manager()->SelectValueForShelfAlignment(
231 gfx::Rect(0, height() - kNumBlackPixels, width(), kNumBlackPixels),
232 gfx::Rect(0, 0, kNumBlackPixels, height()),
233 gfx::Rect(width() - kNumBlackPixels, 0, kNumBlackPixels, height()));
234 canvas->FillRect(black_rect, SK_ColorBLACK);
235 }
236
237 bool ShelfWidget::DelegateView::CanActivate() const {
238 // Allow to activate as fallback.
239 if (shelf_widget_->activating_as_fallback_)
240 return true;
241 // Allow to activate from the focus cycler.
242 if (focus_cycler_ && focus_cycler_->widget_activating() == GetWidget())
243 return true;
244 // Disallow activating in other cases, especially when using mouse.
245 return false;
246 }
247
248 void ShelfWidget::DelegateView::ReorderChildLayers(ui::Layer* parent_layer) {
249 views::View::ReorderChildLayers(parent_layer);
250 parent_layer->StackAtBottom(&opaque_background_);
251 parent_layer->StackAtTop(&opaque_foreground_);
252 }
253
254 void ShelfWidget::DelegateView::OnBoundsChanged(const gfx::Rect& old_bounds) {
255 opaque_background_.SetBounds(GetLocalBounds());
256 opaque_foreground_.SetBounds(GetLocalBounds());
257 if (dimmer_view_)
258 dimmer_view_->GetDimmerWidget()->SetBounds(GetBoundsInScreen());
259 }
260
261 void ShelfWidget::DelegateView::ForceUndimming(bool force) {
262 if (GetDimmed())
263 dimmer_view_->ForceUndimming(force);
264 }
265
266 int ShelfWidget::DelegateView::GetDimmingAlphaForTest() {
267 if (GetDimmed())
268 return dimmer_view_->GetDimmingAlphaForTest();
269 return -1;
270 }
271
272 gfx::Rect ShelfWidget::DelegateView::GetDimmerBoundsForTest() {
273 if (GetDimmed())
274 return dimmer_view_->GetDimmerWidget()->GetWindowBoundsInScreen();
275 return gfx::Rect();
276 }
277
278 void ShelfWidget::DelegateView::UpdateShelfOpaqueBackground(int alpha) {
279 const float kMaxAlpha = 255.0f;
280 opaque_background_.SetOpacity(alpha / kMaxAlpha);
281 }
282
283 void ShelfWidget::DelegateView::UpdateShelfAssetBackground(int alpha) {
284 asset_background_alpha_ = alpha;
285 SchedulePaint();
286 }
287
288 ShelfWidget::ShelfWidget(WmWindow* shelf_container,
289 WmWindow* status_container,
290 WmShelf* wm_shelf)
291 : wm_shelf_(wm_shelf),
292 shelf_(nullptr),
293 delegate_view_(new DelegateView(wm_shelf, this)),
294 shelf_view_(nullptr),
295 background_animator_(SHELF_BACKGROUND_DEFAULT, wm_shelf_),
296 activating_as_fallback_(false) {
297 background_animator_.AddObserver(this);
298 background_animator_.AddObserver(delegate_view_);
299
300 views::Widget::InitParams params(
301 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
302 params.name = "ShelfWidget";
303 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
304 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
305 params.delegate = delegate_view_;
306 shelf_container->GetRootWindowController()
307 ->ConfigureWidgetInitParamsForContainer(
308 this, shelf_container->GetShellWindowId(), &params);
309 Init(params);
310
311 // The shelf should not take focus when initially shown.
312 set_focus_on_creation(false);
313 SetContentsView(delegate_view_);
314 delegate_view_->SetParentLayer(GetLayer());
315
316 shelf_layout_manager_ = new ShelfLayoutManager(this);
317 shelf_layout_manager_->AddObserver(this);
318 shelf_container->SetLayoutManager(base::WrapUnique(shelf_layout_manager_));
319 background_animator_.PaintBackground(
320 shelf_layout_manager_->GetShelfBackgroundType(),
321 BACKGROUND_CHANGE_IMMEDIATE);
322 wm_shelf_->SetShelfLayoutManager(shelf_layout_manager_);
323
324 // TODO(jamescook): Move ownership to RootWindowController.
325 status_area_widget_ = new StatusAreaWidget(status_container, wm_shelf_);
326 status_area_widget_->CreateTrayViews();
327 if (WmShell::Get()->GetSessionStateDelegate()->IsActiveUserSessionStarted())
328 status_area_widget_->Show();
329 WmShell::Get()->focus_cycler()->AddWidget(status_area_widget_);
330 background_animator_.AddObserver(status_area_widget_);
331 status_container->SetLayoutManager(
332 base::MakeUnique<StatusAreaLayoutManager>(this));
333
334 views::Widget::AddObserver(this);
335 }
336
337 ShelfWidget::~ShelfWidget() {
338 // Must call Shutdown() before destruction.
339 DCHECK(!status_area_widget_);
340 WmShell::Get()->focus_cycler()->RemoveWidget(this);
341 SetFocusCycler(nullptr);
342 RemoveObserver(this);
343 background_animator_.RemoveObserver(delegate_view_);
344 background_animator_.RemoveObserver(this);
345 }
346
347 void ShelfWidget::SetPaintsBackground(
348 ShelfBackgroundType background_type,
349 BackgroundAnimatorChangeType change_type) {
350 background_animator_.PaintBackground(background_type, change_type);
351 }
352
353 ShelfBackgroundType ShelfWidget::GetBackgroundType() const {
354 return background_animator_.target_background_type();
355 }
356
357 void ShelfWidget::HideShelfBehindBlackBar(bool hide, int animation_time_ms) {
358 if (IsShelfHiddenBehindBlackBar() == hide)
359 return;
360
361 ui::Layer* opaque_foreground = delegate_view_->opaque_foreground();
362 float target_opacity = hide ? 1.0f : 0.0f;
363 std::unique_ptr<ui::ScopedLayerAnimationSettings> opaque_foreground_animation;
364 opaque_foreground_animation.reset(
365 new ui::ScopedLayerAnimationSettings(opaque_foreground->GetAnimator()));
366 opaque_foreground_animation->SetTransitionDuration(
367 base::TimeDelta::FromMilliseconds(animation_time_ms));
368 opaque_foreground_animation->SetPreemptionStrategy(
369 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS);
370
371 opaque_foreground->SetOpacity(target_opacity);
372 }
373
374 bool ShelfWidget::IsShelfHiddenBehindBlackBar() const {
375 return delegate_view_->opaque_foreground()->GetTargetOpacity() != 0.0f;
376 }
377
378 // static
379 bool ShelfWidget::ShelfAlignmentAllowed() {
380 if (WmShell::Get()->system_tray_delegate()->IsUserSupervised())
381 return false;
382
383 LoginStatus login_status =
384 WmShell::Get()->system_tray_delegate()->GetUserLoginStatus();
385
386 switch (login_status) {
387 case LoginStatus::LOCKED:
388 // Shelf alignment changes can be requested while being locked, but will
389 // be applied upon unlock.
390 case LoginStatus::USER:
391 case LoginStatus::OWNER:
392 return true;
393 case LoginStatus::PUBLIC:
394 case LoginStatus::SUPERVISED:
395 case LoginStatus::GUEST:
396 case LoginStatus::KIOSK_APP:
397 case LoginStatus::NOT_LOGGED_IN:
398 return false;
399 }
400
401 NOTREACHED();
402 return false;
403 }
404
405 ShelfAlignment ShelfWidget::GetAlignment() const {
406 return wm_shelf_->GetAlignment();
407 }
408
409 void ShelfWidget::OnShelfAlignmentChanged() {
410 status_area_widget_->SetShelfAlignment(GetAlignment());
411 delegate_view_->SchedulePaint();
412 }
413
414 void ShelfWidget::SetDimsShelf(bool dimming) {
415 delegate_view_->SetDimmed(dimming);
416 // Repaint all children, allowing updates to reflect dimmed state eg:
417 // status area background, app list button and overflow button.
418 if (shelf_view_)
419 shelf_view_->SchedulePaintForAllButtons();
420 status_area_widget_->SchedulePaint();
421 }
422
423 bool ShelfWidget::GetDimsShelf() const {
424 return delegate_view_->GetDimmed();
425 }
426
427 ShelfView* ShelfWidget::CreateShelfView() {
428 DCHECK(!shelf_);
429 DCHECK(!shelf_view_);
430
431 shelf_view_ =
432 new ShelfView(WmShell::Get()->shelf_model(),
433 WmShell::Get()->shelf_delegate(), wm_shelf_, this);
434 shelf_view_->Init();
435 GetContentsView()->AddChildView(shelf_view_);
436 return shelf_view_;
437 }
438
439 void ShelfWidget::PostCreateShelf() {
440 SetFocusCycler(WmShell::Get()->focus_cycler());
441
442 // Ensure the newly created |shelf_| gets current values.
443 background_animator_.Initialize(this);
444
445 shelf_view_->SetVisible(
446 WmShell::Get()->GetSessionStateDelegate()->IsActiveUserSessionStarted());
447 shelf_layout_manager_->LayoutShelf();
448 shelf_layout_manager_->UpdateAutoHideState();
449 Show();
450 }
451
452 bool ShelfWidget::IsShelfVisible() const {
453 return shelf_view_ && shelf_view_->visible();
454 }
455
456 void ShelfWidget::SetShelfVisibility(bool visible) {
457 if (shelf_view_)
458 shelf_view_->SetVisible(visible);
459 }
460
461 bool ShelfWidget::IsShowingContextMenu() const {
462 return shelf_view_ && shelf_view_->IsShowingMenu();
463 }
464
465 bool ShelfWidget::IsShowingOverflowBubble() const {
466 return shelf_view_ && shelf_view_->IsShowingOverflowBubble();
467 }
468
469 void ShelfWidget::SetFocusCycler(FocusCycler* focus_cycler) {
470 delegate_view_->set_focus_cycler(focus_cycler);
471 if (focus_cycler)
472 focus_cycler->AddWidget(this);
473 }
474
475 FocusCycler* ShelfWidget::GetFocusCycler() {
476 return delegate_view_->focus_cycler();
477 }
478
479 void ShelfWidget::Shutdown() {
480 // Tear down the dimmer before |shelf_layout_manager_|, since the dimmer uses
481 // |shelf_layout_manager_| to get the shelf's WmWindow, via WmShelf.
482 delegate_view_->SetDimmed(false);
483
484 // Shutting down the status area widget may cause some widgets (e.g. bubbles)
485 // to close, so uninstall the ShelfLayoutManager event filters first. Don't
486 // reset the pointer until later because other widgets (e.g. app list) may
487 // access it later in shutdown.
488 if (shelf_layout_manager_)
489 shelf_layout_manager_->PrepareForShutdown();
490
491 if (status_area_widget_) {
492 background_animator_.RemoveObserver(status_area_widget_);
493 WmShell::Get()->focus_cycler()->RemoveWidget(status_area_widget_);
494 status_area_widget_->Shutdown();
495 status_area_widget_ = nullptr;
496 }
497 }
498
499 void ShelfWidget::ForceUndimming(bool force) {
500 delegate_view_->ForceUndimming(force);
501 }
502
503 void ShelfWidget::OnWidgetActivationChanged(views::Widget* widget,
504 bool active) {
505 activating_as_fallback_ = false;
506 if (active)
507 delegate_view_->SetPaneFocusAndFocusDefault();
508 else
509 delegate_view_->GetFocusManager()->ClearFocus();
510 }
511
512 int ShelfWidget::GetDimmingAlphaForTest() {
513 if (delegate_view_)
514 return delegate_view_->GetDimmingAlphaForTest();
515 return -1;
516 }
517
518 gfx::Rect ShelfWidget::GetDimmerBoundsForTest() {
519 if (delegate_view_)
520 return delegate_view_->GetDimmerBoundsForTest();
521 return gfx::Rect();
522 }
523
524 void ShelfWidget::DisableDimmingAnimationsForTest() {
525 DCHECK(delegate_view_);
526 delegate_view_->disable_dimming_animations_for_test();
527 }
528
529 void ShelfWidget::UpdateShelfItemBackground(int alpha) {
530 if (shelf_view_)
531 shelf_view_->UpdateShelfItemBackground(alpha);
532 }
533
534 void ShelfWidget::WillDeleteShelfLayoutManager() {
535 shelf_layout_manager_->RemoveObserver(this);
536 shelf_layout_manager_ = nullptr;
537 }
538
539 } // namespace ash
OLDNEW
« no previous file with comments | « ash/shelf/shelf_widget.h ('k') | ash/shelf/shelf_widget_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698