Moved color selection to style helper

This commit is contained in:
M66B
2020-06-28 23:36:31 +02:00
parent 23eb8b1c97
commit 20a81ac386
8 changed files with 168 additions and 158 deletions

View File

@@ -37,7 +37,6 @@ import android.database.Cursor;
import android.database.MatrixCursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
@@ -275,12 +274,11 @@ public class FragmentCompose extends FragmentBase {
private static final int REQUEST_TAKE_PHOTO = 7;
private static final int REQUEST_RECORD_AUDIO = 8;
private static final int REQUEST_OPENPGP = 9;
private static final int REQUEST_COLOR = 10;
private static final int REQUEST_CONTACT_GROUP = 11;
private static final int REQUEST_ANSWER = 12;
private static final int REQUEST_LINK = 13;
private static final int REQUEST_DISCARD = 14;
private static final int REQUEST_SEND = 15;
private static final int REQUEST_CONTACT_GROUP = 10;
private static final int REQUEST_ANSWER = 11;
private static final int REQUEST_LINK = 12;
private static final int REQUEST_DISCARD = 13;
private static final int REQUEST_SEND = 14;
@Override
public void onCreate(Bundle savedInstanceState) {
@@ -1446,25 +1444,7 @@ public class FragmentCompose extends FragmentBase {
private boolean onActionStyle(int action) {
Log.i("Style action=" + action);
if (action == R.id.menu_color) {
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm != null)
imm.hideSoftInputFromWindow(etBody.getWindowToken(), 0);
Bundle args = new Bundle();
args.putInt("color", Color.TRANSPARENT);
args.putString("title", getString(R.string.title_style_color));
args.putInt("start", etBody.getSelectionStart());
args.putInt("end", etBody.getSelectionEnd());
FragmentDialogColor fragment = new FragmentDialogColor();
fragment.setArguments(args);
fragment.setTargetFragment(FragmentCompose.this, REQUEST_COLOR);
fragment.show(getParentFragmentManager(), "account:color");
return true;
} else
return StyleHelper.apply(action, view.findViewById(action), etBody);
return StyleHelper.apply(action, view.findViewById(action), etBody);
}
private void onActionRecordAudio() {
@@ -1752,10 +1732,6 @@ public class FragmentCompose extends FragmentBase {
if (resultCode == RESULT_OK && data != null)
onAnswerSelected(data.getBundleExtra("args"));
break;
case REQUEST_COLOR:
if (resultCode == RESULT_OK && data != null)
onColorSelected(data.getBundleExtra("args"));
break;
case REQUEST_LINK:
if (resultCode == RESULT_OK && data != null)
onLinkSelected(data.getBundleExtra("args"));
@@ -2764,14 +2740,6 @@ public class FragmentCompose extends FragmentBase {
etBody.getText().insert(etBody.getSelectionStart(), spanned);
}
private void onColorSelected(Bundle args) {
int color = args.getInt("color");
int start = args.getInt("start");
int end = args.getInt("end");
etBody.setSelection(start, end);
StyleHelper.apply(R.id.menu_color, null, etBody, color);
}
private void onLinkSelected(Bundle args) {
String link = args.getString("link");
int start = args.getInt("start");

View File

@@ -1,5 +1,8 @@
package eu.faircode.email;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
@@ -10,13 +13,18 @@ import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import androidx.appcompat.widget.PopupMenu;
import com.flask.colorpicker.ColorPickerView;
import com.flask.colorpicker.builder.ColorPickerClickListener;
import com.flask.colorpicker.builder.ColorPickerDialogBuilder;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
@@ -77,75 +85,158 @@ public class StyleHelper {
return true;
}
case R.id.menu_size: {
case R.id.menu_style: {
final int s = start;
final int e = end;
final SpannableString t = ss;
int order = 1;
PopupMenu popupMenu = new PopupMenu(anchor.getContext(), anchor);
popupMenu.getMenu().add(Menu.NONE, R.string.title_style_size_small, order++, R.string.title_style_size_small);
popupMenu.getMenu().add(Menu.NONE, R.string.title_style_size_medium, order++, R.string.title_style_size_medium);
popupMenu.getMenu().add(Menu.NONE, R.string.title_style_size_large, order++, R.string.title_style_size_large);
popupMenu.getMenu().add(0, R.string.title_style_size_small, order++, R.string.title_style_size_small);
popupMenu.getMenu().add(0, R.string.title_style_size_medium, order++, R.string.title_style_size_medium);
popupMenu.getMenu().add(0, R.string.title_style_size_large, order++, R.string.title_style_size_large);
popupMenu.getMenu().add(1, R.string.title_style_font_cursive, order++, R.string.title_style_font_cursive);
popupMenu.getMenu().add(1, R.string.title_style_font_serif, order++, R.string.title_style_font_serif);
popupMenu.getMenu().add(1, R.string.title_style_font_sans_serif, order++, R.string.title_style_font_sans_serif);
popupMenu.getMenu().add(1, R.string.title_style_font_monospace, order++, R.string.title_style_font_monospace);
popupMenu.getMenu().add(1, R.string.title_style_font_default, order++, R.string.title_style_font_default);
popupMenu.getMenu().add(1, R.string.title_style_color, order++, R.string.title_style_color);
popupMenu.getMenu().add(2, R.string.title_style_font_cursive, order++, R.string.title_style_font_cursive);
popupMenu.getMenu().add(2, R.string.title_style_font_serif, order++, R.string.title_style_font_serif);
popupMenu.getMenu().add(2, R.string.title_style_font_sans_serif, order++, R.string.title_style_font_sans_serif);
popupMenu.getMenu().add(2, R.string.title_style_font_monospace, order++, R.string.title_style_font_monospace);
popupMenu.getMenu().add(3, R.string.title_style_clear, order++, R.string.title_style_clear);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getGroupId() == Menu.NONE) {
RelativeSizeSpan[] spans = t.getSpans(s, e, RelativeSizeSpan.class);
for (RelativeSizeSpan span : spans)
t.removeSpan(span);
switch (item.getGroupId()) {
case 0:
return setSize(item);
case 1:
return setColor(item);
case 2:
return setFont(item);
case 3:
return clear(item);
default:
return false;
}
}
Float size;
if (item.getItemId() == R.string.title_style_size_small)
size = 0.8f;
else if (item.getItemId() == R.string.title_style_size_large)
size = 1.25f;
else
size = null;
private boolean setSize(MenuItem item) {
RelativeSizeSpan[] spans = t.getSpans(s, e, RelativeSizeSpan.class);
for (RelativeSizeSpan span : spans)
t.removeSpan(span);
if (size != null)
t.setSpan(new RelativeSizeSpan(size), s, e, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Float size;
if (item.getItemId() == R.string.title_style_size_small)
size = 0.8f;
else if (item.getItemId() == R.string.title_style_size_large)
size = 1.25f;
else
size = null;
etBody.setText(t);
etBody.setSelection(s, e);
} else {
TypefaceSpan[] spans = t.getSpans(s, e, TypefaceSpan.class);
for (TypefaceSpan span : spans)
t.removeSpan(span);
if (size != null)
t.setSpan(new RelativeSizeSpan(size), s, e, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
String face;
switch (item.getItemId()) {
case R.string.title_style_font_cursive:
face = "cursive";
break;
case R.string.title_style_font_serif:
face = "serif";
break;
case R.string.title_style_font_sans_serif:
face = "sans-serif";
break;
case R.string.title_style_font_monospace:
face = "monospace";
break;
default:
face = null;
}
etBody.setText(t);
etBody.setSelection(s, e);
if (face != null)
t.setSpan(new TypefaceSpan(face), s, e, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return true;
}
etBody.setText(t);
etBody.setSelection(s, e);
private boolean setColor(MenuItem item) {
InputMethodManager imm = (InputMethodManager) etBody.getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm != null)
imm.hideSoftInputFromWindow(etBody.getWindowToken(), 0);
ColorPickerDialogBuilder builder = ColorPickerDialogBuilder
.with(etBody.getContext())
.setTitle(R.string.title_color)
.showColorEdit(true)
.wheelType(ColorPickerView.WHEEL_TYPE.FLOWER)
.density(6)
.lightnessSliderOnly()
.setPositiveButton(android.R.string.ok, new ColorPickerClickListener() {
@Override
public void onClick(DialogInterface dialog, int selectedColor, Integer[] allColors) {
_setColor(selectedColor);
}
})
.setNegativeButton(R.string.title_reset, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
_setColor(null);
}
});
Dialog dialog = builder.build();
try {
Field fColorEdit = builder.getClass().getDeclaredField("colorEdit");
fColorEdit.setAccessible(true);
EditText colorEdit = (EditText) fColorEdit.get(builder);
colorEdit.setTextColor(Helper.resolveColor(etBody.getContext(), android.R.attr.textColorPrimary));
} catch (Throwable ex) {
Log.w(ex);
}
return false;
dialog.show();
return true;
}
private void _setColor(Integer color) {
for (ForegroundColorSpan span : t.getSpans(s, e, ForegroundColorSpan.class))
t.removeSpan(span);
if (color != null)
t.setSpan(new ForegroundColorSpan(color), s, e, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
etBody.setText(t);
etBody.setSelection(s, e);
}
private boolean setFont(MenuItem item) {
TypefaceSpan[] spans = t.getSpans(s, e, TypefaceSpan.class);
for (TypefaceSpan span : spans)
t.removeSpan(span);
String face;
switch (item.getItemId()) {
case R.string.title_style_font_cursive:
face = "cursive";
break;
case R.string.title_style_font_serif:
face = "serif";
break;
case R.string.title_style_font_sans_serif:
face = "sans-serif";
break;
case R.string.title_style_font_monospace:
face = "monospace";
break;
default:
face = null;
}
if (face != null)
t.setSpan(new TypefaceSpan(face), s, e, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
etBody.setText(t);
etBody.setSelection(s, e);
return true;
}
private boolean clear(MenuItem item) {
for (Object span : t.getSpans(s, e, Object.class))
if (!(span instanceof ImageSpan))
t.removeSpan(span);
etBody.setText(t);
etBody.setSelection(s, e);
return true;
}
});
@@ -154,18 +245,6 @@ public class StyleHelper {
return true;
}
case R.id.menu_color: {
for (ForegroundColorSpan span : ss.getSpans(start, end, ForegroundColorSpan.class))
ss.removeSpan(span);
ss.setSpan(new ForegroundColorSpan((int) args[0]), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
etBody.setText(ss);
etBody.setSelection(start, end);
return true;
}
case R.id.menu_link: {
String url = (String) args[0];