Add support for separate color for code blocks

This commit is contained in:
Edu Garcia 2018-05-10 12:11:19 +10:00
parent 35c39d0f6d
commit e503e0ffcb
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

@ -145,9 +145,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;
@ -198,7 +204,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;
@ -278,10 +286,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
@ -312,9 +324,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);
@ -437,7 +451,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;
@ -464,7 +480,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;
@ -528,12 +546,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;