| 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 "components/exo/notification_surface.h" | 7 #include "components/exo/notification_surface.h" |
| 8 #include "components/exo/surface.h" | 8 #include "components/exo/surface.h" |
| 9 #include "third_party/skia/include/core/SkColor.h" | 9 #include "third_party/skia/include/core/SkColor.h" |
| 10 #include "ui/base/l10n/l10n_util.h" | 10 #include "ui/base/l10n/l10n_util.h" |
| 11 #include "ui/base/resource/resource_bundle.h" | 11 #include "ui/base/resource/resource_bundle.h" |
| 12 #include "ui/message_center/message_center_style.h" | 12 #include "ui/message_center/message_center_style.h" |
| 13 #include "ui/resources/grit/ui_resources.h" | 13 #include "ui/resources/grit/ui_resources.h" |
| 14 #include "ui/strings/grit/ui_strings.h" | 14 #include "ui/strings/grit/ui_strings.h" |
| 15 #include "ui/views/background.h" | 15 #include "ui/views/background.h" |
| 16 #include "ui/views/border.h" | 16 #include "ui/views/border.h" |
| 17 #include "ui/views/controls/button/image_button.h" | 17 #include "ui/views/controls/button/image_button.h" |
| 18 #include "ui/views/widget/widget.h" | 18 #include "ui/views/widget/widget.h" |
| 19 | 19 |
| 20 namespace arc { | 20 namespace arc { |
| 21 | 21 |
| 22 ArcCustomNotificationView::ArcCustomNotificationView( | 22 ArcCustomNotificationView::ArcCustomNotificationView( |
| 23 ArcCustomNotificationItem* item, | 23 ArcCustomNotificationItem* item, |
| 24 exo::NotificationSurface* surface) | 24 exo::NotificationSurface* surface) |
| 25 : item_(item), surface_(surface) { | 25 : item_(item), surface_(surface) { |
| 26 item_->AddObserver(this); | 26 item_->AddObserver(this); |
| 27 OnItemPinnedChanged(); | 27 OnItemPinnedChanged(); |
| 28 surface_->window()->AddObserver(this); |
| 28 } | 29 } |
| 29 | 30 |
| 30 ArcCustomNotificationView::~ArcCustomNotificationView() { | 31 ArcCustomNotificationView::~ArcCustomNotificationView() { |
| 31 if (item_) | 32 if (item_) |
| 32 item_->RemoveObserver(this); | 33 item_->RemoveObserver(this); |
| 34 if (surface_ && surface_->window()) |
| 35 surface_->window()->RemoveObserver(this); |
| 33 } | 36 } |
| 34 | 37 |
| 35 void ArcCustomNotificationView::CreateFloatingCloseButton() { | 38 void ArcCustomNotificationView::CreateFloatingCloseButton() { |
| 36 floating_close_button_ = new views::ImageButton(this); | 39 floating_close_button_ = new views::ImageButton(this); |
| 37 floating_close_button_->set_background( | 40 floating_close_button_->set_background( |
| 38 views::Background::CreateSolidBackground(SK_ColorTRANSPARENT)); | 41 views::Background::CreateSolidBackground(SK_ColorTRANSPARENT)); |
| 39 floating_close_button_->SetBorder( | 42 floating_close_button_->SetBorder( |
| 40 views::Border::CreateEmptyBorder(5, 5, 5, 5)); | 43 views::Border::CreateEmptyBorder(5, 5, 5, 5)); |
| 41 | 44 |
| 42 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 45 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 59 params.parent = surface_->window(); | 62 params.parent = surface_->window(); |
| 60 | 63 |
| 61 floating_close_button_widget_.reset(new views::Widget); | 64 floating_close_button_widget_.reset(new views::Widget); |
| 62 floating_close_button_widget_->Init(params); | 65 floating_close_button_widget_->Init(params); |
| 63 floating_close_button_widget_->SetContentsView(floating_close_button_); | 66 floating_close_button_widget_->SetContentsView(floating_close_button_); |
| 64 floating_close_button_widget_->Show(); | 67 floating_close_button_widget_->Show(); |
| 65 | 68 |
| 66 Layout(); | 69 Layout(); |
| 67 } | 70 } |
| 68 | 71 |
| 72 void ArcCustomNotificationView::UpdatePreferredSize() { |
| 73 gfx::Size preferred_size = surface_->GetSize(); |
| 74 if (preferred_size.width() != message_center::kNotificationWidth) { |
| 75 const float scale = static_cast<float>(message_center::kNotificationWidth) / |
| 76 preferred_size.width(); |
| 77 preferred_size.SetSize(message_center::kNotificationWidth, |
| 78 preferred_size.height() * scale); |
| 79 } |
| 80 |
| 81 SetPreferredSize(preferred_size); |
| 82 } |
| 83 |
| 69 void ArcCustomNotificationView::ViewHierarchyChanged( | 84 void ArcCustomNotificationView::ViewHierarchyChanged( |
| 70 const views::View::ViewHierarchyChangedDetails& details) { | 85 const views::View::ViewHierarchyChangedDetails& details) { |
| 71 views::Widget* widget = GetWidget(); | 86 views::Widget* widget = GetWidget(); |
| 72 | 87 |
| 73 // Bail if native_view() has attached to a different widget. | 88 // Bail if native_view() has attached to a different widget. |
| 74 if (widget && native_view() && | 89 if (widget && native_view() && |
| 75 views::Widget::GetTopLevelWidgetForNativeView(native_view()) != widget) { | 90 views::Widget::GetTopLevelWidgetForNativeView(native_view()) != widget) { |
| 76 return; | 91 return; |
| 77 } | 92 } |
| 78 | 93 |
| 79 views::NativeViewHost::ViewHierarchyChanged(details); | 94 views::NativeViewHost::ViewHierarchyChanged(details); |
| 80 | 95 |
| 81 if (!widget || !surface_ || !details.is_add) | 96 if (!widget || !surface_ || !details.is_add) |
| 82 return; | 97 return; |
| 83 | 98 |
| 84 gfx::Size preferred_size = surface_->GetSize(); | 99 UpdatePreferredSize(); |
| 85 if (preferred_size.width() != message_center::kNotificationWidth) { | |
| 86 const float scale = static_cast<float>(message_center::kNotificationWidth) / | |
| 87 preferred_size.width(); | |
| 88 preferred_size.SetSize(message_center::kNotificationWidth, | |
| 89 preferred_size.height() * scale); | |
| 90 } | |
| 91 | |
| 92 SetPreferredSize(preferred_size); | |
| 93 Attach(surface_->window()); | 100 Attach(surface_->window()); |
| 94 } | 101 } |
| 95 | 102 |
| 96 void ArcCustomNotificationView::Layout() { | 103 void ArcCustomNotificationView::Layout() { |
| 97 views::NativeViewHost::Layout(); | 104 views::NativeViewHost::Layout(); |
| 98 | 105 |
| 99 if (!surface_ || !GetWidget()) | 106 if (!surface_ || !GetWidget()) |
| 100 return; | 107 return; |
| 101 | 108 |
| 102 // Scale notification surface if necessary. | 109 // Scale notification surface if necessary. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 121 floating_close_button_widget_->SetBounds(close_button_bounds); | 128 floating_close_button_widget_->SetBounds(close_button_bounds); |
| 122 } | 129 } |
| 123 | 130 |
| 124 void ArcCustomNotificationView::ButtonPressed(views::Button* sender, | 131 void ArcCustomNotificationView::ButtonPressed(views::Button* sender, |
| 125 const ui::Event& event) { | 132 const ui::Event& event) { |
| 126 if (item_ && !item_->pinned() && sender == floating_close_button_) { | 133 if (item_ && !item_->pinned() && sender == floating_close_button_) { |
| 127 item_->CloseFromCloseButton(); | 134 item_->CloseFromCloseButton(); |
| 128 } | 135 } |
| 129 } | 136 } |
| 130 | 137 |
| 138 void ArcCustomNotificationView::OnWindowBoundsChanged(aura::Window* window, |
| 139 const gfx::Rect& old_bounds, |
| 140 const gfx::Rect& new_bounds) { |
| 141 UpdatePreferredSize(); |
| 142 } |
| 143 |
| 144 void ArcCustomNotificationView::OnWindowDestroying(aura::Window* window) { |
| 145 window->RemoveObserver(this); |
| 146 } |
| 147 |
| 131 void ArcCustomNotificationView::OnItemDestroying() { | 148 void ArcCustomNotificationView::OnItemDestroying() { |
| 132 item_->RemoveObserver(this); | 149 item_->RemoveObserver(this); |
| 133 item_ = nullptr; | 150 item_ = nullptr; |
| 134 | 151 |
| 135 // Reset |surface_| with |item_| since no one is observing the |surface_| | 152 // Reset |surface_| with |item_| since no one is observing the |surface_| |
| 136 // after |item_| is gone and this view should be removed soon. | 153 // after |item_| is gone and this view should be removed soon. |
| 137 surface_ = nullptr; | 154 surface_ = nullptr; |
| 138 } | 155 } |
| 139 | 156 |
| 140 void ArcCustomNotificationView::OnItemPinnedChanged() { | 157 void ArcCustomNotificationView::OnItemPinnedChanged() { |
| 141 if (item_->pinned() && floating_close_button_widget_) { | 158 if (item_->pinned() && floating_close_button_widget_) { |
| 142 floating_close_button_widget_.reset(); | 159 floating_close_button_widget_.reset(); |
| 143 } else if (!item_->pinned() && !floating_close_button_widget_) { | 160 } else if (!item_->pinned() && !floating_close_button_widget_) { |
| 144 CreateFloatingCloseButton(); | 161 CreateFloatingCloseButton(); |
| 145 } | 162 } |
| 146 } | 163 } |
| 147 | 164 |
| 148 void ArcCustomNotificationView::OnItemNotificationSurfaceRemoved() { | 165 void ArcCustomNotificationView::OnItemNotificationSurfaceRemoved() { |
| 149 surface_ = nullptr; | 166 surface_ = nullptr; |
| 150 } | 167 } |
| 151 | 168 |
| 152 } // namespace arc | 169 } // namespace arc |
| OLD | NEW |