Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Side by Side Diff: ui/views/bubble/bubble_frame_view.cc

Issue 2907983002: Allow dialogs to use a custom View as their title. (Closed)
Patch Set: comments 3 Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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()).release()),
85 custom_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 std::unique_ptr<Label> BubbleFrameView::CreateDefaultTitleLabel(
108 const base::string16& title_text) {
109 auto title = base::MakeUnique<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
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
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 } // custom_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 delete default_title_;
255 default_title_ = nullptr;
256 delete custom_title_;
257 custom_title_ = title_view;
258 // Keep the title after the icon for focus order.
259 AddChildViewAt(custom_title_, 1);
244 } 260 }
245 261
246 const char* BubbleFrameView::GetClassName() const { 262 const char* BubbleFrameView::GetClassName() const {
247 return kViewClassName; 263 return kViewClassName;
248 } 264 }
249 265
250 gfx::Insets BubbleFrameView::GetInsets() const { 266 gfx::Insets BubbleFrameView::GetInsets() const {
251 gfx::Insets insets = content_margins_; 267 gfx::Insets insets = content_margins_;
252 268
253 const int icon_height = title_icon_->GetPreferredSize().height(); 269 const int icon_height = title_icon_->GetPreferredSize().height();
254 const int label_height = title_->GetPreferredSize().height(); 270 const int label_height = title()->GetPreferredSize().height();
255 const bool has_title = icon_height > 0 || label_height > 0; 271 const bool has_title = icon_height > 0 || label_height > 0;
256 const int title_padding = has_title ? title_margins_.height() : 0; 272 const int title_padding = has_title ? title_margins_.height() : 0;
257 const int title_height = std::max(icon_height, label_height) + title_padding; 273 const int title_height = std::max(icon_height, label_height) + title_padding;
258 const int close_height = 274 const int close_height =
259 GetWidget()->widget_delegate()->ShouldShowCloseButton() 275 GetWidget()->widget_delegate()->ShouldShowCloseButton()
260 ? close_->height() + LayoutProvider::Get()->GetDistanceMetric( 276 ? close_->height() + LayoutProvider::Get()->GetDistanceMetric(
261 DISTANCE_CLOSE_BUTTON_MARGIN) 277 DISTANCE_CLOSE_BUTTON_MARGIN)
262 : 0; 278 : 0;
263 insets += gfx::Insets(std::max(title_height, close_height), 0, 0, 0); 279 insets += gfx::Insets(std::max(title_height, close_height), 0, 0, 0);
264 return insets; 280 return insets;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 #endif // OS_MACOSX 313 #endif // OS_MACOSX
298 // Non-dialog bubbles should be non-resizable, so its max size is its 314 // Non-dialog bubbles should be non-resizable, so its max size is its
299 // preferred size. 315 // preferred size.
300 return GetPreferredSize(); 316 return GetPreferredSize();
301 #endif 317 #endif
302 } 318 }
303 319
304 void BubbleFrameView::Layout() { 320 void BubbleFrameView::Layout() {
305 // The title margins may not be set, but make sure that's only the case when 321 // The title margins may not be set, but make sure that's only the case when
306 // there's no title. 322 // there's no title.
307 DCHECK(!title_margins_.IsEmpty() || !title_->visible()); 323 DCHECK(!title_margins_.IsEmpty() ||
324 (!custom_title_ && !default_title_->visible()));
308 325
309 const gfx::Rect contents_bounds = GetContentsBounds(); 326 const gfx::Rect contents_bounds = GetContentsBounds();
310 gfx::Rect bounds = contents_bounds; 327 gfx::Rect bounds = contents_bounds;
311 bounds.Inset(title_margins_); 328 bounds.Inset(title_margins_);
312 if (bounds.IsEmpty()) 329 if (bounds.IsEmpty())
313 return; 330 return;
314 331
315 // The close button is positioned somewhat closer to the edge of the bubble. 332 int title_label_right = bounds.right();
316 const int close_margin = 333 if (close_->visible()) {
317 LayoutProvider::Get()->GetDistanceMetric(DISTANCE_CLOSE_BUTTON_MARGIN); 334 // The close button is positioned somewhat closer to the edge of the bubble.
318 close_->SetPosition( 335 const int close_margin =
319 gfx::Point(contents_bounds.right() - close_margin - close_->width(), 336 LayoutProvider::Get()->GetDistanceMetric(DISTANCE_CLOSE_BUTTON_MARGIN);
320 contents_bounds.y() + close_margin)); 337 close_->SetPosition(
338 gfx::Point(contents_bounds.right() - close_margin - close_->width(),
339 contents_bounds.y() + close_margin));
340 title_label_right = close_->x() - close_margin;
Peter Kasting 2017/06/23 06:00:09 As I tried to convey in email, I think this would
Bret 2017/06/24 01:12:33 Done.
341 }
321 342
322 gfx::Size title_icon_pref_size(title_icon_->GetPreferredSize()); 343 gfx::Size title_icon_pref_size(title_icon_->GetPreferredSize());
323 int padding = 0; 344 const int title_icon_padding =
324 int title_height = title_icon_pref_size.height(); 345 title_icon_pref_size.width() > 0 ? title_margins_.left() : 0;
325 346 const int title_label_x =
326 if (title_->visible() && !title_->text().empty()) { 347 bounds.x() + title_icon_pref_size.width() + title_icon_padding;
327 if (title_icon_pref_size.width() > 0) 348 const int title_available_width =
328 padding = title_margins_.left(); 349 std::max(1, title_label_right - title_label_x);
329 350 const int title_preferred_height =
330 const int title_label_x = 351 title()->GetHeightForWidth(title_available_width);
331 bounds.x() + title_icon_pref_size.width() + padding; 352 const int title_height =
332 title_->SizeToFit(std::max(1, close_->x() - title_label_x)); 353 std::max(title_icon_pref_size.height(), title_preferred_height);
333 title_height = std::max(title_height, title_->height()); 354 title()->SetBounds(title_label_x,
334 title_->SetPosition(gfx::Point( 355 bounds.y() + (title_height - title_preferred_height) / 2,
335 title_label_x, bounds.y() + (title_height - title_->height()) / 2)); 356 title_available_width, title_height);
336 }
337 357
338 title_icon_->SetBounds(bounds.x(), bounds.y(), title_icon_pref_size.width(), 358 title_icon_->SetBounds(bounds.x(), bounds.y(), title_icon_pref_size.width(),
339 title_height); 359 title_height);
340 bounds.set_width(title_->bounds().right() - bounds.x());
341 bounds.set_height(title_height);
342 360
343 if (footnote_container_) { 361 if (footnote_container_) {
344 const int width = contents_bounds.width(); 362 const int width = contents_bounds.width();
345 const int height = footnote_container_->GetHeightForWidth(width); 363 const int height = footnote_container_->GetHeightForWidth(width);
346 footnote_container_->SetBounds( 364 footnote_container_->SetBounds(
347 contents_bounds.x(), contents_bounds.bottom() - height, width, height); 365 contents_bounds.x(), contents_bounds.bottom() - height, width, height);
348 } 366 }
349 } 367 }
350 368
351 void BubbleFrameView::OnThemeChanged() { 369 void BubbleFrameView::OnThemeChanged() {
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 bubble_border_->GetArrowOffset(window_bounds.size()) - offscreen_adjust); 540 bubble_border_->GetArrowOffset(window_bounds.size()) - offscreen_adjust);
523 if (offscreen_adjust) 541 if (offscreen_adjust)
524 SchedulePaint(); 542 SchedulePaint();
525 } 543 }
526 544
527 gfx::Size BubbleFrameView::GetSizeForClientSize( 545 gfx::Size BubbleFrameView::GetSizeForClientSize(
528 const gfx::Size& client_size) const { 546 const gfx::Size& client_size) const {
529 // Accommodate the width of the title bar elements. 547 // Accommodate the width of the title bar elements.
530 int title_bar_width = title_margins_.width() + border()->GetInsets().width(); 548 int title_bar_width = title_margins_.width() + border()->GetInsets().width();
531 gfx::Size title_icon_size = title_icon_->GetPreferredSize(); 549 gfx::Size title_icon_size = title_icon_->GetPreferredSize();
532 gfx::Size title_label_size = title_->GetPreferredSize(); 550 gfx::Size title_label_size = title()->GetPreferredSize();
533 if (title_icon_size.width() > 0 && title_label_size.width() > 0) 551 if (title_icon_size.width() > 0 && title_label_size.width() > 0)
534 title_bar_width += title_margins_.left(); 552 title_bar_width += title_margins_.left();
535 title_bar_width += title_icon_size.width(); 553 title_bar_width += title_icon_size.width();
536 if (close_->visible()) 554 if (close_->visible())
537 title_bar_width += close_->width() + 1; 555 title_bar_width += close_->width() + 1;
538 556
539 gfx::Size size(client_size); 557 gfx::Size size(client_size);
540 gfx::Insets client_insets = GetInsets(); 558 gfx::Insets client_insets = GetInsets();
541 size.Enlarge(client_insets.width(), client_insets.height()); 559 size.Enlarge(client_insets.width(), client_insets.height());
542 size.SetToMax(gfx::Size(title_bar_width, 0)); 560 size.SetToMax(gfx::Size(title_bar_width, 0));
543 561
544 if (footnote_container_) 562 if (footnote_container_)
545 size.Enlarge(0, footnote_container_->GetHeightForWidth(size.width())); 563 size.Enlarge(0, footnote_container_->GetHeightForWidth(size.width()));
546 564
547 DialogDelegate* dialog_delegate = 565 DialogDelegate* dialog_delegate =
548 GetWidget()->widget_delegate()->AsDialogDelegate(); 566 GetWidget()->widget_delegate()->AsDialogDelegate();
549 if (dialog_delegate && dialog_delegate->ShouldSnapFrameWidth()) 567 if (dialog_delegate && dialog_delegate->ShouldSnapFrameWidth())
550 size.set_width(LayoutProvider::Get()->GetSnappedDialogWidth(size.width())); 568 size.set_width(LayoutProvider::Get()->GetSnappedDialogWidth(size.width()));
551 569
552 return size; 570 return size;
553 } 571 }
554 572
555 } // namespace views 573 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698