mirror of https://github.com/M66B/FairEmail.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
192 lines
6.5 KiB
192 lines
6.5 KiB
package eu.faircode.email;
|
|
|
|
/*
|
|
This file is part of FairEmail.
|
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
Copyright 2018-2021 by Marcel Bokhorst (M66B)
|
|
*/
|
|
|
|
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;
|
|
import android.view.MenuItem;
|
|
import android.view.SubMenu;
|
|
import android.view.View;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.appcompat.view.ContextThemeWrapper;
|
|
import androidx.appcompat.view.menu.MenuBuilder;
|
|
import androidx.appcompat.view.menu.MenuPopupHelper;
|
|
import androidx.appcompat.widget.PopupMenu;
|
|
import androidx.lifecycle.Lifecycle;
|
|
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) {
|
|
super(new ContextThemeWrapper(context, R.style.popupMenuStyle), anchor);
|
|
Log.i("Instantiate " + this);
|
|
|
|
owner.getLifecycle().addObserver(this);
|
|
}
|
|
|
|
public void insertIcons(Context context) {
|
|
insertIcons(new ContextThemeWrapper(context, R.style.popupMenuStyle), getMenu());
|
|
}
|
|
|
|
@Override
|
|
public void show() {
|
|
Log.i("Show " + this);
|
|
try {
|
|
super.show();
|
|
} catch (Throwable ex) {
|
|
Log.e(ex);
|
|
}
|
|
}
|
|
|
|
public void showWithIcons(Context context, View anchor) {
|
|
MenuPopupHelper menuHelper = new MenuPopupHelper(context, (MenuBuilder) getMenu(), anchor);
|
|
menuHelper.setForceShowIcon(true);
|
|
menuHelper.setGravity(Gravity.END);
|
|
menuHelper.show();
|
|
}
|
|
|
|
@Override
|
|
public void setOnMenuItemClickListener(@Nullable OnMenuItemClickListener listener) {
|
|
super.setOnMenuItemClickListener(new OnMenuItemClickListener() {
|
|
@Override
|
|
public boolean onMenuItemClick(MenuItem item) {
|
|
try {
|
|
// Handle click just before destroy
|
|
return listener.onMenuItemClick(item);
|
|
} catch (Throwable ex) {
|
|
Log.w(ex);
|
|
return false;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
|
public void onDestroy() {
|
|
Log.i("Destroy " + this);
|
|
this.dismiss();
|
|
}
|
|
|
|
static void insertIcons(Context context, Menu menu) {
|
|
for (int i = 0; i < menu.size(); i++) {
|
|
MenuItem item = menu.getItem(i);
|
|
CharSequence title = item.getTitle();
|
|
insertIcon(context, item);
|
|
if (item.hasSubMenu()) {
|
|
SubMenu sub = item.getSubMenu();
|
|
boolean has = false;
|
|
for (int j = 0; j < sub.size(); j++)
|
|
if (sub.getItem(j).getIcon() != null) {
|
|
has = true;
|
|
insertIcons(context, sub);
|
|
break;
|
|
}
|
|
if (has)
|
|
sub.setHeaderTitle(title);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void insertIcon(Context context, MenuItem menuItem) {
|
|
if (menuItem.getItemId() == R.id.menu_zoom)
|
|
return;
|
|
Drawable icon = menuItem.getIcon();
|
|
|
|
if (icon == null)
|
|
icon = new ColorDrawable(Color.TRANSPARENT);
|
|
else {
|
|
int color = Helper.resolveColor(context, R.attr.colorAccent);
|
|
icon.setTint(color);
|
|
if (!menuItem.isEnabled()) {
|
|
icon.mutate();
|
|
icon.setAlpha(Math.round(Helper.LOW_LIGHT * 255));
|
|
}
|
|
}
|
|
|
|
int iconSize = context.getResources().getDimensionPixelSize(R.dimen.menu_item_icon_size);
|
|
icon.setBounds(0, 0, iconSize, iconSize);
|
|
ImageSpan imageSpan = new CenteredImageSpan(icon);
|
|
|
|
SpannableStringBuilder ssb = new SpannableStringBuilder(menuItem.getTitle());
|
|
ssb.insert(0, "\uFFFC\u2002"); // object replacement character, en space
|
|
ssb.setSpan(imageSpan, 0, 1, 0);
|
|
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;
|
|
}
|
|
}
|
|
}
|