Make castv2 performance test work.
[chromium-blink-merge.git] / ui / views / bubble / bubble_frame_view.cc
blobdff1e993c38982f0e6869eebfe6b7b7a9b7b85c6
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"
7 #include <algorithm>
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"
25 namespace {
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
37 // |window_bounds|.
38 int GetOffScreenLength(const gfx::Rect& available_bounds,
39 const gfx::Rect& window_bounds,
40 bool vertical) {
41 if (available_bounds.IsEmpty() || available_bounds.Contains(window_bounds))
42 return 0;
44 // window_bounds
45 // +---------------------------------+
46 // | top |
47 // | +------------------+ |
48 // | left | available_bounds | right |
49 // | +------------------+ |
50 // | bottom |
51 // +---------------------------------+
52 if (vertical)
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());
59 } // namespace
61 namespace views {
63 // static
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()),
70 title_(nullptr),
71 close_(nullptr),
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);
81 AddChildView(title_);
83 close_ = CreateCloseButton(this);
84 close_->SetVisible(false);
85 AddChildView(close_);
88 BubbleFrameView::~BubbleFrameView() {}
90 // static
91 gfx::Insets BubbleFrameView::GetTitleInsets() {
92 return gfx::Insets(
93 kTitleTopInset, kTitleLeftInset, kTitleBottomInset, kTitleRightInset);
96 // static
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());
108 close->SetTooltipText(l10n_util::GetStringUTF16(IDS_APP_CLOSE));
109 return close;
112 gfx::Rect BubbleFrameView::GetBoundsForClientView() const {
113 gfx::Rect client_bounds = GetLocalBounds();
114 client_bounds.Inset(GetInsets());
115 client_bounds.Inset(bubble_border_->GetInsets());
116 return client_bounds;
119 gfx::Rect BubbleFrameView::GetWindowBoundsForClientBounds(
120 const gfx::Rect& client_bounds) const {
121 return const_cast<BubbleFrameView*>(this)->GetUpdatedWindowBounds(
122 gfx::Rect(), client_bounds.size(), false);
125 int BubbleFrameView::NonClientHitTest(const gfx::Point& point) {
126 if (!bounds().Contains(point))
127 return HTNOWHERE;
128 if (close_->visible() && close_->GetMirroredBounds().Contains(point))
129 return HTCLOSE;
131 // Allow dialogs to show the system menu and be dragged.
132 if (GetWidget()->widget_delegate()->AsDialogDelegate()) {
133 gfx::Rect sys_rect(0, 0, title_->x(), title_->y());
134 sys_rect.set_origin(gfx::Point(GetMirroredXForRect(sys_rect), 0));
135 if (sys_rect.Contains(point))
136 return HTSYSMENU;
137 if (point.y() < title_->bounds().bottom())
138 return HTCAPTION;
141 return GetWidget()->client_view()->NonClientHitTest(point);
144 void BubbleFrameView::GetWindowMask(const gfx::Size& size,
145 gfx::Path* window_mask) {
146 // NOTE: this only provides implementations for the types used by dialogs.
147 if ((bubble_border_->arrow() != BubbleBorder::NONE &&
148 bubble_border_->arrow() != BubbleBorder::FLOAT) ||
149 (bubble_border_->shadow() != BubbleBorder::SMALL_SHADOW &&
150 bubble_border_->shadow() != BubbleBorder::NO_SHADOW_OPAQUE_BORDER))
151 return;
153 // Use a window mask roughly matching the border in the image assets.
154 static const int kBorderStrokeSize = 1;
155 static const SkScalar kCornerRadius = SkIntToScalar(6);
156 const gfx::Insets border_insets = bubble_border_->GetInsets();
157 SkRect rect = { SkIntToScalar(border_insets.left() - kBorderStrokeSize),
158 SkIntToScalar(border_insets.top() - kBorderStrokeSize),
159 SkIntToScalar(size.width() - border_insets.right() +
160 kBorderStrokeSize),
161 SkIntToScalar(size.height() - border_insets.bottom() +
162 kBorderStrokeSize) };
163 if (bubble_border_->shadow() == BubbleBorder::NO_SHADOW_OPAQUE_BORDER) {
164 window_mask->addRoundRect(rect, kCornerRadius, kCornerRadius);
165 } else {
166 static const int kBottomBorderShadowSize = 2;
167 rect.fBottom += SkIntToScalar(kBottomBorderShadowSize);
168 window_mask->addRect(rect);
172 void BubbleFrameView::ResetWindowControls() {
173 close_->SetVisible(GetWidget()->widget_delegate()->ShouldShowCloseButton());
176 void BubbleFrameView::UpdateWindowIcon() {
177 gfx::ImageSkia image;
178 if (GetWidget()->widget_delegate()->ShouldShowWindowIcon())
179 image = GetWidget()->widget_delegate()->GetWindowIcon();
180 title_icon_->SetImage(&image);
184 void BubbleFrameView::UpdateWindowTitle() {
185 title_->SetText(GetWidget()->widget_delegate()->GetWindowTitle());
186 title_->SetVisible(GetWidget()->widget_delegate()->ShouldShowWindowTitle());
189 void BubbleFrameView::SizeConstraintsChanged() {}
191 void BubbleFrameView::SetTitleFontList(const gfx::FontList& font_list) {
192 title_->SetFontList(font_list);
195 gfx::Insets BubbleFrameView::GetInsets() const {
196 gfx::Insets insets = content_margins_;
198 const int icon_height = title_icon_->GetPreferredSize().height();
199 const int label_height = title_->GetPreferredSize().height();
200 const bool has_title = icon_height > 0 || label_height > 0;
201 const int title_padding = has_title ? kTitleTopInset + kTitleBottomInset : 0;
202 const int title_height = std::max(icon_height, label_height) + title_padding;
203 const int close_height = close_->visible() ? close_->height() : 0;
204 insets += gfx::Insets(std::max(title_height, close_height), 0, 0, 0);
205 return insets;
208 gfx::Size BubbleFrameView::GetPreferredSize() const {
209 return GetSizeForClientSize(GetWidget()->client_view()->GetPreferredSize());
212 gfx::Size BubbleFrameView::GetMinimumSize() const {
213 return GetSizeForClientSize(GetWidget()->client_view()->GetMinimumSize());
216 void BubbleFrameView::Layout() {
217 gfx::Rect bounds(GetContentsBounds());
218 bounds.Inset(GetTitleInsets());
219 if (bounds.IsEmpty())
220 return;
222 // The close button top inset is actually smaller than the title top inset.
223 close_->SetPosition(gfx::Point(bounds.right() - close_->width(),
224 bounds.y() - 5));
226 gfx::Size title_icon_size(title_icon_->GetPreferredSize());
227 gfx::Size title_label_size(title_->GetPreferredSize());
228 int padding = 0;
229 if (title_icon_size.width() > 0 && title_label_size.width() > 0)
230 padding = kTitleHorizontalPadding;
231 const int title_height = std::max(title_icon_size.height(),
232 title_label_size.height());
234 const int title_icon_width = std::max(0, close_->x() - bounds.x());
235 title_icon_size.SetToMin(gfx::Size(title_icon_width, title_height));
236 gfx::Rect title_icon_bounds(
237 bounds.x(), bounds.y(), title_icon_size.width(), title_height);
238 title_icon_->SetBoundsRect(title_icon_bounds);
240 const int title_label_x = title_icon_->bounds().right() + padding;
241 const int title_label_width = std::max(0, close_->x() - title_label_x);
242 title_label_size.SetToMin(gfx::Size(title_label_width,
243 title_label_size.height()));
244 gfx::Rect title_label_bounds(
245 title_label_x, bounds.y(), title_label_size.width(), title_height);
246 title_->SetBoundsRect(title_label_bounds);
248 bounds.set_width(
249 title_icon_size.width() + title_label_size.width() + padding);
250 bounds.set_height(title_height);
252 if (titlebar_extra_view_) {
253 const int extra_width = close_->x() - bounds.right();
254 gfx::Size size = titlebar_extra_view_->GetPreferredSize();
255 size.SetToMin(gfx::Size(std::max(0, extra_width), size.height()));
256 gfx::Rect titlebar_extra_view_bounds(
257 close_->x() - size.width(),
258 bounds.y(),
259 size.width(),
260 bounds.height());
261 titlebar_extra_view_bounds.Subtract(bounds);
262 titlebar_extra_view_->SetBoundsRect(titlebar_extra_view_bounds);
266 const char* BubbleFrameView::GetClassName() const {
267 return kViewClassName;
270 void BubbleFrameView::ChildPreferredSizeChanged(View* child) {
271 if (child == titlebar_extra_view_ || child == title_)
272 Layout();
275 void BubbleFrameView::OnThemeChanged() {
276 UpdateWindowTitle();
277 ResetWindowControls();
278 UpdateWindowIcon();
281 void BubbleFrameView::OnNativeThemeChanged(const ui::NativeTheme* theme) {
282 if (bubble_border_ && bubble_border_->use_theme_background_color()) {
283 bubble_border_->set_background_color(GetNativeTheme()->
284 GetSystemColor(ui::NativeTheme::kColorId_DialogBackground));
285 SchedulePaint();
289 void BubbleFrameView::ButtonPressed(Button* sender, const ui::Event& event) {
290 if (sender == close_)
291 GetWidget()->Close();
294 void BubbleFrameView::SetBubbleBorder(scoped_ptr<BubbleBorder> border) {
295 bubble_border_ = border.get();
296 SetBorder(border.Pass());
298 // Update the background, which relies on the border.
299 set_background(new views::BubbleBackground(bubble_border_));
302 void BubbleFrameView::SetTitlebarExtraView(View* view) {
303 DCHECK(view);
304 DCHECK(!titlebar_extra_view_);
305 AddChildView(view);
306 titlebar_extra_view_ = view;
309 gfx::Rect BubbleFrameView::GetUpdatedWindowBounds(const gfx::Rect& anchor_rect,
310 gfx::Size client_size,
311 bool adjust_if_offscreen) {
312 gfx::Size size(GetSizeForClientSize(client_size));
314 const BubbleBorder::Arrow arrow = bubble_border_->arrow();
315 if (adjust_if_offscreen && BubbleBorder::has_arrow(arrow)) {
316 // Try to mirror the anchoring if the bubble does not fit on the screen.
317 if (!bubble_border_->is_arrow_at_center(arrow)) {
318 MirrorArrowIfOffScreen(true, anchor_rect, size);
319 MirrorArrowIfOffScreen(false, anchor_rect, size);
320 } else {
321 const bool mirror_vertical = BubbleBorder::is_arrow_on_horizontal(arrow);
322 MirrorArrowIfOffScreen(mirror_vertical, anchor_rect, size);
323 OffsetArrowIfOffScreen(anchor_rect, size);
327 // Calculate the bounds with the arrow in its updated location and offset.
328 return bubble_border_->GetBounds(anchor_rect, size);
331 gfx::Rect BubbleFrameView::GetAvailableScreenBounds(const gfx::Rect& rect) {
332 // The bubble attempts to fit within the current screen bounds.
333 // TODO(scottmg): Native is wrong. http://crbug.com/133312
334 return gfx::Screen::GetNativeScreen()->GetDisplayNearestPoint(
335 rect.CenterPoint()).work_area();
338 bool BubbleFrameView::IsCloseButtonVisible() const {
339 return close_->visible();
342 gfx::Rect BubbleFrameView::GetCloseButtonBounds() const {
343 return close_->bounds();
346 void BubbleFrameView::MirrorArrowIfOffScreen(
347 bool vertical,
348 const gfx::Rect& anchor_rect,
349 const gfx::Size& client_size) {
350 // Check if the bounds don't fit on screen.
351 gfx::Rect available_bounds(GetAvailableScreenBounds(anchor_rect));
352 gfx::Rect window_bounds(bubble_border_->GetBounds(anchor_rect, client_size));
353 if (GetOffScreenLength(available_bounds, window_bounds, vertical) > 0) {
354 BubbleBorder::Arrow arrow = bubble_border()->arrow();
355 // Mirror the arrow and get the new bounds.
356 bubble_border_->set_arrow(
357 vertical ? BubbleBorder::vertical_mirror(arrow) :
358 BubbleBorder::horizontal_mirror(arrow));
359 gfx::Rect mirror_bounds =
360 bubble_border_->GetBounds(anchor_rect, client_size);
361 // Restore the original arrow if mirroring doesn't show more of the bubble.
362 // Otherwise it should invoke parent's Layout() to layout the content based
363 // on the new bubble border.
364 if (GetOffScreenLength(available_bounds, mirror_bounds, vertical) >=
365 GetOffScreenLength(available_bounds, window_bounds, vertical))
366 bubble_border_->set_arrow(arrow);
367 else if (parent())
368 parent()->Layout();
372 void BubbleFrameView::OffsetArrowIfOffScreen(const gfx::Rect& anchor_rect,
373 const gfx::Size& client_size) {
374 BubbleBorder::Arrow arrow = bubble_border()->arrow();
375 DCHECK(BubbleBorder::is_arrow_at_center(arrow));
377 // Get the desired bubble bounds without adjustment.
378 bubble_border_->set_arrow_offset(0);
379 gfx::Rect window_bounds(bubble_border_->GetBounds(anchor_rect, client_size));
381 gfx::Rect available_bounds(GetAvailableScreenBounds(anchor_rect));
382 if (available_bounds.IsEmpty() || available_bounds.Contains(window_bounds))
383 return;
385 // Calculate off-screen adjustment.
386 const bool is_horizontal = BubbleBorder::is_arrow_on_horizontal(arrow);
387 int offscreen_adjust = 0;
388 if (is_horizontal) {
389 if (window_bounds.x() < available_bounds.x())
390 offscreen_adjust = available_bounds.x() - window_bounds.x();
391 else if (window_bounds.right() > available_bounds.right())
392 offscreen_adjust = available_bounds.right() - window_bounds.right();
393 } else {
394 if (window_bounds.y() < available_bounds.y())
395 offscreen_adjust = available_bounds.y() - window_bounds.y();
396 else if (window_bounds.bottom() > available_bounds.bottom())
397 offscreen_adjust = available_bounds.bottom() - window_bounds.bottom();
400 // For center arrows, arrows are moved in the opposite direction of
401 // |offscreen_adjust|, e.g. positive |offscreen_adjust| means bubble
402 // window needs to be moved to the right and that means we need to move arrow
403 // to the left, and that means negative offset.
404 bubble_border_->set_arrow_offset(
405 bubble_border_->GetArrowOffset(window_bounds.size()) - offscreen_adjust);
406 if (offscreen_adjust)
407 SchedulePaint();
410 gfx::Size BubbleFrameView::GetSizeForClientSize(
411 const gfx::Size& client_size) const {
412 // Accommodate the width of the title bar elements.
413 int title_bar_width = GetInsets().width() + border()->GetInsets().width();
414 gfx::Size title_icon_size = title_icon_->GetPreferredSize();
415 gfx::Size title_label_size = title_->GetPreferredSize();
416 if (title_icon_size.width() > 0 || title_label_size.width() > 0)
417 title_bar_width += kTitleLeftInset;
418 if (title_icon_size.width() > 0 && title_label_size.width() > 0)
419 title_bar_width += kTitleHorizontalPadding;
420 title_bar_width += title_icon_size.width();
421 title_bar_width += title_label_size.width();
422 if (close_->visible())
423 title_bar_width += close_->width() + 1;
424 if (titlebar_extra_view_ != NULL)
425 title_bar_width += titlebar_extra_view_->GetPreferredSize().width();
426 gfx::Size size(client_size);
427 size.SetToMax(gfx::Size(title_bar_width, 0));
428 const gfx::Insets insets(GetInsets());
429 size.Enlarge(insets.width(), insets.height());
430 return size;
433 } // namespace views