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

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: Added GetDefaultSpacingMetric to ViewsDelegate to start process of hiding layout constants 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/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()
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;
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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 // TODO(bsep): Setting the minimum size is redundant with MdTextButton, so 315 // TODO(bsep): Setting the minimum size is redundant with MdTextButton, so
300 // this can be deleted when harmony is always on. 316 // this can be deleted when harmony is always on.
301 const int minimum_width = 317 const int minimum_width = ViewsDelegate::GetInstance()->GetSpacingMetric(
302 ViewsDelegate::GetInstance()->GetDialogButtonMinimumWidth(); 318 SpacingMetric::DIALOG_BUTTON_MINIMUM_WIDTH);
303 button->SetMinSize(gfx::Size(minimum_width, 0)); 319 button->SetMinSize(gfx::Size(minimum_width, 0));
304 320
305 button->SetGroup(kButtonGroup); 321 button->SetGroup(kButtonGroup);
306 return button; 322 return button;
307 } 323 }
308 324
309 int DialogClientView::GetButtonHeight() const { 325 int DialogClientView::GetButtonHeight() const {
310 return std::max( 326 return std::max(
311 ok_button_ ? ok_button_->GetPreferredSize().height() : 0, 327 ok_button_ ? ok_button_->GetPreferredSize().height() : 0,
312 cancel_button_ ? cancel_button_->GetPreferredSize().height() : 0); 328 cancel_button_ ? cancel_button_->GetPreferredSize().height() : 0);
313 } 329 }
314 330
315 int DialogClientView::GetExtraViewHeight() const { 331 int DialogClientView::GetExtraViewHeight() const {
316 return ShouldShow(extra_view_) ? extra_view_->GetPreferredSize().height() : 0; 332 return ShouldShow(extra_view_) ? extra_view_->GetPreferredSize().height() : 0;
317 } 333 }
318 334
319 int DialogClientView::GetButtonsAndExtraViewRowHeight() const { 335 int DialogClientView::GetButtonsAndExtraViewRowHeight() const {
320 return std::max(GetExtraViewHeight(), GetButtonHeight()); 336 return std::max(GetExtraViewHeight(), GetButtonHeight());
321 } 337 }
322 338
323 gfx::Insets DialogClientView::GetButtonRowInsets() const { 339 gfx::Insets DialogClientView::GetButtonRowInsets() const {
324 if (GetButtonsAndExtraViewRowHeight() == 0) 340 if (GetButtonsAndExtraViewRowHeight() == 0)
325 return gfx::Insets(); 341 return gfx::Insets();
326 342
327 // 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
328 // 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
329 // 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
330 // ViewsDelegate::GetRelatedControlVerticalSpacing. 346 // ViewsDelegate::GetSpacingMetric(SpacingMetric::RELATED_VERTICAL_CONTROL).
331 // 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.
332 const int top = button_row_insets_.top() == 0 348 const int top = button_row_insets_.top() == 0
333 ? ViewsDelegate::GetInstance() 349 ? ViewsDelegate::GetInstance()->GetSpacingMetric(
334 ->GetDialogRelatedControlVerticalSpacing() 350 SpacingMetric::RELATED_VERTICAL_CONTROL)
335 : button_row_insets_.top(); 351 : button_row_insets_.top();
336 return gfx::Insets(top, button_row_insets_.left(), 352 return gfx::Insets(top, button_row_insets_.left(),
337 button_row_insets_.bottom(), button_row_insets_.right()); 353 button_row_insets_.bottom(), button_row_insets_.right());
338 } 354 }
339 355
340 void DialogClientView::SetupFocusChain() { 356 void DialogClientView::SetupFocusChain() {
341 // 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.
342 std::vector<View*> child_views; 358 std::vector<View*> child_views;
343 child_views.push_back(contents_view()); 359 child_views.push_back(contents_view());
344 child_views.push_back(extra_view_); 360 child_views.push_back(extra_view_);
(...skipping 17 matching lines...) Expand all
362 } 378 }
363 379
364 int DialogClientView::GetExtraViewSpacing() const { 380 int DialogClientView::GetExtraViewSpacing() const {
365 if (!ShouldShow(extra_view_) || !has_dialog_buttons()) 381 if (!ShouldShow(extra_view_) || !has_dialog_buttons())
366 return 0; 382 return 0;
367 383
368 int extra_view_padding = 0; 384 int extra_view_padding = 0;
369 if (GetDialogDelegate()->GetExtraViewPadding(&extra_view_padding)) 385 if (GetDialogDelegate()->GetExtraViewPadding(&extra_view_padding))
370 return extra_view_padding; 386 return extra_view_padding;
371 387
372 return ViewsDelegate::GetInstance() 388 return ViewsDelegate::GetInstance()->GetSpacingMetric(
373 ->GetDialogRelatedButtonHorizontalSpacing(); 389 views::SpacingMetric::RELATED_HORIZONTAL_BUTTON);
374 } 390 }
375 391
376 void DialogClientView::SetupViews() { 392 void DialogClientView::SetupViews() {
377 const int buttons = GetDialogDelegate()->GetDialogButtons(); 393 const int buttons = GetDialogDelegate()->GetDialogButtons();
378 394
379 if (buttons & ui::DIALOG_BUTTON_OK) { 395 if (buttons & ui::DIALOG_BUTTON_OK) {
380 if (!ok_button_) { 396 if (!ok_button_) {
381 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK); 397 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK);
382 AddChildView(ok_button_); 398 AddChildView(ok_button_);
383 } 399 }
(...skipping 20 matching lines...) Expand all
404 return; 420 return;
405 421
406 extra_view_ = GetDialogDelegate()->CreateExtraView(); 422 extra_view_ = GetDialogDelegate()->CreateExtraView();
407 if (extra_view_) { 423 if (extra_view_) {
408 extra_view_->SetGroup(kButtonGroup); 424 extra_view_->SetGroup(kButtonGroup);
409 AddChildView(extra_view_); 425 AddChildView(extra_view_);
410 } 426 }
411 } 427 }
412 428
413 } // namespace views 429 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698