Add support for separate color for code blocks (#37)

This commit is contained in:
Edu Garcia 2018-05-22 17:55:35 +10:00 committed by Dimitry
parent 31506a7acd
commit 78ddacbe1a
2 changed files with 38 additions and 8 deletions

View File

@ -31,12 +31,12 @@ public class CodeSpan extends MetricAffectingSpan implements LeadingMarginSpan {
public void updateDrawState(TextPaint ds) { public void updateDrawState(TextPaint ds) {
apply(ds); apply(ds);
if (!multiline) { if (!multiline) {
ds.bgColor = theme.getCodeBackgroundColor(ds); ds.bgColor = theme.getCodeBackgroundColor(ds, false);
} }
} }
private void apply(TextPaint p) { private void apply(TextPaint p) {
theme.applyCodeTextStyle(p); theme.applyCodeTextStyle(p, multiline);
} }
@Override @Override
@ -50,7 +50,7 @@ public class CodeSpan extends MetricAffectingSpan implements LeadingMarginSpan {
if (multiline) { if (multiline) {
paint.setStyle(Paint.Style.FILL); paint.setStyle(Paint.Style.FILL);
paint.setColor(theme.getCodeBackgroundColor(p)); paint.setColor(theme.getCodeBackgroundColor(p, true));
final int left; final int left;
final int right; final int right;

View File

@ -147,9 +147,15 @@ public class SpannableTheme {
// by default - main text color // by default - main text color
protected final int codeTextColor; protected final int codeTextColor;
// by default - codeTextColor
protected final int codeBlockTextColor;
// by default 0.1 alpha of textColor/codeTextColor // by default 0.1 alpha of textColor/codeTextColor
protected final int codeBackgroundColor; protected final int codeBackgroundColor;
// by default codeBackgroundColor
protected final int codeBlockBackgroundColor;
// by default `width` of a space char... it's fun and games, but span doesn't have access to paint in `getLeadingMargin` // by default `width` of a space char... it's fun and games, but span doesn't have access to paint in `getLeadingMargin`
// so, we need to set this value explicitly (think of an utility method, that takes TextView/TextPaint and measures space char) // so, we need to set this value explicitly (think of an utility method, that takes TextView/TextPaint and measures space char)
protected final int codeMultilineMargin; protected final int codeMultilineMargin;
@ -200,7 +206,9 @@ public class SpannableTheme {
this.bulletListItemStrokeWidth = builder.bulletListItemStrokeWidth; this.bulletListItemStrokeWidth = builder.bulletListItemStrokeWidth;
this.bulletWidth = builder.bulletWidth; this.bulletWidth = builder.bulletWidth;
this.codeTextColor = builder.codeTextColor; this.codeTextColor = builder.codeTextColor;
this.codeBlockTextColor = builder.codeBlockTextColor;
this.codeBackgroundColor = builder.codeBackgroundColor; this.codeBackgroundColor = builder.codeBackgroundColor;
this.codeBlockBackgroundColor = builder.codeBlockBackgroundColor;
this.codeMultilineMargin = builder.codeMultilineMargin; this.codeMultilineMargin = builder.codeMultilineMargin;
this.codeTypeface = builder.codeTypeface; this.codeTypeface = builder.codeTypeface;
this.codeTextSize = builder.codeTextSize; this.codeTextSize = builder.codeTextSize;
@ -298,10 +306,14 @@ public class SpannableTheme {
return width; return width;
} }
public void applyCodeTextStyle(@NonNull Paint paint) { public void applyCodeTextStyle(@NonNull Paint paint, boolean multiline) {
if (codeTextColor != 0) { if (multiline && codeBlockTextColor != 0) {
paint.setColor(codeTextColor); paint.setColor(codeBlockTextColor);
} else {
if (codeTextColor != 0) {
paint.setColor(codeTextColor);
}
} }
// custom typeface was set // custom typeface was set
@ -332,9 +344,11 @@ public class SpannableTheme {
return codeMultilineMargin; return codeMultilineMargin;
} }
public int getCodeBackgroundColor(@NonNull Paint paint) { public int getCodeBackgroundColor(@NonNull Paint paint, boolean multiline) {
final int color; final int color;
if (codeBackgroundColor != 0) { if (multiline && codeBlockBackgroundColor != 0) {
color = codeBlockBackgroundColor;
} else if (codeBackgroundColor != 0) {
color = codeBackgroundColor; color = codeBackgroundColor;
} else { } else {
color = ColorUtils.applyAlpha(paint.getColor(), CODE_DEF_BACKGROUND_COLOR_ALPHA); color = ColorUtils.applyAlpha(paint.getColor(), CODE_DEF_BACKGROUND_COLOR_ALPHA);
@ -457,7 +471,9 @@ public class SpannableTheme {
private int bulletListItemStrokeWidth; private int bulletListItemStrokeWidth;
private int bulletWidth; private int bulletWidth;
private int codeTextColor; private int codeTextColor;
private int codeBlockTextColor;
private int codeBackgroundColor; private int codeBackgroundColor;
private int codeBlockBackgroundColor;
private int codeMultilineMargin; private int codeMultilineMargin;
private Typeface codeTypeface; private Typeface codeTypeface;
private int codeTextSize; private int codeTextSize;
@ -484,7 +500,9 @@ public class SpannableTheme {
this.bulletListItemStrokeWidth = theme.bulletListItemStrokeWidth; this.bulletListItemStrokeWidth = theme.bulletListItemStrokeWidth;
this.bulletWidth = theme.bulletWidth; this.bulletWidth = theme.bulletWidth;
this.codeTextColor = theme.codeTextColor; this.codeTextColor = theme.codeTextColor;
this.codeBlockTextColor = theme.codeBlockTextColor;
this.codeBackgroundColor = theme.codeBackgroundColor; this.codeBackgroundColor = theme.codeBackgroundColor;
this.codeBlockBackgroundColor = theme.codeBlockBackgroundColor;
this.codeMultilineMargin = theme.codeMultilineMargin; this.codeMultilineMargin = theme.codeMultilineMargin;
this.codeTypeface = theme.codeTypeface; this.codeTypeface = theme.codeTypeface;
this.codeTextSize = theme.codeTextSize; this.codeTextSize = theme.codeTextSize;
@ -548,12 +566,24 @@ public class SpannableTheme {
return this; return this;
} }
@NonNull
public Builder codeBlockTextColor(@ColorInt int codeBlockTextColor) {
this.codeBlockTextColor = codeBlockTextColor;
return this;
}
@NonNull @NonNull
public Builder codeBackgroundColor(@ColorInt int codeBackgroundColor) { public Builder codeBackgroundColor(@ColorInt int codeBackgroundColor) {
this.codeBackgroundColor = codeBackgroundColor; this.codeBackgroundColor = codeBackgroundColor;
return this; return this;
} }
@NonNull
public Builder codeBlockBackgroundColor(@ColorInt int codeBlockBackgroundColor) {
this.codeBlockBackgroundColor = codeBlockBackgroundColor;
return this;
}
@NonNull @NonNull
public Builder codeMultilineMargin(@Dimension int codeMultilineMargin) { public Builder codeMultilineMargin(@Dimension int codeMultilineMargin) {
this.codeMultilineMargin = codeMultilineMargin; this.codeMultilineMargin = codeMultilineMargin;