OLD | NEW |
| (Empty) |
1 // Copyright 2013 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/overflow_button.h" | |
6 | |
7 #include "ash/common/shelf/shelf_constants.h" | |
8 #include "ash/common/shelf/shelf_view.h" | |
9 #include "ash/common/shelf/wm_shelf.h" | |
10 #include "ash/resources/vector_icons/vector_icons.h" | |
11 #include "ash/strings/grit/ash_strings.h" | |
12 #include "base/memory/ptr_util.h" | |
13 #include "ui/base/l10n/l10n_util.h" | |
14 #include "ui/gfx/canvas.h" | |
15 #include "ui/gfx/image/image_skia_operations.h" | |
16 #include "ui/gfx/paint_vector_icon.h" | |
17 #include "ui/gfx/skbitmap_operations.h" | |
18 #include "ui/views/animation/flood_fill_ink_drop_ripple.h" | |
19 #include "ui/views/animation/ink_drop_impl.h" | |
20 #include "ui/views/animation/ink_drop_mask.h" | |
21 | |
22 namespace ash { | |
23 | |
24 OverflowButton::OverflowButton(ShelfView* shelf_view, WmShelf* wm_shelf) | |
25 : CustomButton(nullptr), | |
26 shelf_view_(shelf_view), | |
27 wm_shelf_(wm_shelf), | |
28 background_color_(kShelfDefaultBaseColor) { | |
29 DCHECK(shelf_view_); | |
30 | |
31 SetInkDropMode(InkDropMode::ON); | |
32 set_ink_drop_base_color(kShelfInkDropBaseColor); | |
33 set_ink_drop_visible_opacity(kShelfInkDropVisibleOpacity); | |
34 set_hide_ink_drop_when_showing_context_menu(false); | |
35 bottom_image_ = gfx::CreateVectorIcon(kShelfOverflowIcon, kShelfIconColor); | |
36 | |
37 SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY); | |
38 SetAccessibleName(l10n_util::GetStringUTF16(IDS_ASH_SHELF_OVERFLOW_NAME)); | |
39 } | |
40 | |
41 OverflowButton::~OverflowButton() {} | |
42 | |
43 void OverflowButton::OnShelfAlignmentChanged() { | |
44 SchedulePaint(); | |
45 } | |
46 | |
47 void OverflowButton::OnOverflowBubbleShown() { | |
48 AnimateInkDrop(views::InkDropState::ACTIVATED, nullptr); | |
49 } | |
50 | |
51 void OverflowButton::OnOverflowBubbleHidden() { | |
52 AnimateInkDrop(views::InkDropState::DEACTIVATED, nullptr); | |
53 } | |
54 | |
55 void OverflowButton::UpdateShelfItemBackground(SkColor color) { | |
56 background_color_ = color; | |
57 SchedulePaint(); | |
58 } | |
59 | |
60 void OverflowButton::OnPaint(gfx::Canvas* canvas) { | |
61 gfx::Rect bounds = CalculateButtonBounds(); | |
62 PaintBackground(canvas, bounds); | |
63 PaintForeground(canvas, bounds); | |
64 } | |
65 | |
66 std::unique_ptr<views::InkDrop> OverflowButton::CreateInkDrop() { | |
67 std::unique_ptr<views::InkDropImpl> ink_drop = | |
68 CreateDefaultFloodFillInkDropImpl(); | |
69 ink_drop->SetShowHighlightOnHover(false); | |
70 ink_drop->SetAutoHighlightMode(views::InkDropImpl::AutoHighlightMode::NONE); | |
71 return std::move(ink_drop); | |
72 } | |
73 | |
74 std::unique_ptr<views::InkDropRipple> OverflowButton::CreateInkDropRipple() | |
75 const { | |
76 gfx::Insets insets = GetLocalBounds().InsetsFrom(CalculateButtonBounds()); | |
77 return base::MakeUnique<views::FloodFillInkDropRipple>( | |
78 size(), insets, GetInkDropCenterBasedOnLastEvent(), GetInkDropBaseColor(), | |
79 ink_drop_visible_opacity()); | |
80 } | |
81 | |
82 bool OverflowButton::ShouldEnterPushedState(const ui::Event& event) { | |
83 if (shelf_view_->IsShowingOverflowBubble()) | |
84 return false; | |
85 | |
86 return CustomButton::ShouldEnterPushedState(event); | |
87 } | |
88 | |
89 void OverflowButton::NotifyClick(const ui::Event& event) { | |
90 CustomButton::NotifyClick(event); | |
91 shelf_view_->ButtonPressed(this, event, GetInkDrop()); | |
92 } | |
93 | |
94 std::unique_ptr<views::InkDropMask> OverflowButton::CreateInkDropMask() const { | |
95 gfx::Insets insets = GetLocalBounds().InsetsFrom(CalculateButtonBounds()); | |
96 return base::MakeUnique<views::RoundRectInkDropMask>( | |
97 size(), insets, kOverflowButtonCornerRadius); | |
98 } | |
99 | |
100 void OverflowButton::PaintBackground(gfx::Canvas* canvas, | |
101 const gfx::Rect& bounds) { | |
102 cc::PaintFlags flags; | |
103 flags.setFlags(cc::PaintFlags::kAntiAlias_Flag); | |
104 flags.setColor(background_color_); | |
105 canvas->DrawRoundRect(bounds, kOverflowButtonCornerRadius, flags); | |
106 } | |
107 | |
108 void OverflowButton::PaintForeground(gfx::Canvas* canvas, | |
109 const gfx::Rect& bounds) { | |
110 const gfx::ImageSkia* image = nullptr; | |
111 | |
112 switch (wm_shelf_->GetAlignment()) { | |
113 case SHELF_ALIGNMENT_LEFT: | |
114 if (left_image_.isNull()) { | |
115 left_image_ = gfx::ImageSkiaOperations::CreateRotatedImage( | |
116 bottom_image_, SkBitmapOperations::ROTATION_90_CW); | |
117 } | |
118 image = &left_image_; | |
119 break; | |
120 case SHELF_ALIGNMENT_RIGHT: | |
121 if (right_image_.isNull()) { | |
122 right_image_ = gfx::ImageSkiaOperations::CreateRotatedImage( | |
123 bottom_image_, SkBitmapOperations::ROTATION_270_CW); | |
124 } | |
125 image = &right_image_; | |
126 break; | |
127 default: | |
128 image = &bottom_image_; | |
129 break; | |
130 } | |
131 | |
132 canvas->DrawImageInt(*image, | |
133 bounds.x() + ((bounds.width() - image->width()) / 2), | |
134 bounds.y() + ((bounds.height() - image->height()) / 2)); | |
135 } | |
136 | |
137 gfx::Rect OverflowButton::CalculateButtonBounds() const { | |
138 ShelfAlignment alignment = wm_shelf_->GetAlignment(); | |
139 gfx::Rect content_bounds = GetContentsBounds(); | |
140 // Align the button to the top of a bottom-aligned shelf, to the right edge | |
141 // a left-aligned shelf, and to the left edge of a right-aligned shelf. | |
142 const int inset = (GetShelfConstant(SHELF_SIZE) - kOverflowButtonSize) / 2; | |
143 const int x = alignment == SHELF_ALIGNMENT_LEFT | |
144 ? content_bounds.right() - inset - kOverflowButtonSize | |
145 : content_bounds.x() + inset; | |
146 return gfx::Rect(x, content_bounds.y() + inset, kOverflowButtonSize, | |
147 kOverflowButtonSize); | |
148 } | |
149 | |
150 } // namespace ash | |
OLD | NEW |