[email protected] | adbc18d | 2012-09-27 16:24:20 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Evan Stade | e9974e2 | 2017-09-23 00:02:26 | [diff] [blame] | 5 | #include "ash/message_center/message_center_bubble.h" |
[email protected] | adbc18d | 2012-09-27 16:24:20 | [diff] [blame] | 6 | |
Evan Stade | e9974e2 | 2017-09-23 00:02:26 | [diff] [blame] | 7 | #include "ash/message_center/message_center_view.h" |
avi | c326e4f5 | 2015-12-24 17:44:49 | [diff] [blame] | 8 | #include "base/macros.h" |
Evan Stade | 9cf7882 | 2017-08-16 18:32:13 | [diff] [blame] | 9 | #include "ui/message_center/message_center.h" |
Evan Stade | 353058c | 2017-09-08 21:31:13 | [diff] [blame] | 10 | #include "ui/message_center/public/cpp/message_center_constants.h" |
Evan Stade | 9cf7882 | 2017-08-16 18:32:13 | [diff] [blame] | 11 | #include "ui/views/bubble/tray_bubble_view.h" |
[email protected] | 8315892 | 2013-04-06 05:11:35 | [diff] [blame] | 12 | #include "ui/views/layout/fill_layout.h" |
[email protected] | adbc18d | 2012-09-27 16:24:20 | [diff] [blame] | 13 | #include "ui/views/widget/widget.h" |
| 14 | |
Evan Stade | 2b9cc98 | 2017-08-18 23:38:06 | [diff] [blame] | 15 | using message_center::MessageCenter; |
Evan Stade | 2b9cc98 | 2017-08-18 23:38:06 | [diff] [blame] | 16 | |
| 17 | namespace ash { |
[email protected] | adbc18d | 2012-09-27 16:24:20 | [diff] [blame] | 18 | |
Evan Stade | 9cf7882 | 2017-08-16 18:32:13 | [diff] [blame] | 19 | namespace { |
| 20 | const int kDefaultMaxHeight = 400; |
| 21 | } |
| 22 | |
[email protected] | 8315892 | 2013-04-06 05:11:35 | [diff] [blame] | 23 | // ContentsView //////////////////////////////////////////////////////////////// |
[email protected] | 15a0b17 | 2012-10-18 23:57:44 | [diff] [blame] | 24 | |
[email protected] | 8315892 | 2013-04-06 05:11:35 | [diff] [blame] | 25 | // Handles changes in MessageCenterView sizes. |
| 26 | class ContentsView : public views::View { |
[email protected] | 7416aa39 | 2013-01-17 01:31:41 | [diff] [blame] | 27 | public: |
[email protected] | 8315892 | 2013-04-06 05:11:35 | [diff] [blame] | 28 | explicit ContentsView(MessageCenterBubble* bubble, views::View* contents); |
dcheng | 34688b71 | 2014-10-28 00:36:41 | [diff] [blame] | 29 | ~ContentsView() override; |
[email protected] | 15a0b17 | 2012-10-18 23:57:44 | [diff] [blame] | 30 | |
[email protected] | 73662cf8 | 2013-03-08 03:55:38 | [diff] [blame] | 31 | // Overridden from views::View: |
dcheng | 34688b71 | 2014-10-28 00:36:41 | [diff] [blame] | 32 | int GetHeightForWidth(int width) const override; |
[email protected] | 15a0b17 | 2012-10-18 23:57:44 | [diff] [blame] | 33 | |
[email protected] | fbb4e49d | 2013-03-15 08:17:39 | [diff] [blame] | 34 | protected: |
| 35 | // Overridden from views::View: |
dcheng | 34688b71 | 2014-10-28 00:36:41 | [diff] [blame] | 36 | void ChildPreferredSizeChanged(View* child) override; |
[email protected] | fbb4e49d | 2013-03-15 08:17:39 | [diff] [blame] | 37 | |
[email protected] | 15a0b17 | 2012-10-18 23:57:44 | [diff] [blame] | 38 | private: |
[email protected] | f6bf26fe | 2013-04-24 19:33:40 | [diff] [blame] | 39 | base::WeakPtr<MessageCenterBubble> bubble_; |
[email protected] | 73662cf8 | 2013-03-08 03:55:38 | [diff] [blame] | 40 | |
[email protected] | 8315892 | 2013-04-06 05:11:35 | [diff] [blame] | 41 | DISALLOW_COPY_AND_ASSIGN(ContentsView); |
[email protected] | adbc18d | 2012-09-27 16:24:20 | [diff] [blame] | 42 | }; |
| 43 | |
[email protected] | 8315892 | 2013-04-06 05:11:35 | [diff] [blame] | 44 | ContentsView::ContentsView(MessageCenterBubble* bubble, views::View* contents) |
[email protected] | f6bf26fe | 2013-04-24 19:33:40 | [diff] [blame] | 45 | : bubble_(bubble->AsWeakPtr()) { |
[email protected] | 8315892 | 2013-04-06 05:11:35 | [diff] [blame] | 46 | SetLayoutManager(new views::FillLayout()); |
| 47 | AddChildView(contents); |
[email protected] | 63961255 | 2013-03-19 21:10:56 | [diff] [blame] | 48 | } |
| 49 | |
Chris Watkins | c24daf6 | 2017-11-28 03:43:09 | [diff] [blame^] | 50 | ContentsView::~ContentsView() = default; |
[email protected] | 63961255 | 2013-03-19 21:10:56 | [diff] [blame] | 51 | |
[email protected] | e1b62b7 | 2014-05-20 17:24:44 | [diff] [blame] | 52 | int ContentsView::GetHeightForWidth(int width) const { |
[email protected] | 8315892 | 2013-04-06 05:11:35 | [diff] [blame] | 53 | DCHECK_EQ(1, child_count()); |
| 54 | int contents_width = std::max(width - GetInsets().width(), 0); |
| 55 | int contents_height = child_at(0)->GetHeightForWidth(contents_width); |
| 56 | return contents_height + GetInsets().height(); |
[email protected] | 63961255 | 2013-03-19 21:10:56 | [diff] [blame] | 57 | } |
| 58 | |
[email protected] | 8315892 | 2013-04-06 05:11:35 | [diff] [blame] | 59 | void ContentsView::ChildPreferredSizeChanged(View* child) { |
| 60 | // TODO(dharcourt): Reduce the amount of updating this requires. |
[email protected] | f6bf26fe | 2013-04-24 19:33:40 | [diff] [blame] | 61 | if (bubble_.get()) |
| 62 | bubble_->bubble_view()->UpdateBubble(); |
[email protected] | 63961255 | 2013-03-19 21:10:56 | [diff] [blame] | 63 | } |
| 64 | |
[email protected] | 63961255 | 2013-03-19 21:10:56 | [diff] [blame] | 65 | // MessageCenterBubble ///////////////////////////////////////////////////////// |
| 66 | |
Evan Stade | a34eccc | 2017-11-14 04:53:11 | [diff] [blame] | 67 | MessageCenterBubble::MessageCenterBubble( |
| 68 | MessageCenter* message_center, |
| 69 | message_center::UiController* ui_controller) |
Evan Stade | 9cf7882 | 2017-08-16 18:32:13 | [diff] [blame] | 70 | : message_center_(message_center), |
Evan Stade | a34eccc | 2017-11-14 04:53:11 | [diff] [blame] | 71 | ui_controller_(ui_controller), |
Evan Stade | 9cf7882 | 2017-08-16 18:32:13 | [diff] [blame] | 72 | max_height_(kDefaultMaxHeight) {} |
[email protected] | adbc18d | 2012-09-27 16:24:20 | [diff] [blame] | 73 | |
[email protected] | 63961255 | 2013-03-19 21:10:56 | [diff] [blame] | 74 | MessageCenterBubble::~MessageCenterBubble() { |
yoshiki | 58f821b3 | 2016-06-28 05:28:59 | [diff] [blame] | 75 | // Removs this from the widget observers just in case. MessageCenterBubble |
| 76 | // might be destoryed without calling its Widget's Close/CloseNow. |
Evan Stade | 9cf7882 | 2017-08-16 18:32:13 | [diff] [blame] | 77 | if (bubble_view_ && bubble_view_->GetWidget()) |
| 78 | bubble_view_->GetWidget()->RemoveObserver(this); |
| 79 | if (bubble_view_) |
| 80 | bubble_view_->ResetDelegate(); |
| 81 | } |
| 82 | |
| 83 | void MessageCenterBubble::BubbleViewDestroyed() { |
| 84 | bubble_view_ = nullptr; |
| 85 | message_center_view_ = nullptr; |
| 86 | } |
| 87 | |
| 88 | void MessageCenterBubble::SetMaxHeight(int height) { |
| 89 | // Maximum height makes sense only for the new design. |
| 90 | if (height == 0) |
| 91 | height = kDefaultMaxHeight; |
| 92 | if (height == max_height_) |
| 93 | return; |
| 94 | |
| 95 | max_height_ = height; |
| 96 | if (bubble_view_) |
| 97 | bubble_view_->SetMaxHeight(max_height_); |
[email protected] | 63961255 | 2013-03-19 21:10:56 | [diff] [blame] | 98 | } |
[email protected] | adbc18d | 2012-09-27 16:24:20 | [diff] [blame] | 99 | |
[email protected] | 940c2ba | 2013-06-20 17:13:58 | [diff] [blame] | 100 | void MessageCenterBubble::SetSettingsVisible() { |
| 101 | if (message_center_view_) |
| 102 | message_center_view_->SetSettingsVisible(true); |
| 103 | else |
| 104 | initially_settings_visible_ = true; |
| 105 | } |
| 106 | |
[email protected] | f8846e2 | 2012-10-30 05:12:12 | [diff] [blame] | 107 | void MessageCenterBubble::InitializeContents( |
[email protected] | 26fc10e | 2012-11-29 05:36:22 | [diff] [blame] | 108 | views::TrayBubbleView* new_bubble_view) { |
Evan Stade | 9cf7882 | 2017-08-16 18:32:13 | [diff] [blame] | 109 | bubble_view_ = new_bubble_view; |
| 110 | bubble_view_->GetWidget()->AddObserver(this); |
Evan Stade | a34eccc | 2017-11-14 04:53:11 | [diff] [blame] | 111 | message_center_view_ = |
| 112 | new MessageCenterView(message_center_, ui_controller_, max_height_, |
| 113 | initially_settings_visible_); |
Evan Stade | 9cf7882 | 2017-08-16 18:32:13 | [diff] [blame] | 114 | bubble_view_->AddChildView(new ContentsView(this, message_center_view_)); |
yhanada | fff4cd8 | 2017-03-02 00:06:25 | [diff] [blame] | 115 | message_center_view_->Init(); |
[email protected] | 2aa1329b | 2013-02-27 21:33:41 | [diff] [blame] | 116 | // Resize the content of the bubble view to the given bubble size. This is |
| 117 | // necessary in case of the bubble border forcing a bigger size then the |
| 118 | // |new_bubble_view| actually wants. See crbug.com/169390. |
Evan Stade | 9cf7882 | 2017-08-16 18:32:13 | [diff] [blame] | 119 | bubble_view_->Layout(); |
[email protected] | 15a0b17 | 2012-10-18 23:57:44 | [diff] [blame] | 120 | UpdateBubbleView(); |
[email protected] | 15a0b17 | 2012-10-18 23:57:44 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | b85a5ae | 2013-08-05 19:32:50 | [diff] [blame] | 123 | void MessageCenterBubble::OnWidgetClosing(views::Widget* widget) { |
Evan Stade | 9cf7882 | 2017-08-16 18:32:13 | [diff] [blame] | 124 | if (bubble_view_ && bubble_view_->GetWidget()) |
| 125 | bubble_view_->GetWidget()->RemoveObserver(this); |
[email protected] | b85a5ae | 2013-08-05 19:32:50 | [diff] [blame] | 126 | if (message_center_view_) |
| 127 | message_center_view_->SetIsClosing(true); |
| 128 | } |
| 129 | |
Evan Stade | 9cf7882 | 2017-08-16 18:32:13 | [diff] [blame] | 130 | bool MessageCenterBubble::IsVisible() const { |
| 131 | return bubble_view() && bubble_view()->GetWidget()->IsVisible(); |
[email protected] | 15a0b17 | 2012-10-18 23:57:44 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | size_t MessageCenterBubble::NumMessageViewsForTest() const { |
[email protected] | 8315892 | 2013-04-06 05:11:35 | [diff] [blame] | 135 | return message_center_view_->NumMessageViewsForTest(); |
[email protected] | 15a0b17 | 2012-10-18 23:57:44 | [diff] [blame] | 136 | } |
| 137 | |
Evan Stade | 9cf7882 | 2017-08-16 18:32:13 | [diff] [blame] | 138 | void MessageCenterBubble::UpdateBubbleView() { |
| 139 | if (!bubble_view_) |
| 140 | return; // Could get called after view is closed |
Evan Stade | 2b9cc98 | 2017-08-18 23:38:06 | [diff] [blame] | 141 | message_center_view_->SetNotifications( |
| 142 | message_center_->GetVisibleNotifications()); |
Evan Stade | 9cf7882 | 2017-08-16 18:32:13 | [diff] [blame] | 143 | bubble_view_->GetWidget()->Show(); |
| 144 | bubble_view_->UpdateBubble(); |
| 145 | } |
| 146 | |
Evan Stade | 2b9cc98 | 2017-08-18 23:38:06 | [diff] [blame] | 147 | } // namespace ash |