blob: fee41f531b837243a7cf66c2120c91dde63de7f9 [file] [log] [blame]
[email protected]adbc18d2012-09-27 16:24:201// 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 Stadee9974e22017-09-23 00:02:265#include "ash/message_center/message_center_bubble.h"
[email protected]adbc18d2012-09-27 16:24:206
Evan Stadee9974e22017-09-23 00:02:267#include "ash/message_center/message_center_view.h"
avic326e4f52015-12-24 17:44:498#include "base/macros.h"
Evan Stade9cf78822017-08-16 18:32:139#include "ui/message_center/message_center.h"
Evan Stade353058c2017-09-08 21:31:1310#include "ui/message_center/public/cpp/message_center_constants.h"
Evan Stade9cf78822017-08-16 18:32:1311#include "ui/views/bubble/tray_bubble_view.h"
[email protected]83158922013-04-06 05:11:3512#include "ui/views/layout/fill_layout.h"
[email protected]adbc18d2012-09-27 16:24:2013#include "ui/views/widget/widget.h"
14
Evan Stade2b9cc982017-08-18 23:38:0615using message_center::MessageCenter;
Evan Stade2b9cc982017-08-18 23:38:0616
17namespace ash {
[email protected]adbc18d2012-09-27 16:24:2018
Evan Stade9cf78822017-08-16 18:32:1319namespace {
20const int kDefaultMaxHeight = 400;
21}
22
[email protected]83158922013-04-06 05:11:3523// ContentsView ////////////////////////////////////////////////////////////////
[email protected]15a0b172012-10-18 23:57:4424
[email protected]83158922013-04-06 05:11:3525// Handles changes in MessageCenterView sizes.
26class ContentsView : public views::View {
[email protected]7416aa392013-01-17 01:31:4127 public:
[email protected]83158922013-04-06 05:11:3528 explicit ContentsView(MessageCenterBubble* bubble, views::View* contents);
dcheng34688b712014-10-28 00:36:4129 ~ContentsView() override;
[email protected]15a0b172012-10-18 23:57:4430
[email protected]73662cf82013-03-08 03:55:3831 // Overridden from views::View:
dcheng34688b712014-10-28 00:36:4132 int GetHeightForWidth(int width) const override;
[email protected]15a0b172012-10-18 23:57:4433
[email protected]fbb4e49d2013-03-15 08:17:3934 protected:
35 // Overridden from views::View:
dcheng34688b712014-10-28 00:36:4136 void ChildPreferredSizeChanged(View* child) override;
[email protected]fbb4e49d2013-03-15 08:17:3937
[email protected]15a0b172012-10-18 23:57:4438 private:
[email protected]f6bf26fe2013-04-24 19:33:4039 base::WeakPtr<MessageCenterBubble> bubble_;
[email protected]73662cf82013-03-08 03:55:3840
[email protected]83158922013-04-06 05:11:3541 DISALLOW_COPY_AND_ASSIGN(ContentsView);
[email protected]adbc18d2012-09-27 16:24:2042};
43
[email protected]83158922013-04-06 05:11:3544ContentsView::ContentsView(MessageCenterBubble* bubble, views::View* contents)
[email protected]f6bf26fe2013-04-24 19:33:4045 : bubble_(bubble->AsWeakPtr()) {
[email protected]83158922013-04-06 05:11:3546 SetLayoutManager(new views::FillLayout());
47 AddChildView(contents);
[email protected]639612552013-03-19 21:10:5648}
49
Chris Watkinsc24daf62017-11-28 03:43:0950ContentsView::~ContentsView() = default;
[email protected]639612552013-03-19 21:10:5651
[email protected]e1b62b72014-05-20 17:24:4452int ContentsView::GetHeightForWidth(int width) const {
[email protected]83158922013-04-06 05:11:3553 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]639612552013-03-19 21:10:5657}
58
[email protected]83158922013-04-06 05:11:3559void ContentsView::ChildPreferredSizeChanged(View* child) {
60 // TODO(dharcourt): Reduce the amount of updating this requires.
[email protected]f6bf26fe2013-04-24 19:33:4061 if (bubble_.get())
62 bubble_->bubble_view()->UpdateBubble();
[email protected]639612552013-03-19 21:10:5663}
64
[email protected]639612552013-03-19 21:10:5665// MessageCenterBubble /////////////////////////////////////////////////////////
66
Evan Stadea34eccc2017-11-14 04:53:1167MessageCenterBubble::MessageCenterBubble(
68 MessageCenter* message_center,
69 message_center::UiController* ui_controller)
Evan Stade9cf78822017-08-16 18:32:1370 : message_center_(message_center),
Evan Stadea34eccc2017-11-14 04:53:1171 ui_controller_(ui_controller),
Evan Stade9cf78822017-08-16 18:32:1372 max_height_(kDefaultMaxHeight) {}
[email protected]adbc18d2012-09-27 16:24:2073
[email protected]639612552013-03-19 21:10:5674MessageCenterBubble::~MessageCenterBubble() {
yoshiki58f821b32016-06-28 05:28:5975 // Removs this from the widget observers just in case. MessageCenterBubble
76 // might be destoryed without calling its Widget's Close/CloseNow.
Evan Stade9cf78822017-08-16 18:32:1377 if (bubble_view_ && bubble_view_->GetWidget())
78 bubble_view_->GetWidget()->RemoveObserver(this);
79 if (bubble_view_)
80 bubble_view_->ResetDelegate();
81}
82
83void MessageCenterBubble::BubbleViewDestroyed() {
84 bubble_view_ = nullptr;
85 message_center_view_ = nullptr;
86}
87
88void 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]639612552013-03-19 21:10:5698}
[email protected]adbc18d2012-09-27 16:24:2099
[email protected]940c2ba2013-06-20 17:13:58100void MessageCenterBubble::SetSettingsVisible() {
101 if (message_center_view_)
102 message_center_view_->SetSettingsVisible(true);
103 else
104 initially_settings_visible_ = true;
105}
106
[email protected]f8846e22012-10-30 05:12:12107void MessageCenterBubble::InitializeContents(
[email protected]26fc10e2012-11-29 05:36:22108 views::TrayBubbleView* new_bubble_view) {
Evan Stade9cf78822017-08-16 18:32:13109 bubble_view_ = new_bubble_view;
110 bubble_view_->GetWidget()->AddObserver(this);
Evan Stadea34eccc2017-11-14 04:53:11111 message_center_view_ =
112 new MessageCenterView(message_center_, ui_controller_, max_height_,
113 initially_settings_visible_);
Evan Stade9cf78822017-08-16 18:32:13114 bubble_view_->AddChildView(new ContentsView(this, message_center_view_));
yhanadafff4cd82017-03-02 00:06:25115 message_center_view_->Init();
[email protected]2aa1329b2013-02-27 21:33:41116 // 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 Stade9cf78822017-08-16 18:32:13119 bubble_view_->Layout();
[email protected]15a0b172012-10-18 23:57:44120 UpdateBubbleView();
[email protected]15a0b172012-10-18 23:57:44121}
122
[email protected]b85a5ae2013-08-05 19:32:50123void MessageCenterBubble::OnWidgetClosing(views::Widget* widget) {
Evan Stade9cf78822017-08-16 18:32:13124 if (bubble_view_ && bubble_view_->GetWidget())
125 bubble_view_->GetWidget()->RemoveObserver(this);
[email protected]b85a5ae2013-08-05 19:32:50126 if (message_center_view_)
127 message_center_view_->SetIsClosing(true);
128}
129
Evan Stade9cf78822017-08-16 18:32:13130bool MessageCenterBubble::IsVisible() const {
131 return bubble_view() && bubble_view()->GetWidget()->IsVisible();
[email protected]15a0b172012-10-18 23:57:44132}
133
134size_t MessageCenterBubble::NumMessageViewsForTest() const {
[email protected]83158922013-04-06 05:11:35135 return message_center_view_->NumMessageViewsForTest();
[email protected]15a0b172012-10-18 23:57:44136}
137
Evan Stade9cf78822017-08-16 18:32:13138void MessageCenterBubble::UpdateBubbleView() {
139 if (!bubble_view_)
140 return; // Could get called after view is closed
Evan Stade2b9cc982017-08-18 23:38:06141 message_center_view_->SetNotifications(
142 message_center_->GetVisibleNotifications());
Evan Stade9cf78822017-08-16 18:32:13143 bubble_view_->GetWidget()->Show();
144 bubble_view_->UpdateBubble();
145}
146
Evan Stade2b9cc982017-08-18 23:38:06147} // namespace ash