summaryrefslogtreecommitdiffstats
path: root/views/window
diff options
context:
space:
mode:
Diffstat (limited to 'views/window')
-rw-r--r--views/window/dialog_client_view.cc54
-rw-r--r--views/window/dialog_client_view.h14
2 files changed, 57 insertions, 11 deletions
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
diff --git a/views/window/dialog_client_view.h b/views/window/dialog_client_view.h
index 1c3d19f..d7a5fb2 100644
--- a/views/window/dialog_client_view.h
+++ b/views/window/dialog_client_view.h
@@ -50,6 +50,11 @@ class DialogClientView : public ClientView,
NativeButton* ok_button() const { return ok_button_; }
NativeButton* cancel_button() const { return cancel_button_; }
+ // Overridden from View:
+ virtual void NativeViewHierarchyChanged(bool attached,
+ gfx::NativeView native_view,
+ RootView* root_view);
+
// Overridden from ClientView:
virtual bool CanClose() const;
virtual void WindowClosing();
@@ -98,6 +103,9 @@ class DialogClientView : public ClientView,
// Closes the window.
void Close();
+ // Updates focus listener.
+ void UpdateFocusListener();
+
// The dialog buttons.
NativeButton* ok_button_;
NativeButton* cancel_button_;
@@ -114,6 +122,12 @@ class DialogClientView : public ClientView,
// True if the window was Accepted by the user using the OK button.
bool accepted_;
+ // true if focus listener is added.
+ bool listening_to_focus_;
+
+ // When ancestor gets changed focus manager gets changed as well.
+ FocusManager* saved_focus_manager_;
+
// Static resource initialization
static void InitClass();
static gfx::Font* dialog_button_font_;