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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: ui/views/window/dialog_client_view.cc
diff --git a/ui/views/window/dialog_client_view.cc b/ui/views/window/dialog_client_view.cc
index 094d098e299b5c7a4d9569c5fbad0d282d2ef40b..db3dc2f5e37ec4cd7739c3c244737ab2d0eda775 100644
--- a/ui/views/window/dialog_client_view.cc
+++ b/ui/views/window/dialog_client_view.cc
@@ -19,6 +19,7 @@
#include "ui/views/layout/grid_layout.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/style/platform_style.h"
+#include "ui/views/view_observer.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/dialog_delegate.h"
@@ -50,6 +51,36 @@ gfx::Size GetBoundingSizeForVerticalStack(const gfx::Size& size1,
size1.height() + size2.height());
}
+// ViewDeletionObserver implements an observer to track the deletion of the
+// view in focus.
+class ViewDeletionObserver : public ViewObserver {
+ public:
+ explicit ViewDeletionObserver(View* observed_view)
+ : observed_view_(observed_view) {
+ if (observed_view_)
+ observed_view_->AddObserver(this);
+ }
+
+ ~ViewDeletionObserver() override {
+ if (observed_view_)
+ observed_view_->RemoveObserver(this);
+ }
+
+ // ViewObserver:
+ void OnViewIsDeleting(View* observed_view) override {
+ DCHECK_EQ(observed_view, observed_view_);
+ observed_view_ = nullptr;
+ observed_view_->RemoveObserver(this);
+ }
+
+ View* observed_view() { return observed_view_; }
+
+ private:
+ View* observed_view_ = nullptr;
+
+ DISALLOW_COPY_AND_ASSIGN(ViewDeletionObserver);
+};
+
} // namespace
// Simple container to bubble child view changes up the view hierarchy.
@@ -326,6 +357,8 @@ void DialogClientView::SetupLayout() {
base::AutoReset<bool> auto_reset(&adding_or_removing_views_, true);
GridLayout* layout = new GridLayout(button_row_container_);
layout->set_minimum_size(minimum_size_);
+ FocusManager* focus_manager = GetFocusManager();
+ ViewDeletionObserver deletion_observer(focus_manager->GetFocusedView());
// Clobber any existing LayoutManager since it has weak references to child
// Views which may be removed by SetupViews().
@@ -413,6 +446,14 @@ void DialogClientView::SetupLayout() {
column_set->LinkColumnSizes(link[0], link[1], link[2], -1);
layout->AddPaddingRow(kFixed, insets.bottom());
+
+ // The default focus is lost when child views are added back into the dialog.
+ // This restores focus if the button is still available.
+ View* previously_focused_view = deletion_observer.observed_view();
+ if (previously_focused_view && !focus_manager->GetFocusedView() &&
+ Contains(previously_focused_view)) {
+ previously_focused_view->RequestFocus();
+ }
}
void DialogClientView::SetupViews() {

Powered by Google App Engine
This is Rietveld 408576698