Skip to content
This repository was archived by the owner on Dec 1, 2017. It is now read-only.

Add macro control for enabling / disabling dspec. #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions library/src/main/java/org/lucasr/dspec/DesignSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,20 @@

package org.lucasr.dspec;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
* Draw a baseline grid, keylines, and spacing markers on top of a {@link View}.
Expand Down Expand Up @@ -118,6 +114,7 @@
* @see #fromResource(View, int)
*/
public class DesignSpec extends Drawable {
private static final boolean DEFAULT_DESIGN_SPEC_ENABLED = true;
private static final boolean DEFAULT_BASELINE_GRID_VISIBLE = false;
private static final boolean DEFAULT_KEYLINES_VISIBLE = true;
private static final boolean DEFAULT_SPACINGS_VISIBLE = true;
Expand Down Expand Up @@ -215,6 +212,8 @@ public boolean equals(Object o) {

private final float mDensity;

private boolean mDesignSpecEnabled = DEFAULT_DESIGN_SPEC_ENABLED;

private boolean mBaselineGridVisible = DEFAULT_BASELINE_GRID_VISIBLE;
private float mBaselineGridCellSize;
private final Paint mBaselineGridPaint;
Expand Down Expand Up @@ -247,11 +246,32 @@ public DesignSpec(Resources resources, View hostView) {
mBaselineGridCellSize = mDensity * DEFAULT_BASELINE_GRID_CELL_SIZE_DIP;
}

/**
* Whether or not the design spec is enabled.
*/
public boolean isDesignSpecEnabled() {
return mDesignSpecEnabled;
}

/**
* Enables or disables design spec.
*/
public DesignSpec setDesignSpecEnabled(boolean enabled) {
if (mDesignSpecEnabled == enabled) {
return this;
}

mDesignSpecEnabled = enabled;
invalidateSelf();

return this;
}

/**
* Whether or not the baseline grid should be drawn.
*/
public boolean isBaselineGridVisible() {
return mBaselineGridVisible;
return mDesignSpecEnabled && mBaselineGridVisible;
}

/**
Expand Down Expand Up @@ -301,7 +321,7 @@ public DesignSpec setBaselineGridColor(int color) {
* Whether or not the keylines should be drawn.
*/
public boolean areKeylinesVisible() {
return mKeylinesVisible;
return mDesignSpecEnabled && mKeylinesVisible;
}

/**
Expand Down Expand Up @@ -349,7 +369,7 @@ public DesignSpec addKeyline(float position, From from) {
* Whether or not the spacing markers should be drawn.
*/
public boolean areSpacingsVisible() {
return mSpacingsVisible;
return mDesignSpecEnabled && mSpacingsVisible;
}

/**
Expand Down Expand Up @@ -394,7 +414,7 @@ public DesignSpec addSpacing(float position, float size, From from) {
}

private void drawBaselineGrid(Canvas canvas) {
if (!mBaselineGridVisible) {
if (!isBaselineGridVisible()) {
return;
}

Expand All @@ -415,7 +435,7 @@ private void drawBaselineGrid(Canvas canvas) {
}

private void drawKeylines(Canvas canvas) {
if (!mKeylinesVisible) {
if (!areKeylinesVisible()) {
return;
}

Expand Down Expand Up @@ -470,7 +490,7 @@ private void drawKeylines(Canvas canvas) {
}

private void drawSpacings(Canvas canvas) {
if (!mSpacingsVisible) {
if (!areSpacingsVisible()) {
return;
}

Expand Down
11 changes: 11 additions & 0 deletions sample/src/main/java/org/lucasr/dspec/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ protected void onCreate(Bundle savedInstanceState) {

@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Enable DSpec")
.setCheckable(true)
.setChecked(mDesignSpecLayout.getDesignSpec().isDesignSpecEnabled())
.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override public boolean onMenuItemClick(MenuItem item) {
boolean checked = !item.isChecked();
item.setChecked(checked);
mDesignSpecLayout.getDesignSpec().setDesignSpecEnabled(checked);
return true;
}
});
menu.add(getString(R.string.show_baseline_grid))
.setCheckable(true)
.setChecked(mDesignSpecLayout.getDesignSpec().isBaselineGridVisible())
Expand Down