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/events/keycodes/keyboard_codes.h" | 10 #include "ui/events/keycodes/keyboard_codes.h" |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 GetWidget()->Close(); | 85 GetWidget()->Close(); |
| 86 } | 86 } |
| 87 } | 87 } |
| 88 | 88 |
| 89 void DialogClientView::UpdateDialogButtons() { | 89 void DialogClientView::UpdateDialogButtons() { |
| 90 const int buttons = GetDialogDelegate()->GetDialogButtons(); | 90 const int buttons = GetDialogDelegate()->GetDialogButtons(); |
| 91 | 91 |
| 92 if (buttons & ui::DIALOG_BUTTON_OK) { | 92 if (buttons & ui::DIALOG_BUTTON_OK) { |
| 93 if (!ok_button_) { | 93 if (!ok_button_) { |
| 94 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK); | 94 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK); |
| 95 AddChildView(ok_button_); | 95 AddOKButton(ok_button_); |
| 96 } | 96 } |
| 97 | 97 |
| 98 UpdateButton(ok_button_, ui::DIALOG_BUTTON_OK); | 98 UpdateButton(ok_button_, ui::DIALOG_BUTTON_OK); |
| 99 } else if (ok_button_) { | 99 } else if (ok_button_) { |
| 100 delete ok_button_; | 100 delete ok_button_; |
| 101 ok_button_ = NULL; | 101 ok_button_ = NULL; |
| 102 } | 102 } |
| 103 | 103 |
| 104 if (buttons & ui::DIALOG_BUTTON_CANCEL) { | 104 if (buttons & ui::DIALOG_BUTTON_CANCEL) { |
| 105 if (!cancel_button_) { | 105 if (!cancel_button_) { |
| 106 cancel_button_ = CreateDialogButton(ui::DIALOG_BUTTON_CANCEL); | 106 cancel_button_ = CreateDialogButton(ui::DIALOG_BUTTON_CANCEL); |
| 107 AddChildView(cancel_button_); | 107 AddCancelButton(cancel_button_); |
| 108 } | 108 } |
| 109 | 109 |
| 110 UpdateButton(cancel_button_, ui::DIALOG_BUTTON_CANCEL); | 110 UpdateButton(cancel_button_, ui::DIALOG_BUTTON_CANCEL); |
| 111 } else if (cancel_button_) { | 111 } else if (cancel_button_) { |
| 112 delete cancel_button_; | 112 delete cancel_button_; |
| 113 cancel_button_ = NULL; | 113 cancel_button_ = NULL; |
| 114 } | 114 } |
| 115 } | 115 } |
| 116 | 116 |
| 117 /////////////////////////////////////////////////////////////////////////////// | 117 /////////////////////////////////////////////////////////////////////////////// |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 226 UpdateDialogButtons(); | 226 UpdateDialogButtons(); |
| 227 CreateExtraView(); | 227 CreateExtraView(); |
| 228 } else if (!details.is_add && details.child != this) { | 228 } else if (!details.is_add && details.child != this) { |
| 229 if (details.child == ok_button_) | 229 if (details.child == ok_button_) |
| 230 ok_button_ = nullptr; | 230 ok_button_ = nullptr; |
| 231 else if (details.child == cancel_button_) | 231 else if (details.child == cancel_button_) |
| 232 cancel_button_ = nullptr; | 232 cancel_button_ = nullptr; |
| 233 else if (details.child == extra_view_) | 233 else if (details.child == extra_view_) |
| 234 extra_view_ = nullptr; | 234 extra_view_ = nullptr; |
| 235 } | 235 } |
| 236 | |
| 237 SetupFocusChain(); | |
| 238 } | 236 } |
| 239 | 237 |
| 240 void DialogClientView::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 238 void DialogClientView::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
| 241 // The old dialog style needs an explicit background color, while the new | 239 // The old dialog style needs an explicit background color, while the new |
| 242 // dialog style simply inherits the bubble's frame view color. | 240 // dialog style simply inherits the bubble's frame view color. |
| 243 const DialogDelegate* dialog = GetDialogDelegate(); | 241 const DialogDelegate* dialog = GetDialogDelegate(); |
| 244 | 242 |
| 245 if (dialog && !dialog->UseNewStyleForThisDialog()) { | 243 if (dialog && !dialog->UseNewStyleForThisDialog()) { |
| 246 set_background(views::Background::CreateSolidBackground(GetNativeTheme()-> | 244 set_background(views::Background::CreateSolidBackground(GetNativeTheme()-> |
| 247 GetSystemColor(ui::NativeTheme::kColorId_DialogBackground))); | 245 GetSystemColor(ui::NativeTheme::kColorId_DialogBackground))); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 278 return GetWidget()->widget_delegate()->AsDialogDelegate(); | 276 return GetWidget()->widget_delegate()->AsDialogDelegate(); |
| 279 } | 277 } |
| 280 | 278 |
| 281 void DialogClientView::CreateExtraView() { | 279 void DialogClientView::CreateExtraView() { |
| 282 if (extra_view_) | 280 if (extra_view_) |
| 283 return; | 281 return; |
| 284 | 282 |
| 285 extra_view_ = GetDialogDelegate()->CreateExtraView(); | 283 extra_view_ = GetDialogDelegate()->CreateExtraView(); |
| 286 if (extra_view_) { | 284 if (extra_view_) { |
| 287 extra_view_->SetGroup(kButtonGroup); | 285 extra_view_->SetGroup(kButtonGroup); |
| 288 AddChildView(extra_view_); | 286 // Add |extra_view_| after contents_view, if it exists in the view hierarchy |
| 287 // to ensure the correct focus order. | |
| 288 int content_view_index = GetIndexOf(contents_view()); | |
| 289 // The content_view should be the first child, if it exists in the view | |
| 290 // hierarchy. | |
| 291 DCHECK(content_view_index == -1 || content_view_index == 0); | |
| 292 AddChildViewAt(extra_view_, content_view_index + 1); | |
| 289 } | 293 } |
| 290 } | 294 } |
| 291 | 295 |
| 292 void DialogClientView::ChildPreferredSizeChanged(View* child) { | 296 void DialogClientView::ChildPreferredSizeChanged(View* child) { |
| 293 if (child == extra_view_) | 297 if (child == extra_view_) |
| 294 Layout(); | 298 Layout(); |
| 295 } | 299 } |
| 296 | 300 |
| 297 void DialogClientView::ChildVisibilityChanged(View* child) { | 301 void DialogClientView::ChildVisibilityChanged(View* child) { |
| 298 ChildPreferredSizeChanged(child); | 302 ChildPreferredSizeChanged(child); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 313 button->SetStyle(Button::STYLE_BUTTON); | 317 button->SetStyle(Button::STYLE_BUTTON); |
| 314 } | 318 } |
| 315 button->SetFocusable(true); | 319 button->SetFocusable(true); |
| 316 | 320 |
| 317 const int kDialogMinButtonWidth = 75; | 321 const int kDialogMinButtonWidth = 75; |
| 318 button->SetMinSize(gfx::Size(kDialogMinButtonWidth, 0)); | 322 button->SetMinSize(gfx::Size(kDialogMinButtonWidth, 0)); |
| 319 button->SetGroup(kButtonGroup); | 323 button->SetGroup(kButtonGroup); |
| 320 return button; | 324 return button; |
| 321 } | 325 } |
| 322 | 326 |
| 327 void DialogClientView::AddOKButton(LabelButton* ok_button) { | |
| 328 // Find the view which should precede |ok_button| in focus order. | |
| 329 View* previous_view = nullptr; | |
| 330 if (!kIsOkButtonOnLeftSide && cancel_button_) | |
| 331 previous_view = cancel_button_; | |
| 332 else if (extra_view_) | |
| 333 previous_view = extra_view_; | |
| 334 else | |
| 335 previous_view = contents_view(); | |
| 336 int previous_index = GetIndexOf(previous_view); | |
| 337 | |
| 338 // previous_index may be -1. This may happen when the contents_view() is null | |
| 339 // or hasn't been added to the view hierarchy. | |
| 340 AddChildViewAt(ok_button, previous_index + 1); | |
| 341 } | |
| 342 | |
| 343 void DialogClientView::AddCancelButton(LabelButton* cancel_button) { | |
| 344 // Find the view which should precede |cancel_button| in focus order. | |
| 345 View* previous_view = nullptr; | |
| 346 if (kIsOkButtonOnLeftSide && ok_button_) | |
| 347 previous_view = ok_button_; | |
| 348 else if (extra_view_) | |
| 349 previous_view = extra_view_; | |
| 350 else | |
| 351 previous_view = contents_view(); | |
| 352 int previous_index = GetIndexOf(previous_view); | |
| 353 | |
| 354 // previous_index may be -1. This may happen when the contents_view() is null | |
| 355 // or hasn't been added to the view hierarchy. | |
| 356 AddChildViewAt(cancel_button, previous_index + 1); | |
| 357 } | |
| 358 | |
| 323 void DialogClientView::UpdateButton(LabelButton* button, | 359 void DialogClientView::UpdateButton(LabelButton* button, |
| 324 ui::DialogButton type) { | 360 ui::DialogButton type) { |
| 325 DialogDelegate* dialog = GetDialogDelegate(); | 361 DialogDelegate* dialog = GetDialogDelegate(); |
| 326 button->SetText(dialog->GetDialogButtonLabel(type)); | 362 button->SetText(dialog->GetDialogButtonLabel(type)); |
| 327 button->SetEnabled(dialog->IsDialogButtonEnabled(type)); | 363 button->SetEnabled(dialog->IsDialogButtonEnabled(type)); |
| 328 button->SetIsDefault(type == dialog->GetDefaultDialogButton()); | 364 button->SetIsDefault(type == dialog->GetDefaultDialogButton()); |
| 329 } | 365 } |
| 330 | 366 |
| 331 int DialogClientView::GetButtonsAndExtraViewRowHeight() const { | 367 int DialogClientView::GetButtonsAndExtraViewRowHeight() const { |
| 332 int extra_view_height = ShouldShow(extra_view_) ? | 368 int extra_view_height = ShouldShow(extra_view_) ? |
| 333 extra_view_->GetPreferredSize().height() : 0; | 369 extra_view_->GetPreferredSize().height() : 0; |
| 334 int buttons_height = std::max( | 370 int buttons_height = std::max( |
| 335 ok_button_ ? ok_button_->GetPreferredSize().height() : 0, | 371 ok_button_ ? ok_button_->GetPreferredSize().height() : 0, |
| 336 cancel_button_ ? cancel_button_->GetPreferredSize().height() : 0); | 372 cancel_button_ ? cancel_button_->GetPreferredSize().height() : 0); |
| 337 return std::max(extra_view_height, buttons_height); | 373 return std::max(extra_view_height, buttons_height); |
| 338 } | 374 } |
| 339 | 375 |
| 340 gfx::Insets DialogClientView::GetButtonRowInsets() const { | 376 gfx::Insets DialogClientView::GetButtonRowInsets() const { |
| 341 return GetButtonsAndExtraViewRowHeight() == 0 ? gfx::Insets() | 377 return GetButtonsAndExtraViewRowHeight() == 0 ? gfx::Insets() |
| 342 : button_row_insets_; | 378 : button_row_insets_; |
| 343 } | 379 } |
| 344 | 380 |
| 345 void DialogClientView::SetupFocusChain() { | |
|
sky
2016/03/22 16:19:23
This code seems simpler to me. Is there a reason y
karandeepb
2016/04/06 23:06:53
extra_view_ is being currently added to child view
| |
| 346 // Create a vector of child views in the order of intended focus. | |
| 347 std::vector<View*> child_views; | |
| 348 child_views.push_back(contents_view()); | |
| 349 child_views.push_back(extra_view_); | |
| 350 if (kIsOkButtonOnLeftSide) { | |
| 351 child_views.push_back(ok_button_); | |
| 352 child_views.push_back(cancel_button_); | |
| 353 } else { | |
| 354 child_views.push_back(cancel_button_); | |
| 355 child_views.push_back(ok_button_); | |
| 356 } | |
| 357 | |
| 358 // Remove all null views from the vector. | |
| 359 child_views.erase( | |
| 360 std::remove(child_views.begin(), child_views.end(), nullptr), | |
| 361 child_views.end()); | |
| 362 | |
| 363 // Setup focus. | |
| 364 for (size_t i = 0; i < child_views.size(); i++) { | |
| 365 child_views[i]->SetNextFocusableView( | |
| 366 i + 1 != child_views.size() ? child_views[i + 1] : nullptr); | |
| 367 } | |
| 368 } | |
| 369 | |
| 370 } // namespace views | 381 } // namespace views |
| OLD | NEW |