From fdeeff6fd381d6f9d259d9ae0f440230731d2f71 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Mon, 20 Nov 2017 20:37:25 +0100 Subject: make keyword styling work in quotes --- .../de/pixart/messenger/utils/StylingHelper.java | 27 ++++++++++++++-------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'src/main/java/de/pixart/messenger/utils') diff --git a/src/main/java/de/pixart/messenger/utils/StylingHelper.java b/src/main/java/de/pixart/messenger/utils/StylingHelper.java index 70b714594..8d5e73ccf 100644 --- a/src/main/java/de/pixart/messenger/utils/StylingHelper.java +++ b/src/main/java/de/pixart/messenger/utils/StylingHelper.java @@ -45,6 +45,8 @@ import android.widget.EditText; import java.util.Arrays; import java.util.List; +import de.pixart.messenger.ui.text.QuoteSpan; + public class StylingHelper { private static List> SPAN_CLASSES = Arrays.asList( @@ -63,17 +65,11 @@ public class StylingHelper { } } - public static void format(final Editable editable, @ColorInt int color) { - final int syntaxColor = Color.argb( - Math.round(Color.alpha(color) * 0.6f), - Color.red(color), - Color.green(color), - Color.blue(color) - ); + public static void format(final Editable editable, @ColorInt int textColor) { for (ImStyleParser.Style style : ImStyleParser.parse(editable)) { editable.setSpan(createSpanForStyle(style), style.getStart() + 1, style.getEnd(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - editable.setSpan(new ForegroundColorSpan(syntaxColor), style.getStart(), style.getStart() + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - editable.setSpan(new ForegroundColorSpan(syntaxColor), style.getEnd(), style.getEnd() + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + makeKeywordOpaque(editable, style.getStart(), style.getStart() + 1, textColor); + makeKeywordOpaque(editable, style.getEnd(), style.getEnd() + 1, textColor); } } @@ -92,6 +88,19 @@ public class StylingHelper { } } + private static void makeKeywordOpaque(final Editable editable, int start, int end, @ColorInt int fallbackTextColor) { + QuoteSpan[] quoteSpans = editable.getSpans(start, end, QuoteSpan.class); + @ColorInt int textColor = quoteSpans.length > 0 ? quoteSpans[0].getColor() : fallbackTextColor; + @ColorInt int keywordColor = transformColor(textColor); + editable.setSpan(new ForegroundColorSpan(keywordColor), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + + private static + @ColorInt + int transformColor(@ColorInt int c) { + return Color.argb(Math.round(Color.alpha(c) * 0.6f), Color.red(c), Color.green(c), Color.blue(c)); + } + public static class MessageEditorStyler implements TextWatcher { private final EditText mEditText; -- cgit v1.2.3