Add Alignment Functionality for Sonar

Summary:
Part of a hackathon project. (https://fb.facebook.com/groups/230455004101832/permalink/454928784987785/)

We're adding an alignment mode for Sonar that shows the pixel level alignment.

Reviewed By: danielbuechele

Differential Revision: D9121197

fbshipit-source-id: 21af36fd7eeea631d580ccebff8e648fa276bf35
This commit is contained in:
Phoomraphee Luenam
2018-08-06 04:09:27 -07:00
committed by Facebook Github Bot
parent 0c536b3188
commit 8ea2809742
2 changed files with 137 additions and 16 deletions

View File

@@ -12,6 +12,7 @@ import android.graphics.Rect;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Build; import android.os.Build;
import android.view.View; import android.view.View;
import android.util.Log;
/** /**
* A singleton instance of a overlay drawable used for highlighting node bounds. See {@link * A singleton instance of a overlay drawable used for highlighting node bounds. See {@link
@@ -31,24 +32,49 @@ public class HighlightedOverlay {
*/ */
public static void setHighlighted( public static void setHighlighted(
View targetView, Rect margin, Rect padding, Rect contentBounds) { View targetView, Rect margin, Rect padding, Rect contentBounds) {
if (!VIEW_OVERLAY_SUPPORT) { setHighlightedAndAlignment(targetView, margin, padding, contentBounds, false);
return;
}
contentBounds.set(
contentBounds.left + padding.left,
contentBounds.top + padding.top,
contentBounds.right - padding.right,
contentBounds.bottom - padding.bottom);
padding = enclose(padding, contentBounds);
margin = enclose(margin, padding);
final float density = targetView.getContext().getResources().getDisplayMetrics().density;
final Drawable overlay = BoundsDrawable.getInstance(density, margin, padding, contentBounds);
targetView.getOverlay().add(overlay);
} }
public static void setHighlightedAndAlignment(
View targetView, Rect margin, Rect padding, Rect contentBounds, boolean isAlignmentMode) {
if (!VIEW_OVERLAY_SUPPORT) {
return;
}
contentBounds.set(
contentBounds.left + padding.left,
contentBounds.top + padding.top,
contentBounds.right - padding.right,
contentBounds.bottom - padding.bottom);
padding = enclose(padding, contentBounds);
margin = enclose(margin, padding);
final float density = targetView.getContext().getResources().getDisplayMetrics().density;
final Drawable overlay = BoundsDrawable.getInstance(density, margin, padding, contentBounds);
targetView.getOverlay().add(overlay);
if(isAlignmentMode) {
int[] coords = new int[2];
targetView.getLocationOnScreen(coords);
Rect lineContentBounds =
new Rect(
coords[0] + contentBounds.left,
coords[1] + contentBounds.top,
coords[0] + contentBounds.right,
coords[1] + contentBounds.bottom);
final Drawable lineOverlay = LinesDrawable.getInstance(density, margin, padding, lineContentBounds);
targetView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
targetView.getRootView().getOverlay().add(lineOverlay);
}
}
public static void removeHighlight(View targetView) { public static void removeHighlight(View targetView) {
if (!VIEW_OVERLAY_SUPPORT) { if (!VIEW_OVERLAY_SUPPORT) {
return; return;
@@ -56,6 +82,9 @@ public class HighlightedOverlay {
final float density = targetView.getContext().getResources().getDisplayMetrics().density; final float density = targetView.getContext().getResources().getDisplayMetrics().density;
final Drawable overlay = BoundsDrawable.getInstance(density); final Drawable overlay = BoundsDrawable.getInstance(density);
final Drawable overlay2 = LinesDrawable.getInstance(density);
targetView.getRootView().getOverlay().remove(overlay2);
targetView.getOverlay().remove(overlay); targetView.getOverlay().remove(overlay);
} }

View File

@@ -0,0 +1,92 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
package com.facebook.sonar.plugins.inspector;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.DashPathEffect;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.Region;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import javax.annotation.Nullable;
public class LinesDrawable extends Drawable {
private static @Nullable LinesDrawable sInstance;
private final Rect mWorkRect;
private final Rect mMarginBounds;
private final Rect mPaddingBounds;
private final Rect mContentBounds;
private final float mDensity;
public static LinesDrawable getInstance(
float density, Rect marginBounds, Rect paddingBounds, Rect contentBounds) {
final LinesDrawable drawable = getInstance(density);
drawable.setBounds(marginBounds, paddingBounds, contentBounds);
return drawable;
}
public static LinesDrawable getInstance(float density) {
if (sInstance == null) {
sInstance = new LinesDrawable(density);
}
return sInstance;
}
private LinesDrawable(float density) {
mWorkRect = new Rect();
mMarginBounds = new Rect();
mPaddingBounds = new Rect();
mContentBounds = new Rect();
mDensity = density;
}
public void setBounds(Rect marginBounds, Rect paddingBounds, Rect contentBounds) {
mMarginBounds.set(marginBounds);
mPaddingBounds.set(paddingBounds);
mContentBounds.set(contentBounds);
setBounds(marginBounds);
}
@Override
public void draw(Canvas canvas) {
Paint dashPaint = new Paint();
dashPaint.setColor(0xFF800000);
dashPaint.setStyle(Paint.Style.STROKE);
dashPaint.setStrokeWidth(3);
dashPaint.setPathEffect(new DashPathEffect(new float[]{10, 10}, 0));
canvas.drawLine(mContentBounds.right, 0, mContentBounds.right, 100000, dashPaint);
canvas.drawLine(mContentBounds.left, 0, mContentBounds.left, 100000, dashPaint);
canvas.drawLine(0, mContentBounds.top, 100000, mContentBounds.top, dashPaint);
canvas.drawLine(0, mContentBounds.bottom, 100000, mContentBounds.bottom, dashPaint);
}
@Override
public void setAlpha(int alpha) {
// No-op
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
// No-op
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
}