make keyword styling work in quotes

This commit is contained in:
Daniel Gultsch 2017-11-07 13:23:49 +01:00
parent 22c37bd430
commit 49b4153fb5
2 changed files with 27 additions and 12 deletions

View File

@ -2,6 +2,7 @@ package eu.siacs.conversations.ui.text;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Paint; import android.graphics.Paint;
import android.support.annotation.ColorInt;
import android.text.Layout; import android.text.Layout;
import android.text.TextPaint; import android.text.TextPaint;
import android.text.style.CharacterStyle; import android.text.style.CharacterStyle;
@ -49,4 +50,9 @@ public class QuoteSpan extends CharacterStyle implements LeadingMarginSpan {
p.setStyle(style); p.setStyle(style);
p.setColor(color); p.setColor(color);
} }
@ColorInt
public int getColor() {
return this.color;
}
} }

View File

@ -45,6 +45,8 @@ import android.widget.EditText;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import eu.siacs.conversations.ui.text.QuoteSpan;
public class StylingHelper { public class StylingHelper {
private static List<? extends Class<? extends ParcelableSpan>> SPAN_CLASSES = Arrays.asList( private static List<? extends Class<? extends ParcelableSpan>> SPAN_CLASSES = Arrays.asList(
@ -56,24 +58,18 @@ public class StylingHelper {
public static void clear(final Editable editable) { public static void clear(final Editable editable) {
final int end = editable.length() - 1; final int end = editable.length() - 1;
for(Class<?extends ParcelableSpan> clazz : SPAN_CLASSES) { for (Class<? extends ParcelableSpan> clazz : SPAN_CLASSES) {
for (ParcelableSpan span : editable.getSpans(0, end, clazz)) { for (ParcelableSpan span : editable.getSpans(0, end, clazz)) {
editable.removeSpan(span); editable.removeSpan(span);
} }
} }
} }
public static void format(final Editable editable, @ColorInt int color) { public static void format(final Editable editable, @ColorInt int textColor) {
final int syntaxColor = Color.argb( for (ImStyleParser.Style style : ImStyleParser.parse(editable)) {
Math.round(Color.alpha(color) * 0.6f),
Color.red(color),
Color.green(color),
Color.blue(color)
);
for(ImStyleParser.Style style : ImStyleParser.parse(editable)) {
editable.setSpan(createSpanForStyle(style), style.getStart() + 1, style.getEnd(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 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); makeKeywordOpaque(editable, style.getStart(), style.getStart() + 1, textColor);
editable.setSpan(new ForegroundColorSpan(syntaxColor),style.getEnd(),style.getEnd()+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 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 { public static class MessageEditorStyler implements TextWatcher {
private final EditText mEditText; private final EditText mEditText;
@ -113,7 +122,7 @@ public class StylingHelper {
@Override @Override
public void afterTextChanged(Editable editable) { public void afterTextChanged(Editable editable) {
clear(editable); clear(editable);
format(editable,mEditText.getCurrentTextColor()); format(editable, mEditText.getCurrentTextColor());
} }
} }
} }