|
|
|
@ -20,10 +20,14 @@ package eu.faircode.email;
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
|
import android.graphics.Color;
|
|
|
|
|
import android.graphics.Paint;
|
|
|
|
|
import android.graphics.Rect;
|
|
|
|
|
import android.graphics.drawable.ColorDrawable;
|
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
|
import android.text.SpannableStringBuilder;
|
|
|
|
|
import android.text.style.DynamicDrawableSpan;
|
|
|
|
|
import android.text.style.ImageSpan;
|
|
|
|
|
import android.view.Gravity;
|
|
|
|
|
import android.view.Menu;
|
|
|
|
@ -42,6 +46,8 @@ import androidx.lifecycle.LifecycleObserver;
|
|
|
|
|
import androidx.lifecycle.LifecycleOwner;
|
|
|
|
|
import androidx.lifecycle.OnLifecycleEvent;
|
|
|
|
|
|
|
|
|
|
import java.lang.ref.WeakReference;
|
|
|
|
|
|
|
|
|
|
public class PopupMenuLifecycle extends PopupMenu implements LifecycleObserver {
|
|
|
|
|
|
|
|
|
|
public PopupMenuLifecycle(@NonNull Context context, LifecycleOwner owner, @NonNull View anchor) {
|
|
|
|
@ -132,7 +138,7 @@ public class PopupMenuLifecycle extends PopupMenu implements LifecycleObserver {
|
|
|
|
|
|
|
|
|
|
int iconSize = context.getResources().getDimensionPixelSize(R.dimen.menu_item_icon_size);
|
|
|
|
|
icon.setBounds(0, 0, iconSize, iconSize);
|
|
|
|
|
ImageSpan imageSpan = new ImageSpan(icon);
|
|
|
|
|
ImageSpan imageSpan = new CenteredImageSpan(icon);
|
|
|
|
|
|
|
|
|
|
SpannableStringBuilder ssb = new SpannableStringBuilder(menuItem.getTitle());
|
|
|
|
|
ssb.insert(0, "\uFFFC\u2002"); // object replacement character, en space
|
|
|
|
@ -140,4 +146,46 @@ public class PopupMenuLifecycle extends PopupMenu implements LifecycleObserver {
|
|
|
|
|
menuItem.setTitle(ssb);
|
|
|
|
|
menuItem.setIcon(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static class CenteredImageSpan extends ImageSpan {
|
|
|
|
|
public CenteredImageSpan(final Drawable drawable) {
|
|
|
|
|
this(drawable, DynamicDrawableSpan.ALIGN_BOTTOM);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CenteredImageSpan(final Drawable drawable, final int verticalAlignment) {
|
|
|
|
|
super(drawable, verticalAlignment);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void draw(@NonNull Canvas canvas, CharSequence text,
|
|
|
|
|
int start, int end, float x,
|
|
|
|
|
int top, int y, int bottom, @NonNull Paint paint) {
|
|
|
|
|
getDrawable().draw(canvas);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getSize(@NonNull Paint paint, CharSequence text,
|
|
|
|
|
int start, int end,
|
|
|
|
|
@Nullable Paint.FontMetricsInt fm) {
|
|
|
|
|
Drawable d = getDrawable();
|
|
|
|
|
Rect rect = d.getBounds();
|
|
|
|
|
|
|
|
|
|
if (fm != null) {
|
|
|
|
|
int descent = 0;
|
|
|
|
|
int padding = 0;
|
|
|
|
|
if (rect.bottom - (fm.descent - fm.ascent) >= 0) {
|
|
|
|
|
descent = fm.descent;
|
|
|
|
|
padding = rect.bottom - (fm.descent - fm.ascent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fm.descent = padding / 2 + descent;
|
|
|
|
|
fm.bottom = fm.descent;
|
|
|
|
|
|
|
|
|
|
fm.ascent = -rect.bottom + fm.descent;
|
|
|
|
|
fm.top = fm.ascent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rect.right;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|