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

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

Issue 2734653002: chromeos: Move files in //ash/common to //ash (Closed)
Patch Set: fix a11y tests, fix docs Created 3 years, 9 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/common/shelf/shelf_widget.h ('k') | ash/common/shelf/shelf_window_watcher.h » ('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/common/shelf/shelf_widget.h"
6
7 #include "ash/animation/animation_change_type.h"
8 #include "ash/common/focus_cycler.h"
9 #include "ash/common/session/session_state_delegate.h"
10 #include "ash/common/shelf/app_list_button.h"
11 #include "ash/common/shelf/shelf_background_animator_observer.h"
12 #include "ash/common/shelf/shelf_constants.h"
13 #include "ash/common/shelf/shelf_delegate.h"
14 #include "ash/common/shelf/shelf_layout_manager.h"
15 #include "ash/common/shelf/shelf_view.h"
16 #include "ash/common/shelf/wm_shelf.h"
17 #include "ash/common/shelf/wm_shelf_util.h"
18 #include "ash/common/system/status_area_layout_manager.h"
19 #include "ash/common/system/status_area_widget.h"
20 #include "ash/common/wm_shell.h"
21 #include "ash/common/wm_window.h"
22 #include "ash/root_window_controller.h"
23 #include "ash/wm/window_properties.h"
24 #include "base/memory/ptr_util.h"
25 #include "ui/compositor/layer.h"
26 #include "ui/compositor/scoped_layer_animation_settings.h"
27 #include "ui/gfx/skbitmap_operations.h"
28 #include "ui/views/accessible_pane_view.h"
29 #include "ui/views/layout/fill_layout.h"
30 #include "ui/views/widget/widget.h"
31 #include "ui/views/widget/widget_delegate.h"
32
33 namespace ash {
34
35 // The contents view of the Shelf. This view contains ShelfView and
36 // sizes it to the width of the shelf minus the size of the status area.
37 class ShelfWidget::DelegateView : public views::WidgetDelegate,
38 public views::AccessiblePaneView,
39 public ShelfBackgroundAnimatorObserver {
40 public:
41 explicit DelegateView(ShelfWidget* shelf);
42 ~DelegateView() override;
43
44 void set_focus_cycler(FocusCycler* focus_cycler) {
45 focus_cycler_ = focus_cycler;
46 }
47 FocusCycler* focus_cycler() { return focus_cycler_; }
48
49 ui::Layer* opaque_background() { return &opaque_background_; }
50 ui::Layer* opaque_foreground() { return &opaque_foreground_; }
51
52 void SetParentLayer(ui::Layer* layer);
53
54 // views::WidgetDelegateView overrides:
55 views::Widget* GetWidget() override { return View::GetWidget(); }
56 const views::Widget* GetWidget() const override { return View::GetWidget(); }
57
58 bool CanActivate() const override;
59 void ReorderChildLayers(ui::Layer* parent_layer) override;
60 // This will be called when the parent local bounds change.
61 void OnBoundsChanged(const gfx::Rect& old_bounds) override;
62
63 // ShelfBackgroundAnimatorObserver:
64 void UpdateShelfBackground(SkColor color) override;
65
66 private:
67 ShelfWidget* shelf_widget_;
68 FocusCycler* focus_cycler_;
69 // A black background layer that may be visible depending on a
70 // ShelfBackgroundAnimator.
71 // TODO(bruthig): Remove opaque_background_ (see https://crbug.com/621551).
72 ui::Layer opaque_background_;
73 // A black foreground layer which is shown while transitioning between users.
74 // Note: Since the back- and foreground layers have different functions they
75 // can be used simultaneously - so no repurposing possible.
76 ui::Layer opaque_foreground_;
77
78 DISALLOW_COPY_AND_ASSIGN(DelegateView);
79 };
80
81 ShelfWidget::DelegateView::DelegateView(ShelfWidget* shelf_widget)
82 : shelf_widget_(shelf_widget),
83 focus_cycler_(nullptr),
84 opaque_background_(ui::LAYER_SOLID_COLOR),
85 opaque_foreground_(ui::LAYER_SOLID_COLOR) {
86 DCHECK(shelf_widget_);
87 SetLayoutManager(new views::FillLayout());
88 set_allow_deactivate_on_esc(true);
89 opaque_background_.SetColor(SK_ColorBLACK);
90 opaque_background_.SetBounds(GetLocalBounds());
91 opaque_foreground_.SetColor(SK_ColorBLACK);
92 opaque_foreground_.SetBounds(GetLocalBounds());
93 opaque_foreground_.SetOpacity(0.0f);
94 }
95
96 ShelfWidget::DelegateView::~DelegateView() {}
97
98 void ShelfWidget::DelegateView::SetParentLayer(ui::Layer* layer) {
99 layer->Add(&opaque_background_);
100 layer->Add(&opaque_foreground_);
101 ReorderLayers();
102 }
103
104 bool ShelfWidget::DelegateView::CanActivate() const {
105 // Allow to activate as fallback.
106 if (shelf_widget_->activating_as_fallback_)
107 return true;
108 // Allow to activate from the focus cycler.
109 if (focus_cycler_ && focus_cycler_->widget_activating() == GetWidget())
110 return true;
111 // Disallow activating in other cases, especially when using mouse.
112 return false;
113 }
114
115 void ShelfWidget::DelegateView::ReorderChildLayers(ui::Layer* parent_layer) {
116 views::View::ReorderChildLayers(parent_layer);
117 parent_layer->StackAtBottom(&opaque_background_);
118 parent_layer->StackAtTop(&opaque_foreground_);
119 }
120
121 void ShelfWidget::DelegateView::OnBoundsChanged(const gfx::Rect& old_bounds) {
122 opaque_background_.SetBounds(GetLocalBounds());
123 opaque_foreground_.SetBounds(GetLocalBounds());
124 }
125
126 void ShelfWidget::DelegateView::UpdateShelfBackground(SkColor color) {
127 opaque_background_.SetColor(color);
128 }
129
130 ShelfWidget::ShelfWidget(WmWindow* shelf_container, WmShelf* wm_shelf)
131 : wm_shelf_(wm_shelf),
132 status_area_widget_(nullptr),
133 delegate_view_(new DelegateView(this)),
134 shelf_view_(nullptr),
135 background_animator_(SHELF_BACKGROUND_DEFAULT,
136 wm_shelf_,
137 WmShell::Get()->wallpaper_controller()),
138 activating_as_fallback_(false) {
139 DCHECK(wm_shelf_);
140 background_animator_.AddObserver(this);
141 background_animator_.AddObserver(delegate_view_);
142
143 views::Widget::InitParams params(
144 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
145 params.name = "ShelfWidget";
146 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
147 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
148 params.delegate = delegate_view_;
149 shelf_container->GetRootWindowController()
150 ->ConfigureWidgetInitParamsForContainer(
151 this, shelf_container->GetShellWindowId(), &params);
152 Init(params);
153
154 // The shelf should not take focus when initially shown.
155 set_focus_on_creation(false);
156 SetContentsView(delegate_view_);
157 delegate_view_->SetParentLayer(GetLayer());
158
159 shelf_layout_manager_ = new ShelfLayoutManager(this, wm_shelf_);
160 shelf_layout_manager_->AddObserver(this);
161 shelf_container->SetLayoutManager(base::WrapUnique(shelf_layout_manager_));
162 background_animator_.PaintBackground(
163 shelf_layout_manager_->GetShelfBackgroundType(),
164 AnimationChangeType::IMMEDIATE);
165
166 views::Widget::AddObserver(this);
167 }
168
169 ShelfWidget::~ShelfWidget() {
170 // Must call Shutdown() before destruction.
171 DCHECK(!status_area_widget_);
172 WmShell::Get()->focus_cycler()->RemoveWidget(this);
173 SetFocusCycler(nullptr);
174 RemoveObserver(this);
175 background_animator_.RemoveObserver(delegate_view_);
176 background_animator_.RemoveObserver(this);
177 }
178
179 void ShelfWidget::CreateStatusAreaWidget(WmWindow* status_container) {
180 DCHECK(status_container);
181 DCHECK(!status_area_widget_);
182 // TODO(jamescook): Move ownership to RootWindowController.
183 status_area_widget_ = new StatusAreaWidget(status_container, wm_shelf_);
184 status_area_widget_->CreateTrayViews();
185 if (WmShell::Get()->GetSessionStateDelegate()->IsActiveUserSessionStarted())
186 status_area_widget_->Show();
187 WmShell::Get()->focus_cycler()->AddWidget(status_area_widget_);
188 background_animator_.AddObserver(status_area_widget_);
189 status_container->SetLayoutManager(
190 base::MakeUnique<StatusAreaLayoutManager>(this));
191 }
192
193 void ShelfWidget::SetPaintsBackground(ShelfBackgroundType background_type,
194 AnimationChangeType change_type) {
195 background_animator_.PaintBackground(background_type, change_type);
196 }
197
198 ShelfBackgroundType ShelfWidget::GetBackgroundType() const {
199 return background_animator_.target_background_type();
200 }
201
202 void ShelfWidget::HideShelfBehindBlackBar(bool hide, int animation_time_ms) {
203 if (IsShelfHiddenBehindBlackBar() == hide)
204 return;
205
206 ui::Layer* opaque_foreground = delegate_view_->opaque_foreground();
207 float target_opacity = hide ? 1.0f : 0.0f;
208 std::unique_ptr<ui::ScopedLayerAnimationSettings> opaque_foreground_animation;
209 opaque_foreground_animation.reset(
210 new ui::ScopedLayerAnimationSettings(opaque_foreground->GetAnimator()));
211 opaque_foreground_animation->SetTransitionDuration(
212 base::TimeDelta::FromMilliseconds(animation_time_ms));
213 opaque_foreground_animation->SetPreemptionStrategy(
214 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS);
215
216 opaque_foreground->SetOpacity(target_opacity);
217 }
218
219 bool ShelfWidget::IsShelfHiddenBehindBlackBar() const {
220 return delegate_view_->opaque_foreground()->GetTargetOpacity() != 0.0f;
221 }
222
223 void ShelfWidget::OnShelfAlignmentChanged() {
224 shelf_view_->OnShelfAlignmentChanged();
225 // TODO(jamescook): Status area should not cache alignment.
226 status_area_widget_->SetShelfAlignment(wm_shelf_->GetAlignment());
227 delegate_view_->SchedulePaint();
228 }
229
230 ShelfView* ShelfWidget::CreateShelfView() {
231 DCHECK(!shelf_view_);
232
233 shelf_view_ =
234 new ShelfView(WmShell::Get()->shelf_model(),
235 WmShell::Get()->shelf_delegate(), wm_shelf_, this);
236 shelf_view_->Init();
237 GetContentsView()->AddChildView(shelf_view_);
238 return shelf_view_;
239 }
240
241 void ShelfWidget::PostCreateShelf() {
242 SetFocusCycler(WmShell::Get()->focus_cycler());
243
244 // Ensure the newly created |shelf_| gets current values.
245 background_animator_.NotifyObserver(this);
246
247 // TODO(jamescook): The IsActiveUserSessionStarted() check may not be needed
248 // because the shelf is only created after the first user session is active.
249 // The ShelfView seems to always be visible after login. At the lock screen
250 // the shelf is hidden because its container is hidden. During auto-hide it is
251 // hidden because ShelfWidget is transparent. Some of the ShelfView visibility
252 // code could be simplified. http://crbug.com/674773
253 shelf_view_->SetVisible(
254 WmShell::Get()->GetSessionStateDelegate()->IsActiveUserSessionStarted());
255 shelf_layout_manager_->LayoutShelf();
256 shelf_layout_manager_->UpdateAutoHideState();
257 Show();
258 }
259
260 bool ShelfWidget::IsShelfVisible() const {
261 return shelf_view_ && shelf_view_->visible();
262 }
263
264 bool ShelfWidget::IsShowingAppList() const {
265 return GetAppListButton() && GetAppListButton()->is_showing_app_list();
266 }
267
268 bool ShelfWidget::IsShowingContextMenu() const {
269 return shelf_view_ && shelf_view_->IsShowingMenu();
270 }
271
272 bool ShelfWidget::IsShowingOverflowBubble() const {
273 return shelf_view_ && shelf_view_->IsShowingOverflowBubble();
274 }
275
276 void ShelfWidget::SetFocusCycler(FocusCycler* focus_cycler) {
277 delegate_view_->set_focus_cycler(focus_cycler);
278 if (focus_cycler)
279 focus_cycler->AddWidget(this);
280 }
281
282 FocusCycler* ShelfWidget::GetFocusCycler() {
283 return delegate_view_->focus_cycler();
284 }
285
286 void ShelfWidget::Shutdown() {
287 // Shutting down the status area widget may cause some widgets (e.g. bubbles)
288 // to close, so uninstall the ShelfLayoutManager event filters first. Don't
289 // reset the pointer until later because other widgets (e.g. app list) may
290 // access it later in shutdown.
291 if (shelf_layout_manager_)
292 shelf_layout_manager_->PrepareForShutdown();
293
294 if (status_area_widget_) {
295 background_animator_.RemoveObserver(status_area_widget_);
296 WmShell::Get()->focus_cycler()->RemoveWidget(status_area_widget_);
297 status_area_widget_->Shutdown();
298 status_area_widget_ = nullptr;
299 }
300
301 CloseNow();
302 }
303
304 void ShelfWidget::UpdateIconPositionForPanel(WmWindow* panel) {
305 if (!shelf_view_)
306 return;
307
308 WmWindow* shelf_window = WmWindow::Get(this->GetNativeWindow());
309 shelf_view_->UpdatePanelIconPosition(
310 panel->aura_window()->GetProperty(kShelfIDKey),
311 shelf_window->ConvertRectFromScreen(panel->GetBoundsInScreen())
312 .CenterPoint());
313 }
314
315 gfx::Rect ShelfWidget::GetScreenBoundsOfItemIconForWindow(WmWindow* window) {
316 // Window animations can be triggered during session restore before the shelf
317 // view is created. In that case, return default empty bounds.
318 if (!shelf_view_)
319 return gfx::Rect();
320
321 ShelfID id = window->aura_window()->GetProperty(kShelfIDKey);
322 gfx::Rect bounds(shelf_view_->GetIdealBoundsOfItemIcon(id));
323 gfx::Point screen_origin;
324 views::View::ConvertPointToScreen(shelf_view_, &screen_origin);
325 return gfx::Rect(screen_origin.x() + bounds.x(),
326 screen_origin.y() + bounds.y(), bounds.width(),
327 bounds.height());
328 }
329
330 AppListButton* ShelfWidget::GetAppListButton() const {
331 return shelf_view_ ? shelf_view_->GetAppListButton() : nullptr;
332 }
333
334 app_list::ApplicationDragAndDropHost*
335 ShelfWidget::GetDragAndDropHostForAppList() {
336 return shelf_view_;
337 }
338
339 void ShelfWidget::OnWidgetActivationChanged(views::Widget* widget,
340 bool active) {
341 activating_as_fallback_ = false;
342 if (active)
343 delegate_view_->SetPaneFocusAndFocusDefault();
344 else
345 delegate_view_->GetFocusManager()->ClearFocus();
346 }
347
348 void ShelfWidget::UpdateShelfItemBackground(SkColor color) {
349 if (shelf_view_)
350 shelf_view_->UpdateShelfItemBackground(color);
351 }
352
353 void ShelfWidget::WillDeleteShelfLayoutManager() {
354 shelf_layout_manager_->RemoveObserver(this);
355 shelf_layout_manager_ = nullptr;
356 }
357
358 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/shelf/shelf_widget.h ('k') | ash/common/shelf/shelf_window_watcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698