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

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 2 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 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 std::unique_ptr<Label> BubbleFrameView::CreateDefaultTitleLabel(
108 const base::string16& title_text) {
109 std::unique_ptr<Label> title(
110 new Label(title_text, style::CONTEXT_DIALOG_TITLE));
sky 2017/06/22 14:48:48 MakeUnique?
Bret 2017/06/22 22:18:30 Done.
111 title->SetHorizontalAlignment(gfx::ALIGN_LEFT);
112 title->set_collapse_when_hidden(true);
113 title->SetMultiLine(true);
114 return title;
115 }
116
117 // static
110 Button* BubbleFrameView::CreateCloseButton(ButtonListener* listener) { 118 Button* BubbleFrameView::CreateCloseButton(ButtonListener* listener) {
111 ImageButton* close_button = nullptr; 119 ImageButton* close_button = nullptr;
112 if (ui::MaterialDesignController::IsSecondaryUiMaterial()) { 120 if (ui::MaterialDesignController::IsSecondaryUiMaterial()) {
113 close_button = CreateVectorImageButton(listener); 121 close_button = CreateVectorImageButton(listener);
114 SetImageFromVectorIcon(close_button, ui::kCloseIcon); 122 SetImageFromVectorIcon(close_button, ui::kCloseIcon);
115 } else { 123 } else {
116 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); 124 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
117 close_button = new ImageButton(listener); 125 close_button = new ImageButton(listener);
118 close_button->SetImage(CustomButton::STATE_NORMAL, 126 close_button->SetImage(CustomButton::STATE_NORMAL,
119 *rb->GetImageNamed(IDR_CLOSE_DIALOG).ToImageSkia()); 127 *rb->GetImageNamed(IDR_CLOSE_DIALOG).ToImageSkia());
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 175
168 // Allow dialogs to show the system menu and be dragged. 176 // Allow dialogs to show the system menu and be dragged.
169 if (GetWidget()->widget_delegate()->AsDialogDelegate() && 177 if (GetWidget()->widget_delegate()->AsDialogDelegate() &&
170 !GetWidget()->widget_delegate()->AsBubbleDialogDelegate()) { 178 !GetWidget()->widget_delegate()->AsBubbleDialogDelegate()) {
171 gfx::Rect bounds(GetContentsBounds()); 179 gfx::Rect bounds(GetContentsBounds());
172 bounds.Inset(title_margins_); 180 bounds.Inset(title_margins_);
173 gfx::Rect sys_rect(0, 0, bounds.x(), bounds.y()); 181 gfx::Rect sys_rect(0, 0, bounds.x(), bounds.y());
174 sys_rect.set_origin(gfx::Point(GetMirroredXForRect(sys_rect), 0)); 182 sys_rect.set_origin(gfx::Point(GetMirroredXForRect(sys_rect), 0));
175 if (sys_rect.Contains(point)) 183 if (sys_rect.Contains(point))
176 return HTSYSMENU; 184 return HTSYSMENU;
177 if (point.y() < title_->bounds().bottom()) 185 if (point.y() < title()->bounds().bottom())
178 return HTCAPTION; 186 return HTCAPTION;
179 } 187 }
180 188
181 return GetWidget()->client_view()->NonClientHitTest(point); 189 return GetWidget()->client_view()->NonClientHitTest(point);
182 } 190 }
183 191
184 void BubbleFrameView::GetWindowMask(const gfx::Size& size, 192 void BubbleFrameView::GetWindowMask(const gfx::Size& size,
185 gfx::Path* window_mask) { 193 gfx::Path* window_mask) {
186 if (bubble_border_->shadow() != BubbleBorder::SMALL_SHADOW && 194 if (bubble_border_->shadow() != BubbleBorder::SMALL_SHADOW &&
187 bubble_border_->shadow() != BubbleBorder::NO_SHADOW_OPAQUE_BORDER && 195 bubble_border_->shadow() != BubbleBorder::NO_SHADOW_OPAQUE_BORDER &&
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 } 234 }
227 235
228 void BubbleFrameView::UpdateWindowIcon() { 236 void BubbleFrameView::UpdateWindowIcon() {
229 gfx::ImageSkia image; 237 gfx::ImageSkia image;
230 if (GetWidget()->widget_delegate()->ShouldShowWindowIcon()) 238 if (GetWidget()->widget_delegate()->ShouldShowWindowIcon())
231 image = GetWidget()->widget_delegate()->GetWindowIcon(); 239 image = GetWidget()->widget_delegate()->GetWindowIcon();
232 title_icon_->SetImage(&image); 240 title_icon_->SetImage(&image);
233 } 241 }
234 242
235 void BubbleFrameView::UpdateWindowTitle() { 243 void BubbleFrameView::UpdateWindowTitle() {
236 title_->SetText(GetWidget()->widget_delegate()->GetWindowTitle()); 244 if (default_title_) {
237 title_->SetVisible(GetWidget()->widget_delegate()->ShouldShowWindowTitle()); 245 const WidgetDelegate* delegate = GetWidget()->widget_delegate();
246 default_title_->SetVisible(delegate->ShouldShowWindowTitle());
247 default_title_->SetText(delegate->GetWindowTitle());
248 } // delegate_title_'s updates are handled by its creator.
238 } 249 }
239 250
240 void BubbleFrameView::SizeConstraintsChanged() {} 251 void BubbleFrameView::SizeConstraintsChanged() {}
241 252
242 void BubbleFrameView::SetTitleFontList(const gfx::FontList& font_list) { 253 void BubbleFrameView::SetTitleView(View* title_view) {
243 title_->SetFontList(font_list); 254 DCHECK(title_view);
255 delete default_title_;
256 default_title_ = nullptr;
257 delete delegate_title_;
258 delegate_title_ = title_view;
259 // Keep the title after the icon for focus order.
260 AddChildViewAt(delegate_title_, 1);
244 } 261 }
245 262
246 const char* BubbleFrameView::GetClassName() const { 263 const char* BubbleFrameView::GetClassName() const {
247 return kViewClassName; 264 return kViewClassName;
248 } 265 }
249 266
250 gfx::Insets BubbleFrameView::GetInsets() const { 267 gfx::Insets BubbleFrameView::GetInsets() const {
251 gfx::Insets insets = content_margins_; 268 gfx::Insets insets = content_margins_;
252 269
253 const int icon_height = title_icon_->GetPreferredSize().height(); 270 const int icon_height = title_icon_->GetPreferredSize().height();
254 const int label_height = title_->GetPreferredSize().height(); 271 const int label_height = title()->GetPreferredSize().height();
255 const bool has_title = icon_height > 0 || label_height > 0; 272 const bool has_title = icon_height > 0 || label_height > 0;
256 const int title_padding = has_title ? title_margins_.height() : 0; 273 const int title_padding = has_title ? title_margins_.height() : 0;
257 const int title_height = std::max(icon_height, label_height) + title_padding; 274 const int title_height = std::max(icon_height, label_height) + title_padding;
258 const int close_height = 275 const int close_height =
259 GetWidget()->widget_delegate()->ShouldShowCloseButton() 276 GetWidget()->widget_delegate()->ShouldShowCloseButton()
260 ? close_->height() + LayoutProvider::Get()->GetDistanceMetric( 277 ? close_->height() + LayoutProvider::Get()->GetDistanceMetric(
261 DISTANCE_CLOSE_BUTTON_MARGIN) 278 DISTANCE_CLOSE_BUTTON_MARGIN)
262 : 0; 279 : 0;
263 insets += gfx::Insets(std::max(title_height, close_height), 0, 0, 0); 280 insets += gfx::Insets(std::max(title_height, close_height), 0, 0, 0);
264 return insets; 281 return insets;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 #endif // OS_MACOSX 314 #endif // OS_MACOSX
298 // Non-dialog bubbles should be non-resizable, so its max size is its 315 // Non-dialog bubbles should be non-resizable, so its max size is its
299 // preferred size. 316 // preferred size.
300 return GetPreferredSize(); 317 return GetPreferredSize();
301 #endif 318 #endif
302 } 319 }
303 320
304 void BubbleFrameView::Layout() { 321 void BubbleFrameView::Layout() {
305 // The title margins may not be set, but make sure that's only the case when 322 // The title margins may not be set, but make sure that's only the case when
306 // there's no title. 323 // there's no title.
307 DCHECK(!title_margins_.IsEmpty() || !title_->visible()); 324 DCHECK(!title_margins_.IsEmpty() ||
325 (!delegate_title_ && !default_title_->visible()));
308 326
309 const gfx::Rect contents_bounds = GetContentsBounds(); 327 const gfx::Rect contents_bounds = GetContentsBounds();
310 gfx::Rect bounds = contents_bounds; 328 gfx::Rect bounds = contents_bounds;
311 bounds.Inset(title_margins_); 329 bounds.Inset(title_margins_);
312 if (bounds.IsEmpty()) 330 if (bounds.IsEmpty())
313 return; 331 return;
314 332
315 // The close button is positioned somewhat closer to the edge of the bubble. 333 int title_label_right = bounds.right();
316 const int close_margin = 334 if (close_->visible()) {
317 LayoutProvider::Get()->GetDistanceMetric(DISTANCE_CLOSE_BUTTON_MARGIN); 335 // The close button is positioned somewhat closer to the edge of the bubble.
318 close_->SetPosition( 336 const int close_margin =
319 gfx::Point(contents_bounds.right() - close_margin - close_->width(), 337 LayoutProvider::Get()->GetDistanceMetric(DISTANCE_CLOSE_BUTTON_MARGIN);
320 contents_bounds.y() + close_margin)); 338 close_->SetPosition(
339 gfx::Point(contents_bounds.right() - close_margin - close_->width(),
340 contents_bounds.y() + close_margin));
341 title_label_right = close_->x();
342 }
321 343
322 gfx::Size title_icon_pref_size(title_icon_->GetPreferredSize()); 344 gfx::Size title_icon_pref_size(title_icon_->GetPreferredSize());
323 int padding = 0; 345 const int title_icon_padding =
324 int title_height = title_icon_pref_size.height(); 346 title_icon_pref_size.width() > 0 ? title_margins_.left() : 0;
325 347 const int title_label_x =
326 if (title_->visible() && !title_->text().empty()) { 348 bounds.x() + title_icon_pref_size.width() + title_icon_padding;
327 if (title_icon_pref_size.width() > 0) 349 const int title_available_width =
328 padding = title_margins_.left(); 350 std::max(1, title_label_right - title_label_x);
329 351 const int title_preferred_height =
330 const int title_label_x = 352 title()->GetHeightForWidth(title_available_width);
331 bounds.x() + title_icon_pref_size.width() + padding; 353 const int title_height =
332 title_->SizeToFit(std::max(1, close_->x() - title_label_x)); 354 std::max(title_icon_pref_size.height(), title_preferred_height);
333 title_height = std::max(title_height, title_->height()); 355 title()->SetBounds(title_label_x,
334 title_->SetPosition(gfx::Point( 356 bounds.y() + (title_height - title_preferred_height) / 2,
335 title_label_x, bounds.y() + (title_height - title_->height()) / 2)); 357 title_available_width, title_height);
336 }
337 358
338 title_icon_->SetBounds(bounds.x(), bounds.y(), title_icon_pref_size.width(), 359 title_icon_->SetBounds(bounds.x(), bounds.y(), title_icon_pref_size.width(),
339 title_height); 360 title_height);
340 bounds.set_width(title_->bounds().right() - bounds.x());
341 bounds.set_height(title_height);
342 361
343 if (footnote_container_) { 362 if (footnote_container_) {
344 const int width = contents_bounds.width(); 363 const int width = contents_bounds.width();
345 const int height = footnote_container_->GetHeightForWidth(width); 364 const int height = footnote_container_->GetHeightForWidth(width);
346 footnote_container_->SetBounds( 365 footnote_container_->SetBounds(
347 contents_bounds.x(), contents_bounds.bottom() - height, width, height); 366 contents_bounds.x(), contents_bounds.bottom() - height, width, height);
348 } 367 }
349 } 368 }
350 369
351 void BubbleFrameView::OnThemeChanged() { 370 void BubbleFrameView::OnThemeChanged() {
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 bubble_border_->GetArrowOffset(window_bounds.size()) - offscreen_adjust); 541 bubble_border_->GetArrowOffset(window_bounds.size()) - offscreen_adjust);
523 if (offscreen_adjust) 542 if (offscreen_adjust)
524 SchedulePaint(); 543 SchedulePaint();
525 } 544 }
526 545
527 gfx::Size BubbleFrameView::GetSizeForClientSize( 546 gfx::Size BubbleFrameView::GetSizeForClientSize(
528 const gfx::Size& client_size) const { 547 const gfx::Size& client_size) const {
529 // Accommodate the width of the title bar elements. 548 // Accommodate the width of the title bar elements.
530 int title_bar_width = title_margins_.width() + border()->GetInsets().width(); 549 int title_bar_width = title_margins_.width() + border()->GetInsets().width();
531 gfx::Size title_icon_size = title_icon_->GetPreferredSize(); 550 gfx::Size title_icon_size = title_icon_->GetPreferredSize();
532 gfx::Size title_label_size = title_->GetPreferredSize(); 551 gfx::Size title_label_size = title()->GetPreferredSize();
533 if (title_icon_size.width() > 0 && title_label_size.width() > 0) 552 if (title_icon_size.width() > 0 && title_label_size.width() > 0)
534 title_bar_width += title_margins_.left(); 553 title_bar_width += title_margins_.left();
535 title_bar_width += title_icon_size.width(); 554 title_bar_width += title_icon_size.width();
536 if (close_->visible()) 555 if (close_->visible())
537 title_bar_width += close_->width() + 1; 556 title_bar_width += close_->width() + 1;
538 557
539 gfx::Size size(client_size); 558 gfx::Size size(client_size);
540 gfx::Insets client_insets = GetInsets(); 559 gfx::Insets client_insets = GetInsets();
541 size.Enlarge(client_insets.width(), client_insets.height()); 560 size.Enlarge(client_insets.width(), client_insets.height());
542 size.SetToMax(gfx::Size(title_bar_width, 0)); 561 size.SetToMax(gfx::Size(title_bar_width, 0));
543 562
544 if (footnote_container_) 563 if (footnote_container_)
545 size.Enlarge(0, footnote_container_->GetHeightForWidth(size.width())); 564 size.Enlarge(0, footnote_container_->GetHeightForWidth(size.width()));
546 565
547 DialogDelegate* dialog_delegate = 566 DialogDelegate* dialog_delegate =
548 GetWidget()->widget_delegate()->AsDialogDelegate(); 567 GetWidget()->widget_delegate()->AsDialogDelegate();
549 if (dialog_delegate && dialog_delegate->ShouldSnapFrameWidth()) 568 if (dialog_delegate && dialog_delegate->ShouldSnapFrameWidth())
550 size.set_width(LayoutProvider::Get()->GetSnappedDialogWidth(size.width())); 569 size.set_width(LayoutProvider::Get()->GetSnappedDialogWidth(size.width()));
551 570
552 return size; 571 return size;
553 } 572 }
554 573
555 } // namespace views 574 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698