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

Side by Side Diff: ui/views/window/dialog_client_view.cc

Issue 2660553005: Harmony - convert hung renderer dialog. (Closed)
Patch Set: comments 4 Created 3 years, 10 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/window/dialog_client_view.h" 5 #include "ui/views/window/dialog_client_view.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "build/build_config.h" 9 #include "build/build_config.h"
10 #include "ui/base/material_design/material_design_controller.h" 10 #include "ui/base/material_design/material_design_controller.h"
11 #include "ui/events/keycodes/keyboard_codes.h" 11 #include "ui/events/keycodes/keyboard_codes.h"
12 #include "ui/views/background.h" 12 #include "ui/views/background.h"
13 #include "ui/views/border.h" 13 #include "ui/views/border.h"
14 #include "ui/views/controls/button/blue_button.h" 14 #include "ui/views/controls/button/blue_button.h"
15 #include "ui/views/controls/button/custom_button.h" 15 #include "ui/views/controls/button/custom_button.h"
16 #include "ui/views/controls/button/label_button.h" 16 #include "ui/views/controls/button/label_button.h"
17 #include "ui/views/controls/button/md_text_button.h" 17 #include "ui/views/controls/button/md_text_button.h"
18 #include "ui/views/layout/layout_constants.h"
19 #include "ui/views/style/platform_style.h" 18 #include "ui/views/style/platform_style.h"
20 #include "ui/views/views_delegate.h" 19 #include "ui/views/views_delegate.h"
21 #include "ui/views/widget/widget.h" 20 #include "ui/views/widget/widget.h"
22 #include "ui/views/window/dialog_delegate.h" 21 #include "ui/views/window/dialog_delegate.h"
23 22
24 namespace views { 23 namespace views {
25 24
26 namespace { 25 namespace {
27 26
28 // The group used by the buttons. This name is chosen voluntarily big not to 27 // The group used by the buttons. This name is chosen voluntarily big not to
(...skipping 19 matching lines...) Expand all
48 if (!button) 47 if (!button)
49 return; 48 return;
50 49
51 const gfx::Size size = button->GetPreferredSize(); 50 const gfx::Size size = button->GetPreferredSize();
52 row_bounds->set_width(row_bounds->width() - size.width()); 51 row_bounds->set_width(row_bounds->width() - size.width());
53 DCHECK_LE(button_height, row_bounds->height()); 52 DCHECK_LE(button_height, row_bounds->height());
54 button->SetBounds( 53 button->SetBounds(
55 row_bounds->right(), 54 row_bounds->right(),
56 row_bounds->y() + (row_bounds->height() - button_height) / 2, 55 row_bounds->y() + (row_bounds->height() - button_height) / 2,
57 size.width(), button_height); 56 size.width(), button_height);
58 int spacing = ViewsDelegate::GetInstance() 57 const int spacing =
59 ? ViewsDelegate::GetInstance() 58 ViewsDelegate::GetInstance()->GetDialogRelatedButtonHorizontalSpacing();
60 ->GetDialogRelatedButtonHorizontalSpacing()
61 : kRelatedButtonHSpacing;
62 row_bounds->set_width(row_bounds->width() - spacing); 59 row_bounds->set_width(row_bounds->width() - spacing);
63 } 60 }
64 61
65 } // namespace 62 } // namespace
66 63
67 /////////////////////////////////////////////////////////////////////////////// 64 ///////////////////////////////////////////////////////////////////////////////
68 // DialogClientView, public: 65 // DialogClientView, public:
69 66
70 DialogClientView::DialogClientView(Widget* owner, View* contents_view) 67 DialogClientView::DialogClientView(Widget* owner, View* contents_view)
71 : ClientView(owner, contents_view) { 68 : ClientView(owner, contents_view),
72 button_row_insets_ = 69 button_row_insets_(
73 ViewsDelegate::GetInstance() 70 ViewsDelegate::GetInstance()->GetDialogButtonInsets()) {
74 ? ViewsDelegate::GetInstance()->GetDialogButtonInsets()
75 : gfx::Insets(0, kButtonHEdgeMarginNew, kButtonVEdgeMarginNew,
76 kButtonHEdgeMarginNew);
77 // Doing this now ensures this accelerator will have lower priority than 71 // Doing this now ensures this accelerator will have lower priority than
78 // one set by the contents view. 72 // one set by the contents view.
79 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); 73 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
80
81 if (ViewsDelegate::GetInstance())
82 button_row_insets_ = ViewsDelegate::GetInstance()->GetDialogButtonInsets();
83 } 74 }
84 75
85 DialogClientView::~DialogClientView() { 76 DialogClientView::~DialogClientView() {
86 } 77 }
87 78
88 void DialogClientView::AcceptWindow() { 79 void DialogClientView::AcceptWindow() {
89 // Only notify the delegate once. See |delegate_allowed_close_|'s comment. 80 // Only notify the delegate once. See |delegate_allowed_close_|'s comment.
90 if (!delegate_allowed_close_ && GetDialogDelegate()->Accept()) { 81 if (!delegate_allowed_close_ && GetDialogDelegate()->Accept()) {
91 delegate_allowed_close_ = true; 82 delegate_allowed_close_ = true;
92 GetWidget()->Close(); 83 GetWidget()->Close();
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 return this; 141 return this;
151 } 142 }
152 143
153 //////////////////////////////////////////////////////////////////////////////// 144 ////////////////////////////////////////////////////////////////////////////////
154 // DialogClientView, View overrides: 145 // DialogClientView, View overrides:
155 146
156 gfx::Size DialogClientView::GetPreferredSize() const { 147 gfx::Size DialogClientView::GetPreferredSize() const {
157 // Initialize the size to fit the buttons and extra view row. 148 // Initialize the size to fit the buttons and extra view row.
158 int extra_view_padding = 0; 149 int extra_view_padding = 0;
159 if (!GetDialogDelegate()->GetExtraViewPadding(&extra_view_padding)) 150 if (!GetDialogDelegate()->GetExtraViewPadding(&extra_view_padding))
160 extra_view_padding = ViewsDelegate::GetInstance() 151 extra_view_padding =
161 ? ViewsDelegate::GetInstance() 152 ViewsDelegate::GetInstance()->GetDialogRelatedButtonHorizontalSpacing();
162 ->GetDialogRelatedButtonHorizontalSpacing()
163 : kRelatedButtonHSpacing;
164 gfx::Size size( 153 gfx::Size size(
165 (ok_button_ ? ok_button_->GetPreferredSize().width() : 0) + 154 (ok_button_ ? ok_button_->GetPreferredSize().width() : 0) +
166 (cancel_button_ ? cancel_button_->GetPreferredSize().width() : 0) + 155 (cancel_button_ ? cancel_button_->GetPreferredSize().width() : 0) +
167 (cancel_button_ && ok_button_ 156 (cancel_button_ && ok_button_
168 ? (ViewsDelegate::GetInstance() 157 ? ViewsDelegate::GetInstance()
169 ? ViewsDelegate::GetInstance() 158 ->GetDialogRelatedButtonHorizontalSpacing()
170 ->GetDialogRelatedButtonHorizontalSpacing() 159 : 0) +
171 : kRelatedButtonHSpacing) : 0) + 160 (ShouldShow(extra_view_) ? extra_view_->GetPreferredSize().width()
172 (ShouldShow(extra_view_) ? extra_view_->GetPreferredSize().width() : 0) + 161 : 0) +
173 (ShouldShow(extra_view_) && has_dialog_buttons() ? extra_view_padding 162 (ShouldShow(extra_view_) && has_dialog_buttons() ? extra_view_padding
174 : 0), 163 : 0),
175 0); 164 0);
176 165
177 int buttons_height = GetButtonsAndExtraViewRowHeight(); 166 int buttons_height = GetButtonsAndExtraViewRowHeight();
178 if (buttons_height != 0) { 167 if (buttons_height != 0) {
179 size.Enlarge(0, buttons_height + GetButtonsAndExtraViewRowTopPadding()); 168 size.Enlarge(0, buttons_height);
180 // Inset the buttons and extra view. 169 // Inset the buttons and extra view.
181 const gfx::Insets insets = GetButtonRowInsets(); 170 const gfx::Insets insets = GetButtonRowInsets();
182 size.Enlarge(insets.width(), insets.height()); 171 size.Enlarge(insets.width(), insets.height());
183 } 172 }
184 173
185 // Increase the size as needed to fit the contents view. 174 // Increase the size as needed to fit the contents view.
186 // NOTE: The contents view is not inset on the top or side client view edges. 175 // NOTE: The contents view is not inset on the top or side client view edges.
187 gfx::Size contents_size = contents_view()->GetPreferredSize(); 176 gfx::Size contents_size = contents_view()->GetPreferredSize();
188 size.Enlarge(0, contents_size.height()); 177 size.Enlarge(0, contents_size.height());
189 size.set_width(std::max(size.width(), contents_size.width())); 178 size.set_width(std::max(size.width(), contents_size.width()));
(...skipping 23 matching lines...) Expand all
213 } else { 202 } else {
214 LayoutButton(ok_button_, &row_bounds, button_height); 203 LayoutButton(ok_button_, &row_bounds, button_height);
215 LayoutButton(cancel_button_, &row_bounds, button_height); 204 LayoutButton(cancel_button_, &row_bounds, button_height);
216 } 205 }
217 if (extra_view_) { 206 if (extra_view_) {
218 int custom_padding = 0; 207 int custom_padding = 0;
219 if (has_dialog_buttons() && 208 if (has_dialog_buttons() &&
220 GetDialogDelegate()->GetExtraViewPadding(&custom_padding)) { 209 GetDialogDelegate()->GetExtraViewPadding(&custom_padding)) {
221 // The call to LayoutButton() will already have accounted for some of 210 // The call to LayoutButton() will already have accounted for some of
222 // the padding. 211 // the padding.
223 custom_padding -= GetButtonsAndExtraViewRowTopPadding(); 212 custom_padding -= GetButtonRowInsets().top();
224 row_bounds.set_width(row_bounds.width() - custom_padding); 213 row_bounds.set_width(row_bounds.width() - custom_padding);
225 } 214 }
226 row_bounds.set_width(std::min(row_bounds.width(), 215 row_bounds.set_width(std::min(row_bounds.width(),
227 extra_view_->GetPreferredSize().width())); 216 extra_view_->GetPreferredSize().width()));
228 extra_view_->SetBoundsRect(row_bounds); 217 extra_view_->SetBoundsRect(row_bounds);
229 } 218 }
230 219
231 if (height > 0) 220 if (height > 0)
232 bounds.Inset(0, 0, 0, height + GetButtonsAndExtraViewRowTopPadding()); 221 bounds.Inset(0, 0, 0, height);
233 } 222 }
234 223
235 // Layout the contents view to the top and side edges of the contents bounds. 224 // Layout the contents view to the top and side edges of the contents bounds.
236 // NOTE: The local insets do not apply to the contents view sides or top. 225 // NOTE: The local insets do not apply to the contents view sides or top.
237 const gfx::Rect contents_bounds = GetContentsBounds(); 226 const gfx::Rect contents_bounds = GetContentsBounds();
238 contents_view()->SetBounds(contents_bounds.x(), contents_bounds.y(), 227 contents_view()->SetBounds(contents_bounds.x(), contents_bounds.y(),
239 contents_bounds.width(), bounds.bottom() - contents_bounds.y()); 228 contents_bounds.width(), bounds.bottom() - contents_bounds.y());
240 } 229 }
241 230
242 bool DialogClientView::AcceleratorPressed(const ui::Accelerator& accelerator) { 231 bool DialogClientView::AcceleratorPressed(const ui::Accelerator& accelerator) {
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 PlatformStyle::kDialogDefaultButtonCanBeCancel); 326 PlatformStyle::kDialogDefaultButtonCanBeCancel);
338 327
339 // The default button is always blue in Harmony. 328 // The default button is always blue in Harmony.
340 if (is_default && (ui::MaterialDesignController::IsSecondaryUiMaterial() || 329 if (is_default && (ui::MaterialDesignController::IsSecondaryUiMaterial() ||
341 GetDialogDelegate()->ShouldDefaultButtonBeBlue())) { 330 GetDialogDelegate()->ShouldDefaultButtonBeBlue())) {
342 button = MdTextButton::CreateSecondaryUiBlueButton(this, title); 331 button = MdTextButton::CreateSecondaryUiBlueButton(this, title);
343 } else { 332 } else {
344 button = MdTextButton::CreateSecondaryUiButton(this, title); 333 button = MdTextButton::CreateSecondaryUiButton(this, title);
345 } 334 }
346 335
347 const int kDialogMinButtonWidth = 75; 336 // TODO(bsep): Setting the minimum size is redundant with MdTextButton, so
348 button->SetMinSize(gfx::Size(kDialogMinButtonWidth, 0)); 337 // this can be deleted when harmony is always on.
Peter Kasting 2017/02/16 01:14:47 We use MdTextButton even pre-Harmony. Seems like
Bret 2017/02/16 01:31:04 I thought that too at first, but CreateSecondaryUi
Peter Kasting 2017/02/16 06:22:31 Well that seems dumb. Honestly I don't know why w
338 const int minimum_width =
339 ViewsDelegate::GetInstance()->GetDialogButtonMinimumWidth();
340 button->SetMinSize(gfx::Size(minimum_width, 0));
341
349 button->SetGroup(kButtonGroup); 342 button->SetGroup(kButtonGroup);
350 return button; 343 return button;
351 } 344 }
352 345
353 int DialogClientView::GetButtonHeight() const { 346 int DialogClientView::GetButtonHeight() const {
354 return std::max( 347 return std::max(
355 ok_button_ ? ok_button_->GetPreferredSize().height() : 0, 348 ok_button_ ? ok_button_->GetPreferredSize().height() : 0,
356 cancel_button_ ? cancel_button_->GetPreferredSize().height() : 0); 349 cancel_button_ ? cancel_button_->GetPreferredSize().height() : 0);
357 } 350 }
358 351
359 int DialogClientView::GetExtraViewHeight() const { 352 int DialogClientView::GetExtraViewHeight() const {
360 return ShouldShow(extra_view_) ? extra_view_->GetPreferredSize().height() : 0; 353 return ShouldShow(extra_view_) ? extra_view_->GetPreferredSize().height() : 0;
361 } 354 }
362 355
363 int DialogClientView::GetButtonsAndExtraViewRowHeight() const { 356 int DialogClientView::GetButtonsAndExtraViewRowHeight() const {
364 return std::max(GetExtraViewHeight(), GetButtonHeight()); 357 return std::max(GetExtraViewHeight(), GetButtonHeight());
365 } 358 }
366 359
367 gfx::Insets DialogClientView::GetButtonRowInsets() const { 360 gfx::Insets DialogClientView::GetButtonRowInsets() const {
368 return GetButtonsAndExtraViewRowHeight() == 0 ? gfx::Insets() 361 if (GetButtonsAndExtraViewRowHeight() == 0)
369 : button_row_insets_; 362 return gfx::Insets();
370 }
371 363
372 int DialogClientView::GetButtonsAndExtraViewRowTopPadding() const {
373 int spacing = button_row_insets_.top();
374 // Some subclasses of DialogClientView, in order to do their own layout, set 364 // Some subclasses of DialogClientView, in order to do their own layout, set
375 // button_row_insets_ to gfx::Insets(). To avoid breaking behavior of those 365 // button_row_insets_ to gfx::Insets(). To avoid breaking behavior of those
376 // dialogs, supplying 0 for the top inset of the row falls back to 366 // dialogs, supplying 0 for the top inset of the row falls back to
377 // ViewsDelegate::GetRelatedControlVerticalSpacing or 367 // ViewsDelegate::GetRelatedControlVerticalSpacing.
378 // kRelatedControlVerticalSpacing. 368 // TODO(bsep): The top inset should never be 0 when harmony is enabled.
379 if (!spacing) 369 const int top = button_row_insets_.top() == 0
380 spacing = ViewsDelegate::GetInstance() 370 ? ViewsDelegate::GetInstance()
381 ? ViewsDelegate::GetInstance() 371 ->GetDialogRelatedControlVerticalSpacing()
382 ->GetDialogRelatedControlVerticalSpacing() 372 : button_row_insets_.top();
383 : kRelatedControlVerticalSpacing; 373 return gfx::Insets(top, button_row_insets_.left(),
384 return spacing; 374 button_row_insets_.bottom(), button_row_insets_.right());
385 } 375 }
386 376
387 void DialogClientView::SetupFocusChain() { 377 void DialogClientView::SetupFocusChain() {
388 // Create a vector of child views in the order of intended focus. 378 // Create a vector of child views in the order of intended focus.
389 std::vector<View*> child_views; 379 std::vector<View*> child_views;
390 child_views.push_back(contents_view()); 380 child_views.push_back(contents_view());
391 child_views.push_back(extra_view_); 381 child_views.push_back(extra_view_);
392 if (kIsOkButtonOnLeftSide) { 382 if (kIsOkButtonOnLeftSide) {
393 child_views.push_back(ok_button_); 383 child_views.push_back(ok_button_);
394 child_views.push_back(cancel_button_); 384 child_views.push_back(cancel_button_);
395 } else { 385 } else {
396 child_views.push_back(cancel_button_); 386 child_views.push_back(cancel_button_);
397 child_views.push_back(ok_button_); 387 child_views.push_back(ok_button_);
398 } 388 }
399 389
400 // Remove all null views from the vector. 390 // Remove all null views from the vector.
401 child_views.erase( 391 child_views.erase(
402 std::remove(child_views.begin(), child_views.end(), nullptr), 392 std::remove(child_views.begin(), child_views.end(), nullptr),
403 child_views.end()); 393 child_views.end());
404 394
405 // Setup focus by reordering views. It is not safe to use SetNextFocusableView 395 // Setup focus by reordering views. It is not safe to use SetNextFocusableView
406 // since child views may be added externally to this view. 396 // since child views may be added externally to this view.
407 for (size_t i = 0; i < child_views.size(); i++) 397 for (size_t i = 0; i < child_views.size(); i++)
408 ReorderChildView(child_views[i], i); 398 ReorderChildView(child_views[i], i);
409 } 399 }
410 400
411 } // namespace views 401 } // namespace views
OLDNEW
« chrome/browser/ui/views/harmony/layout_delegate.h ('K') | « ui/views/window/dialog_client_view.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698