From 49b4153fb59ad30a6d083ba0231fd50cc052374d Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 7 Nov 2017 13:23:49 +0100 Subject: [PATCH] make keyword styling work in quotes --- .../conversations/ui/text/QuoteSpan.java | 6 ++++ .../conversations/utils/StylingHelper.java | 33 ++++++++++++------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/text/QuoteSpan.java b/src/main/java/eu/siacs/conversations/ui/text/QuoteSpan.java index 272d794ea..0757ad5de 100644 --- a/src/main/java/eu/siacs/conversations/ui/text/QuoteSpan.java +++ b/src/main/java/eu/siacs/conversations/ui/text/QuoteSpan.java @@ -2,6 +2,7 @@ package eu.siacs.conversations.ui.text; import android.graphics.Canvas; import android.graphics.Paint; +import android.support.annotation.ColorInt; import android.text.Layout; import android.text.TextPaint; import android.text.style.CharacterStyle; @@ -49,4 +50,9 @@ public class QuoteSpan extends CharacterStyle implements LeadingMarginSpan { p.setStyle(style); p.setColor(color); } + + @ColorInt + public int getColor() { + return this.color; + } } \ No newline at end of file diff --git a/src/main/java/eu/siacs/conversations/utils/StylingHelper.java b/src/main/java/eu/siacs/conversations/utils/StylingHelper.java index 2e434cebe..bec06d832 100644 --- a/src/main/java/eu/siacs/conversations/utils/StylingHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/StylingHelper.java @@ -45,6 +45,8 @@ import android.widget.EditText; import java.util.Arrays; import java.util.List; +import eu.siacs.conversations.ui.text.QuoteSpan; + public class StylingHelper { private static List> SPAN_CLASSES = Arrays.asList( @@ -56,24 +58,18 @@ public class StylingHelper { public static void clear(final Editable editable) { final int end = editable.length() - 1; - for(Class clazz : SPAN_CLASSES) { + for (Class clazz : SPAN_CLASSES) { for (ParcelableSpan span : editable.getSpans(0, end, clazz)) { editable.removeSpan(span); } } } - 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) - ); - for(ImStyleParser.Style style : ImStyleParser.parse(editable)) { + 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; @@ -113,7 +122,7 @@ public class StylingHelper { @Override public void afterTextChanged(Editable editable) { clear(editable); - format(editable,mEditText.getCurrentTextColor()); + format(editable, mEditText.getCurrentTextColor()); } } }