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

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

Issue 2807653002: Ensure default dialog button focus remains after a dialog update. (Closed)
Patch Set: Showing widget for only the focus tests. Created 3 years, 7 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/checkbox.h" 15 #include "ui/views/controls/button/checkbox.h"
16 #include "ui/views/controls/button/custom_button.h" 16 #include "ui/views/controls/button/custom_button.h"
17 #include "ui/views/controls/button/label_button.h" 17 #include "ui/views/controls/button/label_button.h"
18 #include "ui/views/controls/button/md_text_button.h" 18 #include "ui/views/controls/button/md_text_button.h"
19 #include "ui/views/layout/grid_layout.h" 19 #include "ui/views/layout/grid_layout.h"
20 #include "ui/views/layout/layout_provider.h" 20 #include "ui/views/layout/layout_provider.h"
21 #include "ui/views/style/platform_style.h" 21 #include "ui/views/style/platform_style.h"
22 #include "ui/views/view_observer.h"
22 #include "ui/views/widget/widget.h" 23 #include "ui/views/widget/widget.h"
23 #include "ui/views/window/dialog_delegate.h" 24 #include "ui/views/window/dialog_delegate.h"
24 25
25 namespace views { 26 namespace views {
26 27
27 namespace { 28 namespace {
28 29
29 // The group used by the buttons. This name is chosen voluntarily big not to 30 // The group used by the buttons. This name is chosen voluntarily big not to
30 // conflict with other groups that could be in the dialog content. 31 // conflict with other groups that could be in the dialog content.
31 const int kButtonGroup = 6666; 32 const int kButtonGroup = 6666;
(...skipping 11 matching lines...) Expand all
43 } 44 }
44 45
45 // Returns the bounding box required to contain |size1| and |size2|, placed one 46 // Returns the bounding box required to contain |size1| and |size2|, placed one
46 // atop the other. 47 // atop the other.
47 gfx::Size GetBoundingSizeForVerticalStack(const gfx::Size& size1, 48 gfx::Size GetBoundingSizeForVerticalStack(const gfx::Size& size1,
48 const gfx::Size& size2) { 49 const gfx::Size& size2) {
49 return gfx::Size(std::max(size1.width(), size2.width()), 50 return gfx::Size(std::max(size1.width(), size2.width()),
50 size1.height() + size2.height()); 51 size1.height() + size2.height());
51 } 52 }
52 53
54 // ViewDeletionObserver implements an observer to track the deletion of the
55 // view in focus.
56 class ViewDeletionObserver : public ViewObserver {
57 public:
58 explicit ViewDeletionObserver(View* observed_view)
59 : observed_view_(observed_view) {
60 if (observed_view_)
61 observed_view_->AddObserver(this);
62 }
63
64 ~ViewDeletionObserver() override {
65 if (observed_view_)
66 observed_view_->RemoveObserver(this);
67 }
68
69 // ViewObserver:
70 void OnViewIsDeleting(View* observed_view) override {
71 DCHECK_EQ(observed_view, observed_view_);
72 observed_view_ = nullptr;
73 observed_view_->RemoveObserver(this);
74 }
75
76 View* observed_view() { return observed_view_; }
77
78 private:
79 View* observed_view_ = nullptr;
80
81 DISALLOW_COPY_AND_ASSIGN(ViewDeletionObserver);
82 };
83
53 } // namespace 84 } // namespace
54 85
55 // Simple container to bubble child view changes up the view hierarchy. 86 // Simple container to bubble child view changes up the view hierarchy.
56 class DialogClientView::ButtonRowContainer : public View { 87 class DialogClientView::ButtonRowContainer : public View {
57 public: 88 public:
58 explicit ButtonRowContainer(DialogClientView* owner) : owner_(owner) {} 89 explicit ButtonRowContainer(DialogClientView* owner) : owner_(owner) {}
59 90
60 // View: 91 // View:
61 void ChildPreferredSizeChanged(View* child) override { 92 void ChildPreferredSizeChanged(View* child) override {
62 owner_->ChildPreferredSizeChanged(child); 93 owner_->ChildPreferredSizeChanged(child);
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 View* third = ok_button_; 350 View* third = ok_button_;
320 if (kIsOkButtonOnLeftSide) 351 if (kIsOkButtonOnLeftSide)
321 std::swap(second, third); 352 std::swap(second, third);
322 return {{first, second, third}}; 353 return {{first, second, third}};
323 } 354 }
324 355
325 void DialogClientView::SetupLayout() { 356 void DialogClientView::SetupLayout() {
326 base::AutoReset<bool> auto_reset(&adding_or_removing_views_, true); 357 base::AutoReset<bool> auto_reset(&adding_or_removing_views_, true);
327 GridLayout* layout = new GridLayout(button_row_container_); 358 GridLayout* layout = new GridLayout(button_row_container_);
328 layout->set_minimum_size(minimum_size_); 359 layout->set_minimum_size(minimum_size_);
360 FocusManager* focus_manager = GetFocusManager();
361 ViewDeletionObserver deletion_observer(focus_manager->GetFocusedView());
329 362
330 // Clobber any existing LayoutManager since it has weak references to child 363 // Clobber any existing LayoutManager since it has weak references to child
331 // Views which may be removed by SetupViews(). 364 // Views which may be removed by SetupViews().
332 button_row_container_->SetLayoutManager(layout); 365 button_row_container_->SetLayoutManager(layout);
333 SetupViews(); 366 SetupViews();
334 const std::array<View*, kNumButtons> views = GetButtonRowViews(); 367 const std::array<View*, kNumButtons> views = GetButtonRowViews();
335 368
336 // Visibility changes on |extra_view_| must be observed to re-Layout. However, 369 // Visibility changes on |extra_view_| must be observed to re-Layout. However,
337 // when hidden it's not included in the button row (it can't influence layout) 370 // when hidden it's not included in the button row (it can't influence layout)
338 // and it can't be added to |button_row_container_| (GridLayout complains). 371 // and it can't be added to |button_row_container_| (GridLayout complains).
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 // Checkbox (which extends LabelButton). Otherwise, link everything. 439 // Checkbox (which extends LabelButton). Otherwise, link everything.
407 bool skip_first_link = 440 bool skip_first_link =
408 views[0] && (!CustomButton::AsCustomButton(views[0]) || 441 views[0] && (!CustomButton::AsCustomButton(views[0]) ||
409 views[0]->GetClassName() == Checkbox::kViewClassName); 442 views[0]->GetClassName() == Checkbox::kViewClassName);
410 if (skip_first_link) 443 if (skip_first_link)
411 column_set->LinkColumnSizes(link[1], link[2], -1); 444 column_set->LinkColumnSizes(link[1], link[2], -1);
412 else 445 else
413 column_set->LinkColumnSizes(link[0], link[1], link[2], -1); 446 column_set->LinkColumnSizes(link[0], link[1], link[2], -1);
414 447
415 layout->AddPaddingRow(kFixed, insets.bottom()); 448 layout->AddPaddingRow(kFixed, insets.bottom());
449
450 // The default focus is lost when child views are added back into the dialog.
451 // This restores focus if the button is still available.
452 View* previously_focused_view = deletion_observer.observed_view();
453 if (previously_focused_view && !focus_manager->GetFocusedView() &&
454 Contains(previously_focused_view)) {
455 previously_focused_view->RequestFocus();
456 }
416 } 457 }
417 458
418 void DialogClientView::SetupViews() { 459 void DialogClientView::SetupViews() {
419 button_row_container_->RemoveAllChildViews(false /* delete children */); 460 button_row_container_->RemoveAllChildViews(false /* delete children */);
420 // If SetupLayout() "stored" a hidden |extra_view_| in |this|, ensure it can 461 // If SetupLayout() "stored" a hidden |extra_view_| in |this|, ensure it can
421 // be re-added to the layout when becoming visible. 462 // be re-added to the layout when becoming visible.
422 if (extra_view_) 463 if (extra_view_)
423 RemoveChildView(extra_view_); 464 RemoveChildView(extra_view_);
424 465
425 UpdateDialogButton(&ok_button_, ui::DIALOG_BUTTON_OK); 466 UpdateDialogButton(&ok_button_, ui::DIALOG_BUTTON_OK);
426 UpdateDialogButton(&cancel_button_, ui::DIALOG_BUTTON_CANCEL); 467 UpdateDialogButton(&cancel_button_, ui::DIALOG_BUTTON_CANCEL);
427 468
428 if (extra_view_) 469 if (extra_view_)
429 return; 470 return;
430 471
431 extra_view_ = GetDialogDelegate()->CreateExtraView(); 472 extra_view_ = GetDialogDelegate()->CreateExtraView();
432 if (extra_view_) 473 if (extra_view_)
433 extra_view_->SetGroup(kButtonGroup); 474 extra_view_->SetGroup(kButtonGroup);
434 } 475 }
435 476
436 } // namespace views 477 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698