Skip to content

Commit d885242

Browse files
imhappipaulfthomas
authored andcommitted
[Carousel] Fix a11y issue with switch access highlighting incorrect bounds
PiperOrigin-RevId: 618961042
1 parent 94e365f commit d885242

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

lib/java/com/google/android/material/carousel/MaskableFrameLayout.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import android.util.AttributeSet;
2525
import android.view.MotionEvent;
2626
import android.view.View;
27+
import android.view.accessibility.AccessibilityNodeInfo;
2728
import android.widget.FrameLayout;
2829
import androidx.annotation.NonNull;
2930
import androidx.annotation.Nullable;
@@ -45,6 +46,7 @@ public class MaskableFrameLayout extends FrameLayout implements Maskable, Shapea
4546

4647
private float maskXPercentage = NOT_SET;
4748
private final RectF maskRect = new RectF();
49+
private final Rect screenBoundsRect = new Rect();
4850
@Nullable private OnMaskChangedListener onMaskChangedListener;
4951
@NonNull private ShapeAppearanceModel shapeAppearanceModel;
5052
private final ShapeableDelegate shapeableDelegate = ShapeableDelegate.create(this);
@@ -218,4 +220,25 @@ public boolean onTouchEvent(MotionEvent event) {
218220
protected void dispatchDraw(Canvas canvas) {
219221
shapeableDelegate.maybeClip(canvas, super::dispatchDraw);
220222
}
223+
224+
@Override
225+
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
226+
super.onInitializeAccessibilityNodeInfo(info);
227+
// Note: This is a workaround until b/273752775 is resolved. A11y bounds should be set by the
228+
// a11y framework.
229+
info.getBoundsInScreen(screenBoundsRect);
230+
231+
// If the child starts from a negative x, the screen bounds are already cut off at the
232+
// parent so there is no need to reduce the screen bound's left bound. Similarly for negative y.
233+
if (getX() > 0) {
234+
screenBoundsRect.left = (int) (screenBoundsRect.left + maskRect.left);
235+
}
236+
if (getY() > 0) {
237+
screenBoundsRect.top = (int) (screenBoundsRect.top + maskRect.top);
238+
}
239+
screenBoundsRect.right = screenBoundsRect.left + Math.round(maskRect.width());
240+
screenBoundsRect.bottom = screenBoundsRect.top + Math.round(maskRect.height());
241+
242+
info.setBoundsInScreen(screenBoundsRect);
243+
}
221244
}

0 commit comments

Comments
 (0)