summaryrefslogtreecommitdiffstats
path: root/ash/wm/shelf_layout_manager.cc
blob: c747c0f4a94ae3bea8091dcbf18cb674d5daeb30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/wm/shelf_layout_manager.h"

#include "ash/launcher/launcher.h"
#include "ash/shell.h"
#include "base/auto_reset.h"
#include "ui/aura/root_window.h"
#include "ui/aura/screen_aura.h"
#include "ui/gfx/compositor/layer.h"
#include "ui/gfx/compositor/layer_animation_observer.h"
#include "ui/gfx/compositor/layer_animator.h"
#include "ui/gfx/compositor/scoped_layer_animation_settings.h"
#include "ui/views/widget/widget.h"

namespace ash {
namespace internal {

namespace {

ui::Layer* GetLayer(views::Widget* widget) {
  return widget->GetNativeView()->layer();
}

}  // namespace

////////////////////////////////////////////////////////////////////////////////
// ShelfLayoutManager, public:

ShelfLayoutManager::ShelfLayoutManager(views::Widget* launcher,
                                       views::Widget* status)
    : in_layout_(false),
      visible_(true),
      max_height_(-1),
      launcher_(launcher),
      status_(status) {
  gfx::Rect launcher_bounds = launcher->GetWindowScreenBounds();
  gfx::Rect status_bounds = status->GetWindowScreenBounds();
  max_height_ = std::max(launcher_bounds.height(), status_bounds.height());
}

ShelfLayoutManager::~ShelfLayoutManager() {
  // Without a shelf we don't need special insets anymore.
  aura::RootWindow::GetInstance()->
      screen()->set_work_area_insets(gfx::Insets());
}

void ShelfLayoutManager::LayoutShelf() {
  AutoReset<bool> auto_reset_in_layout(&in_layout_, true);
  StopAnimating();
  TargetBounds target_bounds;
  float target_opacity = visible_ ? 1.0f : 0.0f;
  CalculateTargetBounds(visible_, &target_bounds);
  GetLayer(launcher_)->SetOpacity(target_opacity);
  GetLayer(status_)->SetOpacity(target_opacity);
  launcher_->SetBounds(target_bounds.launcher_bounds);
  status_->SetBounds(target_bounds.status_bounds);
  Shell::GetInstance()->launcher()->SetStatusWidth(
      target_bounds.status_bounds.width());
  aura::RootWindow::GetInstance()->screen()->set_work_area_insets(
      target_bounds.work_area_insets);
}

void ShelfLayoutManager::SetVisible(bool visible) {
  ui::Layer* launcher_layer = GetLayer(launcher_);
  ui::Layer* status_layer = GetLayer(status_);

  // TODO(vollick): once visibility is animatable, use GetTargetVisibility.
  bool current_visibility = visible_ &&
      launcher_layer->GetTargetOpacity() > 0.0f &&
      status_layer->GetTargetOpacity() > 0.0f;

  if (visible == current_visibility)
    return;  // Nothing changed.

  StopAnimating();

  visible_ = visible;
  TargetBounds target_bounds;
  float target_opacity = visible ? 1.0f : 0.0f;
  CalculateTargetBounds(visible, &target_bounds);

  ui::ScopedLayerAnimationSettings launcher_animation_setter(
      launcher_layer->GetAnimator());
  ui::ScopedLayerAnimationSettings status_animation_setter(
      status_layer->GetAnimator());

  launcher_animation_setter.AddObserver(this);
  status_animation_setter.AddObserver(this);

  launcher_layer->SetBounds(target_bounds.launcher_bounds);
  launcher_layer->SetOpacity(target_opacity);
  status_layer->SetBounds(target_bounds.status_bounds);
  status_layer->SetOpacity(target_opacity);
}

////////////////////////////////////////////////////////////////////////////////
// ShelfLayoutManager, aura::LayoutManager implementation:

void ShelfLayoutManager::OnWindowResized() {
  LayoutShelf();
}

void ShelfLayoutManager::OnWindowAddedToLayout(aura::Window* child) {
}

void ShelfLayoutManager::OnWillRemoveWindowFromLayout(aura::Window* child) {
}

void ShelfLayoutManager::OnChildWindowVisibilityChanged(aura::Window* child,
                                                        bool visible) {
}

void ShelfLayoutManager::SetChildBounds(aura::Window* child,
                                        const gfx::Rect& requested_bounds) {
  SetChildBoundsDirect(child, requested_bounds);
  if (!in_layout_)
    LayoutShelf();
}

////////////////////////////////////////////////////////////////////////////////
// ShelfLayoutManager, private:

void ShelfLayoutManager::StopAnimating() {
  StopObservingImplicitAnimations();
  GetLayer(launcher_)->GetAnimator()->StopAnimating();
  GetLayer(status_)->GetAnimator()->StopAnimating();
}

void ShelfLayoutManager::CalculateTargetBounds(bool visible,
                                               TargetBounds* target_bounds) {
  const gfx::Rect& available_bounds(aura::RootWindow::GetInstance()->bounds());
  int y = available_bounds.bottom() - (visible ? max_height_ : 0);
  gfx::Rect status_bounds(status_->GetWindowScreenBounds());
  target_bounds->status_bounds = gfx::Rect(
      available_bounds.right() - status_bounds.width(),
      y + (max_height_ - status_bounds.height()) / 2,
      status_bounds.width(), status_bounds.height());
  gfx::Rect launcher_bounds(launcher_->GetWindowScreenBounds());
  target_bounds->launcher_bounds = gfx::Rect(
      available_bounds.x(), y + (max_height_ - launcher_bounds.height()) / 2,
      available_bounds.width(),
      launcher_bounds.height());
  if (visible)
    target_bounds->work_area_insets = gfx::Insets(0, 0, max_height_, 0);
}

void ShelfLayoutManager::OnImplicitAnimationsCompleted() {
  TargetBounds target_bounds;
  CalculateTargetBounds(visible_, &target_bounds);
  aura::RootWindow::GetInstance()->screen()->set_work_area_insets(
      target_bounds.work_area_insets);
}

}  // namespace internal
}  // namespace ash