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 return GetSizeForClientSize(GetWidget()->client_view()->GetPreferredSize());
216 gfx::Size
BubbleFrameView::GetMinimumSize() const {
217 return GetSizeForClientSize(GetWidget()->client_view()->GetMinimumSize());
220 void BubbleFrameView::Layout() {
221 gfx::Rect
bounds(GetContentsBounds());
222 bounds
.Inset(GetTitleInsets());
223 if (bounds
.IsEmpty())
226 // The close button top inset is actually smaller than the title top inset.
227 close_
->SetPosition(gfx::Point(bounds
.right() - close_
->width(),
230 gfx::Size
title_icon_size(title_icon_
->GetPreferredSize());
231 gfx::Size
title_label_size(title_
->GetPreferredSize());
233 if (title_icon_size
.width() > 0 && title_label_size
.width() > 0)
234 padding
= kTitleHorizontalPadding
;
235 const int title_height
= std::max(title_icon_size
.height(),
236 title_label_size
.height());
238 const int title_icon_width
= std::max(0, close_
->x() - bounds
.x());
239 title_icon_size
.SetToMin(gfx::Size(title_icon_width
, title_height
));
240 gfx::Rect
title_icon_bounds(
241 bounds
.x(), bounds
.y(), title_icon_size
.width(), title_height
);
242 title_icon_
->SetBoundsRect(title_icon_bounds
);
244 const int title_label_x
= title_icon_
->bounds().right() + padding
;
245 const int title_label_width
= std::max(0, close_
->x() - title_label_x
);
246 title_label_size
.SetToMin(gfx::Size(title_label_width
,
247 title_label_size
.height()));
248 gfx::Rect
title_label_bounds(
249 title_label_x
, bounds
.y(), title_label_size
.width(), title_height
);
250 title_
->SetBoundsRect(title_label_bounds
);
253 title_icon_size
.width() + title_label_size
.width() + padding
);
254 bounds
.set_height(title_height
);
256 if (titlebar_extra_view_
) {
257 const int extra_width
= close_
->x() - bounds
.right();
258 gfx::Size size
= titlebar_extra_view_
->GetPreferredSize();
259 size
.SetToMin(gfx::Size(std::max(0, extra_width
), size
.height()));
260 gfx::Rect
titlebar_extra_view_bounds(
261 close_
->x() - size
.width(),
265 titlebar_extra_view_bounds
.Subtract(bounds
);
266 titlebar_extra_view_
->SetBoundsRect(titlebar_extra_view_bounds
);
270 const char* BubbleFrameView::GetClassName() const {
271 return kViewClassName
;
274 void BubbleFrameView::ChildPreferredSizeChanged(View
* child
) {
275 if (child
== titlebar_extra_view_
|| child
== title_
)
279 void BubbleFrameView::OnThemeChanged() {
281 ResetWindowControls();
285 void BubbleFrameView::OnNativeThemeChanged(const ui::NativeTheme
* theme
) {
286 if (bubble_border_
&& bubble_border_
->use_theme_background_color()) {
287 bubble_border_
->set_background_color(GetNativeTheme()->
288 GetSystemColor(ui::NativeTheme::kColorId_DialogBackground
));
293 void BubbleFrameView::ButtonPressed(Button
* sender
, const ui::Event
& event
) {
294 if (sender
== close_
)
295 GetWidget()->Close();
298 void BubbleFrameView::SetBubbleBorder(scoped_ptr
<BubbleBorder
> border
) {
299 bubble_border_
= border
.get();
300 SetBorder(border
.Pass());
302 // Update the background, which relies on the border.
303 set_background(new views::BubbleBackground(bubble_border_
));
306 void BubbleFrameView::SetTitlebarExtraView(View
* view
) {
308 DCHECK(!titlebar_extra_view_
);
310 titlebar_extra_view_
= view
;
313 gfx::Rect
BubbleFrameView::GetUpdatedWindowBounds(const gfx::Rect
& anchor_rect
,
314 gfx::Size client_size
,
315 bool adjust_if_offscreen
) {
316 gfx::Size
size(GetSizeForClientSize(client_size
));
318 const BubbleBorder::Arrow arrow
= bubble_border_
->arrow();
319 if (adjust_if_offscreen
&& BubbleBorder::has_arrow(arrow
)) {
320 // Try to mirror the anchoring if the bubble does not fit on the screen.
321 if (!bubble_border_
->is_arrow_at_center(arrow
)) {
322 MirrorArrowIfOffScreen(true, anchor_rect
, size
);
323 MirrorArrowIfOffScreen(false, anchor_rect
, size
);
325 const bool mirror_vertical
= BubbleBorder::is_arrow_on_horizontal(arrow
);
326 MirrorArrowIfOffScreen(mirror_vertical
, anchor_rect
, size
);
327 OffsetArrowIfOffScreen(anchor_rect
, size
);
331 // Calculate the bounds with the arrow in its updated location and offset.
332 return bubble_border_
->GetBounds(anchor_rect
, size
);
335 gfx::Rect
BubbleFrameView::GetAvailableScreenBounds(const gfx::Rect
& rect
) {
336 // The bubble attempts to fit within the current screen bounds.
337 // TODO(scottmg): Native is wrong. http://crbug.com/133312
338 return gfx::Screen::GetNativeScreen()->GetDisplayNearestPoint(
339 rect
.CenterPoint()).work_area();
342 bool BubbleFrameView::IsCloseButtonVisible() const {
343 return close_
->visible();
346 gfx::Rect
BubbleFrameView::GetCloseButtonBounds() const {
347 return close_
->bounds();
350 void BubbleFrameView::MirrorArrowIfOffScreen(
352 const gfx::Rect
& anchor_rect
,
353 const gfx::Size
& client_size
) {
354 // Check if the bounds don't fit on screen.
355 gfx::Rect
available_bounds(GetAvailableScreenBounds(anchor_rect
));
356 gfx::Rect
window_bounds(bubble_border_
->GetBounds(anchor_rect
, client_size
));
357 if (GetOffScreenLength(available_bounds
, window_bounds
, vertical
) > 0) {
358 BubbleBorder::Arrow arrow
= bubble_border()->arrow();
359 // Mirror the arrow and get the new bounds.
360 bubble_border_
->set_arrow(
361 vertical
? BubbleBorder::vertical_mirror(arrow
) :
362 BubbleBorder::horizontal_mirror(arrow
));
363 gfx::Rect mirror_bounds
=
364 bubble_border_
->GetBounds(anchor_rect
, client_size
);
365 // Restore the original arrow if mirroring doesn't show more of the bubble.
366 // Otherwise it should invoke parent's Layout() to layout the content based
367 // on the new bubble border.
368 if (GetOffScreenLength(available_bounds
, mirror_bounds
, vertical
) >=
369 GetOffScreenLength(available_bounds
, window_bounds
, vertical
))
370 bubble_border_
->set_arrow(arrow
);
376 void BubbleFrameView::OffsetArrowIfOffScreen(const gfx::Rect
& anchor_rect
,
377 const gfx::Size
& client_size
) {
378 BubbleBorder::Arrow arrow
= bubble_border()->arrow();
379 DCHECK(BubbleBorder::is_arrow_at_center(arrow
));
381 // Get the desired bubble bounds without adjustment.
382 bubble_border_
->set_arrow_offset(0);
383 gfx::Rect
window_bounds(bubble_border_
->GetBounds(anchor_rect
, client_size
));
385 gfx::Rect
available_bounds(GetAvailableScreenBounds(anchor_rect
));
386 if (available_bounds
.IsEmpty() || available_bounds
.Contains(window_bounds
))
389 // Calculate off-screen adjustment.
390 const bool is_horizontal
= BubbleBorder::is_arrow_on_horizontal(arrow
);
391 int offscreen_adjust
= 0;
393 if (window_bounds
.x() < available_bounds
.x())
394 offscreen_adjust
= available_bounds
.x() - window_bounds
.x();
395 else if (window_bounds
.right() > available_bounds
.right())
396 offscreen_adjust
= available_bounds
.right() - window_bounds
.right();
398 if (window_bounds
.y() < available_bounds
.y())
399 offscreen_adjust
= available_bounds
.y() - window_bounds
.y();
400 else if (window_bounds
.bottom() > available_bounds
.bottom())
401 offscreen_adjust
= available_bounds
.bottom() - window_bounds
.bottom();
404 // For center arrows, arrows are moved in the opposite direction of
405 // |offscreen_adjust|, e.g. positive |offscreen_adjust| means bubble
406 // window needs to be moved to the right and that means we need to move arrow
407 // to the left, and that means negative offset.
408 bubble_border_
->set_arrow_offset(
409 bubble_border_
->GetArrowOffset(window_bounds
.size()) - offscreen_adjust
);
410 if (offscreen_adjust
)
414 gfx::Size
BubbleFrameView::GetSizeForClientSize(
415 const gfx::Size
& client_size
) const {
416 // Accommodate the width of the title bar elements.
417 int title_bar_width
= GetInsets().width() + border()->GetInsets().width();
418 gfx::Size title_icon_size
= title_icon_
->GetPreferredSize();
419 gfx::Size title_label_size
= title_
->GetPreferredSize();
420 if (title_icon_size
.width() > 0 || title_label_size
.width() > 0)
421 title_bar_width
+= kTitleLeftInset
;
422 if (title_icon_size
.width() > 0 && title_label_size
.width() > 0)
423 title_bar_width
+= kTitleHorizontalPadding
;
424 title_bar_width
+= title_icon_size
.width();
425 title_bar_width
+= title_label_size
.width();
426 if (close_
->visible())
427 title_bar_width
+= close_
->width() + 1;
428 if (titlebar_extra_view_
!= NULL
)
429 title_bar_width
+= titlebar_extra_view_
->GetPreferredSize().width();
430 gfx::Size
size(client_size
);
431 size
.SetToMax(gfx::Size(title_bar_width
, 0));
432 const gfx::Insets
insets(GetInsets());
433 size
.Enlarge(insets
.width(), insets
.height());