Add support for separate color for code blocks
This commit is contained in:
parent
35c39d0f6d
commit
e503e0ffcb
@ -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;
|
||||||
|
@ -145,9 +145,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;
|
||||||
@ -198,7 +204,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;
|
||||||
@ -278,11 +286,15 @@ public class SpannableTheme {
|
|||||||
return width;
|
return width;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void applyCodeTextStyle(@NonNull Paint paint) {
|
public void applyCodeTextStyle(@NonNull Paint paint, boolean multiline) {
|
||||||
|
|
||||||
|
if (multiline && codeBlockTextColor != 0) {
|
||||||
|
paint.setColor(codeBlockTextColor);
|
||||||
|
} else {
|
||||||
if (codeTextColor != 0) {
|
if (codeTextColor != 0) {
|
||||||
paint.setColor(codeTextColor);
|
paint.setColor(codeTextColor);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// custom typeface was set
|
// custom typeface was set
|
||||||
if (codeTypeface != null) {
|
if (codeTypeface != null) {
|
||||||
@ -312,9 +324,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);
|
||||||
@ -437,7 +451,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;
|
||||||
@ -464,7 +480,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;
|
||||||
@ -528,12 +546,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;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user