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 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
287 settings_button_ = new ControlButton(this); | 287 settings_button_ = new ControlButton(this); |
288 settings_button_->SetImage(views::CustomButton::STATE_NORMAL, | 288 settings_button_->SetImage(views::CustomButton::STATE_NORMAL, |
289 message_center::GetSettingsIcon()); | 289 message_center::GetSettingsIcon()); |
290 settings_button_->SetAccessibleName(l10n_util::GetStringUTF16( | 290 settings_button_->SetAccessibleName(l10n_util::GetStringUTF16( |
291 IDS_MESSAGE_NOTIFICATION_SETTINGS_BUTTON_ACCESSIBLE_NAME)); | 291 IDS_MESSAGE_NOTIFICATION_SETTINGS_BUTTON_ACCESSIBLE_NAME)); |
292 settings_button_->SetTooltipText(l10n_util::GetStringUTF16( | 292 settings_button_->SetTooltipText(l10n_util::GetStringUTF16( |
293 IDS_MESSAGE_NOTIFICATION_SETTINGS_BUTTON_ACCESSIBLE_NAME)); | 293 IDS_MESSAGE_NOTIFICATION_SETTINGS_BUTTON_ACCESSIBLE_NAME)); |
294 control_buttons_view_->AddChildView(settings_button_); | 294 control_buttons_view_->AddChildView(settings_button_); |
295 } | 295 } |
296 | 296 |
297 void ArcCustomNotificationView::CreateFloatingControlButtons() { | 297 void ArcCustomNotificationView::MaybeCreateFloatingControlButtons() { |
298 // Floating close button is a transient child of |surface_| and also part | 298 // Floating close button is a transient child of |surface_| and also part |
299 // of the hosting widget's focus chain. It could only be created when both | 299 // of the hosting widget's focus chain. It could only be created when both |
300 // are present. | 300 // are present. Further, if we are being destroyed (|item_| is null), don't |
301 if (!surface_ || !GetWidget()) | 301 // create the control buttons. |
302 if (!surface_ || !GetWidget() || !item_) | |
302 return; | 303 return; |
303 | 304 |
304 // Creates the control_buttons_view_, which collects all control buttons into | 305 // Creates the control_buttons_view_, which collects all control buttons into |
305 // a horizontal box. | 306 // a horizontal box. |
306 control_buttons_view_ = new views::View(); | 307 control_buttons_view_ = new views::View(); |
307 control_buttons_view_->SetLayoutManager( | 308 control_buttons_view_->SetLayoutManager( |
308 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0)); | 309 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0)); |
309 | 310 |
310 if (item_ && item_->IsOpeningSettingsSupported()) | 311 if (item_->IsOpeningSettingsSupported()) |
311 CreateSettingsButton(); | 312 CreateSettingsButton(); |
312 if (item_ && !item_->pinned()) | 313 if (!item_->pinned()) |
313 CreateCloseButton(); | 314 CreateCloseButton(); |
314 | 315 |
315 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); | 316 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); |
316 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | 317 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
317 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 318 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
318 params.parent = surface_->window(); | 319 params.parent = surface_->window(); |
319 | 320 |
320 floating_control_buttons_widget_.reset(new views::Widget); | 321 floating_control_buttons_widget_.reset(new views::Widget); |
321 floating_control_buttons_widget_->Init(params); | 322 floating_control_buttons_widget_->Init(params); |
322 floating_control_buttons_widget_->SetContentsView(control_buttons_view_); | 323 floating_control_buttons_widget_->SetContentsView(control_buttons_view_); |
(...skipping 20 matching lines...) Expand all Loading... | |
343 surface_->window()->RemoveObserver(this); | 344 surface_->window()->RemoveObserver(this); |
344 surface_->window()->RemovePreTargetHandler(event_forwarder_.get()); | 345 surface_->window()->RemovePreTargetHandler(event_forwarder_.get()); |
345 } | 346 } |
346 | 347 |
347 surface_ = surface; | 348 surface_ = surface; |
348 | 349 |
349 if (surface_ && surface_->window()) { | 350 if (surface_ && surface_->window()) { |
350 surface_->window()->AddObserver(this); | 351 surface_->window()->AddObserver(this); |
351 surface_->window()->AddPreTargetHandler(event_forwarder_.get()); | 352 surface_->window()->AddPreTargetHandler(event_forwarder_.get()); |
352 | 353 |
353 CreateFloatingControlButtons(); | 354 MaybeCreateFloatingControlButtons(); |
354 | 355 |
355 if (GetWidget()) | 356 if (GetWidget()) |
356 AttachSurface(); | 357 AttachSurface(); |
357 } | 358 } |
358 } | 359 } |
359 | 360 |
361 // N.B. |UpdatePreferredSize| calls SetPreferredSize, which may update the | |
362 // notification. If this is during a clear all operation, we may receive an | |
363 // OnItemDestroying event. That is, calling this method may indirectly cause | |
yhanada1
2017/04/21 08:06:49
method?
Eliot Courtney
2017/04/25 04:09:24
With crrev.com/2833403002 this comment is no longe
| |
364 // item_ to become null. | |
360 void ArcCustomNotificationView::UpdatePreferredSize() { | 365 void ArcCustomNotificationView::UpdatePreferredSize() { |
361 gfx::Size preferred_size = | 366 gfx::Size preferred_size = |
362 surface_ ? surface_->GetSize() : item_ ? item_->snapshot().size() | 367 surface_ ? surface_->GetSize() : item_ ? item_->snapshot().size() |
363 : gfx::Size(); | 368 : gfx::Size(); |
364 if (preferred_size.IsEmpty()) | 369 if (preferred_size.IsEmpty()) |
365 return; | 370 return; |
366 | 371 |
367 if (preferred_size.width() != message_center::kNotificationWidth) { | 372 if (preferred_size.width() != message_center::kNotificationWidth) { |
368 const float scale = static_cast<float>(message_center::kNotificationWidth) / | 373 const float scale = static_cast<float>(message_center::kNotificationWidth) / |
369 preferred_size.width(); | 374 preferred_size.width(); |
370 preferred_size.SetSize(message_center::kNotificationWidth, | 375 preferred_size.SetSize(message_center::kNotificationWidth, |
371 preferred_size.height() * scale); | 376 preferred_size.height() * scale); |
372 } | 377 } |
373 | 378 |
374 SetPreferredSize(preferred_size); | 379 SetPreferredSize(preferred_size); |
375 } | 380 } |
376 | 381 |
377 void ArcCustomNotificationView::UpdateControlButtonsVisibility() { | 382 void ArcCustomNotificationView::UpdateControlButtonsVisibility() { |
378 if (!surface_) | 383 if (!surface_) |
379 return; | 384 return; |
380 | 385 |
381 if (!floating_control_buttons_widget_) { | 386 if (!floating_control_buttons_widget_) |
382 if (GetWidget()) | 387 MaybeCreateFloatingControlButtons(); |
383 CreateFloatingControlButtons(); | 388 if (!floating_control_buttons_widget_) |
yoshiki
2017/04/21 15:21:38
I think the following structure is better for read
Eliot Courtney
2017/04/25 04:09:24
Done.
| |
384 else | 389 return; |
385 return; | |
386 } | |
387 | 390 |
388 const bool target_visiblity = | 391 const bool target_visiblity = |
389 IsMouseHovered() || (close_button_ && close_button_->HasFocus()) || | 392 IsMouseHovered() || (close_button_ && close_button_->HasFocus()) || |
390 (settings_button_ && settings_button_->HasFocus()); | 393 (settings_button_ && settings_button_->HasFocus()); |
391 if (target_visiblity == floating_control_buttons_widget_->IsVisible()) | 394 if (target_visiblity == floating_control_buttons_widget_->IsVisible()) |
392 return; | 395 return; |
393 | 396 |
394 if (target_visiblity) | 397 if (target_visiblity) |
395 floating_control_buttons_widget_->Show(); | 398 floating_control_buttons_widget_->Show(); |
396 else | 399 else |
397 floating_control_buttons_widget_->Hide(); | 400 floating_control_buttons_widget_->Hide(); |
398 } | 401 } |
399 | 402 |
400 void ArcCustomNotificationView::UpdatePinnedState() { | 403 void ArcCustomNotificationView::UpdatePinnedState() { |
401 DCHECK(item_); | 404 if (!item_) |
405 return; | |
402 | 406 |
403 if (item_->pinned() && close_button_) { | 407 if (item_->pinned() && close_button_) { |
404 control_buttons_view_->RemoveChildView(close_button_.get()); | 408 control_buttons_view_->RemoveChildView(close_button_.get()); |
405 close_button_.reset(); | 409 close_button_.reset(); |
406 Layout(); | 410 Layout(); |
407 } else if (!item_->pinned() && !close_button_) { | 411 } else if (!item_->pinned() && !close_button_) { |
408 CreateCloseButton(); | 412 CreateCloseButton(); |
409 Layout(); | 413 Layout(); |
410 } | 414 } |
411 } | 415 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
445 | 449 |
446 void ArcCustomNotificationView::StartControlButtonsColorAnimation() { | 450 void ArcCustomNotificationView::StartControlButtonsColorAnimation() { |
447 if (control_button_color_animation_) | 451 if (control_button_color_animation_) |
448 control_button_color_animation_->End(); | 452 control_button_color_animation_->End(); |
449 control_button_color_animation_.reset(new gfx::LinearAnimation(this)); | 453 control_button_color_animation_.reset(new gfx::LinearAnimation(this)); |
450 control_button_color_animation_->SetDuration(kBackgroundColorChangeDuration); | 454 control_button_color_animation_->SetDuration(kBackgroundColorChangeDuration); |
451 control_button_color_animation_->Start(); | 455 control_button_color_animation_->Start(); |
452 } | 456 } |
453 | 457 |
454 bool ArcCustomNotificationView::ShouldUpdateControlButtonsColor() const { | 458 bool ArcCustomNotificationView::ShouldUpdateControlButtonsColor() const { |
455 DCHECK(item_); | 459 // Don't update the control button color when we are about to be destroyed. |
460 if (!item_) | |
461 return false; | |
yoshiki
2017/04/21 15:21:38
I think it's better to check item_ in the caller i
Eliot Courtney
2017/04/25 04:09:24
I thought it would be good to create preconditions
| |
456 | 462 |
457 if (settings_button_ && | 463 if (settings_button_ && |
458 settings_button_->background()->get_color() != | 464 settings_button_->background()->get_color() != |
459 GetControlButtonBackgroundColor(item_->shown_contents())) | 465 GetControlButtonBackgroundColor(item_->shown_contents())) |
460 return true; | 466 return true; |
461 if (close_button_ && | 467 if (close_button_ && |
462 close_button_->background()->get_color() != | 468 close_button_->background()->get_color() != |
463 GetControlButtonBackgroundColor(item_->shown_contents())) | 469 GetControlButtonBackgroundColor(item_->shown_contents())) |
464 return true; | 470 return true; |
465 return false; | 471 return false; |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
718 } | 724 } |
719 if (close_button_) { | 725 if (close_button_) { |
720 close_button_->set_background( | 726 close_button_->set_background( |
721 views::Background::CreateSolidBackground(current_color)); | 727 views::Background::CreateSolidBackground(current_color)); |
722 close_button_->SchedulePaint(); | 728 close_button_->SchedulePaint(); |
723 } | 729 } |
724 } | 730 } |
725 } | 731 } |
726 | 732 |
727 } // namespace arc | 733 } // namespace arc |
OLD | NEW |