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

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: Addressing comments. 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/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/grid_layout.h" 18 #include "ui/views/layout/grid_layout.h"
19 #include "ui/views/style/platform_style.h" 19 #include "ui/views/style/platform_style.h"
20 #include "ui/views/view_observer.h"
20 #include "ui/views/views_delegate.h" 21 #include "ui/views/views_delegate.h"
21 #include "ui/views/widget/widget.h" 22 #include "ui/views/widget/widget.h"
22 #include "ui/views/window/dialog_delegate.h" 23 #include "ui/views/window/dialog_delegate.h"
23 24
24 namespace views { 25 namespace views {
25 26
26 namespace { 27 namespace {
27 28
28 // The group used by the buttons. This name is chosen voluntarily big not to 29 // The group used by the buttons. This name is chosen voluntarily big not to
29 // conflict with other groups that could be in the dialog content. 30 // conflict with other groups that could be in the dialog content.
(...skipping 12 matching lines...) Expand all
42 } 43 }
43 44
44 // Returns the bounding box required to contain |size1| and |size2|, placed one 45 // Returns the bounding box required to contain |size1| and |size2|, placed one
45 // atop the other. 46 // atop the other.
46 gfx::Size GetBoundingSizeForVerticalStack(const gfx::Size& size1, 47 gfx::Size GetBoundingSizeForVerticalStack(const gfx::Size& size1,
47 const gfx::Size& size2) { 48 const gfx::Size& size2) {
48 return gfx::Size(std::max(size1.width(), size2.width()), 49 return gfx::Size(std::max(size1.width(), size2.width()),
49 size1.height() + size2.height()); 50 size1.height() + size2.height());
50 } 51 }
51 52
53 // ViewDeletionObserver implements an observer to track the deletion of the
54 // view in focus.
55 class ViewDeletionObserver : public ViewObserver {
56 public:
57 explicit ViewDeletionObserver(View* observed_view)
58 : observed_view_(observed_view) {
59 if (observed_view_)
60 observed_view_->AddObserver(this);
61 }
62
63 ~ViewDeletionObserver() override {
64 if (observed_view_)
65 observed_view_->RemoveObserver(this);
66 }
67
68 // ViewObserver:
69 void OnViewIsDeleting(View* observed_view) override {
70 DCHECK_EQ(observed_view, observed_view_);
71 observed_view_ = nullptr;
72 observed_view_->RemoveObserver(this);
73 }
74
75 View* focused_view() { return observed_view_; }
sky 2017/05/18 16:05:29 Style guide says accessors like this should match
Ackerman 2017/05/18 18:24:17 Done.
76
77 private:
78 View* observed_view_ = nullptr;
79
80 DISALLOW_COPY_AND_ASSIGN(ViewDeletionObserver);
81 };
82
52 } // namespace 83 } // namespace
53 84
54 // Simple container to bubble child view changes up the view hierarchy. 85 // Simple container to bubble child view changes up the view hierarchy.
55 class DialogClientView::ButtonRowContainer : public View { 86 class DialogClientView::ButtonRowContainer : public View {
56 public: 87 public:
57 explicit ButtonRowContainer(DialogClientView* owner) : owner_(owner) {} 88 explicit ButtonRowContainer(DialogClientView* owner) : owner_(owner) {}
58 89
59 // View: 90 // View:
60 void ChildPreferredSizeChanged(View* child) override { 91 void ChildPreferredSizeChanged(View* child) override {
61 owner_->ChildPreferredSizeChanged(child); 92 owner_->ChildPreferredSizeChanged(child);
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 View* third = ok_button_; 343 View* third = ok_button_;
313 if (kIsOkButtonOnLeftSide) 344 if (kIsOkButtonOnLeftSide)
314 std::swap(second, third); 345 std::swap(second, third);
315 return {{first, second, third}}; 346 return {{first, second, third}};
316 } 347 }
317 348
318 void DialogClientView::SetupLayout() { 349 void DialogClientView::SetupLayout() {
319 base::AutoReset<bool> auto_reset(&adding_or_removing_views_, true); 350 base::AutoReset<bool> auto_reset(&adding_or_removing_views_, true);
320 GridLayout* layout = new GridLayout(button_row_container_); 351 GridLayout* layout = new GridLayout(button_row_container_);
321 layout->set_minimum_size(minimum_size_); 352 layout->set_minimum_size(minimum_size_);
353 FocusManager* focus_manager = GetFocusManager();
354 ViewDeletionObserver deletion_observer(focus_manager->GetFocusedView());
322 355
323 // Clobber any existing LayoutManager since it has weak references to child 356 // Clobber any existing LayoutManager since it has weak references to child
324 // Views which may be removed by SetupViews(). 357 // Views which may be removed by SetupViews().
325 button_row_container_->SetLayoutManager(layout); 358 button_row_container_->SetLayoutManager(layout);
326 SetupViews(); 359 SetupViews();
327 const std::array<View*, kNumButtons> views = GetButtonRowViews(); 360 const std::array<View*, kNumButtons> views = GetButtonRowViews();
328 361
329 // Visibility changes on |extra_view_| must be observed to re-Layout. However, 362 // Visibility changes on |extra_view_| must be observed to re-Layout. However,
330 // when hidden it's not included in the button row (it can't influence layout) 363 // when hidden it's not included in the button row (it can't influence layout)
331 // and it can't be added to |button_row_container_| (GridLayout complains). 364 // and it can't be added to |button_row_container_| (GridLayout complains).
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 } 425 }
393 426
394 if (ui::MaterialDesignController::IsSecondaryUiMaterial()) { 427 if (ui::MaterialDesignController::IsSecondaryUiMaterial()) {
395 // Only link the extra view column if it is a button. 428 // Only link the extra view column if it is a button.
396 if (views[0] && !CustomButton::AsCustomButton(views[0])) 429 if (views[0] && !CustomButton::AsCustomButton(views[0]))
397 column_set->LinkColumnSizes(link[1], link[2], -1); 430 column_set->LinkColumnSizes(link[1], link[2], -1);
398 else 431 else
399 column_set->LinkColumnSizes(link[0], link[1], link[2], -1); 432 column_set->LinkColumnSizes(link[0], link[1], link[2], -1);
400 } 433 }
401 layout->AddPaddingRow(kFixed, insets.bottom()); 434 layout->AddPaddingRow(kFixed, insets.bottom());
435
436 // The default focus is lost when child views are added back into the dialog.
437 // This restores focus if the button is still available.
438 View* previously_focused_view = deletion_observer.focused_view();
439 if (previously_focused_view && !focus_manager->GetFocusedView() &&
440 Contains(previously_focused_view))
sky 2017/05/18 16:05:29 As the conditional spans multiple lines use {}
Ackerman 2017/05/18 18:24:17 Done.
441 previously_focused_view->RequestFocus();
402 } 442 }
403 443
404 void DialogClientView::SetupViews() { 444 void DialogClientView::SetupViews() {
405 button_row_container_->RemoveAllChildViews(false /* delete children */); 445 button_row_container_->RemoveAllChildViews(false /* delete children */);
406 // If SetupLayout() "stored" a hidden |extra_view_| in |this|, ensure it can 446 // If SetupLayout() "stored" a hidden |extra_view_| in |this|, ensure it can
407 // be re-added to the layout when becoming visible. 447 // be re-added to the layout when becoming visible.
408 if (extra_view_) 448 if (extra_view_)
409 RemoveChildView(extra_view_); 449 RemoveChildView(extra_view_);
410 450
411 UpdateDialogButton(&ok_button_, ui::DIALOG_BUTTON_OK); 451 UpdateDialogButton(&ok_button_, ui::DIALOG_BUTTON_OK);
412 UpdateDialogButton(&cancel_button_, ui::DIALOG_BUTTON_CANCEL); 452 UpdateDialogButton(&cancel_button_, ui::DIALOG_BUTTON_CANCEL);
413 453
414 if (extra_view_) 454 if (extra_view_)
415 return; 455 return;
416 456
417 extra_view_ = GetDialogDelegate()->CreateExtraView(); 457 extra_view_ = GetDialogDelegate()->CreateExtraView();
418 if (extra_view_) 458 if (extra_view_)
419 extra_view_->SetGroup(kButtonGroup); 459 extra_view_->SetGroup(kButtonGroup);
420 } 460 }
421 461
422 } // namespace views 462 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698