From e503e0ffcba6f3e23a9df68c1e2aba5b88748ca9 Mon Sep 17 00:00:00 2001 From: Edu Garcia Date: Thu, 10 May 2018 12:11:19 +1000 Subject: [PATCH] Add support for separate color for code blocks --- .../ru/noties/markwon/spans/CodeSpan.java | 6 +-- .../noties/markwon/spans/SpannableTheme.java | 40 ++++++++++++++++--- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/library/src/main/java/ru/noties/markwon/spans/CodeSpan.java b/library/src/main/java/ru/noties/markwon/spans/CodeSpan.java index 0149503c..b1098654 100644 --- a/library/src/main/java/ru/noties/markwon/spans/CodeSpan.java +++ b/library/src/main/java/ru/noties/markwon/spans/CodeSpan.java @@ -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; diff --git a/library/src/main/java/ru/noties/markwon/spans/SpannableTheme.java b/library/src/main/java/ru/noties/markwon/spans/SpannableTheme.java index c402e24f..171d9b3e 100644 --- a/library/src/main/java/ru/noties/markwon/spans/SpannableTheme.java +++ b/library/src/main/java/ru/noties/markwon/spans/SpannableTheme.java @@ -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;