OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/bubble/bubble_frame_view.h" | 5 #include "ui/views/bubble/bubble_frame_view.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
11 #include "ui/base/default_style.h" | 11 #include "ui/base/default_style.h" |
12 #include "ui/base/hit_test.h" | 12 #include "ui/base/hit_test.h" |
13 #include "ui/base/l10n/l10n_util.h" | 13 #include "ui/base/l10n/l10n_util.h" |
14 #include "ui/base/material_design/material_design_controller.h" | 14 #include "ui/base/material_design/material_design_controller.h" |
15 #include "ui/base/resource/resource_bundle.h" | 15 #include "ui/base/resource/resource_bundle.h" |
16 #include "ui/compositor/paint_context.h" | 16 #include "ui/compositor/paint_context.h" |
17 #include "ui/compositor/paint_recorder.h" | 17 #include "ui/compositor/paint_recorder.h" |
18 #include "ui/display/display.h" | 18 #include "ui/display/display.h" |
19 #include "ui/display/screen.h" | 19 #include "ui/display/screen.h" |
20 #include "ui/gfx/geometry/vector2d.h" | 20 #include "ui/gfx/geometry/vector2d.h" |
21 #include "ui/gfx/path.h" | 21 #include "ui/gfx/path.h" |
22 #include "ui/gfx/skia_util.h" | 22 #include "ui/gfx/skia_util.h" |
23 #include "ui/native_theme/native_theme.h" | 23 #include "ui/native_theme/native_theme.h" |
24 #include "ui/resources/grit/ui_resources.h" | 24 #include "ui/resources/grit/ui_resources.h" |
25 #include "ui/strings/grit/ui_strings.h" | 25 #include "ui/strings/grit/ui_strings.h" |
26 #include "ui/vector_icons/vector_icons.h" | 26 #include "ui/vector_icons/vector_icons.h" |
27 #include "ui/views/bubble/bubble_border.h" | 27 #include "ui/views/bubble/bubble_border.h" |
28 #include "ui/views/bubble/bubble_dialog_delegate.h" | |
28 #include "ui/views/controls/button/image_button.h" | 29 #include "ui/views/controls/button/image_button.h" |
29 #include "ui/views/controls/button/image_button_factory.h" | 30 #include "ui/views/controls/button/image_button_factory.h" |
30 #include "ui/views/controls/image_view.h" | 31 #include "ui/views/controls/image_view.h" |
31 #include "ui/views/controls/label.h" | |
32 #include "ui/views/layout/box_layout.h" | 32 #include "ui/views/layout/box_layout.h" |
33 #include "ui/views/layout/layout_provider.h" | 33 #include "ui/views/layout/layout_provider.h" |
34 #include "ui/views/resources/grit/views_resources.h" | 34 #include "ui/views/resources/grit/views_resources.h" |
35 #include "ui/views/widget/widget.h" | 35 #include "ui/views/widget/widget.h" |
36 #include "ui/views/widget/widget_delegate.h" | 36 #include "ui/views/widget/widget_delegate.h" |
37 #include "ui/views/window/client_view.h" | 37 #include "ui/views/window/client_view.h" |
38 #include "ui/views/window/dialog_delegate.h" | 38 #include "ui/views/window/dialog_delegate.h" |
39 | 39 |
40 namespace views { | 40 namespace views { |
41 | 41 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
74 | 74 |
75 // static | 75 // static |
76 const char BubbleFrameView::kViewClassName[] = "BubbleFrameView"; | 76 const char BubbleFrameView::kViewClassName[] = "BubbleFrameView"; |
77 | 77 |
78 BubbleFrameView::BubbleFrameView(const gfx::Insets& title_margins, | 78 BubbleFrameView::BubbleFrameView(const gfx::Insets& title_margins, |
79 const gfx::Insets& content_margins) | 79 const gfx::Insets& content_margins) |
80 : bubble_border_(nullptr), | 80 : bubble_border_(nullptr), |
81 title_margins_(title_margins), | 81 title_margins_(title_margins), |
82 content_margins_(content_margins), | 82 content_margins_(content_margins), |
83 title_icon_(new views::ImageView()), | 83 title_icon_(new views::ImageView()), |
84 title_(nullptr), | 84 default_title_(CreateDefaultTitleLabel(base::string16())), |
85 delegate_title_(nullptr), | |
85 close_(nullptr), | 86 close_(nullptr), |
86 footnote_container_(nullptr), | 87 footnote_container_(nullptr), |
87 close_button_clicked_(false) { | 88 close_button_clicked_(false) { |
88 AddChildView(title_icon_); | 89 AddChildView(title_icon_); |
89 | 90 |
90 title_ = new Label(base::string16(), style::CONTEXT_DIALOG_TITLE); | 91 default_title_->SetVisible(false); |
91 title_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 92 AddChildView(default_title_); |
92 title_->set_collapse_when_hidden(true); | |
93 title_->SetVisible(false); | |
94 title_->SetMultiLine(true); | |
95 AddChildView(title_); | |
96 | 93 |
97 close_ = CreateCloseButton(this); | 94 close_ = CreateCloseButton(this); |
98 close_->SetVisible(false); | 95 close_->SetVisible(false); |
99 #if defined(OS_WIN) | 96 #if defined(OS_WIN) |
100 // Windows will automatically create a tooltip for the close button based on | 97 // Windows will automatically create a tooltip for the close button based on |
101 // the HTCLOSE result from NonClientHitTest(). | 98 // the HTCLOSE result from NonClientHitTest(). |
102 close_->SetTooltipText(base::string16()); | 99 close_->SetTooltipText(base::string16()); |
103 #endif | 100 #endif |
104 AddChildView(close_); | 101 AddChildView(close_); |
105 } | 102 } |
106 | 103 |
107 BubbleFrameView::~BubbleFrameView() {} | 104 BubbleFrameView::~BubbleFrameView() {} |
108 | 105 |
109 // static | 106 // static |
107 Label* BubbleFrameView::CreateDefaultTitleLabel( | |
Peter Kasting
2017/06/17 01:16:20
Nit: Since this is passing out ownership, can it r
Bret
2017/06/21 22:37:54
Done.
| |
108 const base::string16& title_text) { | |
109 Label* title = new Label(title_text, style::CONTEXT_DIALOG_TITLE); | |
110 title->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
111 title->set_collapse_when_hidden(true); | |
112 title->SetMultiLine(true); | |
113 return title; | |
114 } | |
115 | |
116 // static | |
110 Button* BubbleFrameView::CreateCloseButton(ButtonListener* listener) { | 117 Button* BubbleFrameView::CreateCloseButton(ButtonListener* listener) { |
111 ImageButton* close_button = nullptr; | 118 ImageButton* close_button = nullptr; |
112 if (ui::MaterialDesignController::IsSecondaryUiMaterial()) { | 119 if (ui::MaterialDesignController::IsSecondaryUiMaterial()) { |
113 close_button = CreateVectorImageButton(listener); | 120 close_button = CreateVectorImageButton(listener); |
114 SetImageFromVectorIcon(close_button, ui::kCloseIcon); | 121 SetImageFromVectorIcon(close_button, ui::kCloseIcon); |
115 } else { | 122 } else { |
116 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | 123 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
117 close_button = new ImageButton(listener); | 124 close_button = new ImageButton(listener); |
118 close_button->SetImage(CustomButton::STATE_NORMAL, | 125 close_button->SetImage(CustomButton::STATE_NORMAL, |
119 *rb->GetImageNamed(IDR_CLOSE_DIALOG).ToImageSkia()); | 126 *rb->GetImageNamed(IDR_CLOSE_DIALOG).ToImageSkia()); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
167 | 174 |
168 // Allow dialogs to show the system menu and be dragged. | 175 // Allow dialogs to show the system menu and be dragged. |
169 if (GetWidget()->widget_delegate()->AsDialogDelegate() && | 176 if (GetWidget()->widget_delegate()->AsDialogDelegate() && |
170 !GetWidget()->widget_delegate()->AsBubbleDialogDelegate()) { | 177 !GetWidget()->widget_delegate()->AsBubbleDialogDelegate()) { |
171 gfx::Rect bounds(GetContentsBounds()); | 178 gfx::Rect bounds(GetContentsBounds()); |
172 bounds.Inset(title_margins_); | 179 bounds.Inset(title_margins_); |
173 gfx::Rect sys_rect(0, 0, bounds.x(), bounds.y()); | 180 gfx::Rect sys_rect(0, 0, bounds.x(), bounds.y()); |
174 sys_rect.set_origin(gfx::Point(GetMirroredXForRect(sys_rect), 0)); | 181 sys_rect.set_origin(gfx::Point(GetMirroredXForRect(sys_rect), 0)); |
175 if (sys_rect.Contains(point)) | 182 if (sys_rect.Contains(point)) |
176 return HTSYSMENU; | 183 return HTSYSMENU; |
177 if (point.y() < title_->bounds().bottom()) | 184 if (point.y() < title()->bounds().bottom()) |
178 return HTCAPTION; | 185 return HTCAPTION; |
179 } | 186 } |
180 | 187 |
181 return GetWidget()->client_view()->NonClientHitTest(point); | 188 return GetWidget()->client_view()->NonClientHitTest(point); |
182 } | 189 } |
183 | 190 |
184 void BubbleFrameView::GetWindowMask(const gfx::Size& size, | 191 void BubbleFrameView::GetWindowMask(const gfx::Size& size, |
185 gfx::Path* window_mask) { | 192 gfx::Path* window_mask) { |
186 if (bubble_border_->shadow() != BubbleBorder::SMALL_SHADOW && | 193 if (bubble_border_->shadow() != BubbleBorder::SMALL_SHADOW && |
187 bubble_border_->shadow() != BubbleBorder::NO_SHADOW_OPAQUE_BORDER && | 194 bubble_border_->shadow() != BubbleBorder::NO_SHADOW_OPAQUE_BORDER && |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
226 } | 233 } |
227 | 234 |
228 void BubbleFrameView::UpdateWindowIcon() { | 235 void BubbleFrameView::UpdateWindowIcon() { |
229 gfx::ImageSkia image; | 236 gfx::ImageSkia image; |
230 if (GetWidget()->widget_delegate()->ShouldShowWindowIcon()) | 237 if (GetWidget()->widget_delegate()->ShouldShowWindowIcon()) |
231 image = GetWidget()->widget_delegate()->GetWindowIcon(); | 238 image = GetWidget()->widget_delegate()->GetWindowIcon(); |
232 title_icon_->SetImage(&image); | 239 title_icon_->SetImage(&image); |
233 } | 240 } |
234 | 241 |
235 void BubbleFrameView::UpdateWindowTitle() { | 242 void BubbleFrameView::UpdateWindowTitle() { |
236 title_->SetText(GetWidget()->widget_delegate()->GetWindowTitle()); | 243 if (default_title_) { |
237 title_->SetVisible(GetWidget()->widget_delegate()->ShouldShowWindowTitle()); | 244 const WidgetDelegate* delegate = GetWidget()->widget_delegate(); |
245 default_title_->SetVisible(delegate->ShouldShowWindowTitle()); | |
246 default_title_->SetText(delegate->GetWindowTitle()); | |
247 } // delegate_title_'s updates are handled by its creator. | |
238 } | 248 } |
239 | 249 |
240 void BubbleFrameView::SizeConstraintsChanged() {} | 250 void BubbleFrameView::SizeConstraintsChanged() {} |
241 | 251 |
242 void BubbleFrameView::SetTitleFontList(const gfx::FontList& font_list) { | 252 void BubbleFrameView::SetTitleView(View* title_view) { |
243 title_->SetFontList(font_list); | 253 DCHECK(title_view); |
254 if (default_title_) { | |
Peter Kasting
2017/06/17 01:16:20
Nit: Conditional checks here and below are unneces
Bret
2017/06/21 22:37:54
There's not an obvious owner and it'd be really od
Peter Kasting
2017/06/21 23:45:22
shared_ptr is banned in Chromium -- see https://ch
Bret
2017/06/22 01:38:57
Oops, okay. Done.
| |
255 delete default_title_; | |
256 default_title_ = nullptr; | |
257 } | |
258 if (delegate_title_) | |
259 delete delegate_title_; | |
260 delegate_title_ = title_view; | |
261 AddChildView(delegate_title_); | |
Peter Kasting
2017/06/17 01:16:20
Hmm, don't you want to ensure this title is added
Bret
2017/06/21 22:37:54
I didn't think it was important, since they should
Peter Kasting
2017/06/21 23:45:22
Screen readers and accessibility software may trav
Bret
2017/06/22 01:38:57
Done.
| |
244 } | 262 } |
245 | 263 |
246 const char* BubbleFrameView::GetClassName() const { | 264 const char* BubbleFrameView::GetClassName() const { |
247 return kViewClassName; | 265 return kViewClassName; |
248 } | 266 } |
249 | 267 |
250 gfx::Insets BubbleFrameView::GetInsets() const { | 268 gfx::Insets BubbleFrameView::GetInsets() const { |
251 gfx::Insets insets = content_margins_; | 269 gfx::Insets insets = content_margins_; |
252 | 270 |
253 const int icon_height = title_icon_->GetPreferredSize().height(); | 271 const int icon_height = title_icon_->GetPreferredSize().height(); |
254 const int label_height = title_->GetPreferredSize().height(); | 272 const int label_height = title()->GetPreferredSize().height(); |
255 const bool has_title = icon_height > 0 || label_height > 0; | 273 const bool has_title = icon_height > 0 || label_height > 0; |
256 const int title_padding = has_title ? title_margins_.height() : 0; | 274 const int title_padding = has_title ? title_margins_.height() : 0; |
257 const int title_height = std::max(icon_height, label_height) + title_padding; | 275 const int title_height = std::max(icon_height, label_height) + title_padding; |
258 const int close_height = | 276 const int close_height = |
259 GetWidget()->widget_delegate()->ShouldShowCloseButton() | 277 GetWidget()->widget_delegate()->ShouldShowCloseButton() |
260 ? close_->height() + LayoutProvider::Get()->GetDistanceMetric( | 278 ? close_->height() + LayoutProvider::Get()->GetDistanceMetric( |
261 DISTANCE_CLOSE_BUTTON_MARGIN) | 279 DISTANCE_CLOSE_BUTTON_MARGIN) |
262 : 0; | 280 : 0; |
263 insets += gfx::Insets(std::max(title_height, close_height), 0, 0, 0); | 281 insets += gfx::Insets(std::max(title_height, close_height), 0, 0, 0); |
264 return insets; | 282 return insets; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
297 #endif // OS_MACOSX | 315 #endif // OS_MACOSX |
298 // Non-dialog bubbles should be non-resizable, so its max size is its | 316 // Non-dialog bubbles should be non-resizable, so its max size is its |
299 // preferred size. | 317 // preferred size. |
300 return GetPreferredSize(); | 318 return GetPreferredSize(); |
301 #endif | 319 #endif |
302 } | 320 } |
303 | 321 |
304 void BubbleFrameView::Layout() { | 322 void BubbleFrameView::Layout() { |
305 // The title margins may not be set, but make sure that's only the case when | 323 // The title margins may not be set, but make sure that's only the case when |
306 // there's no title. | 324 // there's no title. |
307 DCHECK(!title_margins_.IsEmpty() || !title_->visible()); | 325 DCHECK(!title_margins_.IsEmpty() || |
326 (!delegate_title_ && !default_title_->visible())); | |
308 | 327 |
309 const gfx::Rect contents_bounds = GetContentsBounds(); | 328 const gfx::Rect contents_bounds = GetContentsBounds(); |
310 gfx::Rect bounds = contents_bounds; | 329 gfx::Rect bounds = contents_bounds; |
311 bounds.Inset(title_margins_); | 330 bounds.Inset(title_margins_); |
312 if (bounds.IsEmpty()) | 331 if (bounds.IsEmpty()) |
313 return; | 332 return; |
314 | 333 |
315 // The close button is positioned somewhat closer to the edge of the bubble. | 334 int title_label_right = bounds.right(); |
316 const int close_margin = | 335 if (close_->visible()) { |
317 LayoutProvider::Get()->GetDistanceMetric(DISTANCE_CLOSE_BUTTON_MARGIN); | 336 // The close button is positioned somewhat closer to the edge of the bubble. |
318 close_->SetPosition( | 337 const int close_margin = |
319 gfx::Point(contents_bounds.right() - close_margin - close_->width(), | 338 LayoutProvider::Get()->GetDistanceMetric(DISTANCE_CLOSE_BUTTON_MARGIN); |
320 contents_bounds.y() + close_margin)); | 339 close_->SetPosition( |
340 gfx::Point(contents_bounds.right() - close_margin - close_->width(), | |
341 contents_bounds.y() + close_margin)); | |
342 title_label_right = close_->x(); | |
Peter Kasting
2017/06/17 01:16:20
In the close-button-invisible case, the bounds hav
Bret
2017/06/21 22:37:54
Visually this looks okay to me, since the close bu
Peter Kasting
2017/06/21 23:45:22
But when it's hovered, it will potentially touch t
Bret
2017/06/22 01:38:57
I'll send you screenshots over email.
Bret
2017/06/22 22:18:30
From the email we decided to subtract close_margin
| |
343 } | |
321 | 344 |
322 gfx::Size title_icon_pref_size(title_icon_->GetPreferredSize()); | 345 gfx::Size title_icon_pref_size(title_icon_->GetPreferredSize()); |
323 int padding = 0; | 346 const int title_icon_padding = |
324 int title_height = title_icon_pref_size.height(); | 347 title_icon_pref_size.width() > 0 ? title_margins_.left() : 0; |
325 | 348 const int title_label_x = |
326 if (title_->visible() && !title_->text().empty()) { | 349 bounds.x() + title_icon_pref_size.width() + title_icon_padding; |
327 if (title_icon_pref_size.width() > 0) | 350 const int title_available_width = |
328 padding = title_margins_.left(); | 351 std::max(1, title_label_right - title_label_x); |
329 | 352 const int title_preferred_height = |
330 const int title_label_x = | 353 title()->GetHeightForWidth(title_available_width); |
331 bounds.x() + title_icon_pref_size.width() + padding; | 354 const int title_height = |
332 title_->SizeToFit(std::max(1, close_->x() - title_label_x)); | 355 std::max(title_icon_pref_size.height(), title_preferred_height); |
333 title_height = std::max(title_height, title_->height()); | 356 title()->SetBounds(title_label_x, |
334 title_->SetPosition(gfx::Point( | 357 bounds.y() + (title_height - title_preferred_height) / 2, |
335 title_label_x, bounds.y() + (title_height - title_->height()) / 2)); | 358 title_available_width, title_height); |
336 } | |
337 | 359 |
338 title_icon_->SetBounds(bounds.x(), bounds.y(), title_icon_pref_size.width(), | 360 title_icon_->SetBounds(bounds.x(), bounds.y(), title_icon_pref_size.width(), |
339 title_height); | 361 title_height); |
340 bounds.set_width(title_->bounds().right() - bounds.x()); | |
341 bounds.set_height(title_height); | |
342 | 362 |
343 if (footnote_container_) { | 363 if (footnote_container_) { |
344 const int width = contents_bounds.width(); | 364 const int width = contents_bounds.width(); |
345 const int height = footnote_container_->GetHeightForWidth(width); | 365 const int height = footnote_container_->GetHeightForWidth(width); |
346 footnote_container_->SetBounds( | 366 footnote_container_->SetBounds( |
347 contents_bounds.x(), contents_bounds.bottom() - height, width, height); | 367 contents_bounds.x(), contents_bounds.bottom() - height, width, height); |
348 } | 368 } |
349 } | 369 } |
350 | 370 |
351 void BubbleFrameView::OnThemeChanged() { | 371 void BubbleFrameView::OnThemeChanged() { |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
522 bubble_border_->GetArrowOffset(window_bounds.size()) - offscreen_adjust); | 542 bubble_border_->GetArrowOffset(window_bounds.size()) - offscreen_adjust); |
523 if (offscreen_adjust) | 543 if (offscreen_adjust) |
524 SchedulePaint(); | 544 SchedulePaint(); |
525 } | 545 } |
526 | 546 |
527 gfx::Size BubbleFrameView::GetSizeForClientSize( | 547 gfx::Size BubbleFrameView::GetSizeForClientSize( |
528 const gfx::Size& client_size) const { | 548 const gfx::Size& client_size) const { |
529 // Accommodate the width of the title bar elements. | 549 // Accommodate the width of the title bar elements. |
530 int title_bar_width = title_margins_.width() + border()->GetInsets().width(); | 550 int title_bar_width = title_margins_.width() + border()->GetInsets().width(); |
531 gfx::Size title_icon_size = title_icon_->GetPreferredSize(); | 551 gfx::Size title_icon_size = title_icon_->GetPreferredSize(); |
532 gfx::Size title_label_size = title_->GetPreferredSize(); | 552 gfx::Size title_label_size = title()->GetPreferredSize(); |
533 if (title_icon_size.width() > 0 && title_label_size.width() > 0) | 553 if (title_icon_size.width() > 0 && title_label_size.width() > 0) |
534 title_bar_width += title_margins_.left(); | 554 title_bar_width += title_margins_.left(); |
535 title_bar_width += title_icon_size.width(); | 555 title_bar_width += title_icon_size.width(); |
536 if (close_->visible()) | 556 if (close_->visible()) |
537 title_bar_width += close_->width() + 1; | 557 title_bar_width += close_->width() + 1; |
538 | 558 |
539 gfx::Size size(client_size); | 559 gfx::Size size(client_size); |
540 gfx::Insets client_insets = GetInsets(); | 560 gfx::Insets client_insets = GetInsets(); |
541 size.Enlarge(client_insets.width(), client_insets.height()); | 561 size.Enlarge(client_insets.width(), client_insets.height()); |
542 size.SetToMax(gfx::Size(title_bar_width, 0)); | 562 size.SetToMax(gfx::Size(title_bar_width, 0)); |
543 | 563 |
544 if (footnote_container_) | 564 if (footnote_container_) |
545 size.Enlarge(0, footnote_container_->GetHeightForWidth(size.width())); | 565 size.Enlarge(0, footnote_container_->GetHeightForWidth(size.width())); |
546 | 566 |
547 DialogDelegate* dialog_delegate = | 567 DialogDelegate* dialog_delegate = |
548 GetWidget()->widget_delegate()->AsDialogDelegate(); | 568 GetWidget()->widget_delegate()->AsDialogDelegate(); |
549 if (dialog_delegate && dialog_delegate->ShouldSnapFrameWidth()) | 569 if (dialog_delegate && dialog_delegate->ShouldSnapFrameWidth()) |
550 size.set_width(LayoutProvider::Get()->GetSnappedDialogWidth(size.width())); | 570 size.set_width(LayoutProvider::Get()->GetSnappedDialogWidth(size.width())); |
551 | 571 |
552 return size; | 572 return size; |
553 } | 573 } |
554 | 574 |
555 } // namespace views | 575 } // namespace views |
OLD | NEW |