OLD | NEW |
| (Empty) |
1 // Copyright 2014 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/shelf/app_list_button.h" | |
6 | |
7 #include "ash/common/ash_constants.h" | |
8 #include "ash/common/material_design/material_design_controller.h" | |
9 #include "ash/common/shelf/ink_drop_button_listener.h" | |
10 #include "ash/common/shelf/shelf_constants.h" | |
11 #include "ash/common/shelf/shelf_item_types.h" | |
12 #include "ash/common/shelf/shelf_types.h" | |
13 #include "ash/common/shelf/wm_shelf_util.h" | |
14 #include "ash/shelf/shelf_layout_manager.h" | |
15 #include "ash/shelf/shelf_view.h" | |
16 #include "ash/shelf/shelf_widget.h" | |
17 #include "ash/shell.h" | |
18 #include "base/command_line.h" | |
19 #include "grit/ash_resources.h" | |
20 #include "grit/ash_strings.h" | |
21 #include "ui/accessibility/ax_view_state.h" | |
22 #include "ui/app_list/app_list_switches.h" | |
23 #include "ui/base/l10n/l10n_util.h" | |
24 #include "ui/base/resource/resource_bundle.h" | |
25 #include "ui/base/ui_base_switches_util.h" | |
26 #include "ui/gfx/canvas.h" | |
27 #include "ui/gfx/paint_vector_icon.h" | |
28 #include "ui/gfx/vector_icons_public.h" | |
29 #include "ui/views/animation/square_ink_drop_ripple.h" | |
30 #include "ui/views/painter.h" | |
31 | |
32 namespace ash { | |
33 | |
34 AppListButton::AppListButton(InkDropButtonListener* listener, | |
35 ShelfView* shelf_view) | |
36 : views::ImageButton(nullptr), | |
37 draw_background_as_active_(false), | |
38 listener_(listener), | |
39 shelf_view_(shelf_view) { | |
40 if (ash::MaterialDesignController::IsShelfMaterial()) { | |
41 SetInkDropMode(InkDropMode::ON_NO_GESTURE_HANDLER); | |
42 set_ink_drop_base_color(kShelfInkDropBaseColor); | |
43 set_ink_drop_visible_opacity(kShelfInkDropVisibleOpacity); | |
44 } | |
45 SetAccessibleName( | |
46 app_list::switches::IsExperimentalAppListEnabled() | |
47 ? l10n_util::GetStringUTF16(IDS_ASH_SHELF_APP_LIST_LAUNCHER_TITLE) | |
48 : l10n_util::GetStringUTF16(IDS_ASH_SHELF_APP_LIST_TITLE)); | |
49 SetSize( | |
50 gfx::Size(GetShelfConstant(SHELF_SIZE), GetShelfConstant(SHELF_SIZE))); | |
51 SetFocusPainter(views::Painter::CreateSolidFocusPainter( | |
52 kFocusBorderColor, gfx::Insets(1, 1, 1, 1))); | |
53 set_notify_action(CustomButton::NOTIFY_ON_PRESS); | |
54 } | |
55 | |
56 AppListButton::~AppListButton() {} | |
57 | |
58 void AppListButton::OnAppListShown() { | |
59 if (ash::MaterialDesignController::IsShelfMaterial()) | |
60 AnimateInkDrop(views::InkDropState::ACTIVATED, nullptr); | |
61 else | |
62 SchedulePaint(); | |
63 } | |
64 | |
65 void AppListButton::OnAppListDismissed() { | |
66 if (ash::MaterialDesignController::IsShelfMaterial()) | |
67 AnimateInkDrop(views::InkDropState::DEACTIVATED, nullptr); | |
68 else | |
69 SchedulePaint(); | |
70 } | |
71 | |
72 bool AppListButton::OnMousePressed(const ui::MouseEvent& event) { | |
73 ImageButton::OnMousePressed(event); | |
74 shelf_view_->PointerPressedOnButton(this, ShelfView::MOUSE, event); | |
75 return true; | |
76 } | |
77 | |
78 void AppListButton::OnMouseReleased(const ui::MouseEvent& event) { | |
79 ImageButton::OnMouseReleased(event); | |
80 shelf_view_->PointerReleasedOnButton(this, ShelfView::MOUSE, false); | |
81 } | |
82 | |
83 void AppListButton::OnMouseCaptureLost() { | |
84 shelf_view_->PointerReleasedOnButton(this, ShelfView::MOUSE, true); | |
85 ImageButton::OnMouseCaptureLost(); | |
86 } | |
87 | |
88 bool AppListButton::OnMouseDragged(const ui::MouseEvent& event) { | |
89 ImageButton::OnMouseDragged(event); | |
90 shelf_view_->PointerDraggedOnButton(this, ShelfView::MOUSE, event); | |
91 return true; | |
92 } | |
93 | |
94 void AppListButton::OnGestureEvent(ui::GestureEvent* event) { | |
95 const bool is_material = ash::MaterialDesignController::IsShelfMaterial(); | |
96 const bool touch_feedback = | |
97 !is_material && switches::IsTouchFeedbackEnabled(); | |
98 switch (event->type()) { | |
99 case ui::ET_GESTURE_SCROLL_BEGIN: | |
100 if (touch_feedback) | |
101 SetDrawBackgroundAsActive(false); | |
102 else if (is_material) | |
103 AnimateInkDrop(views::InkDropState::HIDDEN, event); | |
104 shelf_view_->PointerPressedOnButton(this, ShelfView::TOUCH, *event); | |
105 event->SetHandled(); | |
106 return; | |
107 case ui::ET_GESTURE_SCROLL_UPDATE: | |
108 shelf_view_->PointerDraggedOnButton(this, ShelfView::TOUCH, *event); | |
109 event->SetHandled(); | |
110 return; | |
111 case ui::ET_GESTURE_SCROLL_END: | |
112 case ui::ET_SCROLL_FLING_START: | |
113 shelf_view_->PointerReleasedOnButton(this, ShelfView::TOUCH, false); | |
114 event->SetHandled(); | |
115 return; | |
116 case ui::ET_GESTURE_TAP_DOWN: | |
117 if (touch_feedback) | |
118 SetDrawBackgroundAsActive(true); | |
119 else if (is_material && !Shell::GetInstance()->IsApplistVisible()) | |
120 AnimateInkDrop(views::InkDropState::ACTION_PENDING, event); | |
121 ImageButton::OnGestureEvent(event); | |
122 break; | |
123 case ui::ET_GESTURE_TAP_CANCEL: | |
124 case ui::ET_GESTURE_TAP: | |
125 if (touch_feedback) | |
126 SetDrawBackgroundAsActive(false); | |
127 ImageButton::OnGestureEvent(event); | |
128 break; | |
129 default: | |
130 ImageButton::OnGestureEvent(event); | |
131 return; | |
132 } | |
133 } | |
134 | |
135 void AppListButton::OnPaint(gfx::Canvas* canvas) { | |
136 // Call the base class first to paint any background/borders. | |
137 View::OnPaint(canvas); | |
138 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
139 | |
140 const gfx::ImageSkia& foreground_image = | |
141 MaterialDesignController::IsShelfMaterial() | |
142 ? CreateVectorIcon(gfx::VectorIconId::SHELF_APPLIST, kShelfIconColor) | |
143 : *rb.GetImageNamed(IDR_ASH_SHELF_ICON_APPLIST).ToImageSkia(); | |
144 | |
145 if (ash::MaterialDesignController::IsShelfMaterial()) { | |
146 PaintBackgroundMD(canvas); | |
147 PaintForegroundMD(canvas, foreground_image); | |
148 } else { | |
149 PaintAppListButton(canvas, foreground_image); | |
150 } | |
151 | |
152 views::Painter::PaintFocusPainter(this, canvas, focus_painter()); | |
153 } | |
154 | |
155 void AppListButton::PaintBackgroundMD(gfx::Canvas* canvas) { | |
156 SkPaint background_paint; | |
157 background_paint.setColor(SK_ColorTRANSPARENT); | |
158 background_paint.setFlags(SkPaint::kAntiAlias_Flag); | |
159 background_paint.setStyle(SkPaint::kFill_Style); | |
160 | |
161 const ShelfWidget* shelf_widget = shelf_view_->shelf()->shelf_widget(); | |
162 if (shelf_widget && | |
163 shelf_widget->GetBackgroundType() == | |
164 ShelfBackgroundType::SHELF_BACKGROUND_DEFAULT) { | |
165 background_paint.setColor( | |
166 SkColorSetA(kShelfBaseColor, GetShelfConstant(SHELF_BACKGROUND_ALPHA))); | |
167 } | |
168 | |
169 // Paint the circular background of AppList button. | |
170 gfx::Point circle_center = GetContentsBounds().CenterPoint(); | |
171 if (!IsHorizontalAlignment(shelf_view_->shelf()->alignment())) | |
172 circle_center = gfx::Point(circle_center.y(), circle_center.x()); | |
173 | |
174 canvas->DrawCircle(circle_center, kAppListButtonRadius, background_paint); | |
175 } | |
176 | |
177 void AppListButton::PaintForegroundMD(gfx::Canvas* canvas, | |
178 const gfx::ImageSkia& foreground_image) { | |
179 gfx::Rect foreground_bounds(foreground_image.size()); | |
180 gfx::Rect contents_bounds = GetContentsBounds(); | |
181 | |
182 if (IsHorizontalAlignment(shelf_view_->shelf()->alignment())) { | |
183 foreground_bounds.set_x( | |
184 (contents_bounds.width() - foreground_bounds.width()) / 2); | |
185 foreground_bounds.set_y( | |
186 (contents_bounds.height() - foreground_bounds.height()) / 2); | |
187 } else { | |
188 foreground_bounds.set_x( | |
189 (contents_bounds.height() - foreground_bounds.height()) / 2); | |
190 foreground_bounds.set_y( | |
191 (contents_bounds.width() - foreground_bounds.width()) / 2); | |
192 } | |
193 canvas->DrawImageInt(foreground_image, foreground_bounds.x(), | |
194 foreground_bounds.y()); | |
195 } | |
196 | |
197 void AppListButton::PaintAppListButton(gfx::Canvas* canvas, | |
198 const gfx::ImageSkia& foreground_image) { | |
199 int background_image_id = 0; | |
200 | |
201 if (Shell::GetInstance()->GetAppListTargetVisibility() || | |
202 draw_background_as_active_) { | |
203 background_image_id = IDR_AURA_NOTIFICATION_BACKGROUND_PRESSED; | |
204 } else { | |
205 if (shelf_view_->shelf()->shelf_widget()->GetDimsShelf()) { | |
206 background_image_id = IDR_AURA_NOTIFICATION_BACKGROUND_ON_BLACK; | |
207 } else { | |
208 background_image_id = IDR_AURA_NOTIFICATION_BACKGROUND_NORMAL; | |
209 } | |
210 } | |
211 | |
212 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
213 gfx::ImageSkia background_image = | |
214 *rb.GetImageNamed(background_image_id).ToImageSkia(); | |
215 gfx::Rect background_bounds(background_image.size()); | |
216 ShelfAlignment alignment = shelf_view_->shelf()->alignment(); | |
217 gfx::Rect contents_bounds = GetContentsBounds(); | |
218 | |
219 if (alignment == SHELF_ALIGNMENT_LEFT) { | |
220 background_bounds.set_x(contents_bounds.width() - kShelfItemInset - | |
221 background_image.width()); | |
222 background_bounds.set_y( | |
223 contents_bounds.y() + | |
224 (contents_bounds.height() - background_image.height()) / 2); | |
225 } else if (alignment == SHELF_ALIGNMENT_RIGHT) { | |
226 background_bounds.set_x(kShelfItemInset); | |
227 background_bounds.set_y( | |
228 contents_bounds.y() + | |
229 (contents_bounds.height() - background_image.height()) / 2); | |
230 } else { | |
231 background_bounds.set_y(kShelfItemInset); | |
232 background_bounds.set_x( | |
233 contents_bounds.x() + | |
234 (contents_bounds.width() - background_image.width()) / 2); | |
235 } | |
236 | |
237 canvas->DrawImageInt(background_image, background_bounds.x(), | |
238 background_bounds.y()); | |
239 gfx::Rect foreground_bounds(foreground_image.size()); | |
240 foreground_bounds.set_x( | |
241 background_bounds.x() + | |
242 std::max(0, (background_bounds.width() - foreground_bounds.width()) / 2)); | |
243 foreground_bounds.set_y( | |
244 background_bounds.y() + | |
245 std::max(0, | |
246 (background_bounds.height() - foreground_bounds.height()) / 2)); | |
247 canvas->DrawImageInt(foreground_image, foreground_bounds.x(), | |
248 foreground_bounds.y()); | |
249 } | |
250 | |
251 void AppListButton::GetAccessibleState(ui::AXViewState* state) { | |
252 state->role = ui::AX_ROLE_BUTTON; | |
253 state->name = shelf_view_->GetTitleForView(this); | |
254 } | |
255 | |
256 std::unique_ptr<views::InkDropRipple> AppListButton::CreateInkDropRipple() | |
257 const { | |
258 // TODO(mohsen): A circular SquareInkDropRipple is created with equal small | |
259 // and large sizes to mimic a circular flood fill. Replace with an actual | |
260 // flood fill when circular flood fills are implemented. | |
261 gfx::Size ripple_size(2 * kAppListButtonRadius, 2 * kAppListButtonRadius); | |
262 auto ink_drop_ripple = new views::SquareInkDropRipple( | |
263 ripple_size, 0, ripple_size, 0, GetContentsBounds().CenterPoint(), | |
264 GetInkDropBaseColor(), ink_drop_visible_opacity()); | |
265 ink_drop_ripple->set_activated_shape(views::SquareInkDropRipple::CIRCLE); | |
266 return base::WrapUnique(ink_drop_ripple); | |
267 } | |
268 | |
269 void AppListButton::NotifyClick(const ui::Event& event) { | |
270 ImageButton::NotifyClick(event); | |
271 if (listener_) | |
272 listener_->ButtonPressed(this, event, ink_drop()); | |
273 } | |
274 | |
275 bool AppListButton::ShouldEnterPushedState(const ui::Event& event) { | |
276 if (!shelf_view_->ShouldEventActivateButton(this, event)) | |
277 return false; | |
278 if (Shell::GetInstance()->IsApplistVisible()) | |
279 return false; | |
280 return views::ImageButton::ShouldEnterPushedState(event); | |
281 } | |
282 | |
283 bool AppListButton::ShouldShowInkDropHighlight() const { | |
284 return false; | |
285 } | |
286 | |
287 void AppListButton::SetDrawBackgroundAsActive(bool draw_background_as_active) { | |
288 if (draw_background_as_active_ == draw_background_as_active) | |
289 return; | |
290 draw_background_as_active_ = draw_background_as_active; | |
291 SchedulePaint(); | |
292 } | |
293 | |
294 } // namespace ash | |
OLD | NEW |