OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/system/toast/toast_overlay.h" | 5 #include "ash/system/toast/toast_overlay.h" |
6 | 6 |
| 7 #include "ash/common/shelf/wm_shelf.h" |
7 #include "ash/common/shell_window_ids.h" | 8 #include "ash/common/shell_window_ids.h" |
| 9 #include "ash/common/wm_root_window_controller.h" |
| 10 #include "ash/common/wm_shell.h" |
| 11 #include "ash/common/wm_window.h" |
8 #include "ash/screen_util.h" | 12 #include "ash/screen_util.h" |
9 #include "ash/shelf/shelf.h" | |
10 #include "ash/shelf/shelf_layout_manager.h" | |
11 #include "ash/shell.h" | 13 #include "ash/shell.h" |
12 #include "ash/wm/window_animations.h" | 14 #include "ash/wm/window_animations.h" |
13 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
14 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
15 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
16 #include "grit/ash_strings.h" | 18 #include "grit/ash_strings.h" |
17 #include "third_party/skia/include/core/SkColor.h" | 19 #include "third_party/skia/include/core/SkColor.h" |
18 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
19 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
20 #include "ui/gfx/canvas.h" | 22 #include "ui/gfx/canvas.h" |
(...skipping 23 matching lines...) Expand all Loading... |
44 // Colors for the dismiss button. | 46 // Colors for the dismiss button. |
45 const SkColor kButtonBackgroundColor = SkColorSetARGB(0xFF, 0x32, 0x32, 0x32); | 47 const SkColor kButtonBackgroundColor = SkColorSetARGB(0xFF, 0x32, 0x32, 0x32); |
46 const SkColor kButtonTextColor = SkColorSetARGB(0xFF, 0x7B, 0xAA, 0xF7); | 48 const SkColor kButtonTextColor = SkColorSetARGB(0xFF, 0x7B, 0xAA, 0xF7); |
47 | 49 |
48 // These values are in DIP. | 50 // These values are in DIP. |
49 const int kToastHorizontalSpacing = 16; | 51 const int kToastHorizontalSpacing = 16; |
50 const int kToastVerticalSpacing = 16; | 52 const int kToastVerticalSpacing = 16; |
51 const int kToastMaximumWidth = 568; | 53 const int kToastMaximumWidth = 568; |
52 const int kToastMinimumWidth = 288; | 54 const int kToastMinimumWidth = 288; |
53 | 55 |
| 56 // Returns the shelf for the primary display. |
| 57 WmShelf* GetPrimaryShelf() { |
| 58 return WmShell::Get() |
| 59 ->GetPrimaryRootWindow() |
| 60 ->GetRootWindowController() |
| 61 ->GetShelf(); |
| 62 } |
| 63 |
54 } // anonymous namespace | 64 } // anonymous namespace |
55 | 65 |
56 /////////////////////////////////////////////////////////////////////////////// | 66 /////////////////////////////////////////////////////////////////////////////// |
57 // ToastOverlayLabel | 67 // ToastOverlayLabel |
58 class ToastOverlayLabel : public views::Label { | 68 class ToastOverlayLabel : public views::Label { |
59 public: | 69 public: |
60 explicit ToastOverlayLabel(const std::string& label); | 70 explicit ToastOverlayLabel(const std::string& label); |
61 ~ToastOverlayLabel() override; | 71 ~ToastOverlayLabel() override; |
62 | 72 |
63 private: | 73 private: |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 paint.setColor(kButtonBackgroundColor); | 187 paint.setColor(kButtonBackgroundColor); |
178 canvas->DrawRoundRect(GetLocalBounds(), 2, paint); | 188 canvas->DrawRoundRect(GetLocalBounds(), 2, paint); |
179 views::View::OnPaint(canvas); | 189 views::View::OnPaint(canvas); |
180 } | 190 } |
181 | 191 |
182 gfx::Size ToastOverlayView::GetMinimumSize() const { | 192 gfx::Size ToastOverlayView::GetMinimumSize() const { |
183 return gfx::Size(kToastMinimumWidth, 0); | 193 return gfx::Size(kToastMinimumWidth, 0); |
184 } | 194 } |
185 | 195 |
186 gfx::Size ToastOverlayView::GetMaximumSize() const { | 196 gfx::Size ToastOverlayView::GetMaximumSize() const { |
187 ShelfLayoutManager* shelf_layout_manager = | 197 gfx::Rect work_area_bounds = GetPrimaryShelf()->GetUserWorkAreaBounds(); |
188 Shelf::ForPrimaryDisplay()->shelf_layout_manager(); | |
189 gfx::Rect work_area_bounds = shelf_layout_manager->user_work_area_bounds(); | |
190 | |
191 return gfx::Size(kToastMaximumWidth, work_area_bounds.height() - kOffset * 2); | 198 return gfx::Size(kToastMaximumWidth, work_area_bounds.height() - kOffset * 2); |
192 } | 199 } |
193 | 200 |
194 void ToastOverlayView::ButtonPressed(views::Button* sender, | 201 void ToastOverlayView::ButtonPressed(views::Button* sender, |
195 const ui::Event& event) { | 202 const ui::Event& event) { |
196 overlay_->Show(false); | 203 overlay_->Show(false); |
197 } | 204 } |
198 | 205 |
199 /////////////////////////////////////////////////////////////////////////////// | 206 /////////////////////////////////////////////////////////////////////////////// |
200 // ToastOverlay | 207 // ToastOverlay |
201 ToastOverlay::ToastOverlay(Delegate* delegate, | 208 ToastOverlay::ToastOverlay(Delegate* delegate, |
202 const std::string& text, | 209 const std::string& text, |
203 const std::string& dismiss_text) | 210 const std::string& dismiss_text) |
204 : delegate_(delegate), | 211 : delegate_(delegate), |
205 text_(text), | 212 text_(text), |
206 dismiss_text_(dismiss_text), | 213 dismiss_text_(dismiss_text), |
207 overlay_view_(new ToastOverlayView(this, text, dismiss_text)), | 214 overlay_view_(new ToastOverlayView(this, text, dismiss_text)), |
208 widget_size_(overlay_view_->GetPreferredSize()) { | 215 widget_size_(overlay_view_->GetPreferredSize()) { |
209 views::Widget::InitParams params; | 216 views::Widget::InitParams params; |
210 params.type = views::Widget::InitParams::TYPE_POPUP; | 217 params.type = views::Widget::InitParams::TYPE_POPUP; |
211 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | 218 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
212 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 219 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
213 params.accept_events = true; | 220 params.accept_events = true; |
214 params.keep_on_top = true; | 221 params.keep_on_top = true; |
215 params.remove_standard_frame = true; | 222 params.remove_standard_frame = true; |
216 params.bounds = CalculateOverlayBounds(); | 223 params.bounds = CalculateOverlayBounds(); |
217 // Show toasts above the app list and below the lock screen. | 224 // Show toasts above the app list and below the lock screen. |
| 225 // TODO(jamescook): Either this should be the primary root window, or the |
| 226 // work area bounds computation should be for the target root window. |
218 params.parent = Shell::GetContainer(Shell::GetTargetRootWindow(), | 227 params.parent = Shell::GetContainer(Shell::GetTargetRootWindow(), |
219 kShellWindowId_SystemModalContainer); | 228 kShellWindowId_SystemModalContainer); |
220 overlay_widget_.reset(new views::Widget); | 229 overlay_widget_.reset(new views::Widget); |
221 overlay_widget_->Init(params); | 230 overlay_widget_->Init(params); |
222 overlay_widget_->SetVisibilityChangedAnimationsEnabled(true); | 231 overlay_widget_->SetVisibilityChangedAnimationsEnabled(true); |
223 overlay_widget_->SetContentsView(overlay_view_.get()); | 232 overlay_widget_->SetContentsView(overlay_view_.get()); |
224 overlay_widget_->SetBounds(CalculateOverlayBounds()); | 233 overlay_widget_->SetBounds(CalculateOverlayBounds()); |
225 overlay_widget_->GetNativeView()->SetName("ToastOverlay"); | 234 overlay_widget_->GetNativeView()->SetName("ToastOverlay"); |
226 | 235 |
227 gfx::NativeWindow native_view = overlay_widget_->GetNativeView(); | 236 gfx::NativeWindow native_view = overlay_widget_->GetNativeView(); |
(...skipping 27 matching lines...) Expand all Loading... |
255 overlay_widget_->Show(); | 264 overlay_widget_->Show(); |
256 | 265 |
257 // Notify accessibility about the overlay. | 266 // Notify accessibility about the overlay. |
258 overlay_view_->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, false); | 267 overlay_view_->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, false); |
259 } else { | 268 } else { |
260 overlay_widget_->Hide(); | 269 overlay_widget_->Hide(); |
261 } | 270 } |
262 } | 271 } |
263 | 272 |
264 gfx::Rect ToastOverlay::CalculateOverlayBounds() { | 273 gfx::Rect ToastOverlay::CalculateOverlayBounds() { |
265 ShelfLayoutManager* shelf_layout_manager = | 274 gfx::Rect bounds = GetPrimaryShelf()->GetUserWorkAreaBounds(); |
266 Shelf::ForPrimaryDisplay()->shelf_layout_manager(); | |
267 | |
268 gfx::Rect bounds = shelf_layout_manager->user_work_area_bounds(); | |
269 int target_y = bounds.bottom() - widget_size_.height() - kOffset; | 275 int target_y = bounds.bottom() - widget_size_.height() - kOffset; |
270 bounds.ClampToCenteredSize(widget_size_); | 276 bounds.ClampToCenteredSize(widget_size_); |
271 bounds.set_y(target_y); | 277 bounds.set_y(target_y); |
272 return bounds; | 278 return bounds; |
273 } | 279 } |
274 | 280 |
275 void ToastOverlay::OnImplicitAnimationsScheduled() {} | 281 void ToastOverlay::OnImplicitAnimationsScheduled() {} |
276 | 282 |
277 void ToastOverlay::OnImplicitAnimationsCompleted() { | 283 void ToastOverlay::OnImplicitAnimationsCompleted() { |
278 if (!overlay_widget_->GetLayer()->GetTargetVisibility()) | 284 if (!overlay_widget_->GetLayer()->GetTargetVisibility()) |
279 delegate_->OnClosed(); | 285 delegate_->OnClosed(); |
280 } | 286 } |
281 | 287 |
282 views::Widget* ToastOverlay::widget_for_testing() { | 288 views::Widget* ToastOverlay::widget_for_testing() { |
283 return overlay_widget_.get(); | 289 return overlay_widget_.get(); |
284 } | 290 } |
285 | 291 |
286 void ToastOverlay::ClickDismissButtonForTesting(const ui::Event& event) { | 292 void ToastOverlay::ClickDismissButtonForTesting(const ui::Event& event) { |
287 overlay_view_->button()->NotifyClick(event); | 293 overlay_view_->button()->NotifyClick(event); |
288 } | 294 } |
289 | 295 |
290 } // namespace ash | 296 } // namespace ash |
OLD | NEW |