Bug 1653421 - Part 1: Refactor AccessibleWrap event handling. r=morgan

I split the switch statement into two: the first switch determines the
MOX target accessible. The second one does all the rest. This makes it more
readable and scalable when we add more events that need to be accompanied with
data.

Differential Revision: https://phabricator.services.mozilla.com/D84053
This commit is contained in:
Eitan Isaacson 2020-07-21 23:02:53 +00:00
Родитель f25a4cdbe8
Коммит 66653fbad1
1 изменённых файлов: 62 добавлений и 73 удалений

Просмотреть файл

@ -110,10 +110,68 @@ nsresult AccessibleWrap::HandleAccEvent(AccEvent* aEvent) {
}
uint32_t eventType = aEvent->GetEventType();
mozAccessible* nativeAcc = nil;
Accessible* eventTarget = nullptr;
switch (eventType) {
case nsIAccessibleEvent::EVENT_SELECTION:
case nsIAccessibleEvent::EVENT_SELECTION_ADD:
case nsIAccessibleEvent::EVENT_SELECTION_REMOVE: {
AccSelChangeEvent* selEvent = downcast_accEvent(aEvent);
// The "widget" is the selected widget's container. In OSX
// it is the target of the selection changed event.
eventTarget = selEvent->Widget();
break;
}
default:
eventTarget = aEvent->GetAccessible();
break;
}
mozAccessible* nativeAcc = nil;
eventTarget->GetNativeInterface((void**)&nativeAcc);
if (!nativeAcc) {
return NS_ERROR_FAILURE;
}
switch (eventType) {
case nsIAccessibleEvent::EVENT_STATE_CHANGE: {
AccStateChangeEvent* event = downcast_accEvent(aEvent);
[nativeAcc stateChanged:event->GetState() isEnabled:event->IsStateEnabled()];
break;
}
case nsIAccessibleEvent::EVENT_TEXT_SELECTION_CHANGED: {
MOXTextMarkerDelegate* delegate =
[MOXTextMarkerDelegate getOrCreateForDoc:aEvent->Document()];
AccTextSelChangeEvent* event = downcast_accEvent(aEvent);
AutoTArray<TextRange, 1> ranges;
event->SelectionRanges(&ranges);
if (ranges.Length()) {
// Cache selection in delegate.
[delegate setSelectionFrom:ranges[0].StartContainer()
at:ranges[0].StartOffset()
to:ranges[0].EndContainer()
at:ranges[0].EndOffset()];
}
[nativeAcc handleAccessibleEvent:eventType];
break;
}
case nsIAccessibleEvent::EVENT_TEXT_CARET_MOVED: {
AccCaretMoveEvent* event = downcast_accEvent(aEvent);
if (event->IsSelectionCollapsed()) {
// If the selection is collapsed, invalidate our text selection cache.
MOXTextMarkerDelegate* delegate =
[MOXTextMarkerDelegate getOrCreateForDoc:aEvent->Document()];
[delegate invalidateSelection];
}
[nativeAcc handleAccessibleEvent:eventType];
break;
}
case nsIAccessibleEvent::EVENT_FOCUS:
case nsIAccessibleEvent::EVENT_VALUE_CHANGE:
case nsIAccessibleEvent::EVENT_TEXT_VALUE_CHANGE:
@ -121,85 +179,16 @@ nsresult AccessibleWrap::HandleAccEvent(AccEvent* aEvent) {
case nsIAccessibleEvent::EVENT_MENUPOPUP_START:
case nsIAccessibleEvent::EVENT_MENUPOPUP_END:
case nsIAccessibleEvent::EVENT_REORDER:
if (Accessible* accessible = aEvent->GetAccessible()) {
accessible->GetNativeInterface((void**)&nativeAcc);
if (!nativeAcc) {
return NS_ERROR_FAILURE;
}
}
break;
case nsIAccessibleEvent::EVENT_SELECTION:
case nsIAccessibleEvent::EVENT_SELECTION_ADD:
case nsIAccessibleEvent::EVENT_SELECTION_REMOVE: {
AccSelChangeEvent* selEvent = downcast_accEvent(aEvent);
// The "widget" is the selected widget's container. In OSX
// it is the target of the selection changed event.
if (Accessible* accessible = selEvent->Widget()) {
accessible->GetNativeInterface((void**)&nativeAcc);
if (!nativeAcc) {
return NS_ERROR_FAILURE;
}
}
case nsIAccessibleEvent::EVENT_SELECTION_REMOVE:
[nativeAcc handleAccessibleEvent:eventType];
break;
}
case nsIAccessibleEvent::EVENT_STATE_CHANGE:
if (Accessible* accessible = aEvent->GetAccessible()) {
accessible->GetNativeInterface((void**)&nativeAcc);
if (nativeAcc) {
AccStateChangeEvent* event = downcast_accEvent(aEvent);
[nativeAcc stateChanged:event->GetState() isEnabled:event->IsStateEnabled()];
return NS_OK;
} else {
return NS_ERROR_FAILURE;
}
}
break;
case nsIAccessibleEvent::EVENT_TEXT_SELECTION_CHANGED:
if (Accessible* accessible = aEvent->GetAccessible()) {
accessible->GetNativeInterface((void**)&nativeAcc);
if (!nativeAcc) {
return NS_ERROR_FAILURE;
}
MOXTextMarkerDelegate* delegate =
[MOXTextMarkerDelegate getOrCreateForDoc:aEvent->Document()];
AccTextSelChangeEvent* event = downcast_accEvent(aEvent);
AutoTArray<TextRange, 1> ranges;
event->SelectionRanges(&ranges);
if (ranges.Length()) {
// Cache selection in delegate.
[delegate setSelectionFrom:ranges[0].StartContainer()
at:ranges[0].StartOffset()
to:ranges[0].EndContainer()
at:ranges[0].EndOffset()];
}
}
break;
case nsIAccessibleEvent::EVENT_TEXT_CARET_MOVED:
if (Accessible* accessible = aEvent->GetAccessible()) {
accessible->GetNativeInterface((void**)&nativeAcc);
if (!nativeAcc) {
return NS_ERROR_FAILURE;
}
AccCaretMoveEvent* event = downcast_accEvent(aEvent);
if (event->IsSelectionCollapsed()) {
// If the selection is collapsed, invalidate our text selection cache.
MOXTextMarkerDelegate* delegate =
[MOXTextMarkerDelegate getOrCreateForDoc:aEvent->Document()];
[delegate invalidateSelection];
}
}
break;
default:
break;
}
if (nativeAcc) {
[nativeAcc handleAccessibleEvent:eventType];
}
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;