Skip to content

Commit

Permalink
[FAB] Correctly handle min touch target size
Browse files Browse the repository at this point in the history
In the original logic we only handle touching down events happening within the "actual" button area, which breaks the min touch target size enforcement. Fixes this by calculating the correct area of touch target when handling touching down events.

PiperOrigin-RevId: 516600706
  • Loading branch information
drchen authored and paulfthomas committed Mar 14, 2023
1 parent 3856af1 commit d6f36e8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Expand Up @@ -900,6 +900,12 @@ public void getMeasuredContentRect(@NonNull Rect rect) {
offsetRectWithShadow(rect);
}

private void getTouchTargetRect(@NonNull Rect rect) {
getMeasuredContentRect(rect);
int touchTargetPadding = impl.getTouchTargetPadding();
rect.inset(-touchTargetPadding, -touchTargetPadding);
}

private void offsetRectWithShadow(@NonNull Rect rect) {
rect.left += shadowPadding.left;
rect.top += shadowPadding.top;
Expand All @@ -917,7 +923,8 @@ public Drawable getContentBackground() {
public boolean onTouchEvent(@NonNull MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
// Skipping the gesture if it doesn't start in the FAB 'content' area
if (getContentRect(touchArea) && !touchArea.contains((int) ev.getX(), (int) ev.getY())) {
getTouchTargetRect(touchArea);
if (!touchArea.contains((int) ev.getX(), (int) ev.getY())) {
return false;
}
}
Expand Down
Expand Up @@ -742,16 +742,20 @@ final void updatePadding() {
}

void getPadding(@NonNull Rect rect) {
final int minPadding = ensureMinTouchTargetSize
? (minTouchTargetSize - view.getSizeDimension()) / 2
: 0;

final int touchTargetPadding = getTouchTargetPadding();
final float maxShadowSize = shadowPaddingEnabled ? (getElevation() + pressedTranslationZ) : 0;
final int hPadding = Math.max(minPadding, (int) Math.ceil(maxShadowSize));
final int vPadding = Math.max(minPadding, (int) Math.ceil(maxShadowSize * SHADOW_MULTIPLIER));
final int hPadding = Math.max(touchTargetPadding, (int) Math.ceil(maxShadowSize));
final int vPadding = Math.max(
touchTargetPadding, (int) Math.ceil(maxShadowSize * SHADOW_MULTIPLIER));
rect.set(hPadding, vPadding, hPadding, vPadding);
}

int getTouchTargetPadding() {
return ensureMinTouchTargetSize
? Math.max((minTouchTargetSize - view.getSizeDimension()) / 2, 0)
: 0;
}

void onPaddingUpdated(@NonNull Rect padding) {
Preconditions.checkNotNull(contentBackground, "Didn't initialize content background");
if (shouldAddPadding()) {
Expand Down

0 comments on commit d6f36e8

Please sign in to comment.