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

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

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