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

View File

@ -147,9 +147,15 @@ public class SpannableTheme {
// by default - main text color
protected final int codeTextColor;
// by default - codeTextColor
protected final int codeBlockTextColor;
// by default 0.1 alpha of textColor/codeTextColor
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`
// 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;
@ -200,7 +206,9 @@ public class SpannableTheme {
this.bulletListItemStrokeWidth = builder.bulletListItemStrokeWidth;
this.bulletWidth = builder.bulletWidth;
this.codeTextColor = builder.codeTextColor;
this.codeBlockTextColor = builder.codeBlockTextColor;
this.codeBackgroundColor = builder.codeBackgroundColor;
this.codeBlockBackgroundColor = builder.codeBlockBackgroundColor;
this.codeMultilineMargin = builder.codeMultilineMargin;
this.codeTypeface = builder.codeTypeface;
this.codeTextSize = builder.codeTextSize;
@ -298,10 +306,14 @@ public class SpannableTheme {
return width;
}
public void applyCodeTextStyle(@NonNull Paint paint) {
public void applyCodeTextStyle(@NonNull Paint paint, boolean multiline) {
if (codeTextColor != 0) {
paint.setColor(codeTextColor);
if (multiline && codeBlockTextColor != 0) {
paint.setColor(codeBlockTextColor);
} else {
if (codeTextColor != 0) {
paint.setColor(codeTextColor);
}
}
// custom typeface was set
@ -332,9 +344,11 @@ public class SpannableTheme {
return codeMultilineMargin;
}
public int getCodeBackgroundColor(@NonNull Paint paint) {
public int getCodeBackgroundColor(@NonNull Paint paint, boolean multiline) {
final int color;
if (codeBackgroundColor != 0) {
if (multiline && codeBlockBackgroundColor != 0) {
color = codeBlockBackgroundColor;
} else if (codeBackgroundColor != 0) {
color = codeBackgroundColor;
} else {
color = ColorUtils.applyAlpha(paint.getColor(), CODE_DEF_BACKGROUND_COLOR_ALPHA);
@ -457,7 +471,9 @@ public class SpannableTheme {
private int bulletListItemStrokeWidth;
private int bulletWidth;
private int codeTextColor;
private int codeBlockTextColor;
private int codeBackgroundColor;
private int codeBlockBackgroundColor;
private int codeMultilineMargin;
private Typeface codeTypeface;
private int codeTextSize;
@ -484,7 +500,9 @@ public class SpannableTheme {
this.bulletListItemStrokeWidth = theme.bulletListItemStrokeWidth;
this.bulletWidth = theme.bulletWidth;
this.codeTextColor = theme.codeTextColor;
this.codeBlockTextColor = theme.codeBlockTextColor;
this.codeBackgroundColor = theme.codeBackgroundColor;
this.codeBlockBackgroundColor = theme.codeBlockBackgroundColor;
this.codeMultilineMargin = theme.codeMultilineMargin;
this.codeTypeface = theme.codeTypeface;
this.codeTextSize = theme.codeTextSize;
@ -548,12 +566,24 @@ public class SpannableTheme {
return this;
}
@NonNull
public Builder codeBlockTextColor(@ColorInt int codeBlockTextColor) {
this.codeBlockTextColor = codeBlockTextColor;
return this;
}
@NonNull
public Builder codeBackgroundColor(@ColorInt int codeBackgroundColor) {
this.codeBackgroundColor = codeBackgroundColor;
return this;
}
@NonNull
public Builder codeBlockBackgroundColor(@ColorInt int codeBlockBackgroundColor) {
this.codeBlockBackgroundColor = codeBlockBackgroundColor;
return this;
}
@NonNull
public Builder codeMultilineMargin(@Dimension int codeMultilineMargin) {
this.codeMultilineMargin = codeMultilineMargin;