Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(168)

Unified Diff: ui/arc/notification/arc_custom_notification_view.cc

Issue 2269403004: arc: Defer notification surface creation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase and fix compile Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/arc/notification/arc_custom_notification_view.cc
diff --git a/ui/arc/notification/arc_custom_notification_view.cc b/ui/arc/notification/arc_custom_notification_view.cc
index b17c2344b27f5c27eac41b543205558d04edd53c..8e0c9c9068d7037766198343a7d8bc3f42f34299 100644
--- a/ui/arc/notification/arc_custom_notification_view.cc
+++ b/ui/arc/notification/arc_custom_notification_view.cc
@@ -12,6 +12,8 @@
#include "ui/compositor/layer_animation_observer.h"
#include "ui/display/screen.h"
#include "ui/events/event_handler.h"
+#include "ui/gfx/canvas.h"
+#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/transform.h"
#include "ui/message_center/message_center_style.h"
#include "ui/resources/grit/ui_resources.h"
@@ -114,24 +116,39 @@ class ArcCustomNotificationView::SlideHelper
};
ArcCustomNotificationView::ArcCustomNotificationView(
- ArcCustomNotificationItem* item,
- exo::NotificationSurface* surface)
- : item_(item), event_forwarder_(new EventForwarder(this)) {
- SetSurface(surface);
+ ArcCustomNotificationItem* item)
+ : item_(item),
+ notification_key_(item->notification_key()),
+ event_forwarder_(new EventForwarder(this)) {
+ item_->IncrementWindowRefCount();
item_->AddObserver(this);
- OnItemPinnedChanged();
+
+ ArcNotificationSurfaceManager::Get()->AddObserver(this);
+ exo::NotificationSurface* surface =
+ ArcNotificationSurfaceManager::Get()->GetSurface(notification_key_);
+ if (surface)
+ OnNotificationSurfaceAdded(surface);
// Create a layer as an anchor to insert surface copy during a slide.
SetPaintToLayer(true);
+ UpdatePreferredSize();
}
ArcCustomNotificationView::~ArcCustomNotificationView() {
SetSurface(nullptr);
- if (item_)
+ if (item_) {
+ item_->DecrementWindowRefCount();
item_->RemoveObserver(this);
+ }
+
+ if (ArcNotificationSurfaceManager::Get())
+ ArcNotificationSurfaceManager::Get()->RemoveObserver(this);
}
void ArcCustomNotificationView::CreateFloatingCloseButton() {
+ if (!surface_)
+ return;
+
floating_close_button_ = new views::ImageButton(this);
floating_close_button_->set_background(
views::Background::CreateSolidBackground(SK_ColorTRANSPARENT));
@@ -179,11 +196,21 @@ void ArcCustomNotificationView::SetSurface(exo::NotificationSurface* surface) {
if (surface_ && surface_->window()) {
surface_->window()->AddObserver(this);
surface_->window()->AddPreTargetHandler(event_forwarder_.get());
+
+ if (GetWidget())
+ AttachSurface();
+
+ UpdatePinnedState();
}
}
void ArcCustomNotificationView::UpdatePreferredSize() {
- gfx::Size preferred_size = surface_->GetSize();
+ gfx::Size preferred_size =
+ surface_ ? surface_->GetSize() : item_ ? item_->snapshot().size()
+ : gfx::Size();
+ if (preferred_size.IsEmpty())
+ return;
+
if (preferred_size.width() != message_center::kNotificationWidth) {
const float scale = static_cast<float>(message_center::kNotificationWidth) /
preferred_size.width();
@@ -210,6 +237,34 @@ void ArcCustomNotificationView::UpdateCloseButtonVisiblity() {
floating_close_button_widget_->Hide();
}
+void ArcCustomNotificationView::UpdatePinnedState() {
+ if (item_->pinned() && floating_close_button_widget_) {
+ floating_close_button_widget_.reset();
+ } else if (!item_->pinned() && !floating_close_button_widget_) {
+ CreateFloatingCloseButton();
+ }
+}
+
+void ArcCustomNotificationView::UpdateSnapshot() {
+ // Bail if we have a |surface_| because it controls the sizes and paints UI.
+ if (surface_)
+ return;
+
+ UpdatePreferredSize();
+ SchedulePaint();
+}
+
+void ArcCustomNotificationView::AttachSurface() {
+ if (!GetWidget())
+ return;
+
+ UpdatePreferredSize();
+ Attach(surface_->window());
+
+ // Creates slide helper after this view is added to its parent.
+ slide_helper_.reset(new SlideHelper(this));
+}
+
void ArcCustomNotificationView::ViewHierarchyChanged(
const views::View::ViewHierarchyChangedDetails& details) {
views::Widget* widget = GetWidget();
@@ -230,11 +285,7 @@ void ArcCustomNotificationView::ViewHierarchyChanged(
if (!widget || !surface_ || !details.is_add)
return;
- UpdatePreferredSize();
- Attach(surface_->window());
-
- // Creates slide helper after this view is added to its parent.
- slide_helper_.reset(new SlideHelper(this));
+ AttachSurface();
}
void ArcCustomNotificationView::Layout() {
@@ -267,6 +318,19 @@ void ArcCustomNotificationView::Layout() {
UpdateCloseButtonVisiblity();
}
+void ArcCustomNotificationView::OnPaint(gfx::Canvas* canvas) {
+ views::NativeViewHost::OnPaint(canvas);
+
+ // Bail if there is a |surface_| or no item or no snapshot image.
+ if (surface_ || !item_ || item_->snapshot().isNull())
+ return;
+ const gfx::Rect contents_bounds = GetContentsBounds();
+ canvas->DrawImageInt(item_->snapshot(), 0, 0, item_->snapshot().width(),
+ item_->snapshot().height(), contents_bounds.x(),
+ contents_bounds.y(), contents_bounds.width(),
+ contents_bounds.height(), false);
+}
+
void ArcCustomNotificationView::OnKeyEvent(ui::KeyEvent* event) {
// Forward to parent CustomNotificationView to handle keyboard dismissal.
parent()->OnKeyEvent(event);
@@ -301,7 +365,7 @@ void ArcCustomNotificationView::OnWindowBoundsChanged(
}
void ArcCustomNotificationView::OnWindowDestroying(aura::Window* window) {
- window->RemoveObserver(this);
+ SetSurface(nullptr);
}
void ArcCustomNotificationView::OnItemDestroying() {
@@ -313,15 +377,24 @@ void ArcCustomNotificationView::OnItemDestroying() {
SetSurface(nullptr);
}
-void ArcCustomNotificationView::OnItemPinnedChanged() {
- if (item_->pinned() && floating_close_button_widget_) {
- floating_close_button_widget_.reset();
- } else if (!item_->pinned() && !floating_close_button_widget_) {
- CreateFloatingCloseButton();
- }
+void ArcCustomNotificationView::OnItemUpdated() {
+ UpdatePinnedState();
+ UpdateSnapshot();
}
-void ArcCustomNotificationView::OnItemNotificationSurfaceRemoved() {
+void ArcCustomNotificationView::OnNotificationSurfaceAdded(
+ exo::NotificationSurface* surface) {
+ if (surface->notification_id() != notification_key_)
+ return;
+
+ SetSurface(surface);
+}
+
+void ArcCustomNotificationView::OnNotificationSurfaceRemoved(
+ exo::NotificationSurface* surface) {
+ if (surface->notification_id() != notification_key_)
+ return;
+
SetSurface(nullptr);
}

Powered by Google App Engine
This is Rietveld 408576698