From eae6129496293ed011160f5f4f49930d657650a7 Mon Sep 17 00:00:00 2001 From: "aaronleventhal%moonset.net" Date: Tue, 16 Aug 2005 17:54:29 +0000 Subject: [PATCH] Bug 258285. Find As You Type coming up when it shouldn't (textareas/textfields). r=mats, sr=bryner --- content/events/src/nsEventStateManager.cpp | 92 ++++++++++++---------- content/events/src/nsEventStateManager.h | 8 ++ xpfe/appshell/src/nsWebShellWindow.cpp | 8 +- 3 files changed, 63 insertions(+), 45 deletions(-) diff --git a/content/events/src/nsEventStateManager.cpp b/content/events/src/nsEventStateManager.cpp index ee52d8e0411..a84cab81035 100644 --- a/content/events/src/nsEventStateManager.cpp +++ b/content/events/src/nsEventStateManager.cpp @@ -847,14 +847,6 @@ nsEventStateManager::PreHandleEvent(nsPresContext* aPresContext, if (focusController) { - // Make sure the focus controller is up-to-date, since restoring - // focus memory may have caused focus to go elsewhere. - - if (gLastFocusedDocument && gLastFocusedDocument == mDocument) { - nsCOMPtr focusElement = do_QueryInterface(mCurrentFocus); - focusController->SetFocusedElement(focusElement); - } - PRBool isSuppressed; focusController->GetSuppressFocus(&isSuppressed); while (isSuppressed) { @@ -865,6 +857,14 @@ nsEventStateManager::PreHandleEvent(nsPresContext* aPresContext, focusController->GetSuppressFocus(&isSuppressed); } focusController->SetSuppressFocusScroll(PR_FALSE); + + // Make sure the focus controller is up-to-date, since restoring + // focus memory may have caused focus to go elsewhere. + + if (gLastFocusedDocument && gLastFocusedDocument == mDocument) { + nsCOMPtr focusElement = do_QueryInterface(mCurrentFocus); + focusController->SetFocusedElement(focusElement); + } } } break; @@ -3892,6 +3892,20 @@ nsEventStateManager::SetContentState(nsIContent *aContent, PRInt32 aState) // only raise window if the the focus controller is active SendFocusBlur(mPresContext, aContent, fcActive); +#ifdef DEBUG_aleventhal + nsCOMPtr currentWindow = + do_QueryInterface(GetDocumentOuterWindow(mDocument)); + if (currentWindow) { + nsIFocusController *fc = currentWindow->GetRootFocusController(); + if (fc) { + nsCOMPtr focusedElement; + fc->GetFocusedElement(getter_AddRefs(focusedElement)); + if (!SameCOMIdentity(mCurrentFocus, focusedElement)) { + printf("\n\nFocus out of whack!!!\n\n"); + } + } + } +#endif // If we now have focused content, ensure that the canvas focus ring // is removed. if (mDocument) { @@ -4099,23 +4113,6 @@ nsEventStateManager::SendFocusBlur(nsPresContext* aPresContext, EnsureDocument(presShell); - // Make sure we're not switching command dispatchers, if so, - // surpress the blurred one - if(gLastFocusedDocument && mDocument) { - nsIFocusController *newFocusController = nsnull; - nsIFocusController *oldFocusController = nsnull; - nsCOMPtr newWindow = - do_QueryInterface(GetDocumentOuterWindow(mDocument)); - nsCOMPtr oldWindow = - do_QueryInterface(GetDocumentOuterWindow(gLastFocusedDocument)); - if(newWindow) - newFocusController = newWindow->GetRootFocusController(); - if(oldWindow) - oldFocusController = oldWindow->GetRootFocusController(); - if(oldFocusController && oldFocusController != newFocusController) - oldFocusController->SetSuppressFocus(PR_TRUE, "SendFocusBlur Window Switch"); - } - nsCOMPtr esm; esm = oldPresContext->EventStateManager(); esm->SetFocusedContent(gLastFocusedContent); @@ -4139,6 +4136,7 @@ nsEventStateManager::SendFocusBlur(nsPresContext* aPresContext, if (previousFocus && previousFocus != focusAfterBlur) { // The content node's blur handler focused something else. // In this case, abort firing any more blur or focus events. + EnsureFocusSynchronization(); return NS_OK; } } @@ -4155,23 +4153,6 @@ nsEventStateManager::SendFocusBlur(nsPresContext* aPresContext, nsEventStatus status = nsEventStatus_eIgnore; nsEvent event(PR_TRUE, NS_BLUR_CONTENT); - // Make sure we're not switching command dispatchers, if so, - // surpress the blurred one - if (mDocument) { - nsIFocusController *newFocusController = nsnull; - nsIFocusController *oldFocusController = nsnull; - nsCOMPtr newWindow = - do_QueryInterface(GetDocumentOuterWindow(mDocument)); - nsCOMPtr oldWindow = - do_QueryInterface(GetDocumentOuterWindow(gLastFocusedDocument)); - - if (newWindow) - newFocusController = newWindow->GetRootFocusController(); - oldFocusController = oldWindow->GetRootFocusController(); - if (oldFocusController && oldFocusController != newFocusController) - oldFocusController->SetSuppressFocus(PR_TRUE, "SendFocusBlur Window Switch #2"); - } - gLastFocusedPresContext->EventStateManager()->SetFocusedContent(nsnull); nsCOMPtr temp = gLastFocusedDocument; NS_RELEASE(gLastFocusedDocument); @@ -4183,7 +4164,10 @@ nsEventStateManager::SendFocusBlur(nsPresContext* aPresContext, if (previousFocus && mCurrentFocus != previousFocus) { // The document's blur handler focused something else. - // Abort firing any additional blur or focus events. + // Abort firing any additional blur or focus events, and make sure + // nsFocusController:mFocusedElement is not nulled out, but agrees + // with our current concept of focus. + EnsureFocusSynchronization(); return NS_OK; } @@ -4193,6 +4177,7 @@ nsEventStateManager::SendFocusBlur(nsPresContext* aPresContext, if (previousFocus && mCurrentFocus != previousFocus) { // The window's blur handler focused something else. // Abort firing any additional blur or focus events. + EnsureFocusSynchronization(); return NS_OK; } } @@ -4283,6 +4268,27 @@ nsEventStateManager::GetFocusedContent(nsIContent** aContent) return NS_OK; } +void nsEventStateManager::EnsureFocusSynchronization() +{ + // Sometimes the focus can get out of whack due to a blur handler + // resetting focus. In addition, we fire onchange from the blur handler + // for some controls, which is another place where focus can be changed. + // XXX Ideally we will eventually store focus in one place instead of + // the focus controller, esm, tabbrowser and some frames, so that it + // cannot get out of sync. + // See Bug 304751, calling FireOnChange() inside + // nsComboboxControlFrame::SetFocus() is bad + nsCOMPtr currentWindow = + do_QueryInterface(GetDocumentOuterWindow(mDocument)); + if (currentWindow) { + nsIFocusController *fc = currentWindow->GetRootFocusController(); + if (fc) { + nsCOMPtr focusedElement = do_QueryInterface(mCurrentFocus); + fc->SetFocusedElement(focusedElement); + } + } +} + NS_IMETHODIMP nsEventStateManager::SetFocusedContent(nsIContent* aContent) { diff --git a/content/events/src/nsEventStateManager.h b/content/events/src/nsEventStateManager.h index 6b96022242a..800d91bd379 100644 --- a/content/events/src/nsEventStateManager.h +++ b/content/events/src/nsEventStateManager.h @@ -162,6 +162,14 @@ public: } protected: + /** + * In certain situations the focus controller's concept of focus gets out of + * whack with mCurrentFocus. This is used in known cases to reset the focus + * controller's focus. At some point we should probably move to a single + * focus storage mechanism because tracking it in several places is error-prone. + */ + void EnsureFocusSynchronization(); + void UpdateCursor(nsPresContext* aPresContext, nsEvent* aEvent, nsIFrame* aTargetFrame, nsEventStatus* aStatus); /** * Turn a GUI mouse event into a mouse event targeted at the specified diff --git a/xpfe/appshell/src/nsWebShellWindow.cpp b/xpfe/appshell/src/nsWebShellWindow.cpp index 175b85b2f37..b1c3fcec308 100644 --- a/xpfe/appshell/src/nsWebShellWindow.cpp +++ b/xpfe/appshell/src/nsWebShellWindow.cpp @@ -483,11 +483,15 @@ nsWebShellWindow::HandleEvent(nsGUIEvent *aEvent) // on eventWindow. -bryner nsCOMPtr kungFuDeathGrip(eventWindow); - focusController->SetSuppressFocus(PR_TRUE, "Activation Suppression"); - nsCOMPtr domWindow = do_QueryInterface(piWin); + if (domWindow == focusedWindow) { + break; // Already focused + } + + focusController->SetSuppressFocus(PR_TRUE, "Activation Suppression"); + NS_ASSERTION(domWindow, "windows must support nsIDOMWindowInternal");