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

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: Reduced ViewsDelegate layout/metric functions to only two functions 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"
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 int spacing =
59 ? ViewsDelegate::GetInstance() 59 ViewsDelegate::GetInstance()
60 ->GetDialogRelatedButtonHorizontalSpacing() 60 ? ViewsDelegate::GetInstance()->GetSpacingMetric(
61 : kRelatedButtonHSpacing; 61 ViewsDelegate::SpacingMetric::RELATED_HORIZONTAL_BUTTON)
62 : kRelatedButtonHSpacing;
62 row_bounds->set_width(row_bounds->width() - spacing); 63 row_bounds->set_width(row_bounds->width() - spacing);
63 } 64 }
64 65
65 } // namespace 66 } // namespace
66 67
67 /////////////////////////////////////////////////////////////////////////////// 68 ///////////////////////////////////////////////////////////////////////////////
68 // DialogClientView, public: 69 // DialogClientView, public:
69 70
70 DialogClientView::DialogClientView(Widget* owner, View* contents_view) 71 DialogClientView::DialogClientView(Widget* owner, View* contents_view)
71 : ClientView(owner, contents_view) { 72 : ClientView(owner, contents_view) {
72 button_row_insets_ = 73 button_row_insets_ =
73 ViewsDelegate::GetInstance() 74 ViewsDelegate::GetInstance()
74 ? ViewsDelegate::GetInstance()->GetDialogButtonInsets() 75 ? ViewsDelegate::GetInstance()->GetInsetsMetric(
76 ViewsDelegate::InsetsMetric::DIALOG_BUTTON)
75 : gfx::Insets(0, kButtonHEdgeMarginNew, kButtonVEdgeMarginNew, 77 : gfx::Insets(0, kButtonHEdgeMarginNew, kButtonVEdgeMarginNew,
76 kButtonHEdgeMarginNew); 78 kButtonHEdgeMarginNew);
77 // Doing this now ensures this accelerator will have lower priority than 79 // Doing this now ensures this accelerator will have lower priority than
78 // one set by the contents view. 80 // one set by the contents view.
79 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); 81 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
80
81 if (ViewsDelegate::GetInstance())
82 button_row_insets_ = ViewsDelegate::GetInstance()->GetDialogButtonInsets();
83 } 82 }
84 83
85 DialogClientView::~DialogClientView() { 84 DialogClientView::~DialogClientView() {
86 } 85 }
87 86
88 void DialogClientView::AcceptWindow() { 87 void DialogClientView::AcceptWindow() {
89 // Only notify the delegate once. See |delegate_allowed_close_|'s comment. 88 // Only notify the delegate once. See |delegate_allowed_close_|'s comment.
90 if (!delegate_allowed_close_ && GetDialogDelegate()->Accept()) { 89 if (!delegate_allowed_close_ && GetDialogDelegate()->Accept()) {
91 delegate_allowed_close_ = true; 90 delegate_allowed_close_ = true;
92 GetWidget()->Close(); 91 GetWidget()->Close();
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 return this; 149 return this;
151 } 150 }
152 151
153 //////////////////////////////////////////////////////////////////////////////// 152 ////////////////////////////////////////////////////////////////////////////////
154 // DialogClientView, View overrides: 153 // DialogClientView, View overrides:
155 154
156 gfx::Size DialogClientView::GetPreferredSize() const { 155 gfx::Size DialogClientView::GetPreferredSize() const {
157 // Initialize the size to fit the buttons and extra view row. 156 // Initialize the size to fit the buttons and extra view row.
158 int extra_view_padding = 0; 157 int extra_view_padding = 0;
159 if (!GetDialogDelegate()->GetExtraViewPadding(&extra_view_padding)) 158 if (!GetDialogDelegate()->GetExtraViewPadding(&extra_view_padding))
160 extra_view_padding = ViewsDelegate::GetInstance() 159 extra_view_padding =
161 ? ViewsDelegate::GetInstance() 160 ViewsDelegate::GetInstance()
162 ->GetDialogRelatedButtonHorizontalSpacing() 161 ? ViewsDelegate::GetInstance()->GetSpacingMetric(
163 : kRelatedButtonHSpacing; 162 ViewsDelegate::SpacingMetric::RELATED_HORIZONTAL_BUTTON)
163 : kRelatedButtonHSpacing;
164 gfx::Size size( 164 gfx::Size size(
165 (ok_button_ ? ok_button_->GetPreferredSize().width() : 0) + 165 (ok_button_ ? ok_button_->GetPreferredSize().width() : 0) +
166 (cancel_button_ ? cancel_button_->GetPreferredSize().width() : 0) + 166 (cancel_button_ ? cancel_button_->GetPreferredSize().width() : 0) +
167 (cancel_button_ && ok_button_ 167 (cancel_button_ && ok_button_
168 ? (ViewsDelegate::GetInstance() 168 ? (ViewsDelegate::GetInstance()
169 ? ViewsDelegate::GetInstance() 169 ? ViewsDelegate::GetInstance()->GetSpacingMetric(
170 ->GetDialogRelatedButtonHorizontalSpacing() 170 ViewsDelegate::SpacingMetric::
171 : kRelatedButtonHSpacing) : 0) + 171 RELATED_HORIZONTAL_BUTTON)
172 (ShouldShow(extra_view_) ? extra_view_->GetPreferredSize().width() : 0) + 172 : kRelatedButtonHSpacing)
173 (ShouldShow(extra_view_) && has_dialog_buttons() ? extra_view_padding 173 : 0) +
174 : 0), 174 (ShouldShow(extra_view_) ? extra_view_->GetPreferredSize().width()
175 : 0) +
176 (ShouldShow(extra_view_) && has_dialog_buttons() ? extra_view_padding
177 : 0),
175 0); 178 0);
176 179
177 int buttons_height = GetButtonsAndExtraViewRowHeight(); 180 int buttons_height = GetButtonsAndExtraViewRowHeight();
178 if (buttons_height != 0) { 181 if (buttons_height != 0) {
179 size.Enlarge(0, buttons_height + GetButtonsAndExtraViewRowTopPadding()); 182 size.Enlarge(0, buttons_height + GetButtonsAndExtraViewRowTopPadding());
180 // Inset the buttons and extra view. 183 // Inset the buttons and extra view.
181 const gfx::Insets insets = GetButtonRowInsets(); 184 const gfx::Insets insets = GetButtonRowInsets();
182 size.Enlarge(insets.width(), insets.height()); 185 size.Enlarge(insets.width(), insets.height());
183 } 186 }
184 187
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 gfx::Insets DialogClientView::GetButtonRowInsets() const { 370 gfx::Insets DialogClientView::GetButtonRowInsets() const {
368 return GetButtonsAndExtraViewRowHeight() == 0 ? gfx::Insets() 371 return GetButtonsAndExtraViewRowHeight() == 0 ? gfx::Insets()
369 : button_row_insets_; 372 : button_row_insets_;
370 } 373 }
371 374
372 int DialogClientView::GetButtonsAndExtraViewRowTopPadding() const { 375 int DialogClientView::GetButtonsAndExtraViewRowTopPadding() const {
373 int spacing = button_row_insets_.top(); 376 int spacing = button_row_insets_.top();
374 // Some subclasses of DialogClientView, in order to do their own layout, set 377 // 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 378 // 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 379 // dialogs, supplying 0 for the top inset of the row falls back to
377 // ViewsDelegate::GetRelatedControlVerticalSpacing or 380 // ViewsDelegate::GetSpacingMetric(
381 // ViewsDelegate::SpacingMetric::RELATED_VERTICAL_CONTROL) or
378 // kRelatedControlVerticalSpacing. 382 // kRelatedControlVerticalSpacing.
379 if (!spacing) 383 if (!spacing)
380 spacing = ViewsDelegate::GetInstance() 384 spacing = ViewsDelegate::GetInstance()
381 ? ViewsDelegate::GetInstance() 385 ? ViewsDelegate::GetInstance()->GetSpacingMetric(
382 ->GetDialogRelatedControlVerticalSpacing() 386 ViewsDelegate::SpacingMetric::RELATED_VERTICAL_CONTROL)
383 : kRelatedControlVerticalSpacing; 387 : kRelatedControlVerticalSpacing;
384 return spacing; 388 return spacing;
385 } 389 }
386 390
387 void DialogClientView::SetupFocusChain() { 391 void DialogClientView::SetupFocusChain() {
388 // Create a vector of child views in the order of intended focus. 392 // Create a vector of child views in the order of intended focus.
389 std::vector<View*> child_views; 393 std::vector<View*> child_views;
390 child_views.push_back(contents_view()); 394 child_views.push_back(contents_view());
391 child_views.push_back(extra_view_); 395 child_views.push_back(extra_view_);
392 if (kIsOkButtonOnLeftSide) { 396 if (kIsOkButtonOnLeftSide) {
393 child_views.push_back(ok_button_); 397 child_views.push_back(ok_button_);
394 child_views.push_back(cancel_button_); 398 child_views.push_back(cancel_button_);
395 } else { 399 } else {
396 child_views.push_back(cancel_button_); 400 child_views.push_back(cancel_button_);
397 child_views.push_back(ok_button_); 401 child_views.push_back(ok_button_);
398 } 402 }
399 403
400 // Remove all null views from the vector. 404 // Remove all null views from the vector.
401 child_views.erase( 405 child_views.erase(
402 std::remove(child_views.begin(), child_views.end(), nullptr), 406 std::remove(child_views.begin(), child_views.end(), nullptr),
403 child_views.end()); 407 child_views.end());
404 408
405 // Setup focus by reordering views. It is not safe to use SetNextFocusableView 409 // Setup focus by reordering views. It is not safe to use SetNextFocusableView
406 // since child views may be added externally to this view. 410 // since child views may be added externally to this view.
407 for (size_t i = 0; i < child_views.size(); i++) 411 for (size_t i = 0; i < child_views.size(); i++)
408 ReorderChildView(child_views[i], i); 412 ReorderChildView(child_views[i], i);
409 } 413 }
410 414
411 } // namespace views 415 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698