From bda9556c63579835dd14055a048f4e2094e7b3f5 Mon Sep 17 00:00:00 2001 From: "georgey@chromium.org" Date: Thu, 7 Jan 2010 00:55:16 +0000 Subject: Addded notification when ancestor gets changed. So windows that lack focus manager, because of being created on inactive tab, could do the necessary work when focus manager is actually attached. This is relevant for Windows only, but some support functions (FindAllRootViews) could be useful for other architectures as well. BUG=22481 TEST=in the bug Review URL: http://codereview.chromium.org/492025 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@35675 0039d316-1c4b-4281-b951-d872f2087c98 --- views/window/dialog_client_view.cc | 54 ++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 11 deletions(-) (limited to 'views/window/dialog_client_view.cc') diff --git a/views/window/dialog_client_view.cc b/views/window/dialog_client_view.cc index 64c4677..b25c6bc 100644 --- a/views/window/dialog_client_view.cc +++ b/views/window/dialog_client_view.cc @@ -110,7 +110,9 @@ DialogClientView::DialogClientView(Window* owner, View* contents_view) cancel_button_(NULL), default_button_(NULL), extra_view_(NULL), - accepted_(false) { + accepted_(false), + listening_to_focus_(false), + saved_focus_manager_(NULL) { InitClass(); } @@ -236,6 +238,17 @@ void DialogClientView::CancelWindow() { } /////////////////////////////////////////////////////////////////////////////// +// DialogClientView, View overrides: + +void DialogClientView::NativeViewHierarchyChanged(bool attached, + gfx::NativeView native_view, + RootView* root_view) { + if (attached) { + UpdateFocusListener(); + } +} + +/////////////////////////////////////////////////////////////////////////////// // DialogClientView, ClientView overrides: bool DialogClientView::CanClose() const { @@ -251,10 +264,11 @@ bool DialogClientView::CanClose() const { } void DialogClientView::WindowClosing() { - FocusManager* focus_manager = GetFocusManager(); - DCHECK(focus_manager); - if (focus_manager) - focus_manager->RemoveFocusChangeListener(this); + if (listening_to_focus_) { + DCHECK(saved_focus_manager_); + if (saved_focus_manager_) + saved_focus_manager_->RemoveFocusChangeListener(this); + } ClientView::WindowClosing(); } @@ -302,12 +316,7 @@ void DialogClientView::ViewHierarchyChanged(bool is_add, View* parent, ShowDialogButtons(); ClientView::ViewHierarchyChanged(is_add, parent, child); - FocusManager* focus_manager = GetFocusManager(); - // Listen for focus change events so we can update the default button. - DCHECK(focus_manager); // bug #1291225: crash reports seem to indicate it - // can be NULL. - if (focus_manager) - focus_manager->AddFocusChangeListener(this); + UpdateFocusListener(); // The "extra view" must be created and installed after the contents view // has been inserted into the view hierarchy. @@ -503,4 +512,27 @@ void DialogClientView::Close() { GetDialogDelegate()->OnClose(); } +void DialogClientView::UpdateFocusListener() { + FocusManager* focus_manager = GetFocusManager(); + // Listen for focus change events so we can update the default button. + // focus_manager can be NULL when the dialog is created on un-shown view. + // We start listening for focus changes when the page is visible. + // Focus manager could also change if window host changes a parent. + if (listening_to_focus_) { + if (saved_focus_manager_ == focus_manager) + return; + DCHECK(saved_focus_manager_); + if (saved_focus_manager_) + saved_focus_manager_->RemoveFocusChangeListener(this); + listening_to_focus_ = false; + } + saved_focus_manager_ = focus_manager; + // Listen for focus change events so we can update the default button. + if (focus_manager) { + focus_manager->AddFocusChangeListener(this); + listening_to_focus_ = true; + } +} + + } // namespace views -- cgit v1.1