Bug 501496 part.3 Don't dispatch keypress events if defaultPrevent() of the keydown event is called on Cocoa r=smaug+smichaud

This commit is contained in:
Masayuki Nakano 2013-07-25 15:09:28 +09:00
parent 5c78ca14e0
commit 162b4fa956
2 changed files with 39 additions and 29 deletions

View File

@ -524,9 +524,14 @@ protected:
mCausedOtherKeyEvents = false;
}
bool KeyDownOrPressHandled()
bool IsDefaultPrevented() const
{
return mKeyDownHandled || mKeyPressHandled;
return mKeyDownHandled || mKeyPressHandled || mCausedOtherKeyEvents;
}
bool CanDispatchKeyPressEvent() const
{
return !mKeyPressDispatched && !IsDefaultPrevented();
}
};

View File

@ -1442,7 +1442,7 @@ TextInputHandler::HandleKeyDownEvent(NSEvent* aNativeEvent)
PR_LOG(gLog, PR_LOG_ALWAYS,
("%p TextInputHandler::HandleKeyDownEvent, "
"widget was destroyed by keydown event", this));
return currentKeyEvent->KeyDownOrPressHandled();
return currentKeyEvent->IsDefaultPrevented();
}
// The key down event may have shifted the focus, in which
@ -1452,7 +1452,7 @@ TextInputHandler::HandleKeyDownEvent(NSEvent* aNativeEvent)
PR_LOG(gLog, PR_LOG_ALWAYS,
("%p TextInputHandler::HandleKeyDownEvent, "
"view lost focus by keydown event", this));
return currentKeyEvent->KeyDownOrPressHandled();
return currentKeyEvent->IsDefaultPrevented();
}
// If this is the context menu key command, send a context menu key event.
@ -1473,7 +1473,14 @@ TextInputHandler::HandleKeyDownEvent(NSEvent* aNativeEvent)
Destroyed() ? " and widget was destroyed" : ""));
[mView maybeInitContextMenuTracking];
// Bail, there is nothing else to do here.
return (cmEventHandled || currentKeyEvent->KeyDownOrPressHandled());
return (cmEventHandled || currentKeyEvent->IsDefaultPrevented());
}
if (currentKeyEvent->IsDefaultPrevented()) {
PR_LOG(gLog, PR_LOG_ALWAYS,
("%p TextInputHandler::HandleKeyDownEvent, "
"keydown event's default is prevented", this));
return true;
}
}
@ -1495,7 +1502,7 @@ TextInputHandler::HandleKeyDownEvent(NSEvent* aNativeEvent)
PR_LOG(gLog, PR_LOG_ALWAYS,
("%p TextInputHandler::HandleKeyDownEvent, widget was destroyed",
this));
return currentKeyEvent->KeyDownOrPressHandled();
return currentKeyEvent->IsDefaultPrevented();
}
PR_LOG(gLog, PR_LOG_ALWAYS,
@ -1503,7 +1510,7 @@ TextInputHandler::HandleKeyDownEvent(NSEvent* aNativeEvent)
"IsIMEComposing()=%s",
this, TrueOrFalse(wasComposing), TrueOrFalse(IsIMEComposing())));
if (!currentKeyEvent->mKeyPressDispatched &&
if (currentKeyEvent->CanDispatchKeyPressEvent() &&
!wasComposing && !IsIMEComposing()) {
nsKeyEvent keypressEvent(true, NS_KEY_PRESS, mWidget);
InitKeyEvent(aNativeEvent, keypressEvent);
@ -1521,11 +1528,8 @@ TextInputHandler::HandleKeyDownEvent(NSEvent* aNativeEvent)
// our default action for this key.
if (!(interpretKeyEventsCalled &&
IsNormalCharInputtingEvent(keypressEvent))) {
if (currentKeyEvent->mKeyDownHandled ||
currentKeyEvent->mCausedOtherKeyEvents) {
keypressEvent.mFlags.mDefaultPrevented = true;
}
currentKeyEvent->mKeyPressHandled = DispatchEvent(keypressEvent);
currentKeyEvent->mKeyPressDispatched = true;
PR_LOG(gLog, PR_LOG_ALWAYS,
("%p TextInputHandler::HandleKeyDownEvent, keypress event dispatched",
this));
@ -1536,10 +1540,11 @@ TextInputHandler::HandleKeyDownEvent(NSEvent* aNativeEvent)
PR_LOG(gLog, PR_LOG_ALWAYS,
("%p TextInputHandler::HandleKeyDownEvent, "
"keydown handled=%s, keypress handled=%s",
"keydown handled=%s, keypress handled=%s, causedOtherKeyEvents=%s",
this, TrueOrFalse(currentKeyEvent->mKeyDownHandled),
TrueOrFalse(currentKeyEvent->mKeyPressHandled)));
return currentKeyEvent->KeyDownOrPressHandled();
TrueOrFalse(currentKeyEvent->mKeyPressHandled),
TrueOrFalse(currentKeyEvent->mCausedOtherKeyEvents)));
return currentKeyEvent->IsDefaultPrevented();
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(false);
}
@ -1948,14 +1953,19 @@ TextInputHandler::InsertText(NSAttributedString* aAttrString,
("%p TextInputHandler::InsertText, aAttrString=\"%s\", "
"aReplacementRange=%p { location=%llu, length=%llu }, "
"IsIMEComposing()=%s, IgnoreIMEComposition()=%s, "
"keyevent=%p, keypressDispatched=%s",
"keyevent=%p, keydownHandled=%s, keypressDispatched=%s, "
"causedOtherKeyEvents=%s",
this, GetCharacters([aAttrString string]), aReplacementRange,
aReplacementRange ? aReplacementRange->location : 0,
aReplacementRange ? aReplacementRange->length : 0,
TrueOrFalse(IsIMEComposing()), TrueOrFalse(IgnoreIMEComposition()),
currentKeyEvent ? currentKeyEvent->mKeyEvent : nullptr,
currentKeyEvent ?
TrueOrFalse(currentKeyEvent->mKeyPressDispatched) : "N/A"));
TrueOrFalse(currentKeyEvent->mKeyDownHandled) : "N/A",
currentKeyEvent ?
TrueOrFalse(currentKeyEvent->mKeyPressDispatched) : "N/A",
currentKeyEvent ?
TrueOrFalse(currentKeyEvent->mCausedOtherKeyEvents) : "N/A"));
if (IgnoreIMEComposition()) {
return;
@ -2014,7 +2024,7 @@ TextInputHandler::InsertText(NSAttributedString* aAttrString,
// Don't let the same event be fired twice when hitting
// enter/return! (Bug 420502)
if (currentKeyEvent && currentKeyEvent->mKeyPressDispatched) {
if (currentKeyEvent && !currentKeyEvent->CanDispatchKeyPressEvent()) {
return;
}
@ -2039,9 +2049,6 @@ TextInputHandler::InsertText(NSAttributedString* aAttrString,
if (currentKeyEvent) {
NSEvent* keyEvent = currentKeyEvent->mKeyEvent;
InitKeyEvent(keyEvent, keypressEvent, &str);
if (currentKeyEvent->mKeyDownHandled) {
keypressEvent.mFlags.mDefaultPrevented = true;
}
} else {
nsCocoaUtils::InitInputEvent(keypressEvent, static_cast<NSEvent*>(nullptr));
if (keypressEvent.isChar) {
@ -2084,20 +2091,19 @@ TextInputHandler::DoCommandBySelector(const char* aSelector)
PR_LOG(gLog, PR_LOG_ALWAYS,
("%p TextInputHandler::DoCommandBySelector, aSelector=\"%s\", "
"Destroyed()=%s, keypressHandled=%s, causedOtherKeyEvents=%s",
"Destroyed()=%s, keydownHandled=%s, keypressHandled=%s, "
"causedOtherKeyEvents=%s",
this, aSelector ? aSelector : "", TrueOrFalse(Destroyed()),
currentKeyEvent ?
TrueOrFalse(currentKeyEvent->mKeyDownHandled) : "N/A",
currentKeyEvent ?
TrueOrFalse(currentKeyEvent->mKeyPressHandled) : "N/A",
currentKeyEvent ?
TrueOrFalse(currentKeyEvent->mCausedOtherKeyEvents) : "N/A"));
if (currentKeyEvent && !currentKeyEvent->mKeyPressDispatched) {
if (currentKeyEvent && currentKeyEvent->CanDispatchKeyPressEvent()) {
nsKeyEvent keypressEvent(true, NS_KEY_PRESS, mWidget);
InitKeyEvent(currentKeyEvent->mKeyEvent, keypressEvent);
if (currentKeyEvent->mKeyDownHandled ||
currentKeyEvent->mCausedOtherKeyEvents) {
keypressEvent.mFlags.mDefaultPrevented = true;
}
currentKeyEvent->mKeyPressHandled = DispatchEvent(keypressEvent);
currentKeyEvent->mKeyPressDispatched = true;
PR_LOG(gLog, PR_LOG_ALWAYS,
@ -2107,9 +2113,8 @@ TextInputHandler::DoCommandBySelector(const char* aSelector)
TrueOrFalse(currentKeyEvent->mKeyPressHandled)));
}
return !Destroyed() && currentKeyEvent &&
(currentKeyEvent->mKeyPressHandled ||
currentKeyEvent->mCausedOtherKeyEvents);
return (!Destroyed() && currentKeyEvent &&
currentKeyEvent->IsDefaultPrevented());
}