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

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

Issue 2696263002: Refactor ViewsDelegate and MD-ify the icon-to-text spacing for checkbox and radiobutton (Closed)
Patch Set: Prefer embedded initialization over heap allocation for TestViewsDelegate Created 3 years, 9 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/style/platform_style.h" 19 #include "ui/views/style/platform_style.h"
19 #include "ui/views/views_delegate.h" 20 #include "ui/views/views_delegate.h"
20 #include "ui/views/widget/widget.h" 21 #include "ui/views/widget/widget.h"
21 #include "ui/views/window/dialog_delegate.h" 22 #include "ui/views/window/dialog_delegate.h"
22 23
23 namespace views { 24 namespace views {
24 25
25 namespace { 26 namespace {
26 27
27 // 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
47 if (!button) 48 if (!button)
48 return; 49 return;
49 50
50 const gfx::Size size = button->GetPreferredSize(); 51 const gfx::Size size = button->GetPreferredSize();
51 row_bounds->set_width(row_bounds->width() - size.width()); 52 row_bounds->set_width(row_bounds->width() - size.width());
52 DCHECK_LE(button_height, row_bounds->height()); 53 DCHECK_LE(button_height, row_bounds->height());
53 button->SetBounds( 54 button->SetBounds(
54 row_bounds->right(), 55 row_bounds->right(),
55 row_bounds->y() + (row_bounds->height() - button_height) / 2, 56 row_bounds->y() + (row_bounds->height() - button_height) / 2,
56 size.width(), button_height); 57 size.width(), button_height);
57 const int spacing = 58 int spacing = ViewsDelegate::GetInstance()
Peter Kasting 2017/03/01 06:32:37 Doesn't seem like you need to add null-checks. I
58 ViewsDelegate::GetInstance()->GetDialogRelatedButtonHorizontalSpacing(); 59 ? ViewsDelegate::GetInstance()->GetSpacingMetric(
60 SpacingMetric::RELATED_HORIZONTAL_BUTTON)
61 : kRelatedButtonHSpacing;
59 row_bounds->set_width(row_bounds->width() - spacing); 62 row_bounds->set_width(row_bounds->width() - spacing);
60 } 63 }
61 64
62 } // namespace 65 } // namespace
63 66
64 /////////////////////////////////////////////////////////////////////////////// 67 ///////////////////////////////////////////////////////////////////////////////
65 // DialogClientView, public: 68 // DialogClientView, public:
66 69
67 DialogClientView::DialogClientView(Widget* owner, View* contents_view) 70 DialogClientView::DialogClientView(Widget* owner, View* contents_view)
68 : ClientView(owner, contents_view), 71 : ClientView(owner, contents_view),
69 button_row_insets_( 72 button_row_insets_(ViewsDelegate::GetInstance()
70 ViewsDelegate::GetInstance()->GetDialogButtonInsets()) { 73 ? ViewsDelegate::GetInstance()->GetInsetsMetric(
74 InsetsMetric::DIALOG_BUTTON)
75 : gfx::Insets(0,
76 kButtonHEdgeMarginNew,
77 kButtonVEdgeMarginNew,
78 kButtonHEdgeMarginNew)) {
71 // Doing this now ensures this accelerator will have lower priority than 79 // Doing this now ensures this accelerator will have lower priority than
72 // one set by the contents view. 80 // one set by the contents view.
73 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); 81 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
74 } 82 }
75 83
76 DialogClientView::~DialogClientView() { 84 DialogClientView::~DialogClientView() {
77 } 85 }
78 86
79 void DialogClientView::AcceptWindow() { 87 void DialogClientView::AcceptWindow() {
80 // Only notify the delegate once. See |delegate_allowed_close_|'s comment. 88 // Only notify the delegate once. See |delegate_allowed_close_|'s comment.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 122
115 const DialogClientView* DialogClientView::AsDialogClientView() const { 123 const DialogClientView* DialogClientView::AsDialogClientView() const {
116 return this; 124 return this;
117 } 125 }
118 126
119 //////////////////////////////////////////////////////////////////////////////// 127 ////////////////////////////////////////////////////////////////////////////////
120 // DialogClientView, View overrides: 128 // DialogClientView, View overrides:
121 129
122 gfx::Size DialogClientView::GetPreferredSize() const { 130 gfx::Size DialogClientView::GetPreferredSize() const {
123 // Initialize the size to fit the buttons and extra view row. 131 // Initialize the size to fit the buttons and extra view row.
132 int extra_view_padding = 0;
133 if (!GetDialogDelegate()->GetExtraViewPadding(&extra_view_padding))
134 extra_view_padding = ViewsDelegate::GetInstance()
135 ? ViewsDelegate::GetInstance()->GetSpacingMetric(
136 SpacingMetric::RELATED_HORIZONTAL_BUTTON)
137 : kRelatedButtonHSpacing;
Peter Kasting 2017/03/01 06:32:36 More bad merging?
124 gfx::Size size( 138 gfx::Size size(
125 (ok_button_ ? ok_button_->GetPreferredSize().width() : 0) + 139 (ok_button_ ? ok_button_->GetPreferredSize().width() : 0) +
126 (cancel_button_ ? cancel_button_->GetPreferredSize().width() : 0) + 140 (cancel_button_ ? cancel_button_->GetPreferredSize().width() : 0) +
127 (cancel_button_ && ok_button_ 141 (cancel_button_ && ok_button_
128 ? ViewsDelegate::GetInstance() 142 ? (ViewsDelegate::GetInstance()
129 ->GetDialogRelatedButtonHorizontalSpacing() 143 ? ViewsDelegate::GetInstance()->GetSpacingMetric(
144 SpacingMetric::RELATED_HORIZONTAL_BUTTON)
145 : kRelatedButtonHSpacing)
130 : 0) + 146 : 0) +
131 (ShouldShow(extra_view_) ? extra_view_->GetPreferredSize().width() 147 (ShouldShow(extra_view_) ? extra_view_->GetPreferredSize().width()
132 : 0) + 148 : 0) +
133 GetExtraViewSpacing(), 149 GetExtraViewSpacing(),
134 0); 150 0);
135 151
136 int buttons_height = GetButtonsAndExtraViewRowHeight(); 152 int buttons_height = GetButtonsAndExtraViewRowHeight();
137 if (buttons_height != 0) { 153 if (buttons_height != 0) {
138 size.Enlarge(0, buttons_height); 154 size.Enlarge(0, buttons_height);
139 // Inset the buttons and extra view. 155 // Inset the buttons and extra view.
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 } else { 192 } else {
177 LayoutButton(ok_button_, &row_bounds, button_height); 193 LayoutButton(ok_button_, &row_bounds, button_height);
178 LayoutButton(cancel_button_, &row_bounds, button_height); 194 LayoutButton(cancel_button_, &row_bounds, button_height);
179 } 195 }
180 if (extra_view_) { 196 if (extra_view_) {
181 int custom_padding = 0; 197 int custom_padding = 0;
182 if (has_dialog_buttons() && 198 if (has_dialog_buttons() &&
183 GetDialogDelegate()->GetExtraViewPadding(&custom_padding)) { 199 GetDialogDelegate()->GetExtraViewPadding(&custom_padding)) {
184 // The padding between buttons applied in LayoutButton() will already 200 // The padding between buttons applied in LayoutButton() will already
185 // have accounted for some of the distance here. 201 // have accounted for some of the distance here.
186 custom_padding -= ViewsDelegate::GetInstance() 202 custom_padding -= ViewsDelegate::GetInstance()->GetSpacingMetric(
187 ->GetDialogRelatedButtonHorizontalSpacing(); 203 SpacingMetric::RELATED_HORIZONTAL_BUTTON);
188 row_bounds.set_width(row_bounds.width() - custom_padding); 204 row_bounds.set_width(row_bounds.width() - custom_padding);
189 } 205 }
190 row_bounds.set_width(std::min(row_bounds.width(), 206 row_bounds.set_width(std::min(row_bounds.width(),
191 extra_view_->GetPreferredSize().width())); 207 extra_view_->GetPreferredSize().width()));
192 extra_view_->SetBoundsRect(row_bounds); 208 extra_view_->SetBoundsRect(row_bounds);
193 } 209 }
194 210
195 if (height > 0) { 211 if (height > 0) {
196 // Inset to the top of the buttons, plus their top padding, in order to 212 // Inset to the top of the buttons, plus their top padding, in order to
197 // exclude that area from the content view's bounds. 213 // exclude that area from the content view's bounds.
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 PlatformStyle::kDialogDefaultButtonCanBeCancel); 305 PlatformStyle::kDialogDefaultButtonCanBeCancel);
290 306
291 // The default button is always blue in Harmony. 307 // The default button is always blue in Harmony.
292 if (is_default && (ui::MaterialDesignController::IsSecondaryUiMaterial() || 308 if (is_default && (ui::MaterialDesignController::IsSecondaryUiMaterial() ||
293 GetDialogDelegate()->ShouldDefaultButtonBeBlue())) { 309 GetDialogDelegate()->ShouldDefaultButtonBeBlue())) {
294 button = MdTextButton::CreateSecondaryUiBlueButton(this, title); 310 button = MdTextButton::CreateSecondaryUiBlueButton(this, title);
295 } else { 311 } else {
296 button = MdTextButton::CreateSecondaryUiButton(this, title); 312 button = MdTextButton::CreateSecondaryUiButton(this, title);
297 } 313 }
298 314
299 const int minimum_width = 315 // TODO(bsep): Setting the minimum size is redundant with MdTextButton, so
300 ViewsDelegate::GetInstance()->GetDialogButtonMinimumWidth(); 316 // this can be deleted when harmony is always on.
Peter Kasting 2017/03/01 06:32:37 Another bad merge, I think
317 const int minimum_width = ViewsDelegate::GetInstance()->GetSpacingMetric(
318 SpacingMetric::DIALOG_BUTTON_MINIMUM_WIDTH);
301 button->SetMinSize(gfx::Size(minimum_width, 0)); 319 button->SetMinSize(gfx::Size(minimum_width, 0));
302 320
303 button->SetGroup(kButtonGroup); 321 button->SetGroup(kButtonGroup);
304 return button; 322 return button;
305 } 323 }
306 324
307 int DialogClientView::GetButtonHeight() const { 325 int DialogClientView::GetButtonHeight() const {
308 return std::max( 326 return std::max(
309 ok_button_ ? ok_button_->GetPreferredSize().height() : 0, 327 ok_button_ ? ok_button_->GetPreferredSize().height() : 0,
310 cancel_button_ ? cancel_button_->GetPreferredSize().height() : 0); 328 cancel_button_ ? cancel_button_->GetPreferredSize().height() : 0);
311 } 329 }
312 330
313 int DialogClientView::GetExtraViewHeight() const { 331 int DialogClientView::GetExtraViewHeight() const {
314 return ShouldShow(extra_view_) ? extra_view_->GetPreferredSize().height() : 0; 332 return ShouldShow(extra_view_) ? extra_view_->GetPreferredSize().height() : 0;
315 } 333 }
316 334
317 int DialogClientView::GetButtonsAndExtraViewRowHeight() const { 335 int DialogClientView::GetButtonsAndExtraViewRowHeight() const {
318 return std::max(GetExtraViewHeight(), GetButtonHeight()); 336 return std::max(GetExtraViewHeight(), GetButtonHeight());
319 } 337 }
320 338
321 gfx::Insets DialogClientView::GetButtonRowInsets() const { 339 gfx::Insets DialogClientView::GetButtonRowInsets() const {
322 if (GetButtonsAndExtraViewRowHeight() == 0) 340 if (GetButtonsAndExtraViewRowHeight() == 0)
323 return gfx::Insets(); 341 return gfx::Insets();
324 342
325 // Some subclasses of DialogClientView, in order to do their own layout, set 343 // Some subclasses of DialogClientView, in order to do their own layout, set
326 // button_row_insets_ to gfx::Insets(). To avoid breaking behavior of those 344 // button_row_insets_ to gfx::Insets(). To avoid breaking behavior of those
327 // dialogs, supplying 0 for the top inset of the row falls back to 345 // dialogs, supplying 0 for the top inset of the row falls back to
328 // ViewsDelegate::GetRelatedControlVerticalSpacing. 346 // ViewsDelegate::GetSpacingMetric(SpacingMetric::RELATED_VERTICAL_CONTROL).
329 // TODO(bsep): The top inset should never be 0 when harmony is enabled. 347 // TODO(bsep): The top inset should never be 0 when harmony is enabled.
330 const int top = button_row_insets_.top() == 0 348 const int top = button_row_insets_.top() == 0
331 ? ViewsDelegate::GetInstance() 349 ? ViewsDelegate::GetInstance()->GetSpacingMetric(
332 ->GetDialogRelatedControlVerticalSpacing() 350 SpacingMetric::RELATED_VERTICAL_CONTROL)
333 : button_row_insets_.top(); 351 : button_row_insets_.top();
334 return gfx::Insets(top, button_row_insets_.left(), 352 return gfx::Insets(top, button_row_insets_.left(),
335 button_row_insets_.bottom(), button_row_insets_.right()); 353 button_row_insets_.bottom(), button_row_insets_.right());
336 } 354 }
337 355
338 void DialogClientView::SetupFocusChain() { 356 void DialogClientView::SetupFocusChain() {
339 // Create a vector of child views in the order of intended focus. 357 // Create a vector of child views in the order of intended focus.
340 std::vector<View*> child_views; 358 std::vector<View*> child_views;
341 child_views.push_back(contents_view()); 359 child_views.push_back(contents_view());
342 child_views.push_back(extra_view_); 360 child_views.push_back(extra_view_);
(...skipping 17 matching lines...) Expand all
360 } 378 }
361 379
362 int DialogClientView::GetExtraViewSpacing() const { 380 int DialogClientView::GetExtraViewSpacing() const {
363 if (!ShouldShow(extra_view_) || !has_dialog_buttons()) 381 if (!ShouldShow(extra_view_) || !has_dialog_buttons())
364 return 0; 382 return 0;
365 383
366 int extra_view_padding = 0; 384 int extra_view_padding = 0;
367 if (GetDialogDelegate()->GetExtraViewPadding(&extra_view_padding)) 385 if (GetDialogDelegate()->GetExtraViewPadding(&extra_view_padding))
368 return extra_view_padding; 386 return extra_view_padding;
369 387
370 return ViewsDelegate::GetInstance() 388 return ViewsDelegate::GetInstance()->GetSpacingMetric(
371 ->GetDialogRelatedButtonHorizontalSpacing(); 389 views::SpacingMetric::RELATED_HORIZONTAL_BUTTON);
372 } 390 }
373 391
374 void DialogClientView::SetupViews() { 392 void DialogClientView::SetupViews() {
375 const int buttons = GetDialogDelegate()->GetDialogButtons(); 393 const int buttons = GetDialogDelegate()->GetDialogButtons();
376 394
377 if (buttons & ui::DIALOG_BUTTON_OK) { 395 if (buttons & ui::DIALOG_BUTTON_OK) {
378 if (!ok_button_) { 396 if (!ok_button_) {
379 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK); 397 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK);
380 AddChildView(ok_button_); 398 AddChildView(ok_button_);
381 } 399 }
(...skipping 20 matching lines...) Expand all
402 return; 420 return;
403 421
404 extra_view_ = GetDialogDelegate()->CreateExtraView(); 422 extra_view_ = GetDialogDelegate()->CreateExtraView();
405 if (extra_view_) { 423 if (extra_view_) {
406 extra_view_->SetGroup(kButtonGroup); 424 extra_view_->SetGroup(kButtonGroup);
407 AddChildView(extra_view_); 425 AddChildView(extra_view_);
408 } 426 }
409 } 427 }
410 428
411 } // namespace views 429 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698