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.
5 #include "ui/views/bubble/bubble_frame_view.h"
9 #include "ui/base/hit_test.h"
10 #include "ui/base/l10n/l10n_util.h"
11 #include "ui/base/resource/resource_bundle.h"
12 #include "ui/gfx/path.h"
13 #include "ui/gfx/screen.h"
14 #include "ui/gfx/skia_util.h"
15 #include "ui/native_theme/native_theme.h"
16 #include "ui/resources/grit/ui_resources.h"
17 #include "ui/strings/grit/ui_strings.h"
18 #include "ui/views/bubble/bubble_border.h"
19 #include "ui/views/controls/button/label_button.h"
20 #include "ui/views/controls/image_view.h"
21 #include "ui/views/widget/widget.h"
22 #include "ui/views/widget/widget_delegate.h"
23 #include "ui/views/window/client_view.h"
27 // Insets for the title bar views in pixels.
28 const int kTitleTopInset
= 12;
29 const int kTitleLeftInset
= 19;
30 const int kTitleBottomInset
= 12;
31 const int kTitleRightInset
= 7;
33 // The horizontal padding between the title and the icon.
34 const int kTitleHorizontalPadding
= 5;
36 // Get the |vertical| or horizontal amount that |available_bounds| overflows
38 int GetOffScreenLength(const gfx::Rect
& available_bounds
,
39 const gfx::Rect
& window_bounds
,
41 if (available_bounds
.IsEmpty() || available_bounds
.Contains(window_bounds
))
45 // +---------------------------------+
47 // | +------------------+ |
48 // | left | available_bounds | right |
49 // | +------------------+ |
51 // +---------------------------------+
53 return std::max(0, available_bounds
.y() - window_bounds
.y()) +
54 std::max(0, window_bounds
.bottom() - available_bounds
.bottom());
55 return std::max(0, available_bounds
.x() - window_bounds
.x()) +
56 std::max(0, window_bounds
.right() - available_bounds
.right());
64 const char BubbleFrameView::kViewClassName
[] = "BubbleFrameView";
66 BubbleFrameView::BubbleFrameView(const gfx::Insets
& content_margins
)
67 : bubble_border_(nullptr),
68 content_margins_(content_margins
),
69 title_icon_(new views::ImageView()),
72 titlebar_extra_view_(nullptr) {
73 AddChildView(title_icon_
);
75 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
76 title_
= new Label(base::string16(),
77 rb
.GetFontList(ui::ResourceBundle::MediumFont
));
78 title_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
79 title_
->set_collapse_when_hidden(true);
80 title_
->SetVisible(false);
83 close_
= CreateCloseButton(this);
84 close_
->SetVisible(false);
88 BubbleFrameView::~BubbleFrameView() {}
91 gfx::Insets
BubbleFrameView::GetTitleInsets() {
93 kTitleTopInset
, kTitleLeftInset
, kTitleBottomInset
, kTitleRightInset
);
97 LabelButton
* BubbleFrameView::CreateCloseButton(ButtonListener
* listener
) {
98 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
99 LabelButton
* close
= new LabelButton(listener
, base::string16());
100 close
->SetImage(CustomButton::STATE_NORMAL
,
101 *rb
.GetImageNamed(IDR_CLOSE_DIALOG
).ToImageSkia());
102 close
->SetImage(CustomButton::STATE_HOVERED
,
103 *rb
.GetImageNamed(IDR_CLOSE_DIALOG_H
).ToImageSkia());
104 close
->SetImage(CustomButton::STATE_PRESSED
,
105 *rb
.GetImageNamed(IDR_CLOSE_DIALOG_P
).ToImageSkia());
106 close
->SetBorder(nullptr);
107 close
->SetSize(close
->GetPreferredSize());
109 // Windows will automatically create a tooltip for the close button based on
110 // the HTCLOSE result from NonClientHitTest().
111 close
->SetTooltipText(l10n_util::GetStringUTF16(IDS_APP_CLOSE
));
116 gfx::Rect
BubbleFrameView::GetBoundsForClientView() const {
117 gfx::Rect client_bounds
= GetLocalBounds();
118 client_bounds
.Inset(GetInsets());
119 client_bounds
.Inset(bubble_border_
->GetInsets());
120 return client_bounds
;
123 gfx::Rect
BubbleFrameView::GetWindowBoundsForClientBounds(
124 const gfx::Rect
& client_bounds
) const {
125 return const_cast<BubbleFrameView
*>(this)->GetUpdatedWindowBounds(
126 gfx::Rect(), client_bounds
.size(), false);
129 int BubbleFrameView::NonClientHitTest(const gfx::Point
& point
) {
130 if (!bounds().Contains(point
))
132 if (close_
->visible() && close_
->GetMirroredBounds().Contains(point
))
135 // Allow dialogs to show the system menu and be dragged.
136 if (GetWidget()->widget_delegate()->AsDialogDelegate()) {
137 gfx::Rect
sys_rect(0, 0, title_
->x(), title_
->y());
138 sys_rect
.set_origin(gfx::Point(GetMirroredXForRect(sys_rect
), 0));
139 if (sys_rect
.Contains(point
))
141 if (point
.y() < title_
->bounds().bottom())
145 return GetWidget()->client_view()->NonClientHitTest(point
);
148 void BubbleFrameView::GetWindowMask(const gfx::Size
& size
,
149 gfx::Path
* window_mask
) {
150 // NOTE: this only provides implementations for the types used by dialogs.
151 if ((bubble_border_
->arrow() != BubbleBorder::NONE
&&
152 bubble_border_
->arrow() != BubbleBorder::FLOAT
) ||
153 (bubble_border_
->shadow() != BubbleBorder::SMALL_SHADOW
&&
154 bubble_border_
->shadow() != BubbleBorder::NO_SHADOW_OPAQUE_BORDER
))
157 // Use a window mask roughly matching the border in the image assets.
158 static const int kBorderStrokeSize
= 1;
159 static const SkScalar kCornerRadius
= SkIntToScalar(6);
160 const gfx::Insets border_insets
= bubble_border_
->GetInsets();
161 SkRect rect
= { SkIntToScalar(border_insets
.left() - kBorderStrokeSize
),
162 SkIntToScalar(border_insets
.top() - kBorderStrokeSize
),
163 SkIntToScalar(size
.width() - border_insets
.right() +
165 SkIntToScalar(size
.height() - border_insets
.bottom() +
166 kBorderStrokeSize
) };
167 if (bubble_border_
->shadow() == BubbleBorder::NO_SHADOW_OPAQUE_BORDER
) {
168 window_mask
->addRoundRect(rect
, kCornerRadius
, kCornerRadius
);
170 static const int kBottomBorderShadowSize
= 2;
171 rect
.fBottom
+= SkIntToScalar(kBottomBorderShadowSize
);
172 window_mask
->addRect(rect
);
176 void BubbleFrameView::ResetWindowControls() {
177 close_
->SetVisible(GetWidget()->widget_delegate()->ShouldShowCloseButton());
180 void BubbleFrameView::UpdateWindowIcon() {
181 gfx::ImageSkia image
;
182 if (GetWidget()->widget_delegate()->ShouldShowWindowIcon())
183 image
= GetWidget()->widget_delegate()->GetWindowIcon();
184 title_icon_
->SetImage(&image
);
188 void BubbleFrameView::UpdateWindowTitle() {
189 title_
->SetText(GetWidget()->widget_delegate()->GetWindowTitle());
190 title_
->SetVisible(GetWidget()->widget_delegate()->ShouldShowWindowTitle());
193 void BubbleFrameView::SizeConstraintsChanged() {}
195 void BubbleFrameView::SetTitleFontList(const gfx::FontList
& font_list
) {
196 title_
->SetFontList(font_list
);
199 gfx::Insets
BubbleFrameView::GetInsets() const {
200 gfx::Insets insets
= content_margins_
;
202 const int icon_height
= title_icon_
->GetPreferredSize().height();
203 const int label_height
= title_
->GetPreferredSize().height();
204 const bool has_title
= icon_height
> 0 || label_height
> 0;
205 const int title_padding
= has_title
? kTitleTopInset
+ kTitleBottomInset
: 0;
206 const int title_height
= std::max(icon_height
, label_height
) + title_padding
;
207 const int close_height
= close_
->visible() ? close_
->height() : 0;
208 insets
+= gfx::Insets(std::max(title_height
, close_height
), 0, 0, 0);
212 gfx::Size
BubbleFrameView::GetPreferredSize() const {
213 // Get the preferred size of the client area.
214 gfx::Size client_size
= GetWidget()->client_view()->GetPreferredSize();
215 // Expand it to include the bubble border and space for the arrow.
216 return GetWindowBoundsForClientBounds(gfx::Rect(client_size
)).size();
219 gfx::Size
BubbleFrameView::GetMinimumSize() const {
220 // Get the minimum size of the client area.
221 gfx::Size client_size
= GetWidget()->client_view()->GetMinimumSize();
222 // Expand it to include the bubble border and space for the arrow.
223 return GetWindowBoundsForClientBounds(gfx::Rect(client_size
)).size();
226 void BubbleFrameView::Layout() {
227 gfx::Rect
bounds(GetContentsBounds());
228 bounds
.Inset(GetTitleInsets());
229 if (bounds
.IsEmpty())
232 // The close button top inset is actually smaller than the title top inset.
233 close_
->SetPosition(gfx::Point(bounds
.right() - close_
->width(),
236 gfx::Size
title_icon_size(title_icon_
->GetPreferredSize());
237 gfx::Size
title_label_size(title_
->GetPreferredSize());
239 if (title_icon_size
.width() > 0 && title_label_size
.width() > 0)
240 padding
= kTitleHorizontalPadding
;
241 const int title_height
= std::max(title_icon_size
.height(),
242 title_label_size
.height());
244 const int title_icon_width
= std::max(0, close_
->x() - bounds
.x());
245 title_icon_size
.SetToMin(gfx::Size(title_icon_width
, title_height
));
246 gfx::Rect
title_icon_bounds(
247 bounds
.x(), bounds
.y(), title_icon_size
.width(), title_height
);
248 title_icon_
->SetBoundsRect(title_icon_bounds
);
250 const int title_label_x
= title_icon_
->bounds().right() + padding
;
251 const int title_label_width
= std::max(0, close_
->x() - title_label_x
);
252 title_label_size
.SetToMin(gfx::Size(title_label_width
,
253 title_label_size
.height()));
254 gfx::Rect
title_label_bounds(
255 title_label_x
, bounds
.y(), title_label_size
.width(), title_height
);
256 title_
->SetBoundsRect(title_label_bounds
);
259 title_icon_size
.width() + title_label_size
.width() + padding
);
260 bounds
.set_height(title_height
);
262 if (titlebar_extra_view_
) {
263 const int extra_width
= close_
->x() - bounds
.right();
264 gfx::Size size
= titlebar_extra_view_
->GetPreferredSize();
265 size
.SetToMin(gfx::Size(std::max(0, extra_width
), size
.height()));
266 gfx::Rect
titlebar_extra_view_bounds(
267 close_
->x() - size
.width(),
271 titlebar_extra_view_bounds
.Subtract(bounds
);
272 titlebar_extra_view_
->SetBoundsRect(titlebar_extra_view_bounds
);
276 const char* BubbleFrameView::GetClassName() const {
277 return kViewClassName
;
280 void BubbleFrameView::ChildPreferredSizeChanged(View
* child
) {
281 if (child
== titlebar_extra_view_
|| child
== title_
)
285 void BubbleFrameView::OnThemeChanged() {
287 ResetWindowControls();
291 void BubbleFrameView::OnNativeThemeChanged(const ui::NativeTheme
* theme
) {
292 if (bubble_border_
&& bubble_border_
->use_theme_background_color()) {
293 bubble_border_
->set_background_color(GetNativeTheme()->
294 GetSystemColor(ui::NativeTheme::kColorId_DialogBackground
));
299 void BubbleFrameView::ButtonPressed(Button
* sender
, const ui::Event
& event
) {
300 if (sender
== close_
)
301 GetWidget()->Close();
304 void BubbleFrameView::SetBubbleBorder(scoped_ptr
<BubbleBorder
> border
) {
305 bubble_border_
= border
.get();
306 SetBorder(border
.Pass());
308 // Update the background, which relies on the border.
309 set_background(new views::BubbleBackground(bubble_border_
));
312 void BubbleFrameView::SetTitlebarExtraView(View
* view
) {
314 DCHECK(!titlebar_extra_view_
);
316 titlebar_extra_view_
= view
;
319 gfx::Rect
BubbleFrameView::GetUpdatedWindowBounds(const gfx::Rect
& anchor_rect
,
320 gfx::Size client_size
,
321 bool adjust_if_offscreen
) {
322 gfx::Size
size(GetSizeForClientSize(client_size
));
324 const BubbleBorder::Arrow arrow
= bubble_border_
->arrow();
325 if (adjust_if_offscreen
&& BubbleBorder::has_arrow(arrow
)) {
326 // Try to mirror the anchoring if the bubble does not fit on the screen.
327 if (!bubble_border_
->is_arrow_at_center(arrow
)) {
328 MirrorArrowIfOffScreen(true, anchor_rect
, size
);
329 MirrorArrowIfOffScreen(false, anchor_rect
, size
);
331 const bool mirror_vertical
= BubbleBorder::is_arrow_on_horizontal(arrow
);
332 MirrorArrowIfOffScreen(mirror_vertical
, anchor_rect
, size
);
333 OffsetArrowIfOffScreen(anchor_rect
, size
);
337 // Calculate the bounds with the arrow in its updated location and offset.
338 return bubble_border_
->GetBounds(anchor_rect
, size
);
341 gfx::Rect
BubbleFrameView::GetAvailableScreenBounds(const gfx::Rect
& rect
) {
342 // The bubble attempts to fit within the current screen bounds.
343 // TODO(scottmg): Native is wrong. http://crbug.com/133312
344 return gfx::Screen::GetNativeScreen()->GetDisplayNearestPoint(
345 rect
.CenterPoint()).work_area();
348 bool BubbleFrameView::IsCloseButtonVisible() const {
349 return close_
->visible();
352 gfx::Rect
BubbleFrameView::GetCloseButtonMirroredBounds() const {
353 return close_
->GetMirroredBounds();
356 void BubbleFrameView::MirrorArrowIfOffScreen(
358 const gfx::Rect
& anchor_rect
,
359 const gfx::Size
& client_size
) {
360 // Check if the bounds don't fit on screen.
361 gfx::Rect
available_bounds(GetAvailableScreenBounds(anchor_rect
));
362 gfx::Rect
window_bounds(bubble_border_
->GetBounds(anchor_rect
, client_size
));
363 if (GetOffScreenLength(available_bounds
, window_bounds
, vertical
) > 0) {
364 BubbleBorder::Arrow arrow
= bubble_border()->arrow();
365 // Mirror the arrow and get the new bounds.
366 bubble_border_
->set_arrow(
367 vertical
? BubbleBorder::vertical_mirror(arrow
) :
368 BubbleBorder::horizontal_mirror(arrow
));
369 gfx::Rect mirror_bounds
=
370 bubble_border_
->GetBounds(anchor_rect
, client_size
);
371 // Restore the original arrow if mirroring doesn't show more of the bubble.
372 // Otherwise it should invoke parent's Layout() to layout the content based
373 // on the new bubble border.
374 if (GetOffScreenLength(available_bounds
, mirror_bounds
, vertical
) >=
375 GetOffScreenLength(available_bounds
, window_bounds
, vertical
))
376 bubble_border_
->set_arrow(arrow
);
382 void BubbleFrameView::OffsetArrowIfOffScreen(const gfx::Rect
& anchor_rect
,
383 const gfx::Size
& client_size
) {
384 BubbleBorder::Arrow arrow
= bubble_border()->arrow();
385 DCHECK(BubbleBorder::is_arrow_at_center(arrow
));
387 // Get the desired bubble bounds without adjustment.
388 bubble_border_
->set_arrow_offset(0);
389 gfx::Rect
window_bounds(bubble_border_
->GetBounds(anchor_rect
, client_size
));
391 gfx::Rect
available_bounds(GetAvailableScreenBounds(anchor_rect
));
392 if (available_bounds
.IsEmpty() || available_bounds
.Contains(window_bounds
))
395 // Calculate off-screen adjustment.
396 const bool is_horizontal
= BubbleBorder::is_arrow_on_horizontal(arrow
);
397 int offscreen_adjust
= 0;
399 if (window_bounds
.x() < available_bounds
.x())
400 offscreen_adjust
= available_bounds
.x() - window_bounds
.x();
401 else if (window_bounds
.right() > available_bounds
.right())
402 offscreen_adjust
= available_bounds
.right() - window_bounds
.right();
404 if (window_bounds
.y() < available_bounds
.y())
405 offscreen_adjust
= available_bounds
.y() - window_bounds
.y();
406 else if (window_bounds
.bottom() > available_bounds
.bottom())
407 offscreen_adjust
= available_bounds
.bottom() - window_bounds
.bottom();
410 // For center arrows, arrows are moved in the opposite direction of
411 // |offscreen_adjust|, e.g. positive |offscreen_adjust| means bubble
412 // window needs to be moved to the right and that means we need to move arrow
413 // to the left, and that means negative offset.
414 bubble_border_
->set_arrow_offset(
415 bubble_border_
->GetArrowOffset(window_bounds
.size()) - offscreen_adjust
);
416 if (offscreen_adjust
)
420 gfx::Size
BubbleFrameView::GetSizeForClientSize(
421 const gfx::Size
& client_size
) const {
422 // Accommodate the width of the title bar elements.
423 int title_bar_width
= GetInsets().width() + border()->GetInsets().width();
424 gfx::Size title_icon_size
= title_icon_
->GetPreferredSize();
425 gfx::Size title_label_size
= title_
->GetPreferredSize();
426 if (title_icon_size
.width() > 0 || title_label_size
.width() > 0)
427 title_bar_width
+= kTitleLeftInset
;
428 if (title_icon_size
.width() > 0 && title_label_size
.width() > 0)
429 title_bar_width
+= kTitleHorizontalPadding
;
430 title_bar_width
+= title_icon_size
.width();
431 title_bar_width
+= title_label_size
.width();
432 if (close_
->visible())
433 title_bar_width
+= close_
->width() + 1;
434 if (titlebar_extra_view_
!= NULL
)
435 title_bar_width
+= titlebar_extra_view_
->GetPreferredSize().width();
436 gfx::Size
size(client_size
);
437 size
.SetToMax(gfx::Size(title_bar_width
, 0));
438 const gfx::Insets
insets(GetInsets());
439 size
.Enlarge(insets
.width(), insets
.height());