NekoX/TMessagesProj/src/main/java/org/telegram/ui/ActionBar/ActionBarMenu.java

357 lines
13 KiB
Java
Raw Normal View History

/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2019-01-23 18:03:33 +01:00
* Copyright Nikolai Kudashov, 2013-2018.
*/
2014-11-13 21:10:14 +01:00
package org.telegram.ui.ActionBar;
import android.content.Context;
2014-11-17 03:44:57 +01:00
import android.graphics.drawable.Drawable;
import android.view.View;
2017-03-31 01:58:05 +02:00
import android.view.ViewGroup;
import android.widget.LinearLayout;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.AndroidUtilities;
2020-09-30 15:48:47 +02:00
import org.telegram.ui.Adapters.FiltersView;
2021-01-28 15:15:51 +01:00
import org.telegram.ui.Components.RLottieDrawable;
public class ActionBarMenu extends LinearLayout {
2014-11-11 23:16:17 +01:00
protected ActionBar parentActionBar;
2017-03-31 01:58:05 +02:00
protected boolean isActionMode;
2014-11-11 23:16:17 +01:00
public ActionBarMenu(Context context, ActionBar layer) {
super(context);
setOrientation(LinearLayout.HORIZONTAL);
2014-11-11 23:16:17 +01:00
parentActionBar = layer;
}
public ActionBarMenu(Context context) {
super(context);
}
2017-03-31 01:58:05 +02:00
protected void updateItemsBackgroundColor() {
int count = getChildCount();
for (int a = 0; a < count; a++) {
View view = getChildAt(a);
if (view instanceof ActionBarMenuItem) {
view.setBackgroundDrawable(Theme.createSelectorDrawable(isActionMode ? parentActionBar.itemsActionModeBackgroundColor : parentActionBar.itemsBackgroundColor));
}
2017-03-31 01:58:05 +02:00
}
}
protected void updateItemsColor() {
int count = getChildCount();
for (int a = 0; a < count; a++) {
View view = getChildAt(a);
if (view instanceof ActionBarMenuItem) {
((ActionBarMenuItem) view).setIconColor(isActionMode ? parentActionBar.itemsActionModeColor : parentActionBar.itemsColor);
}
}
}
2014-11-17 03:44:57 +01:00
public ActionBarMenuItem addItem(int id, Drawable drawable) {
2019-07-18 15:01:39 +02:00
return addItem(id, 0, null, isActionMode ? parentActionBar.itemsActionModeBackgroundColor : parentActionBar.itemsBackgroundColor, drawable, AndroidUtilities.dp(48), null);
2014-11-17 03:44:57 +01:00
}
public ActionBarMenuItem addItem(int id, int icon) {
2021-09-20 07:54:41 +02:00
return addItem(id, icon, isActionMode ? parentActionBar.itemsActionModeBackgroundColor : parentActionBar.itemsBackgroundColor, null);
}
public ActionBarMenuItem addItem(int id, int icon, Theme.ResourcesProvider resourcesProvider) {
return addItem(id, icon, isActionMode ? parentActionBar.itemsActionModeBackgroundColor : parentActionBar.itemsBackgroundColor, resourcesProvider);
}
2019-07-18 15:01:39 +02:00
public ActionBarMenuItem addItem(int id, CharSequence text) {
return addItem(id, 0, text, isActionMode ? parentActionBar.itemsActionModeBackgroundColor : parentActionBar.itemsBackgroundColor, null, 0, text);
}
2016-04-22 15:49:00 +02:00
public ActionBarMenuItem addItem(int id, int icon, int backgroundColor) {
2021-09-20 07:54:41 +02:00
return addItem(id, icon, backgroundColor, null);
}
public ActionBarMenuItem addItem(int id, int icon, int backgroundColor, Theme.ResourcesProvider resourcesProvider) {
return addItem(id, icon, null, backgroundColor, null, AndroidUtilities.dp(48), null, resourcesProvider);
2014-11-17 03:44:57 +01:00
}
public ActionBarMenuItem addItemWithWidth(int id, int icon, int width) {
2019-07-18 15:01:39 +02:00
return addItem(id, icon, null, isActionMode ? parentActionBar.itemsActionModeBackgroundColor : parentActionBar.itemsBackgroundColor, null, width, null);
2014-11-14 16:40:15 +01:00
}
2019-05-14 14:08:05 +02:00
public ActionBarMenuItem addItemWithWidth(int id, int icon, int width, CharSequence title) {
2019-07-18 15:01:39 +02:00
return addItem(id, icon, null, isActionMode ? parentActionBar.itemsActionModeBackgroundColor : parentActionBar.itemsBackgroundColor, null, width, title);
2019-05-14 14:08:05 +02:00
}
2019-07-18 15:01:39 +02:00
public ActionBarMenuItem addItem(int id, int icon, CharSequence text, int backgroundColor, Drawable drawable, int width, CharSequence title) {
2021-09-20 07:54:41 +02:00
return addItem(id, icon, text, backgroundColor, drawable, width, title, null);
}
public ActionBarMenuItem addItem(int id, int icon, CharSequence text, int backgroundColor, Drawable drawable, int width, CharSequence title, Theme.ResourcesProvider resourcesProvider) {
ActionBarMenuItem menuItem = new ActionBarMenuItem(getContext(), this, backgroundColor, isActionMode ? parentActionBar.itemsActionModeColor : parentActionBar.itemsColor, text != null, resourcesProvider);
menuItem.setTag(id);
2019-07-18 15:01:39 +02:00
if (text != null) {
menuItem.textView.setText(text);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width != 0 ? width : ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.leftMargin = layoutParams.rightMargin = AndroidUtilities.dp(14);
addView(menuItem, layoutParams);
} else {
if (drawable != null) {
2021-01-28 15:15:51 +01:00
if (drawable instanceof RLottieDrawable) {
menuItem.iconView.setAnimation((RLottieDrawable) drawable);
} else {
menuItem.iconView.setImageDrawable(drawable);
}
2019-07-18 15:01:39 +02:00
} else if (icon != 0) {
menuItem.iconView.setImageResource(icon);
}
addView(menuItem, new LinearLayout.LayoutParams(width, ViewGroup.LayoutParams.MATCH_PARENT));
2014-11-17 03:44:57 +01:00
}
2018-08-27 10:33:11 +02:00
menuItem.setOnClickListener(view -> {
ActionBarMenuItem item = (ActionBarMenuItem) view;
if (item.hasSubMenu()) {
if (parentActionBar.actionBarMenuOnItemClick.canOpenMenu()) {
item.toggleSubMenu();
}
2018-08-27 10:33:11 +02:00
} else if (item.isSearchField()) {
parentActionBar.onSearchFieldVisibilityChanged(item.toggleSearch(true));
} else {
onItemClick((Integer) view.getTag());
}
});
2019-05-14 14:08:05 +02:00
if (title != null) {
menuItem.setContentDescription(title);
}
return menuItem;
}
public void hideAllPopupMenus() {
int count = getChildCount();
for (int a = 0; a < count; a++) {
View view = getChildAt(a);
if (view instanceof ActionBarMenuItem) {
2015-07-22 20:56:37 +02:00
((ActionBarMenuItem) view).closeSubMenu();
}
}
}
2019-05-14 14:08:05 +02:00
protected void setPopupItemsColor(int color, boolean icon) {
2019-12-31 14:08:08 +01:00
for (int a = 0, count = getChildCount(); a < count; a++) {
final View view = getChildAt(a);
2017-03-31 01:58:05 +02:00
if (view instanceof ActionBarMenuItem) {
2019-12-31 14:08:08 +01:00
((ActionBarMenuItem) view).setPopupItemsColor(color, icon);
}
}
}
protected void setPopupItemsSelectorColor(int color) {
for (int a = 0, count = getChildCount(); a < count; a++) {
final View view = getChildAt(a);
if (view instanceof ActionBarMenuItem) {
((ActionBarMenuItem) view).setPopupItemsSelectorColor(color);
2017-03-31 01:58:05 +02:00
}
}
}
protected void redrawPopup(int color) {
2019-12-31 14:08:08 +01:00
for (int a = 0, count = getChildCount(); a < count; a++) {
final View view = getChildAt(a);
2017-03-31 01:58:05 +02:00
if (view instanceof ActionBarMenuItem) {
2019-12-31 14:08:08 +01:00
((ActionBarMenuItem) view).redrawPopup(color);
2017-03-31 01:58:05 +02:00
}
}
}
public void onItemClick(int id) {
2014-11-11 23:16:17 +01:00
if (parentActionBar.actionBarMenuOnItemClick != null) {
parentActionBar.actionBarMenuOnItemClick.onItemClick(id);
}
}
public void clearItems() {
2016-04-25 21:26:41 +02:00
removeAllViews();
}
public void onMenuButtonPressed() {
int count = getChildCount();
for (int a = 0; a < count; a++) {
View view = getChildAt(a);
if (view instanceof ActionBarMenuItem) {
2015-07-22 20:56:37 +02:00
ActionBarMenuItem item = (ActionBarMenuItem) view;
if (item.getVisibility() != VISIBLE) {
continue;
}
if (item.hasSubMenu()) {
item.toggleSubMenu();
2014-11-20 15:45:33 +01:00
break;
2015-07-22 20:56:37 +02:00
} else if (item.overrideMenuClick) {
onItemClick((Integer) item.getTag());
break;
}
}
}
}
2017-12-08 18:35:59 +01:00
public void closeSearchField(boolean closeKeyboard) {
int count = getChildCount();
for (int a = 0; a < count; a++) {
View view = getChildAt(a);
if (view instanceof ActionBarMenuItem) {
2015-07-22 20:56:37 +02:00
ActionBarMenuItem item = (ActionBarMenuItem) view;
2020-04-24 11:21:58 +02:00
if (item.isSearchField() && item.isSearchFieldVisible()) {
2019-12-31 14:08:08 +01:00
if (item.listener == null || item.listener.canCollapseSearch()) {
parentActionBar.onSearchFieldVisibilityChanged(false);
item.toggleSearch(closeKeyboard);
}
break;
}
}
}
}
2017-03-31 01:58:05 +02:00
public void setSearchTextColor(int color, boolean placeholder) {
int count = getChildCount();
for (int a = 0; a < count; a++) {
View view = getChildAt(a);
if (view instanceof ActionBarMenuItem) {
ActionBarMenuItem item = (ActionBarMenuItem) view;
if (item.isSearchField()) {
if (placeholder) {
item.getSearchField().setHintTextColor(color);
} else {
item.getSearchField().setTextColor(color);
}
break;
}
}
}
}
2019-07-18 15:01:39 +02:00
public void setSearchFieldText(String text) {
int count = getChildCount();
for (int a = 0; a < count; a++) {
View view = getChildAt(a);
if (view instanceof ActionBarMenuItem) {
ActionBarMenuItem item = (ActionBarMenuItem) view;
if (item.isSearchField()) {
item.setSearchFieldText(text, false);
item.getSearchField().setSelection(text.length());
}
}
}
}
2019-08-22 01:53:26 +02:00
public void onSearchPressed() {
int count = getChildCount();
for (int a = 0; a < count; a++) {
View view = getChildAt(a);
if (view instanceof ActionBarMenuItem) {
ActionBarMenuItem item = (ActionBarMenuItem) view;
if (item.isSearchField()) {
item.onSearchPressed();
}
}
}
}
2019-01-23 18:03:33 +01:00
public void openSearchField(boolean toggle, String text, boolean animated) {
int count = getChildCount();
for (int a = 0; a < count; a++) {
View view = getChildAt(a);
if (view instanceof ActionBarMenuItem) {
2015-07-22 20:56:37 +02:00
ActionBarMenuItem item = (ActionBarMenuItem) view;
if (item.isSearchField()) {
2015-03-26 18:34:47 +01:00
if (toggle) {
2015-09-24 22:52:02 +02:00
parentActionBar.onSearchFieldVisibilityChanged(item.toggleSearch(true));
2015-03-26 18:34:47 +01:00
}
2019-01-23 18:03:33 +01:00
item.setSearchFieldText(text, animated);
item.getSearchField().setSelection(text.length());
break;
}
}
}
}
2020-09-30 15:48:47 +02:00
public void setFilter(FiltersView.MediaFilterData filter) {
int count = getChildCount();
for (int a = 0; a < count; a++) {
View view = getChildAt(a);
if (view instanceof ActionBarMenuItem) {
ActionBarMenuItem item = (ActionBarMenuItem) view;
if (item.isSearchField()) {
item.addSearchFilter(filter);
break;
}
}
}
}
public ActionBarMenuItem getItem(int id) {
View v = findViewWithTag(id);
if (v instanceof ActionBarMenuItem) {
2015-07-22 20:56:37 +02:00
return (ActionBarMenuItem) v;
}
return null;
}
2018-07-30 04:07:02 +02:00
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
int count = getChildCount();
for (int a = 0; a < count; a++) {
View view = getChildAt(a);
view.setEnabled(enabled);
}
}
2021-03-19 11:25:58 +01:00
public int getItemsMeasuredWidth() {
int w = 0;
int count = getChildCount();
for (int a = 0; a < count; a++) {
View view = getChildAt(a);
if (view instanceof ActionBarMenuItem) {
w += view.getMeasuredWidth();
}
}
return w;
}
2021-12-30 11:52:40 +01:00
public int getVisibleItemsMeasuredWidth() {
int w = 0;
for (int i = 0, count = getChildCount(); i < count; i++) {
View view = getChildAt(i);
if (view instanceof ActionBarMenuItem && view.getVisibility() != View.GONE) {
w += view.getMeasuredWidth();
}
}
return w;
}
2021-03-19 11:25:58 +01:00
public boolean searchFieldVisible() {
int count = getChildCount();
for (int a = 0; a < count; a++) {
View view = getChildAt(a);
if (view instanceof ActionBarMenuItem && ((ActionBarMenuItem) view).getSearchContainer() != null && ((ActionBarMenuItem) view).getSearchContainer().getVisibility() == View.VISIBLE) {
return true;
}
}
return false;
}
public void translateXItems(int offset) {
int count = getChildCount();
for (int a = 0; a < count; a++) {
View view = getChildAt(a);
if (view instanceof ActionBarMenuItem) {
((ActionBarMenuItem) view).setTransitionOffset(offset);
}
}
}
2021-06-25 02:43:10 +02:00
public void clearSearchFilters() {
}
}