Lists or idendation

pull/209/head
M66B 2 years ago
parent 58e883869b
commit 55cadb8dce

@ -1029,12 +1029,35 @@ public class FragmentCompose extends FragmentBase {
return true;
} else {
PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(getContext(), getViewLifecycleOwner(), block_bar.findViewById(action));
if (action == R.id.menu_alignment)
popupMenu.inflate(R.menu.popup_style_alignment);
else if (action == R.id.menu_list)
popupMenu.inflate(R.menu.popup_style_list);
else if (action == R.id.menu_indentation)
popupMenu.inflate(R.menu.popup_style_indentation);
Menu menu = popupMenu.getMenu();
Editable edit = etBody.getText();
Pair<Integer, Integer> b = StyleHelper.getParagraph(etBody, true);
BulletSpan[] bullets = (b == null ? null : edit.getSpans(b.first, b.second, BulletSpan.class));
IndentSpan[] indents = (b == null ? null : edit.getSpans(b.first, b.second, IndentSpan.class));
if (b == null ||
(action == R.id.menu_list && indents.length > 0) ||
(action == R.id.menu_indentation && bullets.length > 0)) {
for (int i = 0; i < menu.size(); i++)
menu.getItem(i).setEnabled(false);
} else {
if (action == R.id.menu_list) {
Pair<Integer, Integer> p = StyleHelper.getParagraph(etBody, false);
Integer maxLevel = (p == null ? null : StyleHelper.getMaxListLevel(edit, p.first, p.second));
menu.findItem(R.id.menu_style_list_increase).setEnabled(maxLevel != null);
menu.findItem(R.id.menu_style_list_decrease).setEnabled(maxLevel != null && maxLevel > 0);
} else if (action == R.id.menu_indentation)
popupMenu.getMenu().findItem(R.id.menu_style_indentation_decrease).setEnabled(indents.length > 0);
}
popupMenu.insertIcons(getContext());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override

@ -201,18 +201,15 @@ public class StyleHelper {
smenu.add(R.id.group_style_font_standard, fonts.size(), 0, R.string.title_style_font_default)
.setIntent(new Intent());
int level = -1;
BulletSpan[] spans = edit.getSpans(start, end, BulletSpan.class);
for (BulletSpan span : spans)
if (span instanceof NumberSpan)
level = ((NumberSpan) span).getLevel();
else if (span instanceof BulletSpanEx)
level = ((BulletSpanEx) span).getLevel();
Integer maxLevel = getMaxListLevel(edit, start, end);
IndentSpan[] indents = edit.getSpans(start, end, IndentSpan.class);
popupMenu.getMenu().findItem(R.id.menu_style_list_increase).setVisible(level >= 0);
popupMenu.getMenu().findItem(R.id.menu_style_list_decrease).setVisible(level > 0);
popupMenu.getMenu().findItem(R.id.menu_style_list_bullets).setEnabled(indents.length == 0);
popupMenu.getMenu().findItem(R.id.menu_style_list_numbered).setEnabled(indents.length == 0);
popupMenu.getMenu().findItem(R.id.menu_style_list_increase).setEnabled(indents.length == 0 && maxLevel != null);
popupMenu.getMenu().findItem(R.id.menu_style_list_decrease).setEnabled(indents.length == 0 && maxLevel != null && maxLevel > 0);
IndentSpan[] indents = edit.getSpans(start, end, IndentSpan.class);
popupMenu.getMenu().findItem(R.id.menu_style_indentation_increase).setEnabled(maxLevel == null);
popupMenu.getMenu().findItem(R.id.menu_style_indentation_decrease).setEnabled(indents.length > 0);
popupMenu.getMenu().findItem(R.id.menu_style_parenthesis).setEnabled(BuildConfig.DEBUG);
@ -966,6 +963,21 @@ public class StyleHelper {
throw new IllegalArgumentException(type.getName());
}
static Integer getMaxListLevel(Editable edit, int start, int end) {
Integer maxLevel = null;
BulletSpan[] bullets = edit.getSpans(start, end, BulletSpan.class);
for (BulletSpan span : bullets) {
Integer level = null;
if (span instanceof NumberSpan)
level = ((NumberSpan) span).getLevel();
else if (span instanceof BulletSpanEx)
level = ((BulletSpanEx) span).getLevel();
if (level != null && (maxLevel == null || level > maxLevel))
maxLevel = level;
}
return maxLevel;
}
static void renumber(Editable text, boolean clean, Context context) {
int bulletGap = context.getResources().getDimensionPixelSize(R.dimen.bullet_gap_size);
int bulletIndent = context.getResources().getDimensionPixelSize(R.dimen.bullet_indent_size);

Loading…
Cancel
Save