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
|
// 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/views/bubble/bubble_frame_view.h"
#include <algorithm>
#include "grit/ui_resources.h"
#include "ui/base/hit_test.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/screen.h"
#include "ui/views/bubble/bubble_border.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/client_view.h"
namespace {
// Get the |vertical| or horizontal screen overflow of the |window_bounds|.
int GetOffScreenLength(const gfx::Rect& monitor_bounds,
const gfx::Rect& window_bounds,
bool vertical) {
if (monitor_bounds.IsEmpty() || monitor_bounds.Contains(window_bounds))
return 0;
// window_bounds
// +-------------------------------+
// | top |
// | +----------------+ |
// | left | monitor_bounds | right |
// | +----------------+ |
// | bottom |
// +-------------------------------+
if (vertical)
return std::max(0, monitor_bounds.y() - window_bounds.y()) +
std::max(0, window_bounds.bottom() - monitor_bounds.bottom());
return std::max(0, monitor_bounds.x() - window_bounds.x()) +
std::max(0, window_bounds.right() - monitor_bounds.right());
}
} // namespace
namespace views {
BubbleFrameView::BubbleFrameView(const gfx::Insets& content_margins)
: bubble_border_(NULL),
content_margins_(content_margins),
title_(NULL),
close_(NULL),
titlebar_extra_view_(NULL),
can_drag_(false) {
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
title_ = new Label(string16(), rb.GetFont(ui::ResourceBundle::MediumFont));
title_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
AddChildView(title_);
close_ = new LabelButton(this, string16());
close_->SetImage(CustomButton::STATE_NORMAL,
*rb.GetImageNamed(IDR_CLOSE_DIALOG).ToImageSkia());
close_->SetImage(CustomButton::STATE_HOVERED,
*rb.GetImageNamed(IDR_CLOSE_DIALOG_H).ToImageSkia());
close_->SetImage(CustomButton::STATE_PRESSED,
*rb.GetImageNamed(IDR_CLOSE_DIALOG_P).ToImageSkia());
close_->SetSize(close_->GetPreferredSize());
close_->set_border(NULL);
close_->SetVisible(false);
AddChildView(close_);
}
BubbleFrameView::~BubbleFrameView() {}
gfx::Rect BubbleFrameView::GetBoundsForClientView() const {
gfx::Rect client_bounds = GetLocalBounds();
client_bounds.Inset(GetInsets());
return client_bounds;
}
gfx::Rect BubbleFrameView::GetWindowBoundsForClientBounds(
const gfx::Rect& client_bounds) const {
return const_cast<BubbleFrameView*>(this)->GetUpdatedWindowBounds(
gfx::Rect(), client_bounds.size(), false);
}
int BubbleFrameView::NonClientHitTest(const gfx::Point& point) {
if (close_->visible() && close_->GetMirroredBounds().Contains(point))
return HTCLOSE;
if (can_drag_ && point.y() < GetInsets().top())
return HTCAPTION;
return GetWidget()->client_view()->NonClientHitTest(point);
}
void BubbleFrameView::GetWindowMask(const gfx::Size& size,
gfx::Path* window_mask) {}
void BubbleFrameView::ResetWindowControls() {}
void BubbleFrameView::UpdateWindowIcon() {}
void BubbleFrameView::UpdateWindowTitle() {}
gfx::Insets BubbleFrameView::GetInsets() const {
gfx::Insets insets = border()->GetInsets();
insets += content_margins_;
insets += gfx::Insets(std::max(title_->text().empty() ? 0 : title_->height(),
close_->visible() ? close_->height() : 0),
0, 0, 0);
return insets;
}
gfx::Size BubbleFrameView::GetPreferredSize() {
gfx::Size client_size(GetWidget()->client_view()->GetPreferredSize());
return GetUpdatedWindowBounds(gfx::Rect(), client_size, false).size();
}
void BubbleFrameView::Layout() {
gfx::Rect bounds = GetLocalBounds();
bounds.Inset(border()->GetInsets());
// Small additional insets yield the desired 10px visual close button insets.
bounds.Inset(0, 2, close_->width() + 1, 0);
close_->SetPosition(gfx::Point(bounds.right(), bounds.y()));
gfx::Rect title_bounds = bounds;
// Small additional insets yield the desired 20px visual title label insets.
title_bounds.Inset(19, 10, 0, 0);
title_bounds.set_size(title_->GetPreferredSize());
title_->SetBoundsRect(title_bounds);
if (titlebar_extra_view_) {
const gfx::Size size = titlebar_extra_view_->GetPreferredSize();
gfx::Rect titlebar_extra_view_bounds(
bounds.right() - size.width(),
title_bounds.y(),
size.width(),
title_bounds.height());
titlebar_extra_view_bounds.Subtract(title_bounds);
titlebar_extra_view_->SetBoundsRect(titlebar_extra_view_bounds);
}
}
std::string BubbleFrameView::GetClassName() const {
return "ui/views/BubbleFrameView";
}
void BubbleFrameView::ButtonPressed(Button* sender, const ui::Event& event) {
if (sender == close_)
GetWidget()->Close();
}
void BubbleFrameView::SetBubbleBorder(BubbleBorder* border) {
bubble_border_ = border;
set_border(bubble_border_);
// Update the background, which relies on the border.
set_background(new views::BubbleBackground(border));
}
void BubbleFrameView::SetTitle(const string16& title) {
title_->SetText(title);
}
void BubbleFrameView::SetShowCloseButton(bool show) {
close_->SetVisible(show);
}
void BubbleFrameView::SetTitlebarExtraView(View* view) {
DCHECK(view);
DCHECK(!titlebar_extra_view_);
AddChildView(view);
titlebar_extra_view_ = view;
}
gfx::Rect BubbleFrameView::GetUpdatedWindowBounds(const gfx::Rect& anchor_rect,
gfx::Size client_size,
bool adjust_if_offscreen) {
// Give the contents a margin.
client_size.Enlarge(content_margins_.width(), content_margins_.height());
const BubbleBorder::ArrowLocation arrow = bubble_border_->arrow_location();
if (adjust_if_offscreen && BubbleBorder::has_arrow(arrow)) {
if (!bubble_border_->is_arrow_at_center(arrow)) {
// Try to mirror the anchoring if the bubble does not fit on the screen.
MirrorArrowIfOffScreen(true, anchor_rect, client_size);
MirrorArrowIfOffScreen(false, anchor_rect, client_size);
} else {
OffsetArrowIfOffScreen(anchor_rect, client_size);
}
}
// Calculate the bounds with the arrow in its updated location and offset.
return bubble_border_->GetBounds(anchor_rect, client_size);
}
gfx::Rect BubbleFrameView::GetMonitorBounds(const gfx::Rect& rect) {
// TODO(scottmg): Native is wrong. http://crbug.com/133312
return gfx::Screen::GetNativeScreen()->GetDisplayNearestPoint(
rect.CenterPoint()).work_area();
}
void BubbleFrameView::MirrorArrowIfOffScreen(
bool vertical,
const gfx::Rect& anchor_rect,
const gfx::Size& client_size) {
// Check if the bounds don't fit on screen.
gfx::Rect monitor_rect(GetMonitorBounds(anchor_rect));
gfx::Rect window_bounds(bubble_border_->GetBounds(anchor_rect, client_size));
if (GetOffScreenLength(monitor_rect, window_bounds, vertical) > 0) {
BubbleBorder::ArrowLocation arrow = bubble_border()->arrow_location();
// Mirror the arrow and get the new bounds.
bubble_border_->set_arrow_location(
vertical ? BubbleBorder::vertical_mirror(arrow) :
BubbleBorder::horizontal_mirror(arrow));
gfx::Rect mirror_bounds =
bubble_border_->GetBounds(anchor_rect, client_size);
// Restore the original arrow if mirroring doesn't show more of the bubble.
if (GetOffScreenLength(monitor_rect, mirror_bounds, vertical) >=
GetOffScreenLength(monitor_rect, window_bounds, vertical))
bubble_border_->set_arrow_location(arrow);
else
SchedulePaint();
}
}
void BubbleFrameView::OffsetArrowIfOffScreen(const gfx::Rect& anchor_rect,
const gfx::Size& client_size) {
BubbleBorder::ArrowLocation arrow = bubble_border()->arrow_location();
DCHECK(BubbleBorder::is_arrow_at_center(arrow));
// Get the desired bubble bounds without adjustment.
bubble_border_->set_arrow_offset(0);
gfx::Rect window_bounds(bubble_border_->GetBounds(anchor_rect, client_size));
gfx::Rect monitor_rect(GetMonitorBounds(anchor_rect));
if (monitor_rect.IsEmpty() || monitor_rect.Contains(window_bounds))
return;
// Calculate off-screen adjustment.
const bool is_horizontal = BubbleBorder::is_arrow_on_horizontal(arrow);
int offscreen_adjust = 0;
if (is_horizontal) {
if (window_bounds.x() < monitor_rect.x())
offscreen_adjust = monitor_rect.x() - window_bounds.x();
else if (window_bounds.right() > monitor_rect.right())
offscreen_adjust = monitor_rect.right() - window_bounds.right();
} else {
if (window_bounds.y() < monitor_rect.y())
offscreen_adjust = monitor_rect.y() - window_bounds.y();
else if (window_bounds.bottom() > monitor_rect.bottom())
offscreen_adjust = monitor_rect.bottom() - window_bounds.bottom();
}
// For center arrows, arrows are moved in the opposite direction of
// |offscreen_adjust|, e.g. positive |offscreen_adjust| means bubble
// window needs to be moved to the right and that means we need to move arrow
// to the left, and that means negative offset.
bubble_border_->set_arrow_offset(
bubble_border_->GetArrowOffset(window_bounds.size()) - offscreen_adjust);
if (offscreen_adjust)
SchedulePaint();
}
} // namespace views
|