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" |
| 10 #include "ui/base/l10n/l10n_util.h" |
| 11 #include "ui/base/resource/resource_bundle.h" |
| 12 #include "ui/resources/grit/ui_resources.h" |
| 13 #include "ui/strings/grit/ui_strings.h" |
| 14 #include "ui/views/background.h" |
| 15 #include "ui/views/border.h" |
| 16 #include "ui/views/controls/button/image_button.h" |
9 #include "ui/views/widget/widget.h" | 17 #include "ui/views/widget/widget.h" |
10 | 18 |
11 namespace arc { | 19 namespace arc { |
12 | 20 |
13 ArcCustomNotificationView::ArcCustomNotificationView( | 21 ArcCustomNotificationView::ArcCustomNotificationView( |
| 22 ArcCustomNotificationItem* item, |
14 exo::NotificationSurface* surface) | 23 exo::NotificationSurface* surface) |
15 : surface_(surface) {} | 24 : item_(item), surface_(surface) { |
| 25 item_->AddObserver(this); |
| 26 OnItemPinnedChanged(); |
| 27 } |
16 | 28 |
17 ArcCustomNotificationView::~ArcCustomNotificationView() {} | 29 ArcCustomNotificationView::~ArcCustomNotificationView() { |
| 30 if (item_) |
| 31 item_->RemoveObserver(this); |
| 32 } |
| 33 |
| 34 void ArcCustomNotificationView::CreateFloatingCloseButton() { |
| 35 floating_close_button_ = new views::ImageButton(this); |
| 36 floating_close_button_->set_background( |
| 37 views::Background::CreateSolidBackground(SK_ColorTRANSPARENT)); |
| 38 floating_close_button_->SetBorder( |
| 39 views::Border::CreateEmptyBorder(5, 5, 5, 5)); |
| 40 |
| 41 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 42 floating_close_button_->SetImage( |
| 43 views::CustomButton::STATE_NORMAL, |
| 44 rb.GetImageSkiaNamed(IDR_NOTIFICATION_CLOSE)); |
| 45 floating_close_button_->SetImage( |
| 46 views::CustomButton::STATE_HOVERED, |
| 47 rb.GetImageSkiaNamed(IDR_NOTIFICATION_CLOSE_HOVER)); |
| 48 floating_close_button_->SetImage( |
| 49 views::CustomButton::STATE_PRESSED, |
| 50 rb.GetImageSkiaNamed(IDR_NOTIFICATION_CLOSE_PRESSED)); |
| 51 floating_close_button_->set_animate_on_state_change(false); |
| 52 floating_close_button_->SetAccessibleName(l10n_util::GetStringUTF16( |
| 53 IDS_MESSAGE_CENTER_CLOSE_NOTIFICATION_BUTTON_ACCESSIBLE_NAME)); |
| 54 |
| 55 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); |
| 56 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
| 57 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 58 params.parent = surface_->window(); |
| 59 |
| 60 floating_close_button_widget_.reset(new views::Widget); |
| 61 floating_close_button_widget_->Init(params); |
| 62 floating_close_button_widget_->SetContentsView(floating_close_button_); |
| 63 floating_close_button_widget_->Show(); |
| 64 |
| 65 Layout(); |
| 66 } |
18 | 67 |
19 void ArcCustomNotificationView::ViewHierarchyChanged( | 68 void ArcCustomNotificationView::ViewHierarchyChanged( |
20 const views::View::ViewHierarchyChangedDetails& details) { | 69 const views::View::ViewHierarchyChangedDetails& details) { |
21 views::Widget* widget = GetWidget(); | 70 views::Widget* widget = GetWidget(); |
22 | 71 |
23 // Bail if native_view() has attached to a different widget. | 72 // Bail if native_view() has attached to a different widget. |
24 if (widget && native_view() && | 73 if (widget && native_view() && |
25 views::Widget::GetTopLevelWidgetForNativeView(native_view()) != widget) { | 74 views::Widget::GetTopLevelWidgetForNativeView(native_view()) != widget) { |
26 return; | 75 return; |
27 } | 76 } |
28 | 77 |
29 views::NativeViewHost::ViewHierarchyChanged(details); | 78 views::NativeViewHost::ViewHierarchyChanged(details); |
30 | 79 |
31 if (!widget || !details.is_add) | 80 if (!widget || !details.is_add) |
32 return; | 81 return; |
33 | 82 |
34 SetPreferredSize(surface_->GetSize()); | 83 SetPreferredSize(surface_->GetSize()); |
35 Attach(surface_->window()); | 84 Attach(surface_->window()); |
36 } | 85 } |
37 | 86 |
| 87 void ArcCustomNotificationView::Layout() { |
| 88 views::NativeViewHost::Layout(); |
| 89 |
| 90 if (!floating_close_button_widget_ || !GetWidget()) |
| 91 return; |
| 92 |
| 93 gfx::Rect surface_local_bounds(surface_->window()->bounds().size()); |
| 94 gfx::Rect close_button_bounds(floating_close_button_->GetPreferredSize()); |
| 95 close_button_bounds.set_x(surface_local_bounds.right() - |
| 96 close_button_bounds.width()); |
| 97 close_button_bounds.set_y(surface_local_bounds.y()); |
| 98 floating_close_button_widget_->SetBounds(close_button_bounds); |
| 99 } |
| 100 |
| 101 void ArcCustomNotificationView::ButtonPressed(views::Button* sender, |
| 102 const ui::Event& event) { |
| 103 if (item_ && !item_->pinned() && sender == floating_close_button_) { |
| 104 item_->CloseFromCloseButton(); |
| 105 } |
| 106 } |
| 107 |
| 108 void ArcCustomNotificationView::OnItemDestroying() { |
| 109 item_->RemoveObserver(this); |
| 110 item_ = nullptr; |
| 111 } |
| 112 |
| 113 void ArcCustomNotificationView::OnItemPinnedChanged() { |
| 114 if (item_->pinned() && floating_close_button_widget_) { |
| 115 floating_close_button_widget_.reset(); |
| 116 } else if (!item_->pinned() && !floating_close_button_widget_) { |
| 117 CreateFloatingCloseButton(); |
| 118 } |
| 119 } |
| 120 |
38 } // namespace arc | 121 } // namespace arc |
OLD | NEW |