Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 86 | 86 |
| 87 void DialogClientView::CancelWindow() { | 87 void DialogClientView::CancelWindow() { |
| 88 // Only notify the delegate once. See |delegate_allowed_close_|'s comment. | 88 // Only notify the delegate once. See |delegate_allowed_close_|'s comment. |
| 89 if (!delegate_allowed_close_ && GetDialogDelegate()->Cancel()) { | 89 if (!delegate_allowed_close_ && GetDialogDelegate()->Cancel()) { |
| 90 delegate_allowed_close_ = true; | 90 delegate_allowed_close_ = true; |
| 91 GetWidget()->Close(); | 91 GetWidget()->Close(); |
| 92 } | 92 } |
| 93 } | 93 } |
| 94 | 94 |
| 95 void DialogClientView::UpdateDialogButtons() { | 95 void DialogClientView::UpdateDialogButtons() { |
| 96 const int buttons = GetDialogDelegate()->GetDialogButtons(); | 96 SetupViews(); |
| 97 | |
| 98 if (buttons & ui::DIALOG_BUTTON_OK) { | |
| 99 if (!ok_button_) { | |
| 100 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK); | |
| 101 AddChildView(ok_button_); | |
| 102 } | |
| 103 | |
| 104 GetDialogDelegate()->UpdateButton(ok_button_, ui::DIALOG_BUTTON_OK); | |
| 105 } else if (ok_button_) { | |
| 106 delete ok_button_; | |
| 107 ok_button_ = nullptr; | |
| 108 } | |
| 109 | |
| 110 if (buttons & ui::DIALOG_BUTTON_CANCEL) { | |
| 111 if (!cancel_button_) { | |
| 112 cancel_button_ = CreateDialogButton(ui::DIALOG_BUTTON_CANCEL); | |
| 113 AddChildView(cancel_button_); | |
| 114 } | |
| 115 | |
| 116 GetDialogDelegate()->UpdateButton(cancel_button_, ui::DIALOG_BUTTON_CANCEL); | |
| 117 } else if (cancel_button_) { | |
| 118 delete cancel_button_; | |
| 119 cancel_button_ = nullptr; | |
| 120 } | |
| 121 | |
| 122 SetupFocusChain(); | 97 SetupFocusChain(); |
| 123 } | 98 } |
| 124 | 99 |
| 125 /////////////////////////////////////////////////////////////////////////////// | 100 /////////////////////////////////////////////////////////////////////////////// |
| 126 // DialogClientView, ClientView overrides: | 101 // DialogClientView, ClientView overrides: |
| 127 | 102 |
| 128 bool DialogClientView::CanClose() { | 103 bool DialogClientView::CanClose() { |
| 129 // If the dialog is closing but no Accept or Cancel action has been performed | 104 // If the dialog is closing but no Accept or Cancel action has been performed |
| 130 // before, it's a Close action. | 105 // before, it's a Close action. |
| 131 if (!delegate_allowed_close_) | 106 if (!delegate_allowed_close_) |
| 132 delegate_allowed_close_ = GetDialogDelegate()->Close(); | 107 delegate_allowed_close_ = GetDialogDelegate()->Close(); |
| 133 return delegate_allowed_close_; | 108 return delegate_allowed_close_; |
| 134 } | 109 } |
| 135 | 110 |
| 136 DialogClientView* DialogClientView::AsDialogClientView() { | 111 DialogClientView* DialogClientView::AsDialogClientView() { |
| 137 return this; | 112 return this; |
| 138 } | 113 } |
| 139 | 114 |
| 140 const DialogClientView* DialogClientView::AsDialogClientView() const { | 115 const DialogClientView* DialogClientView::AsDialogClientView() const { |
| 141 return this; | 116 return this; |
| 142 } | 117 } |
| 143 | 118 |
| 144 //////////////////////////////////////////////////////////////////////////////// | 119 //////////////////////////////////////////////////////////////////////////////// |
| 145 // DialogClientView, View overrides: | 120 // DialogClientView, View overrides: |
| 146 | 121 |
| 147 gfx::Size DialogClientView::GetPreferredSize() const { | 122 gfx::Size DialogClientView::GetPreferredSize() const { |
| 148 // Initialize the size to fit the buttons and extra view row. | 123 // Initialize the size to fit the buttons and extra view row. |
| 149 int extra_view_padding = 0; | |
| 150 if (!GetDialogDelegate()->GetExtraViewPadding(&extra_view_padding)) | |
| 151 extra_view_padding = | |
| 152 ViewsDelegate::GetInstance()->GetDialogRelatedButtonHorizontalSpacing(); | |
| 153 gfx::Size size( | 124 gfx::Size size( |
| 154 (ok_button_ ? ok_button_->GetPreferredSize().width() : 0) + | 125 (ok_button_ ? ok_button_->GetPreferredSize().width() : 0) + |
|
sky
2017/02/22 17:29:48
IMO this code would be more readable if it was a b
tapted
2017/02/22 23:20:43
I'm just going to delete it all in https://coderev
| |
| 155 (cancel_button_ ? cancel_button_->GetPreferredSize().width() : 0) + | 126 (cancel_button_ ? cancel_button_->GetPreferredSize().width() : 0) + |
| 156 (cancel_button_ && ok_button_ | 127 (cancel_button_ && ok_button_ |
| 157 ? ViewsDelegate::GetInstance() | 128 ? ViewsDelegate::GetInstance() |
| 158 ->GetDialogRelatedButtonHorizontalSpacing() | 129 ->GetDialogRelatedButtonHorizontalSpacing() |
| 159 : 0) + | 130 : 0) + |
| 160 (ShouldShow(extra_view_) ? extra_view_->GetPreferredSize().width() | 131 (ShouldShow(extra_view_) ? extra_view_->GetPreferredSize().width() |
| 161 : 0) + | 132 : 0) + |
| 162 (ShouldShow(extra_view_) && has_dialog_buttons() ? extra_view_padding | 133 GetExtraViewSpacing(), |
| 163 : 0), | |
| 164 0); | 134 0); |
| 165 | 135 |
| 166 int buttons_height = GetButtonsAndExtraViewRowHeight(); | 136 int buttons_height = GetButtonsAndExtraViewRowHeight(); |
| 167 if (buttons_height != 0) { | 137 if (buttons_height != 0) { |
| 168 size.Enlarge(0, buttons_height); | 138 size.Enlarge(0, buttons_height); |
| 169 // Inset the buttons and extra view. | 139 // Inset the buttons and extra view. |
| 170 const gfx::Insets insets = GetButtonRowInsets(); | 140 const gfx::Insets insets = GetButtonRowInsets(); |
| 171 size.Enlarge(insets.width(), insets.height()); | 141 size.Enlarge(insets.width(), insets.height()); |
| 172 } | 142 } |
| 173 | 143 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 | 208 |
| 239 bool DialogClientView::AcceleratorPressed(const ui::Accelerator& accelerator) { | 209 bool DialogClientView::AcceleratorPressed(const ui::Accelerator& accelerator) { |
| 240 DCHECK_EQ(accelerator.key_code(), ui::VKEY_ESCAPE); | 210 DCHECK_EQ(accelerator.key_code(), ui::VKEY_ESCAPE); |
| 241 | 211 |
| 242 GetWidget()->Close(); | 212 GetWidget()->Close(); |
| 243 return true; | 213 return true; |
| 244 } | 214 } |
| 245 | 215 |
| 246 void DialogClientView::ViewHierarchyChanged( | 216 void DialogClientView::ViewHierarchyChanged( |
| 247 const ViewHierarchyChangedDetails& details) { | 217 const ViewHierarchyChangedDetails& details) { |
| 218 View* const child = details.child; | |
| 219 | |
| 220 // Dialogs must add children to contents_view(), not client_view(). | |
| 221 if (details.is_add && details.parent == this) { | |
| 222 DCHECK(child == contents_view() || child == ok_button_ || | |
| 223 child == cancel_button_ || child == extra_view_); | |
| 224 } | |
| 225 | |
| 248 ClientView::ViewHierarchyChanged(details); | 226 ClientView::ViewHierarchyChanged(details); |
| 249 if (details.is_add && details.child == this) { | 227 if (details.is_add && child == this) { |
| 250 UpdateDialogButtons(); | 228 UpdateDialogButtons(); |
| 251 CreateExtraView(); | 229 } else if (!details.is_add && child != this) { |
|
sky
2017/02/22 17:29:48
child != this isn't really needed here.
tapted
2017/02/22 23:20:44
Removed (follow-up doesn't need it either)
| |
| 252 } else if (!details.is_add && details.child != this) { | 230 if (child == ok_button_) |
| 253 if (details.child == ok_button_) | |
| 254 ok_button_ = nullptr; | 231 ok_button_ = nullptr; |
| 255 else if (details.child == cancel_button_) | 232 else if (child == cancel_button_) |
| 256 cancel_button_ = nullptr; | 233 cancel_button_ = nullptr; |
| 257 else if (details.child == extra_view_) | 234 else if (child == extra_view_) |
| 258 extra_view_ = nullptr; | 235 extra_view_ = nullptr; |
| 259 } | 236 } |
| 260 } | 237 } |
| 261 | 238 |
| 262 void DialogClientView::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 239 void DialogClientView::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
| 263 // The old dialog style needs an explicit background color, while the new | 240 // The old dialog style needs an explicit background color, while the new |
| 264 // dialog style simply inherits the bubble's frame view color. | 241 // dialog style simply inherits the bubble's frame view color. |
| 265 const DialogDelegate* dialog = GetDialogDelegate(); | 242 const DialogDelegate* dialog = GetDialogDelegate(); |
| 266 | 243 |
| 267 if (dialog && !dialog->ShouldUseCustomFrame()) { | 244 if (dialog && !dialog->ShouldUseCustomFrame()) { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 280 | 257 |
| 281 if (sender == ok_button_) | 258 if (sender == ok_button_) |
| 282 AcceptWindow(); | 259 AcceptWindow(); |
| 283 else if (sender == cancel_button_) | 260 else if (sender == cancel_button_) |
| 284 CancelWindow(); | 261 CancelWindow(); |
| 285 else | 262 else |
| 286 NOTREACHED(); | 263 NOTREACHED(); |
| 287 } | 264 } |
| 288 | 265 |
| 289 //////////////////////////////////////////////////////////////////////////////// | 266 //////////////////////////////////////////////////////////////////////////////// |
| 290 // DialogClientView, protected: | 267 // DialogClientView, private: |
| 291 | |
| 292 DialogClientView::DialogClientView(View* contents_view) | |
| 293 : ClientView(nullptr, contents_view), | |
| 294 ok_button_(nullptr), | |
| 295 cancel_button_(nullptr), | |
| 296 extra_view_(nullptr), | |
| 297 delegate_allowed_close_(false) {} | |
| 298 | 268 |
| 299 DialogDelegate* DialogClientView::GetDialogDelegate() const { | 269 DialogDelegate* DialogClientView::GetDialogDelegate() const { |
| 300 return GetWidget()->widget_delegate()->AsDialogDelegate(); | 270 return GetWidget()->widget_delegate()->AsDialogDelegate(); |
| 301 } | 271 } |
| 302 | 272 |
| 303 void DialogClientView::CreateExtraView() { | |
| 304 if (extra_view_) | |
| 305 return; | |
| 306 | |
| 307 extra_view_ = GetDialogDelegate()->CreateExtraView(); | |
| 308 if (extra_view_) { | |
| 309 extra_view_->SetGroup(kButtonGroup); | |
| 310 AddChildView(extra_view_); | |
| 311 SetupFocusChain(); | |
| 312 } | |
| 313 } | |
| 314 | |
| 315 void DialogClientView::ChildPreferredSizeChanged(View* child) { | 273 void DialogClientView::ChildPreferredSizeChanged(View* child) { |
| 316 if (child == extra_view_) | 274 if (child == extra_view_) |
| 317 Layout(); | 275 Layout(); |
| 318 } | 276 } |
| 319 | 277 |
| 320 void DialogClientView::ChildVisibilityChanged(View* child) { | 278 void DialogClientView::ChildVisibilityChanged(View* child) { |
| 321 ChildPreferredSizeChanged(child); | 279 ChildPreferredSizeChanged(child); |
| 322 } | 280 } |
| 323 | 281 |
| 324 //////////////////////////////////////////////////////////////////////////////// | |
| 325 // DialogClientView, private: | |
| 326 | |
| 327 LabelButton* DialogClientView::CreateDialogButton(ui::DialogButton type) { | 282 LabelButton* DialogClientView::CreateDialogButton(ui::DialogButton type) { |
| 328 const base::string16 title = GetDialogDelegate()->GetDialogButtonLabel(type); | 283 const base::string16 title = GetDialogDelegate()->GetDialogButtonLabel(type); |
| 329 LabelButton* button = nullptr; | 284 LabelButton* button = nullptr; |
| 330 | 285 |
| 331 const bool is_default = | 286 const bool is_default = |
| 332 GetDialogDelegate()->GetDefaultDialogButton() == type && | 287 GetDialogDelegate()->GetDefaultDialogButton() == type && |
| 333 (type != ui::DIALOG_BUTTON_CANCEL || | 288 (type != ui::DIALOG_BUTTON_CANCEL || |
| 334 PlatformStyle::kDialogDefaultButtonCanBeCancel); | 289 PlatformStyle::kDialogDefaultButtonCanBeCancel); |
| 335 | 290 |
| 336 // The default button is always blue in Harmony. | 291 // The default button is always blue in Harmony. |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 399 child_views.erase( | 354 child_views.erase( |
| 400 std::remove(child_views.begin(), child_views.end(), nullptr), | 355 std::remove(child_views.begin(), child_views.end(), nullptr), |
| 401 child_views.end()); | 356 child_views.end()); |
| 402 | 357 |
| 403 // Setup focus by reordering views. It is not safe to use SetNextFocusableView | 358 // Setup focus by reordering views. It is not safe to use SetNextFocusableView |
| 404 // since child views may be added externally to this view. | 359 // since child views may be added externally to this view. |
| 405 for (size_t i = 0; i < child_views.size(); i++) | 360 for (size_t i = 0; i < child_views.size(); i++) |
| 406 ReorderChildView(child_views[i], i); | 361 ReorderChildView(child_views[i], i); |
| 407 } | 362 } |
| 408 | 363 |
| 364 int DialogClientView::GetExtraViewSpacing() const { | |
| 365 if (!ShouldShow(extra_view_) || !has_dialog_buttons()) | |
| 366 return 0; | |
| 367 | |
| 368 int extra_view_padding = 0; | |
| 369 if (GetDialogDelegate()->GetExtraViewPadding(&extra_view_padding)) | |
| 370 return extra_view_padding; | |
| 371 | |
| 372 return ViewsDelegate::GetInstance() | |
| 373 ->GetDialogRelatedButtonHorizontalSpacing(); | |
| 374 } | |
| 375 | |
| 376 void DialogClientView::SetupViews() { | |
| 377 const int buttons = GetDialogDelegate()->GetDialogButtons(); | |
| 378 | |
| 379 if (buttons & ui::DIALOG_BUTTON_OK) { | |
| 380 if (!ok_button_) { | |
| 381 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK); | |
| 382 AddChildView(ok_button_); | |
| 383 } | |
| 384 | |
| 385 GetDialogDelegate()->UpdateButton(ok_button_, ui::DIALOG_BUTTON_OK); | |
| 386 } else if (ok_button_) { | |
| 387 delete ok_button_; | |
| 388 ok_button_ = nullptr; | |
| 389 } | |
| 390 | |
| 391 if (buttons & ui::DIALOG_BUTTON_CANCEL) { | |
| 392 if (!cancel_button_) { | |
| 393 cancel_button_ = CreateDialogButton(ui::DIALOG_BUTTON_CANCEL); | |
| 394 AddChildView(cancel_button_); | |
| 395 } | |
| 396 | |
| 397 GetDialogDelegate()->UpdateButton(cancel_button_, ui::DIALOG_BUTTON_CANCEL); | |
| 398 } else if (cancel_button_) { | |
| 399 delete cancel_button_; | |
| 400 cancel_button_ = nullptr; | |
| 401 } | |
| 402 | |
| 403 if (extra_view_) | |
| 404 return; | |
| 405 | |
| 406 extra_view_ = GetDialogDelegate()->CreateExtraView(); | |
| 407 if (extra_view_) { | |
| 408 extra_view_->SetGroup(kButtonGroup); | |
| 409 AddChildView(extra_view_); | |
| 410 } | |
| 411 } | |
| 412 | |
| 409 } // namespace views | 413 } // namespace views |
| OLD | NEW |