From 6058dd11e584613aa90acff350f40073dc028641 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 3 Oct 2020 14:17:13 +0200 Subject: [PATCH] Align blocks (2) --- .../java/eu/faircode/email/StyleHelper.java | 79 +++++++++++-------- 1 file changed, 46 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/StyleHelper.java b/app/src/main/java/eu/faircode/email/StyleHelper.java index dc2f434127..d30bf492c1 100644 --- a/app/src/main/java/eu/faircode/email/StyleHelper.java +++ b/app/src/main/java/eu/faircode/email/StyleHelper.java @@ -22,6 +22,7 @@ import android.text.style.StyleSpan; import android.text.style.TypefaceSpan; import android.text.style.URLSpan; import android.text.style.UnderlineSpan; +import android.util.Pair; import android.view.MenuItem; import android.view.SubMenu; import android.view.View; @@ -208,7 +209,11 @@ public class StyleHelper { } private boolean setAlignment(MenuItem item) { - AlignmentSpan[] spans = t.getSpans(s, e, AlignmentSpan.class); + Pair paragraph = ensureParagraph(t, s, e); + int start = paragraph.first; + int end = paragraph.second; + + AlignmentSpan[] spans = t.getSpans(start, end, AlignmentSpan.class); for (AlignmentSpan span : spans) t.removeSpan(span); @@ -227,10 +232,10 @@ public class StyleHelper { } if (alignment != null) - t.setSpan(new AlignmentSpan.Standard(alignment), s, e, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + t.setSpan(new AlignmentSpan.Standard(alignment), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); etBody.setText(t); - etBody.setSelection(s, e); + etBody.setSelection(start, end); return true; } @@ -246,36 +251,9 @@ public class StyleHelper { int message_zoom = prefs.getInt("message_zoom", 100); float textSize = Helper.getTextSize(context, 0) * message_zoom / 100f; - int start = s; - int end = e; - - // Expand selection at start - while (start > 0 && t.charAt(start - 1) != '\n') - start--; - - // Expand selection at end - while (end > 0 && end < t.length() && t.charAt(end - 1) != '\n') - end++; - - // Nothing to do - if (start == end) - return false; - - // Create paragraph at start - if (start == 0 && t.charAt(start) != '\n') { - t.insert(0, "\n"); - start++; - end++; - } - - // Create paragraph at end - if (end == t.length() && t.charAt(end - 1) != '\n') { - t.append("\n"); - end++; - } - - if (end == t.length()) - t.append("\n"); // workaround Android bug + Pair paragraph = ensureParagraph(t, s, e); + int start = paragraph.first; + int end = paragraph.second; // Remove existing bullets BulletSpan[] spans = t.getSpans(start, end, BulletSpan.class); @@ -375,6 +353,41 @@ public class StyleHelper { return true; } + + private Pair ensureParagraph(SpannableStringBuilder t, int s, int e) { + int start = s; + int end = e; + + // Expand selection at start + while (start > 0 && t.charAt(start - 1) != '\n') + start--; + + // Expand selection at end + while (end > 0 && end < t.length() && t.charAt(end - 1) != '\n') + end++; + + // Nothing to do + if (start == end) + return null; + + // Create paragraph at start + if (start == 0 && t.charAt(start) != '\n') { + t.insert(0, "\n"); + start++; + end++; + } + + // Create paragraph at end + if (end == t.length() && t.charAt(end - 1) != '\n') { + t.append("\n"); + end++; + } + + if (end == t.length()) + t.append("\n"); // workaround Android bug + + return new Pair(start, end); + } }); popupMenu.show();