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

Side by Side Diff: ash/common/system/tray/tray_background_view.cc

Issue 2147143002: [Chrome OS MD] Draw a 1px separator between 2 tray items (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ash/common/system/tray/tray_background_view.h" 5 #include "ash/common/system/tray/tray_background_view.h"
6 6
7 #include "ash/common/material_design/material_design_controller.h" 7 #include "ash/common/material_design/material_design_controller.h"
8 #include "ash/common/shelf/shelf_constants.h" 8 #include "ash/common/shelf/shelf_constants.h"
9 #include "ash/common/shelf/wm_shelf.h" 9 #include "ash/common/shelf/wm_shelf.h"
10 #include "ash/common/shelf/wm_shelf_observer.h"
10 #include "ash/common/shelf/wm_shelf_util.h" 11 #include "ash/common/shelf/wm_shelf_util.h"
11 #include "ash/common/shell_window_ids.h" 12 #include "ash/common/shell_window_ids.h"
12 #include "ash/common/system/tray/system_tray.h" 13 #include "ash/common/system/tray/system_tray.h"
13 #include "ash/common/system/tray/tray_constants.h" 14 #include "ash/common/system/tray/tray_constants.h"
14 #include "ash/common/system/tray/tray_event_filter.h" 15 #include "ash/common/system/tray/tray_event_filter.h"
15 #include "ash/common/wm_lookup.h" 16 #include "ash/common/wm_lookup.h"
16 #include "ash/common/wm_shell.h" 17 #include "ash/common/wm_shell.h"
17 #include "ash/common/wm_window.h" 18 #include "ash/common/wm_window.h"
18 #include "grit/ash_resources.h" 19 #include "grit/ash_resources.h"
19 #include "ui/accessibility/ax_view_state.h" 20 #include "ui/accessibility/ax_view_state.h"
20 #include "ui/base/nine_image_painter_factory.h" 21 #include "ui/base/nine_image_painter_factory.h"
21 #include "ui/base/ui_base_switches_util.h" 22 #include "ui/base/ui_base_switches_util.h"
22 #include "ui/compositor/layer.h" 23 #include "ui/compositor/layer.h"
23 #include "ui/compositor/layer_animation_element.h" 24 #include "ui/compositor/layer_animation_element.h"
24 #include "ui/compositor/scoped_layer_animation_settings.h" 25 #include "ui/compositor/scoped_layer_animation_settings.h"
25 #include "ui/events/event_constants.h" 26 #include "ui/events/event_constants.h"
26 #include "ui/gfx/animation/tween.h" 27 #include "ui/gfx/animation/tween.h"
27 #include "ui/gfx/canvas.h" 28 #include "ui/gfx/canvas.h"
28 #include "ui/gfx/geometry/rect.h" 29 #include "ui/gfx/geometry/rect.h"
29 #include "ui/gfx/image/image_skia.h" 30 #include "ui/gfx/image/image_skia.h"
30 #include "ui/gfx/image/image_skia_operations.h" 31 #include "ui/gfx/image/image_skia_operations.h"
31 #include "ui/gfx/nine_image_painter.h" 32 #include "ui/gfx/nine_image_painter.h"
33 #include "ui/gfx/scoped_canvas.h"
32 #include "ui/gfx/skia_util.h" 34 #include "ui/gfx/skia_util.h"
33 #include "ui/gfx/transform.h" 35 #include "ui/gfx/transform.h"
34 #include "ui/views/background.h" 36 #include "ui/views/background.h"
35 #include "ui/views/layout/box_layout.h" 37 #include "ui/views/layout/box_layout.h"
36 #include "ui/wm/core/window_animations.h" 38 #include "ui/wm/core/window_animations.h"
37 39
38 namespace { 40 namespace {
39 41
40 const int kAnimationDurationForPopupMs = 200; 42 const int kAnimationDurationForPopupMs = 200;
41 43
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 97
96 void set_alpha(int alpha) { alpha_ = alpha; } 98 void set_alpha(int alpha) { alpha_ = alpha; }
97 99
98 private: 100 private:
99 WmShelf* GetShelf() const { return tray_background_view_->shelf(); } 101 WmShelf* GetShelf() const { return tray_background_view_->shelf(); }
100 102
101 void PaintMaterial(gfx::Canvas* canvas, views::View* view) const { 103 void PaintMaterial(gfx::Canvas* canvas, views::View* view) const {
102 SkPaint background_paint; 104 SkPaint background_paint;
103 background_paint.setFlags(SkPaint::kAntiAlias_Flag); 105 background_paint.setFlags(SkPaint::kAntiAlias_Flag);
104 background_paint.setColor(SkColorSetA(kShelfBaseColor, alpha_)); 106 background_paint.setColor(SkColorSetA(kShelfBaseColor, alpha_));
105 canvas->DrawRoundRect(view->GetLocalBounds(), kTrayRoundedBorderRadius, 107 canvas->DrawRoundRect(view->GetContentsBounds(), kTrayRoundedBorderRadius,
106 background_paint); 108 background_paint);
107 109
108 if (tray_background_view_->draw_background_as_active()) { 110 if (tray_background_view_->draw_background_as_active()) {
109 SkPaint highlight_paint; 111 SkPaint highlight_paint;
110 highlight_paint.setFlags(SkPaint::kAntiAlias_Flag); 112 highlight_paint.setFlags(SkPaint::kAntiAlias_Flag);
111 highlight_paint.setColor(kShelfButtonActivatedHighlightColor); 113 highlight_paint.setColor(kShelfButtonActivatedHighlightColor);
112 canvas->DrawRoundRect(view->GetLocalBounds(), kTrayRoundedBorderRadius, 114 canvas->DrawRoundRect(view->GetContentsBounds(), kTrayRoundedBorderRadius,
113 highlight_paint); 115 highlight_paint);
114 } 116 }
115 } 117 }
116 118
117 void PaintNonMaterial(gfx::Canvas* canvas, views::View* view) const { 119 void PaintNonMaterial(gfx::Canvas* canvas, views::View* view) const {
118 const int kGridSizeForPainter = 9; 120 const int kGridSizeForPainter = 9;
119 const int kImages[kNumOrientations][kNumStates][kGridSizeForPainter] = { 121 const int kImages[kNumOrientations][kNumStates][kGridSizeForPainter] = {
120 { 122 {
121 // Horizontal 123 // Horizontal
122 IMAGE_GRID_HORIZONTAL(IDR_AURA_TRAY_BG_HORIZ), 124 IMAGE_GRID_HORIZONTAL(IDR_AURA_TRAY_BG_HORIZ),
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 PreferredSizeChanged(); 198 PreferredSizeChanged();
197 } 199 }
198 200
199 void TrayBackgroundView::TrayContainer::UpdateLayout() { 201 void TrayBackgroundView::TrayContainer::UpdateLayout() {
200 // Adjust the size of status tray dark background by adding additional 202 // Adjust the size of status tray dark background by adding additional
201 // empty border. 203 // empty border.
202 views::BoxLayout::Orientation orientation = 204 views::BoxLayout::Orientation orientation =
203 IsHorizontalAlignment(alignment_) ? views::BoxLayout::kHorizontal 205 IsHorizontalAlignment(alignment_) ? views::BoxLayout::kHorizontal
204 : views::BoxLayout::kVertical; 206 : views::BoxLayout::kVertical;
205 207
206 if (!ash::MaterialDesignController::IsShelfMaterial()) {
207 // Additional padding used to adjust the user-visible size of status tray
208 // dark background.
209 const int padding = 3;
210 SetBorder(
211 views::Border::CreateEmptyBorder(padding, padding, padding, padding));
212 }
213
214 views::BoxLayout* layout = new views::BoxLayout(orientation, 0, 0, 0); 208 views::BoxLayout* layout = new views::BoxLayout(orientation, 0, 0, 0);
215 layout->SetDefaultFlex(1); 209 layout->SetDefaultFlex(1);
216 views::View::SetLayoutManager(layout); 210 views::View::SetLayoutManager(layout);
217 PreferredSizeChanged(); 211 PreferredSizeChanged();
218 } 212 }
219 213
220 //////////////////////////////////////////////////////////////////////////////// 214 ////////////////////////////////////////////////////////////////////////////////
221 // TrayBackgroundView 215 // TrayBackgroundView
222 216
223 TrayBackgroundView::TrayBackgroundView(WmShelf* wm_shelf) 217 TrayBackgroundView::TrayBackgroundView(WmShelf* wm_shelf,
218 WmShelfObserver* wm_shelf_observer)
224 : wm_shelf_(wm_shelf), 219 : wm_shelf_(wm_shelf),
225 tray_container_(NULL), 220 tray_container_(NULL),
226 shelf_alignment_(SHELF_ALIGNMENT_BOTTOM), 221 shelf_alignment_(SHELF_ALIGNMENT_BOTTOM),
227 background_(NULL), 222 background_(NULL),
228 draw_background_as_active_(false), 223 draw_background_as_active_(false),
229 widget_observer_(new TrayWidgetObserver(this)) { 224 widget_observer_(new TrayWidgetObserver(this)) {
230 DCHECK(wm_shelf_); 225 DCHECK(wm_shelf_);
231 set_notify_enter_exit_on_child(true); 226 set_notify_enter_exit_on_child(true);
232 227
233 tray_container_ = new TrayContainer(shelf_alignment_); 228 tray_container_ = new TrayContainer(shelf_alignment_);
234 SetContents(tray_container_); 229 SetContents(tray_container_);
235 tray_event_filter_.reset(new TrayEventFilter); 230 tray_event_filter_.reset(new TrayEventFilter);
231 wm_shelf_observer_ = wm_shelf_observer;
James Cook 2016/08/09 00:25:18 nit: initialize in the member list above Also, DC
yiyix 2016/08/11 01:23:19 After removing the observer, this is not applicabl
232 is_separator_visible_ = false;
236 233
237 SetPaintToLayer(true); 234 SetPaintToLayer(true);
238 layer()->SetFillsBoundsOpaquely(false); 235 layer()->SetFillsBoundsOpaquely(false);
239 // Start the tray items not visible, because visibility changes are animated. 236 // Start the tray items not visible, because visibility changes are animated.
240 views::View::SetVisible(false); 237 views::View::SetVisible(false);
238 CalculateAndSetTrayContainerBorder();
241 } 239 }
242 240
243 TrayBackgroundView::~TrayBackgroundView() { 241 TrayBackgroundView::~TrayBackgroundView() {
244 if (GetWidget()) 242 if (GetWidget())
245 GetWidget()->RemoveObserver(widget_observer_.get()); 243 GetWidget()->RemoveObserver(widget_observer_.get());
246 StopObservingImplicitAnimations(); 244 StopObservingImplicitAnimations();
247 } 245 }
248 246
249 void TrayBackgroundView::Initialize() { 247 void TrayBackgroundView::Initialize() {
250 GetWidget()->AddObserver(widget_observer_.get()); 248 GetWidget()->AddObserver(widget_observer_.get());
251 } 249 }
252 250
253 // static 251 // static
254 void TrayBackgroundView::InitializeBubbleAnimations( 252 void TrayBackgroundView::InitializeBubbleAnimations(
255 views::Widget* bubble_widget) { 253 views::Widget* bubble_widget) {
256 WmWindow* window = WmLookup::Get()->GetWindowForWidget(bubble_widget); 254 WmWindow* window = WmLookup::Get()->GetWindowForWidget(bubble_widget);
257 window->SetVisibilityAnimationType( 255 window->SetVisibilityAnimationType(
258 ::wm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE); 256 ::wm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE);
259 window->SetVisibilityAnimationTransition(::wm::ANIMATE_HIDE); 257 window->SetVisibilityAnimationTransition(::wm::ANIMATE_HIDE);
260 window->SetVisibilityAnimationDuration( 258 window->SetVisibilityAnimationDuration(
261 base::TimeDelta::FromMilliseconds(kAnimationDurationForPopupMs)); 259 base::TimeDelta::FromMilliseconds(kAnimationDurationForPopupMs));
262 } 260 }
263 261
264 void TrayBackgroundView::SetVisible(bool visible) { 262 void TrayBackgroundView::SetVisible(bool visible) {
265 if (visible == layer()->GetTargetVisibility()) 263 if (visible == layer()->GetTargetVisibility())
266 return; 264 return;
265 // OnVisibilityChange will only be called if the visibility of the tray item
266 // is changed.
267 wm_shelf_observer_->OnVisibilityChange(this);
James Cook 2016/08/09 00:25:17 so I think this should end with "Changing", since
yiyix 2016/08/11 01:23:19 After using index, the method is called after visi
267 268
268 if (visible) { 269 if (visible) {
269 // The alignment of the shelf can change while the TrayBackgroundView is 270 // The alignment of the shelf can change while the TrayBackgroundView is
270 // hidden. Reset the offscreen transform so that the animation to becoming 271 // hidden. Reset the offscreen transform so that the animation to becoming
271 // visible reflects the current layout. 272 // visible reflects the current layout.
272 HideTransformation(); 273 HideTransformation();
273 // SetVisible(false) is defered until the animation for hiding is done. 274 // SetVisible(false) is defered until the animation for hiding is done.
274 // Otherwise the view is immediately hidden and the animation does not 275 // Otherwise the view is immediately hidden and the animation does not
275 // render. 276 // render.
276 views::View::SetVisible(true); 277 views::View::SetVisible(true);
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0)); 359 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0));
359 AddChildView(contents); 360 AddChildView(contents);
360 } 361 }
361 void TrayBackgroundView::SetContentsBackground() { 362 void TrayBackgroundView::SetContentsBackground() {
362 background_ = new TrayBackground(this); 363 background_ = new TrayBackground(this);
363 tray_container_->set_background(background_); 364 tray_container_->set_background(background_);
364 } 365 }
365 366
366 void TrayBackgroundView::SetShelfAlignment(ShelfAlignment alignment) { 367 void TrayBackgroundView::SetShelfAlignment(ShelfAlignment alignment) {
367 shelf_alignment_ = alignment; 368 shelf_alignment_ = alignment;
369 CalculateAndSetTrayContainerBorder();
368 tray_container_->SetAlignment(alignment); 370 tray_container_->SetAlignment(alignment);
369 } 371 }
370 372
373 void TrayBackgroundView::CalculateAndSetTrayContainerBorder() {
374 if (!MaterialDesignController::IsShelfMaterial()) {
375 tray_container()->SetBorder(views::Border::NullBorder());
376 return;
377 }
378 if (IsHorizontalAlignment(shelf()->GetAlignment())) {
James Cook 2016/08/09 00:25:17 optional nit: I'm about to land a CL that will let
yiyix 2016/08/11 01:23:19 Thank you for head up.
379 // Extend hit region horizontally when horizontally aligned.
380 tray_container()->SetBorder(views::Border::CreateEmptyBorder(gfx::Insets(
381 0, kHitRegionPadding, 0, kHitRegionPadding + kSeparatorWidth)));
varkha 2016/08/10 23:27:25 Looks like you can simplify this similar to: // E
yiyix 2016/08/11 01:23:19 Done.
382 } else {
383 // Extend hit region vertically when vertically aligned.
384 tray_container()->SetBorder(views::Border::CreateEmptyBorder(gfx::Insets(
385 kHitRegionPadding, 0, kHitRegionPadding + kSeparatorWidth, 0)));
386 }
387 }
388
371 void TrayBackgroundView::OnImplicitAnimationsCompleted() { 389 void TrayBackgroundView::OnImplicitAnimationsCompleted() {
372 // If there is another animation in the queue, the reverse animation was 390 // If there is another animation in the queue, the reverse animation was
373 // triggered before the completion of animating to invisible. Do not turn off 391 // triggered before the completion of animating to invisible. Do not turn off
374 // the visibility so that the next animation may render. The value of 392 // the visibility so that the next animation may render. The value of
375 // layer()->GetTargetVisibility() can be incorrect if the hide animation was 393 // layer()->GetTargetVisibility() can be incorrect if the hide animation was
376 // aborted to schedule an animation to become visible. As the new animation 394 // aborted to schedule an animation to become visible. As the new animation
377 // is not yet added to the queue. crbug.com/374236 395 // is not yet added to the queue. crbug.com/374236
378 if (layer()->GetAnimator()->is_animating() || layer()->GetTargetVisibility()) 396 if (layer()->GetAnimator()->is_animating() || layer()->GetTargetVisibility())
379 return; 397 return;
380 views::View::SetVisible(false); 398 views::View::SetVisible(false);
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 // Nothing to do here. 516 // Nothing to do here.
499 } 517 }
500 518
501 void TrayBackgroundView::UpdateShelfItemBackground(int alpha) { 519 void TrayBackgroundView::UpdateShelfItemBackground(int alpha) {
502 if (background_) { 520 if (background_) {
503 background_->set_alpha(alpha); 521 background_->set_alpha(alpha);
504 SchedulePaint(); 522 SchedulePaint();
505 } 523 }
506 } 524 }
507 525
526 void TrayBackgroundView::SetSeparatorVisibility(bool is_show) {
James Cook 2016/08/09 00:25:18 nit: is_shown or just shown
varkha 2016/08/10 23:27:24 ... or just "visibility".
yiyix 2016/08/11 01:23:19 Done.
527 if (is_separator_visible_ != is_show)
James Cook 2016/08/09 00:25:18 I don't think you need this if()
yiyix 2016/08/11 01:23:19 Done.
528 is_separator_visible_ = is_show;
529 }
530
531 void TrayBackgroundView::OnPaint(gfx::Canvas* canvas) {
532 if (!MaterialDesignController::IsShelfMaterial() ||
533 shelf()->GetBackgroundType() ==
534 ShelfBackgroundType::SHELF_BACKGROUND_DEFAULT)
varkha 2016/08/10 23:27:25 nit - {} for multi-line condition.
yiyix 2016/08/11 01:23:19 Done.
535 return;
536
537 if (is_separator_visible_) {
James Cook 2016/08/09 00:25:17 nit: put !is_separator_visible_ into the early-exi
yiyix 2016/08/11 01:23:19 Done.
538 const int height = GetTrayConstant(TRAY_ITEM_HEIGHT_LEGACY);
539 const int width = kSeparatorWidth;
540 const int x = height + kHitRegionPadding + kHitRegionPadding;
James Cook 2016/08/09 00:25:18 x is based on the height?
yiyix 2016/08/11 01:23:19 The application icons have the same height and wid
541 const int y = (GetShelfConstant(SHELF_SIZE) - height) / 2;
542 const float scale = canvas->UndoDeviceScaleFactor();
James Cook 2016/08/09 00:25:18 I think you need to do this inside the scoped_canv
yiyix 2016/08/11 01:23:19 There is no drawing functions inside of ScopedCanv
543 SkPaint paint;
544 paint.setColor(kSeparatorColor);
545 paint.setAntiAlias(true);
546 const bool horizontal_shelf =
547 IsHorizontalAlignment(shelf()->GetAlignment());
548 const gfx::Rect bounds = horizontal_shelf ? gfx::Rect(x, y, width, height)
549 : gfx::Rect(y, x, height, width);
550 gfx::ScopedCanvas scoped_canvas(canvas);
551 gfx::RectF rect(gfx::ScaleRect(gfx::RectF(bounds), scale));
552 canvas->DrawLine(horizontal_shelf ? rect.top_right() : rect.bottom_left(),
553 rect.bottom_right(), paint);
554 }
James Cook 2016/08/09 00:25:17 I find this function a little hard to read. It see
yiyix 2016/08/11 01:23:19 I was thinking that i need to construct a rect to
555 }
556
508 } // namespace ash 557 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698