1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/message_center/message_center_bubble.h"
#include "grit/ui_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/size.h"
#include "ui/message_center/message_view.h"
#include "ui/message_center/message_view_factory.h"
#include "ui/views/background.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/text_button.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/grid_layout.h"
#include "ui/views/painter.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
namespace message_center {
namespace {
const int kMessageBubbleBaseMinHeight = 80;
const int kMessageBubbleBaseMaxHeight = 400;
const int kMarginBetweenItems = 10;
const SkColor kMessageCenterBackgroundColor = SkColorSetRGB(0xd3, 0xd3, 0xd3);
const SkColor kBorderDarkColor = SkColorSetRGB(0xaa, 0xaa, 0xaa);
const SkColor kMessageItemBorderWidth = 1;
const SkColor kMessageItemBorderColor = SkColorSetRGB(0xaa, 0xaa, 0xaa);
// The view for the buttons at the bottom of the web notification tray.
class WebNotificationButtonView : public views::View,
public views::ButtonListener {
public:
explicit WebNotificationButtonView(NotificationList::Delegate* list_delegate)
: list_delegate_(list_delegate),
close_all_button_(NULL) {
set_background(views::Background::CreateBackgroundPainter(
true,
views::Painter::CreateVerticalGradient(
MessageBubbleBase::kHeaderBackgroundColorLight,
MessageBubbleBase::kHeaderBackgroundColorDark)));
set_border(views::Border::CreateSolidSidedBorder(
2, 0, 0, 0, kBorderDarkColor));
views::GridLayout* layout = new views::GridLayout(this);
SetLayoutManager(layout);
views::ColumnSet* columns = layout->AddColumnSet(0);
columns->AddPaddingColumn(100, 0);
columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER,
0, /* resize percent */
views::GridLayout::USE_PREF, 0, 0);
columns->AddPaddingColumn(0, 4);
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
close_all_button_ = new views::TextButton(
this, rb.GetLocalizedString(IDS_MESSAGE_CENTER_CLEAR_ALL));
close_all_button_->set_alignment(views::TextButton::ALIGN_CENTER);
close_all_button_->set_focusable(true);
close_all_button_->set_request_focus_on_press(false);
layout->AddPaddingRow(0, 4);
layout->StartRow(0, 0);
layout->AddView(close_all_button_);
}
virtual ~WebNotificationButtonView() {}
void SetCloseAllVisible(bool visible) {
close_all_button_->SetVisible(visible);
}
// Overridden from ButtonListener.
virtual void ButtonPressed(views::Button* sender,
const ui::Event& event) OVERRIDE {
if (sender == close_all_button_)
list_delegate_->SendRemoveAllNotifications();
}
private:
NotificationList::Delegate* list_delegate_;
views::TextButton* close_all_button_;
DISALLOW_COPY_AND_ASSIGN(WebNotificationButtonView);
};
// A custom scroll-view that has a specified size.
class FixedSizedScrollView : public views::ScrollView {
public:
FixedSizedScrollView() {
set_focusable(true);
set_notify_enter_exit_on_child(true);
set_background(views::Background::CreateSolidBackground(
kMessageCenterBackgroundColor));
}
virtual ~FixedSizedScrollView() {}
void SetFixedSize(const gfx::Size& size) {
if (fixed_size_ == size)
return;
fixed_size_ = size;
PreferredSizeChanged();
}
// views::View overrides.
virtual gfx::Size GetPreferredSize() OVERRIDE {
gfx::Size size = fixed_size_.IsEmpty() ?
GetContents()->GetPreferredSize() : fixed_size_;
gfx::Insets insets = GetInsets();
size.Enlarge(insets.width(), insets.height());
return size;
}
virtual void Layout() OVERRIDE {
views::View* contents = GetContents();
gfx::Rect bounds = gfx::Rect(contents->GetPreferredSize());
bounds.set_width(std::max(0, width() - GetScrollBarWidth()));
contents->SetBoundsRect(bounds);
views::ScrollView::Layout();
if (!vertical_scroll_bar()->visible()) {
gfx::Rect bounds = contents->bounds();
bounds.set_width(bounds.width() + GetScrollBarWidth());
contents->SetBoundsRect(bounds);
}
}
virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE {
views::View* contents = GetContents();
gfx::Rect bounds = gfx::Rect(contents->GetPreferredSize());
bounds.set_width(std::max(0, width() - GetScrollBarWidth()));
contents->SetBoundsRect(bounds);
}
private:
gfx::Size fixed_size_;
DISALLOW_COPY_AND_ASSIGN(FixedSizedScrollView);
};
// Container for the messages.
class ScrollContentView : public views::View {
public:
ScrollContentView() {
SetLayoutManager(
new views::BoxLayout(views::BoxLayout::kVertical,
kMarginBetweenItems,
kMarginBetweenItems,
kMarginBetweenItems));
set_background(views::Background::CreateSolidBackground(
kMessageCenterBackgroundColor));
}
virtual ~ScrollContentView() {
}
virtual gfx::Size GetPreferredSize() OVERRIDE {
if (!preferred_size_.IsEmpty())
return preferred_size_;
return views::View::GetPreferredSize();
}
void set_preferred_size(const gfx::Size& size) { preferred_size_ = size; }
private:
gfx::Size preferred_size_;
DISALLOW_COPY_AND_ASSIGN(ScrollContentView);
};
} // namespace
// Message Center contents.
class MessageCenterContentsView : public views::View {
public:
explicit MessageCenterContentsView(NotificationList::Delegate* list_delegate)
: list_delegate_(list_delegate) {
SetLayoutManager(
new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 1));
scroll_content_ = new ScrollContentView;
scroller_ = new FixedSizedScrollView;
scroller_->SetContents(scroll_content_);
AddChildView(scroller_);
scroller_->SetPaintToLayer(true);
scroller_->SetFillsBoundsOpaquely(false);
scroller_->layer()->SetMasksToBounds(true);
button_view_ = new WebNotificationButtonView(list_delegate);
AddChildView(button_view_);
}
void FocusContents() {
scroller_->RequestFocus();
}
void Update(const NotificationList::Notifications& notifications) {
scroll_content_->RemoveAllChildViews(true);
scroll_content_->set_preferred_size(gfx::Size());
size_t num_children = 0;
for (NotificationList::Notifications::const_iterator iter =
notifications.begin(); iter != notifications.end(); ++iter) {
MessageView* view =
MessageViewFactory::ViewForNotification(*iter, list_delegate_);
view->set_scroller(scroller_);
view->set_border(views::Border::CreateSolidBorder(
kMessageItemBorderWidth, kMessageItemBorderColor));
view->SetUpView();
scroll_content_->AddChildView(view);
if (++num_children >=
NotificationList::kMaxVisibleMessageCenterNotifications) {
break;
}
}
if (num_children == 0) {
views::Label* label = new views::Label(l10n_util::GetStringUTF16(
IDS_MESSAGE_CENTER_NO_MESSAGES));
label->SetFont(label->font().DeriveFont(1));
label->SetEnabledColor(SK_ColorGRAY);
scroll_content_->AddChildView(label);
button_view_->SetCloseAllVisible(false);
} else {
button_view_->SetCloseAllVisible(true);
}
SizeScrollContent();
Layout();
if (GetWidget())
GetWidget()->GetRootView()->SchedulePaint();
}
size_t NumMessageViews() const {
return scroll_content_->child_count();
}
private:
void SizeScrollContent() {
gfx::Size scroll_size = scroll_content_->GetPreferredSize();
const int button_height = button_view_->GetPreferredSize().height();
const int min_height = kMessageBubbleBaseMinHeight - button_height;
const int max_height = kMessageBubbleBaseMaxHeight - button_height;
int scroll_height = std::min(std::max(
scroll_size.height(), min_height), max_height);
scroll_size.set_height(scroll_height);
if (scroll_height == min_height)
scroll_content_->set_preferred_size(scroll_size);
else
scroll_content_->set_preferred_size(gfx::Size());
scroller_->SetFixedSize(scroll_size);
scroller_->SizeToPreferredSize();
scroll_content_->InvalidateLayout();
}
NotificationList::Delegate* list_delegate_;
FixedSizedScrollView* scroller_;
ScrollContentView* scroll_content_;
WebNotificationButtonView* button_view_;
DISALLOW_COPY_AND_ASSIGN(MessageCenterContentsView);
};
// Message Center Bubble.
MessageCenterBubble::MessageCenterBubble(NotificationList::Delegate* delegate)
: MessageBubbleBase(delegate),
contents_view_(NULL) {
}
MessageCenterBubble::~MessageCenterBubble() {}
views::TrayBubbleView::InitParams MessageCenterBubble::GetInitParams(
views::TrayBubbleView::AnchorAlignment anchor_alignment) {
views::TrayBubbleView::InitParams init_params =
GetDefaultInitParams(anchor_alignment);
init_params.max_height = kMessageBubbleBaseMaxHeight;
init_params.bubble_width += kMarginBetweenItems * 2;
init_params.can_activate = true;
return init_params;
}
void MessageCenterBubble::InitializeContents(
views::TrayBubbleView* new_bubble_view) {
set_bubble_view(new_bubble_view);
contents_view_ = new MessageCenterContentsView(list_delegate());
bubble_view()->AddChildView(contents_view_);
UpdateBubbleView();
contents_view_->FocusContents();
}
void MessageCenterBubble::OnBubbleViewDestroyed() {
contents_view_ = NULL;
}
void MessageCenterBubble::UpdateBubbleView() {
if (!bubble_view())
return; // Could get called after view is closed
contents_view_->Update(
list_delegate()->GetNotificationList()->notifications());
bubble_view()->Show();
bubble_view()->UpdateBubble();
}
void MessageCenterBubble::OnMouseEnteredView() {
}
void MessageCenterBubble::OnMouseExitedView() {
}
size_t MessageCenterBubble::NumMessageViewsForTest() const {
return contents_view_->NumMessageViews();
}
} // namespace message_center
|