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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
// Copyright (c) 2009 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 "chrome/browser/views/notifications/balloon_view.h"
#include <vector>
#include "app/gfx/canvas.h"
#include "app/gfx/gdi_util.h"
#include "app/gfx/insets.h"
#include "app/gfx/native_widget_types.h"
#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "base/message_loop.h"
#include "base/string_util.h"
#include "chrome/browser/browser_theme_provider.h"
#include "chrome/browser/notifications/balloon.h"
#include "chrome/browser/views/notifications/balloon_view_host.h"
#include "chrome/common/notification_details.h"
#include "chrome/common/notification_source.h"
#include "chrome/common/notification_type.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "views/controls/button/button.h"
#include "views/controls/button/text_button.h"
#include "views/controls/label.h"
#include "views/controls/native/native_view_host.h"
#include "views/painter.h"
#include "views/widget/widget_win.h"
using views::Widget;
namespace {
// How many pixels of overlap there is between the shelf top and the
// balloon bottom.
const int kTopMargin = 1;
const int kBottomMargin = 1;
const int kLeftMargin = 1;
const int kRightMargin = 1;
const int kShelfBorderTopOverlap = 2;
// Properties of the dismiss button.
const int kDismissButtonWidth = 46;
const int kDismissButtonHeight = 20;
// Properties of the origin label.
const int kLeftLabelMargin = 5;
// TODO(johnnyg): Add a shadow for the frame.
const int kLeftShadowWidth = 0;
const int kRightShadowWidth = 0;
const int kTopShadowWidth = 0;
const int kBottomShadowWidth = 0;
// Optional animation.
const bool kAnimateEnabled = true;
// The shelf height for the system default font size. It is scaled
// with changes in the default font size.
const int kDefaultShelfHeight = 22;
} // namespace
class BalloonCloseButtonListener : public views::ButtonListener {
public:
explicit BalloonCloseButtonListener(BalloonView* view)
: view_(view) {}
virtual ~BalloonCloseButtonListener() {}
// The only button currently is the close button.
virtual void ButtonPressed(views::Button* sender, const views::Event&) {
view_->Close();
}
private:
// Non-owned pointer to the view which owns this object.
BalloonView* view_;
};
BalloonViewImpl::BalloonViewImpl()
: balloon_(NULL),
frame_container_(NULL),
html_container_(NULL),
html_contents_(NULL),
shelf_background_(NULL),
balloon_background_(NULL),
close_button_(NULL),
close_button_listener_(NULL),
animation_(NULL),
method_factory_(this) {
// This object is not to be deleted by the views hierarchy,
// as it is owned by the balloon.
SetParentOwned(false);
// Load the sprites for the frames.
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
SkBitmap* shelf_bitmap = rb.GetBitmapNamed(IDR_BALLOON_SHELF);
SkBitmap* border_bitmap = rb.GetBitmapNamed(IDR_BALLOON_BORDER);
// Insets are such because the sprites have 3x3 corners.
gfx::Insets insets(3, 3, 3, 3);
shelf_background_.reset(
views::Painter::CreateImagePainter(*shelf_bitmap, insets, true));
balloon_background_.reset(
views::Painter::CreateImagePainter(*border_bitmap, insets, false));
}
BalloonViewImpl::~BalloonViewImpl() {
}
void BalloonViewImpl::Close() {
MessageLoop::current()->PostTask(FROM_HERE,
method_factory_.NewRunnableMethod(&BalloonViewImpl::DelayedClose));
}
void BalloonViewImpl::DelayedClose() {
html_contents_->Shutdown();
html_container_->CloseNow();
frame_container_->CloseNow();
balloon_->Close(true);
}
void BalloonViewImpl::DidChangeBounds(const gfx::Rect& previous,
const gfx::Rect& current) {
SizeContentsWindow();
}
void BalloonViewImpl::SizeContentsWindow() {
if (!html_container_ || !frame_container_)
return;
gfx::Rect contents_rect = GetContentsRectangle();
html_container_->SetBounds(contents_rect);
html_container_->MoveAbove(frame_container_);
gfx::Path path;
GetContentsMask(contents_rect, &path);
html_container_->SetShape(path.CreateNativeRegion());
}
void BalloonViewImpl::RepositionToBalloon() {
DCHECK(frame_container_);
DCHECK(html_container_);
DCHECK(balloon_);
if (!kAnimateEnabled) {
frame_container_->SetBounds(
gfx::Rect(balloon_->position(), balloon_->size()));
gfx::Rect contents_rect = GetContentsRectangle();
html_container_->SetBounds(contents_rect);
return;
}
anim_frame_end_ = gfx::Rect(balloon_->position().x(),
balloon_->position().y(),
balloon_->size().width(),
balloon_->size().height());
frame_container_->GetBounds(&anim_frame_start_, false);
animation_.reset(new SlideAnimation(this));
animation_->Show();
}
void BalloonViewImpl::AnimationProgressed(const Animation* animation) {
DCHECK(animation == animation_.get());
// Linear interpolation from start to end position.
double e = animation->GetCurrentValue();
double s = (1.0 - e);
gfx::Rect frame_position(
static_cast<int>(s * anim_frame_start_.x() +
e * anim_frame_end_.x()),
static_cast<int>(s * anim_frame_start_.y() +
e * anim_frame_end_.y()),
static_cast<int>(s * anim_frame_start_.width() +
e * anim_frame_end_.width()),
static_cast<int>(s * anim_frame_start_.height() +
e * anim_frame_end_.height()));
frame_container_->SetBounds(frame_position);
html_container_->SetBounds(GetContentsRectangle());
}
void BalloonViewImpl::Show(Balloon* balloon) {
balloon_ = balloon;
close_button_listener_.reset(new BalloonCloseButtonListener(this));
SetBounds(balloon_->position().x(), balloon_->position().y(),
balloon_->size().width(), balloon_->size().height());
// We have to create two windows: one for the contents and one for the
// frame. Why?
// * The contents is an html window which cannot be a
// layered window (because it may have child windows for instance).
// * The frame is a layered window so that we can have nicely rounded
// corners using alpha blending (and we may do other alpha blending
// effects).
// Unfortunately, layered windows cannot have child windows. (Well, they can
// but the child windows don't render).
//
// We carefully keep these two windows in sync to present the illusion of
// one window to the user.
gfx::Rect contents_rect = GetContentsRectangle();
html_contents_ = new BalloonViewHost(balloon);
html_contents_->SetPreferredSize(gfx::Size(10000, 10000));
html_container_ = Widget::CreatePopupWidget(Widget::NotTransparent,
Widget::AcceptEvents,
Widget::DeleteOnDestroy);
html_container_->SetAlwaysOnTop(true);
html_container_->Init(NULL, contents_rect);
html_container_->SetContentsView(html_contents_);
gfx::Rect balloon_rect(x(), y(), width(), height());
frame_container_ = Widget::CreatePopupWidget(Widget::Transparent,
Widget::AcceptEvents,
Widget::DeleteOnDestroy);
frame_container_->SetAlwaysOnTop(true);
frame_container_->Init(NULL, balloon_rect);
frame_container_->SetContentsView(this);
const std::wstring dismiss_text =
l10n_util::GetString(IDS_NOTIFICATION_BALLOON_DISMISS_LABEL);
close_button_ = new views::TextButton(close_button_listener_.get(),
dismiss_text);
close_button_->SetFont(
ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::SmallFont));
close_button_->SetEnabledColor(BrowserThemeProvider::kDefaultColorTabText);
close_button_->set_alignment(views::TextButton::ALIGN_CENTER);
close_button_->SetBounds(width() - kDismissButtonWidth
- kRightMargin,
height() - kDismissButtonHeight
- kShelfBorderTopOverlap
- kBottomMargin,
kDismissButtonWidth,
kDismissButtonHeight);
AddChildView(close_button_);
const std::wstring source_label_text = l10n_util::GetStringF(
IDS_NOTIFICATION_BALLOON_SOURCE_LABEL,
ASCIIToWide(this->balloon_->notification().origin_url().spec()));
views::Label* source_label = new views::Label(source_label_text);
source_label->SetFont(ResourceBundle::GetSharedInstance().GetFont(
ResourceBundle::SmallFont));
source_label->SetColor(BrowserThemeProvider::kDefaultColorTabText);
source_label->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
source_label->SetBounds(kLeftLabelMargin,
height() - kDismissButtonHeight
- kShelfBorderTopOverlap
- kBottomMargin,
width() - kDismissButtonWidth
- kRightMargin,
kDismissButtonHeight);
AddChildView(source_label);
SizeContentsWindow();
html_container_->Show();
frame_container_->Show();
notification_registrar_.Add(this,
NotificationType::NOTIFY_BALLOON_DISCONNECTED, Source<Balloon>(balloon));
}
void BalloonViewImpl::GetContentsMask(const gfx::Rect& rect,
gfx::Path* path) const {
// This needs to remove areas that look like the following from each corner:
//
// xx
// x
path->moveTo(SkScalar(1), SkScalar(0));
// Upper right corner
path->arcTo(rect.width() - SkScalar(2), SkScalar(0),
rect.width() - SkScalar(1), SkScalar(2),
SkScalar(1));
// Lower right corner
path->arcTo(rect.width() - SkScalar(1), rect.height() - SkScalar(2),
rect.width() - SkScalar(2), rect.height() - SkScalar(1),
SkScalar(1));
// Lower left corner
path->arcTo(SkScalar(1), rect.height() - SkScalar(1),
0, rect.height() - SkScalar(2),
SkScalar(1));
// Upper left corner
path->arcTo(0, SkScalar(1), SkScalar(1), 0, SkScalar(1));
}
gfx::Point BalloonViewImpl::GetContentsOffset() const {
return gfx::Point(kTopShadowWidth + kTopMargin,
kLeftShadowWidth + kLeftMargin);
}
int BalloonViewImpl::GetShelfHeight() const {
// TODO(johnnyg): add scaling here.
return kDefaultShelfHeight;
}
int BalloonViewImpl::GetFrameWidth() const {
return size().width() - kLeftShadowWidth - kRightShadowWidth;
}
int BalloonViewImpl::GetTotalFrameHeight() const {
return size().height() - kTopShadowWidth - kBottomShadowWidth;
}
int BalloonViewImpl::GetBalloonFrameHeight() const {
return GetTotalFrameHeight() - GetShelfHeight();
}
gfx::Rect BalloonViewImpl::GetContentsRectangle() const {
if (!frame_container_)
return gfx::Rect();
int contents_width = GetFrameWidth() - kLeftMargin - kRightMargin;
int contents_height = GetBalloonFrameHeight() - kTopMargin - kBottomMargin;
gfx::Point offset = GetContentsOffset();
gfx::Rect frame_rect;
frame_container_->GetBounds(&frame_rect, true);
return gfx::Rect(frame_rect.x() + offset.x(), frame_rect.y() + offset.y(),
contents_width, contents_height);
}
void BalloonViewImpl::Paint(gfx::Canvas* canvas) {
DCHECK(canvas);
int background_width = GetFrameWidth();
int background_height = GetBalloonFrameHeight();
balloon_background_->Paint(background_width, background_height, canvas);
canvas->save();
SkScalar y_offset =
static_cast<SkScalar>(background_height - kShelfBorderTopOverlap);
canvas->translate(0, y_offset);
shelf_background_->Paint(background_width, GetShelfHeight(), canvas);
canvas->restore();
View::Paint(canvas);
}
void BalloonViewImpl::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
if (type != NotificationType::NOTIFY_BALLOON_DISCONNECTED) {
NOTREACHED();
return;
}
// If the renderer process attached to this balloon is disconnected
// (e.g., because of a crash), we want to close the balloon.
notification_registrar_.Remove(this,
NotificationType::NOTIFY_BALLOON_DISCONNECTED, Source<Balloon>(balloon_));
Close();
}
|