forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus_area_widget_delegate.cc
278 lines (235 loc) · 9.56 KB
/
status_area_widget_delegate.cc
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// 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/system/status_area_widget_delegate.h"
#include "ash/focus_cycler.h"
#include "ash/root_window_controller.h"
#include "ash/shelf/shelf.h"
#include "ash/shelf/shelf_layout_manager.h"
#include "ash/shelf/shelf_widget.h"
#include "ash/shell.h"
#include "ash/system/status_area_widget.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/gfx/animation/tween.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/skia_paint_util.h"
#include "ui/views/accessible_pane_view.h"
#include "ui/views/background.h"
#include "ui/views/border.h"
#include "ui/views/layout/grid_layout.h"
namespace ash {
namespace {
constexpr int kPaddingBetweenItems = 8;
class StatusAreaWidgetDelegateAnimationSettings
: public ui::ScopedLayerAnimationSettings {
public:
explicit StatusAreaWidgetDelegateAnimationSettings(ui::Layer* layer)
: ui::ScopedLayerAnimationSettings(layer->GetAnimator()) {
SetTransitionDuration(ShelfConfig::Get()->shelf_animation_duration());
SetPreemptionStrategy(ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
SetTweenType(gfx::Tween::EASE_OUT);
}
~StatusAreaWidgetDelegateAnimationSettings() override = default;
private:
DISALLOW_COPY_AND_ASSIGN(StatusAreaWidgetDelegateAnimationSettings);
};
// Gradient background for the status area shown when it overflows into the
// shelf.
class OverflowGradientBackground : public views::Background {
public:
explicit OverflowGradientBackground(Shelf* shelf) : shelf_(shelf) {}
OverflowGradientBackground(const OverflowGradientBackground&) = delete;
~OverflowGradientBackground() override = default;
OverflowGradientBackground& operator=(const OverflowGradientBackground&) =
delete;
// views::Background:
void Paint(gfx::Canvas* canvas, views::View* view) const override {
gfx::Rect bounds = view->GetContentsBounds();
SkColor shelf_background_color =
shelf_->shelf_widget()->GetShelfBackgroundColor();
cc::PaintFlags flags;
flags.setShader(gfx::CreateGradientShader(
gfx::Point(), gfx::Point(kStatusAreaOverflowGradientSize, 0),
SkColorSetA(shelf_background_color, 0), shelf_background_color));
canvas->DrawRect(bounds, flags);
}
private:
Shelf* shelf_;
};
} // namespace
StatusAreaWidgetDelegate::StatusAreaWidgetDelegate(Shelf* shelf)
: shelf_(shelf), focus_cycler_for_testing_(nullptr) {
DCHECK(shelf_);
set_owned_by_client();
SetOwnedByWidget(true);
// Allow the launcher to surrender the focus to another window upon
// navigation completion by the user.
set_allow_deactivate_on_esc(true);
SetPaintToLayer();
layer()->SetFillsBoundsOpaquely(false);
}
StatusAreaWidgetDelegate::~StatusAreaWidgetDelegate() = default;
void StatusAreaWidgetDelegate::SetFocusCyclerForTesting(
const FocusCycler* focus_cycler) {
focus_cycler_for_testing_ = focus_cycler;
}
bool StatusAreaWidgetDelegate::ShouldFocusOut(bool reverse) {
views::View* focused_view = GetFocusManager()->GetFocusedView();
return (reverse && focused_view == GetFirstFocusableChild()) ||
(!reverse && focused_view == GetLastFocusableChild());
}
void StatusAreaWidgetDelegate::OnStatusAreaCollapseStateChanged(
StatusAreaWidget::CollapseState new_collapse_state) {
switch (new_collapse_state) {
case StatusAreaWidget::CollapseState::EXPANDED:
SetBackground(std::make_unique<OverflowGradientBackground>(shelf_));
break;
case StatusAreaWidget::CollapseState::COLLAPSED:
case StatusAreaWidget::CollapseState::NOT_COLLAPSIBLE:
SetBackground(nullptr);
break;
}
}
views::View* StatusAreaWidgetDelegate::GetDefaultFocusableChild() {
return default_last_focusable_child_ ? GetLastFocusableChild()
: GetFirstFocusableChild();
}
const char* StatusAreaWidgetDelegate::GetClassName() const {
return "ash/StatusAreaWidgetDelegate";
}
views::Widget* StatusAreaWidgetDelegate::GetWidget() {
return View::GetWidget();
}
const views::Widget* StatusAreaWidgetDelegate::GetWidget() const {
return View::GetWidget();
}
void StatusAreaWidgetDelegate::OnGestureEvent(ui::GestureEvent* event) {
views::Widget* target_widget =
static_cast<views::View*>(event->target())->GetWidget();
Shelf* shelf = Shelf::ForWindow(target_widget->GetNativeWindow());
// Convert the event location from current view to screen, since swiping up on
// the shelf can open the fullscreen app list. Updating the bounds of the app
// list during dragging is based on screen coordinate space.
ui::GestureEvent event_in_screen(*event);
gfx::Point location_in_screen(event->location());
View::ConvertPointToScreen(this, &location_in_screen);
event_in_screen.set_location(location_in_screen);
if (shelf->ProcessGestureEvent(event_in_screen))
event->StopPropagation();
else
views::AccessiblePaneView::OnGestureEvent(event);
}
bool StatusAreaWidgetDelegate::CanActivate() const {
// We don't want mouse clicks to activate us, but we need to allow
// activation when the user is using the keyboard (FocusCycler).
const FocusCycler* focus_cycler = focus_cycler_for_testing_
? focus_cycler_for_testing_
: Shell::Get()->focus_cycler();
return focus_cycler->widget_activating() == GetWidget();
}
void StatusAreaWidgetDelegate::CalculateTargetBounds() {
// Use a grid layout so that the trays can be centered in each cell, and
// so that the widget gets laid out correctly when tray sizes change.
views::GridLayout* layout =
SetLayoutManager(std::make_unique<views::GridLayout>());
const auto it = std::find_if(children().crbegin(), children().crend(),
[](const View* v) { return v->GetVisible(); });
const View* last_visible_child = it == children().crend() ? nullptr : *it;
// Set the border for each child, with a different border for the edge child.
for (auto* child : children()) {
if (!child->GetVisible())
continue;
SetBorderOnChild(child, last_visible_child == child);
}
views::ColumnSet* columns = layout->AddColumnSet(0);
if (shelf_->IsHorizontalAlignment()) {
for (auto* child : children()) {
if (!child->GetVisible())
continue;
columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::FILL,
0, /* resize percent */
views::GridLayout::ColumnSize::kUsePreferred, 0, 0);
}
layout->StartRow(0, 0);
for (auto* child : children()) {
if (child->GetVisible())
layout->AddExistingView(child);
}
} else {
columns->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER,
0, /* resize percent */
views::GridLayout::ColumnSize::kUsePreferred, 0, 0);
for (auto* child : children()) {
if (!child->GetVisible())
continue;
layout->StartRow(0, 0);
layout->AddExistingView(child);
}
}
target_bounds_.set_size(GetPreferredSize());
}
gfx::Rect StatusAreaWidgetDelegate::GetTargetBounds() const {
return target_bounds_;
}
void StatusAreaWidgetDelegate::UpdateLayout(bool animate) {
if (animate) {
StatusAreaWidgetDelegateAnimationSettings settings(layer());
Layout();
} else {
Layout();
}
}
void StatusAreaWidgetDelegate::ChildPreferredSizeChanged(View* child) {
const gfx::Size current_size = size();
const gfx::Size new_size = GetPreferredSize();
if (new_size == current_size)
return;
// Need to re-layout the shelf when trays or items are added/removed.
// don't run uring login or unlock if the shelf container is animating.
std::unique_ptr<StatusAreaWidgetDelegateAnimationSettings> settings;
if (!shelf_->shelf_widget()
->GetNativeWindow()
->parent()
->layer()
->GetAnimator()
->is_animating()) {
settings =
std::make_unique<StatusAreaWidgetDelegateAnimationSettings>(layer());
}
shelf_->shelf_layout_manager()->LayoutShelf(/*animate=*/false);
}
void StatusAreaWidgetDelegate::ChildVisibilityChanged(View* child) {
shelf_->shelf_layout_manager()->LayoutShelf(/*animate=*/true);
}
void StatusAreaWidgetDelegate::SetBorderOnChild(views::View* child,
bool is_child_on_edge) {
const int vertical_padding =
(ShelfConfig::Get()->shelf_size() - kTrayItemSize) / 2;
// Edges for horizontal alignment (right-to-left, default).
int top_edge = vertical_padding;
int left_edge = 0;
int bottom_edge = vertical_padding;
// Add some extra space so that borders don't overlap. This padding between
// items also takes care of padding at the edge of the shelf (unless hotseat
// is enabled).
int right_edge = kPaddingBetweenItems;
if (is_child_on_edge) {
right_edge = ShelfConfig::Get()->control_button_edge_spacing(
true /* is_primary_axis_edge */);
}
// Swap edges if alignment is not horizontal (bottom-to-top).
if (!shelf_->IsHorizontalAlignment()) {
std::swap(top_edge, left_edge);
std::swap(bottom_edge, right_edge);
}
child->SetBorder(
views::CreateEmptyBorder(top_edge, left_edge, bottom_edge, right_edge));
// Layout on |child| needs to be updated based on new border value before
// displaying; otherwise |child| will be showing with old border size.
// Fix for crbug.com/623438.
child->Layout();
}
} // namespace ash