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 "ui/arc/notification/arc_custom_notification_view.h" | 5 #include "ui/arc/notification/arc_custom_notification_view.h" |
6 | 6 |
7 #include "ash/wm/window_util.h" | 7 #include "ash/wm/window_util.h" |
8 #include "base/auto_reset.h" | 8 #include "base/auto_reset.h" |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "components/exo/notification_surface.h" | 10 #include "components/exo/notification_surface.h" |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 | 193 |
194 void RequestFocusOnCloseButton() override { | 194 void RequestFocusOnCloseButton() override { |
195 if (owner_->close_button_) | 195 if (owner_->close_button_) |
196 owner_->close_button_->RequestFocus(); | 196 owner_->close_button_->RequestFocus(); |
197 owner_->UpdateControlButtonsVisibility(); | 197 owner_->UpdateControlButtonsVisibility(); |
198 } | 198 } |
199 | 199 |
200 bool IsPinned() const override { | 200 bool IsPinned() const override { |
201 if (!owner_->item_) | 201 if (!owner_->item_) |
202 return false; | 202 return false; |
203 return owner_->item_->pinned(); | 203 return owner_->item_->GetPinned(); |
204 } | 204 } |
205 | 205 |
206 void UpdateControlButtonsVisibility() override { | 206 void UpdateControlButtonsVisibility() override { |
207 owner_->UpdateControlButtonsVisibility(); | 207 owner_->UpdateControlButtonsVisibility(); |
208 } | 208 } |
209 | 209 |
210 private: | 210 private: |
211 ArcCustomNotificationView* const owner_; | 211 ArcCustomNotificationView* const owner_; |
212 | 212 |
213 DISALLOW_COPY_AND_ASSIGN(ContentViewDelegate); | 213 DISALLOW_COPY_AND_ASSIGN(ContentViewDelegate); |
214 }; | 214 }; |
215 | 215 |
216 ArcCustomNotificationView::ControlButton::ControlButton( | 216 ArcCustomNotificationView::ControlButton::ControlButton( |
217 ArcCustomNotificationView* owner) | 217 ArcCustomNotificationView* owner) |
218 : message_center::PaddedButton(owner), owner_(owner) { | 218 : message_center::PaddedButton(owner), owner_(owner) { |
219 if (owner_->item_) { | 219 if (owner_->item_) { |
220 set_background(views::Background::CreateSolidBackground( | 220 set_background(views::Background::CreateSolidBackground( |
221 GetControlButtonBackgroundColor(owner_->item_->shown_contents()))); | 221 GetControlButtonBackgroundColor(owner_->item_->GetShownContents()))); |
222 } else { | 222 } else { |
223 set_background(views::Background::CreateSolidBackground( | 223 set_background(views::Background::CreateSolidBackground( |
224 message_center::kControlButtonBackgroundColor)); | 224 message_center::kControlButtonBackgroundColor)); |
225 } | 225 } |
226 } | 226 } |
227 | 227 |
228 void ArcCustomNotificationView::ControlButton::OnFocus() { | 228 void ArcCustomNotificationView::ControlButton::OnFocus() { |
229 message_center::PaddedButton::OnFocus(); | 229 message_center::PaddedButton::OnFocus(); |
230 owner_->UpdateControlButtonsVisibility(); | 230 owner_->UpdateControlButtonsVisibility(); |
231 } | 231 } |
232 | 232 |
233 void ArcCustomNotificationView::ControlButton::OnBlur() { | 233 void ArcCustomNotificationView::ControlButton::OnBlur() { |
234 message_center::PaddedButton::OnBlur(); | 234 message_center::PaddedButton::OnBlur(); |
235 owner_->UpdateControlButtonsVisibility(); | 235 owner_->UpdateControlButtonsVisibility(); |
236 } | 236 } |
237 | 237 |
238 ArcCustomNotificationView::ArcCustomNotificationView( | 238 ArcCustomNotificationView::ArcCustomNotificationView(ArcNotificationItem* item) |
239 ArcCustomNotificationItem* item) | |
240 : item_(item), | 239 : item_(item), |
241 notification_key_(item->notification_key()), | 240 notification_key_(item->GetNotificationKey()), |
242 event_forwarder_(new EventForwarder(this)) { | 241 event_forwarder_(new EventForwarder(this)) { |
243 SetFocusBehavior(FocusBehavior::ALWAYS); | 242 SetFocusBehavior(FocusBehavior::ALWAYS); |
244 | 243 |
245 item_->IncrementWindowRefCount(); | 244 item_->IncrementWindowRefCount(); |
246 item_->AddObserver(this); | 245 item_->AddObserver(this); |
247 | 246 |
248 ArcNotificationSurfaceManager::Get()->AddObserver(this); | 247 auto* surface_manager = ArcNotificationSurfaceManager::Get(); |
249 exo::NotificationSurface* surface = | 248 if (surface_manager) { |
250 ArcNotificationSurfaceManager::Get()->GetSurface(notification_key_); | 249 surface_manager->AddObserver(this); |
251 if (surface) | 250 exo::NotificationSurface* surface = |
252 OnNotificationSurfaceAdded(surface); | 251 surface_manager->GetSurface(notification_key_); |
| 252 if (surface) |
| 253 OnNotificationSurfaceAdded(surface); |
| 254 } |
253 | 255 |
254 // Create a layer as an anchor to insert surface copy during a slide. | 256 // Create a layer as an anchor to insert surface copy during a slide. |
255 SetPaintToLayer(); | 257 SetPaintToLayer(); |
256 UpdatePreferredSize(); | 258 UpdatePreferredSize(); |
257 } | 259 } |
258 | 260 |
259 ArcCustomNotificationView::~ArcCustomNotificationView() { | 261 ArcCustomNotificationView::~ArcCustomNotificationView() { |
260 SetSurface(nullptr); | 262 SetSurface(nullptr); |
| 263 |
| 264 auto* surface_manager = ArcNotificationSurfaceManager::Get(); |
| 265 if (surface_manager) |
| 266 surface_manager->RemoveObserver(this); |
261 if (item_) { | 267 if (item_) { |
| 268 item_->RemoveObserver(this); |
262 item_->DecrementWindowRefCount(); | 269 item_->DecrementWindowRefCount(); |
263 item_->RemoveObserver(this); | |
264 } | 270 } |
265 | |
266 if (ArcNotificationSurfaceManager::Get()) | |
267 ArcNotificationSurfaceManager::Get()->RemoveObserver(this); | |
268 } | 271 } |
269 | 272 |
270 std::unique_ptr<message_center::CustomNotificationContentViewDelegate> | 273 std::unique_ptr<message_center::CustomNotificationContentViewDelegate> |
271 ArcCustomNotificationView::CreateContentViewDelegate() { | 274 ArcCustomNotificationView::CreateContentViewDelegate() { |
272 return base::MakeUnique<ArcCustomNotificationView::ContentViewDelegate>(this); | 275 return base::MakeUnique<ArcCustomNotificationView::ContentViewDelegate>(this); |
273 } | 276 } |
274 | 277 |
275 void ArcCustomNotificationView::CreateCloseButton() { | 278 void ArcCustomNotificationView::CreateCloseButton() { |
276 DCHECK(control_buttons_view_); | 279 DCHECK(control_buttons_view_); |
277 DCHECK(item_); | 280 DCHECK(item_); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 return; | 313 return; |
311 | 314 |
312 // Creates the control_buttons_view_, which collects all control buttons into | 315 // Creates the control_buttons_view_, which collects all control buttons into |
313 // a horizontal box. | 316 // a horizontal box. |
314 control_buttons_view_ = new views::View(); | 317 control_buttons_view_ = new views::View(); |
315 control_buttons_view_->SetLayoutManager( | 318 control_buttons_view_->SetLayoutManager( |
316 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0)); | 319 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0)); |
317 | 320 |
318 if (item_->IsOpeningSettingsSupported()) | 321 if (item_->IsOpeningSettingsSupported()) |
319 CreateSettingsButton(); | 322 CreateSettingsButton(); |
320 if (!item_->pinned()) | 323 if (!item_->GetPinned()) |
321 CreateCloseButton(); | 324 CreateCloseButton(); |
322 | 325 |
323 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); | 326 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); |
324 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | 327 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
325 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 328 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
326 params.parent = surface_->window(); | 329 params.parent = surface_->window(); |
327 | 330 |
328 floating_control_buttons_widget_.reset(new views::Widget); | 331 floating_control_buttons_widget_.reset(new views::Widget); |
329 floating_control_buttons_widget_->Init(params); | 332 floating_control_buttons_widget_->Init(params); |
330 floating_control_buttons_widget_->SetContentsView(control_buttons_view_); | 333 floating_control_buttons_widget_->SetContentsView(control_buttons_view_); |
(...skipping 28 matching lines...) Expand all Loading... |
359 surface_->window()->AddPreTargetHandler(event_forwarder_.get()); | 362 surface_->window()->AddPreTargetHandler(event_forwarder_.get()); |
360 | 363 |
361 MaybeCreateFloatingControlButtons(); | 364 MaybeCreateFloatingControlButtons(); |
362 | 365 |
363 if (GetWidget()) | 366 if (GetWidget()) |
364 AttachSurface(); | 367 AttachSurface(); |
365 } | 368 } |
366 } | 369 } |
367 | 370 |
368 void ArcCustomNotificationView::UpdatePreferredSize() { | 371 void ArcCustomNotificationView::UpdatePreferredSize() { |
369 gfx::Size preferred_size = | 372 gfx::Size preferred_size; |
370 surface_ ? surface_->GetSize() : item_ ? item_->snapshot().size() | 373 if (surface_) |
371 : gfx::Size(); | 374 preferred_size = surface_->GetSize(); |
| 375 else if (item_) |
| 376 preferred_size = item_->GetSnapshot().size(); |
| 377 |
372 if (preferred_size.IsEmpty()) | 378 if (preferred_size.IsEmpty()) |
373 return; | 379 return; |
374 | 380 |
375 if (preferred_size.width() != message_center::kNotificationWidth) { | 381 if (preferred_size.width() != message_center::kNotificationWidth) { |
376 const float scale = static_cast<float>(message_center::kNotificationWidth) / | 382 const float scale = static_cast<float>(message_center::kNotificationWidth) / |
377 preferred_size.width(); | 383 preferred_size.width(); |
378 preferred_size.SetSize(message_center::kNotificationWidth, | 384 preferred_size.SetSize(message_center::kNotificationWidth, |
379 preferred_size.height() * scale); | 385 preferred_size.height() * scale); |
380 } | 386 } |
381 | 387 |
(...skipping 26 matching lines...) Expand all Loading... |
408 if (target_visiblity) | 414 if (target_visiblity) |
409 floating_control_buttons_widget_->Show(); | 415 floating_control_buttons_widget_->Show(); |
410 else | 416 else |
411 floating_control_buttons_widget_->Hide(); | 417 floating_control_buttons_widget_->Hide(); |
412 } | 418 } |
413 | 419 |
414 void ArcCustomNotificationView::UpdatePinnedState() { | 420 void ArcCustomNotificationView::UpdatePinnedState() { |
415 if (!item_) | 421 if (!item_) |
416 return; | 422 return; |
417 | 423 |
418 if (item_->pinned() && close_button_) { | 424 if (item_->GetPinned() && close_button_) { |
419 control_buttons_view_->RemoveChildView(close_button_.get()); | 425 control_buttons_view_->RemoveChildView(close_button_.get()); |
420 close_button_.reset(); | 426 close_button_.reset(); |
421 Layout(); | 427 Layout(); |
422 } else if (!item_->pinned() && !close_button_) { | 428 } else if (!item_->GetPinned() && !close_button_) { |
423 CreateCloseButton(); | 429 CreateCloseButton(); |
424 Layout(); | 430 Layout(); |
425 } | 431 } |
426 } | 432 } |
427 | 433 |
428 void ArcCustomNotificationView::UpdateSnapshot() { | 434 void ArcCustomNotificationView::UpdateSnapshot() { |
429 // Bail if we have a |surface_| because it controls the sizes and paints UI. | 435 // Bail if we have a |surface_| because it controls the sizes and paints UI. |
430 if (surface_) | 436 if (surface_) |
431 return; | 437 return; |
432 | 438 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 control_button_color_animation_->Start(); | 472 control_button_color_animation_->Start(); |
467 } | 473 } |
468 | 474 |
469 bool ArcCustomNotificationView::ShouldUpdateControlButtonsColor() const { | 475 bool ArcCustomNotificationView::ShouldUpdateControlButtonsColor() const { |
470 // Don't update the control button color when we are about to be destroyed. | 476 // Don't update the control button color when we are about to be destroyed. |
471 if (!item_) | 477 if (!item_) |
472 return false; | 478 return false; |
473 | 479 |
474 if (settings_button_ && | 480 if (settings_button_ && |
475 settings_button_->background()->get_color() != | 481 settings_button_->background()->get_color() != |
476 GetControlButtonBackgroundColor(item_->shown_contents())) | 482 GetControlButtonBackgroundColor(item_->GetShownContents())) |
477 return true; | 483 return true; |
478 if (close_button_ && | 484 if (close_button_ && |
479 close_button_->background()->get_color() != | 485 close_button_->background()->get_color() != |
480 GetControlButtonBackgroundColor(item_->shown_contents())) | 486 GetControlButtonBackgroundColor(item_->GetShownContents())) |
481 return true; | 487 return true; |
482 return false; | 488 return false; |
483 } | 489 } |
484 | 490 |
485 void ArcCustomNotificationView::ViewHierarchyChanged( | 491 void ArcCustomNotificationView::ViewHierarchyChanged( |
486 const views::View::ViewHierarchyChangedDetails& details) { | 492 const views::View::ViewHierarchyChangedDetails& details) { |
487 views::Widget* widget = GetWidget(); | 493 views::Widget* widget = GetWidget(); |
488 | 494 |
489 if (!details.is_add) { | 495 if (!details.is_add) { |
490 // Resets slide helper when this view is removed from its parent. | 496 // Resets slide helper when this view is removed from its parent. |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 | 561 |
556 UpdateControlButtonsVisibility(); | 562 UpdateControlButtonsVisibility(); |
557 | 563 |
558 ash::wm::SnapWindowToPixelBoundary(surface_->window()); | 564 ash::wm::SnapWindowToPixelBoundary(surface_->window()); |
559 } | 565 } |
560 | 566 |
561 void ArcCustomNotificationView::OnPaint(gfx::Canvas* canvas) { | 567 void ArcCustomNotificationView::OnPaint(gfx::Canvas* canvas) { |
562 views::NativeViewHost::OnPaint(canvas); | 568 views::NativeViewHost::OnPaint(canvas); |
563 | 569 |
564 // Bail if there is a |surface_| or no item or no snapshot image. | 570 // Bail if there is a |surface_| or no item or no snapshot image. |
565 if (surface_ || !item_ || item_->snapshot().isNull()) | 571 if (surface_ || !item_ || item_->GetSnapshot().isNull()) |
566 return; | 572 return; |
567 const gfx::Rect contents_bounds = GetContentsBounds(); | 573 const gfx::Rect contents_bounds = GetContentsBounds(); |
568 canvas->DrawImageInt(item_->snapshot(), 0, 0, item_->snapshot().width(), | 574 canvas->DrawImageInt(item_->GetSnapshot(), 0, 0, item_->GetSnapshot().width(), |
569 item_->snapshot().height(), contents_bounds.x(), | 575 item_->GetSnapshot().height(), contents_bounds.x(), |
570 contents_bounds.y(), contents_bounds.width(), | 576 contents_bounds.y(), contents_bounds.width(), |
571 contents_bounds.height(), false); | 577 contents_bounds.height(), false); |
572 } | 578 } |
573 | 579 |
574 void ArcCustomNotificationView::OnKeyEvent(ui::KeyEvent* event) { | 580 void ArcCustomNotificationView::OnKeyEvent(ui::KeyEvent* event) { |
575 // Forward to parent CustomNotificationView to handle keyboard dismissal. | 581 // Forward to parent CustomNotificationView to handle keyboard dismissal. |
576 parent()->OnKeyEvent(event); | 582 parent()->OnKeyEvent(event); |
577 } | 583 } |
578 | 584 |
579 void ArcCustomNotificationView::OnGestureEvent(ui::GestureEvent* event) { | 585 void ArcCustomNotificationView::OnGestureEvent(ui::GestureEvent* event) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
635 const ui::AXActionData& action_data) { | 641 const ui::AXActionData& action_data) { |
636 if (item_ && action_data.action == ui::AX_ACTION_DO_DEFAULT) { | 642 if (item_ && action_data.action == ui::AX_ACTION_DO_DEFAULT) { |
637 item_->ToggleExpansion(); | 643 item_->ToggleExpansion(); |
638 return true; | 644 return true; |
639 } | 645 } |
640 return false; | 646 return false; |
641 } | 647 } |
642 | 648 |
643 void ArcCustomNotificationView::ButtonPressed(views::Button* sender, | 649 void ArcCustomNotificationView::ButtonPressed(views::Button* sender, |
644 const ui::Event& event) { | 650 const ui::Event& event) { |
645 if (item_ && !item_->pinned() && sender == close_button_.get()) { | 651 if (item_ && !item_->GetPinned() && sender == close_button_.get()) { |
646 CHECK_EQ(message_center::CustomNotificationView::kViewClassName, | 652 CHECK_EQ(message_center::CustomNotificationView::kViewClassName, |
647 parent()->GetClassName()); | 653 parent()->GetClassName()); |
648 static_cast<message_center::CustomNotificationView*>(parent()) | 654 static_cast<message_center::CustomNotificationView*>(parent()) |
649 ->OnCloseButtonPressed(); | 655 ->OnCloseButtonPressed(); |
650 } | 656 } |
651 if (item_ && settings_button_ && sender == settings_button_) { | 657 if (item_ && settings_button_ && sender == settings_button_) { |
652 item_->OpenSettings(); | 658 item_->OpenSettings(); |
653 } | 659 } |
654 } | 660 } |
655 | 661 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
705 DCHECK_EQ(animation, control_button_color_animation_.get()); | 711 DCHECK_EQ(animation, control_button_color_animation_.get()); |
706 control_button_color_animation_.reset(); | 712 control_button_color_animation_.reset(); |
707 } | 713 } |
708 | 714 |
709 void ArcCustomNotificationView::AnimationProgressed( | 715 void ArcCustomNotificationView::AnimationProgressed( |
710 const gfx::Animation* animation) { | 716 const gfx::Animation* animation) { |
711 DCHECK_EQ(animation, control_button_color_animation_.get()); | 717 DCHECK_EQ(animation, control_button_color_animation_.get()); |
712 | 718 |
713 if (item_) { | 719 if (item_) { |
714 const SkColor target = | 720 const SkColor target = |
715 GetControlButtonBackgroundColor(item_->shown_contents()); | 721 GetControlButtonBackgroundColor(item_->GetShownContents()); |
716 const SkColor start = | 722 const SkColor start = |
717 target == message_center::kControlButtonBackgroundColor | 723 target == message_center::kControlButtonBackgroundColor |
718 ? SK_ColorTRANSPARENT | 724 ? SK_ColorTRANSPARENT |
719 : message_center::kControlButtonBackgroundColor; | 725 : message_center::kControlButtonBackgroundColor; |
720 const SkColor current_color = gfx::Tween::ColorValueBetween( | 726 const SkColor current_color = gfx::Tween::ColorValueBetween( |
721 animation->GetCurrentValue(), start, target); | 727 animation->GetCurrentValue(), start, target); |
722 if (settings_button_) { | 728 if (settings_button_) { |
723 settings_button_->set_background( | 729 settings_button_->set_background( |
724 views::Background::CreateSolidBackground(current_color)); | 730 views::Background::CreateSolidBackground(current_color)); |
725 settings_button_->SchedulePaint(); | 731 settings_button_->SchedulePaint(); |
726 } | 732 } |
727 if (close_button_) { | 733 if (close_button_) { |
728 close_button_->set_background( | 734 close_button_->set_background( |
729 views::Background::CreateSolidBackground(current_color)); | 735 views::Background::CreateSolidBackground(current_color)); |
730 close_button_->SchedulePaint(); | 736 close_button_->SchedulePaint(); |
731 } | 737 } |
732 } | 738 } |
733 } | 739 } |
734 | 740 |
735 } // namespace arc | 741 } // namespace arc |
OLD | NEW |