mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-18 00:59:35 -04:00
Refactor(side dialog): Allow the dialog to remove its views from the layout
This commit is contained in:
parent
b7fa7eb9eb
commit
ff9e5889ee
@ -2,8 +2,11 @@ package com.kdt;
|
|||||||
|
|
||||||
import static net.kdt.pojavlaunch.Tools.currentDisplayMetrics;
|
import static net.kdt.pojavlaunch.Tools.currentDisplayMetrics;
|
||||||
|
|
||||||
|
import android.animation.Animator;
|
||||||
|
import android.animation.AnimatorListenerAdapter;
|
||||||
import android.animation.ObjectAnimator;
|
import android.animation.ObjectAnimator;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
@ -20,65 +23,62 @@ import androidx.annotation.StringRes;
|
|||||||
import androidx.core.content.res.ResourcesCompat;
|
import androidx.core.content.res.ResourcesCompat;
|
||||||
|
|
||||||
import net.kdt.pojavlaunch.R;
|
import net.kdt.pojavlaunch.R;
|
||||||
|
import net.kdt.pojavlaunch.Tools;
|
||||||
|
|
||||||
public class SideDialogView<T extends View> {
|
/**
|
||||||
|
* The base class for side dialog views
|
||||||
|
* A side dialog is a dialog appearing from one side of the screen
|
||||||
|
* @param <T> The type of the content view root
|
||||||
|
*/
|
||||||
|
public abstract class SideDialogView<T extends View> {
|
||||||
|
|
||||||
|
private final ViewGroup mParent;
|
||||||
|
private final @LayoutRes int mLayoutId;
|
||||||
|
private ViewGroup mDialogLayout;
|
||||||
|
private DefocusableScrollView mScrollView;
|
||||||
|
protected T mDialogContent;
|
||||||
|
|
||||||
private final ViewGroup mDialogLayout;
|
|
||||||
private final DefocusableScrollView mScrollView;
|
|
||||||
protected final T mDialogContent;
|
|
||||||
private final int mMargin;
|
private final int mMargin;
|
||||||
private final ObjectAnimator mSideDialogAnimator;
|
private ObjectAnimator mSideDialogAnimator;
|
||||||
private boolean mDisplaying = false;
|
private boolean mDisplaying = false;
|
||||||
|
/* Whether the layout is built */
|
||||||
|
private boolean mIsInstantiated = false;
|
||||||
|
|
||||||
|
/* UI elements */
|
||||||
|
private Button mStartButton, mEndButton;
|
||||||
|
private TextView mTitleTextview;
|
||||||
|
private View mTitleDivider;
|
||||||
|
|
||||||
|
/* Data to store when the UI element has yet to be inflated */
|
||||||
|
private @StringRes int mStartButtonStringId, mEndButtonStringId, mTitleStringId;
|
||||||
|
private View.OnClickListener mStartButtonListener, mEndButtonListener;
|
||||||
|
|
||||||
private final Button mStartButton, mEndButton;
|
|
||||||
private final TextView mTitleTextview;
|
|
||||||
private final View mTitleDivider;
|
|
||||||
|
|
||||||
public SideDialogView(Context context, ViewGroup parent, @LayoutRes int layoutId) {
|
public SideDialogView(Context context, ViewGroup parent, @LayoutRes int layoutId) {
|
||||||
// Inflate layouts
|
|
||||||
mDialogLayout = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.dialog_side_dialog, parent, false);
|
|
||||||
mScrollView = mDialogLayout.findViewById(R.id.side_dialog_scrollview);
|
|
||||||
mStartButton = mDialogLayout.findViewById(R.id.side_dialog_start_button);
|
|
||||||
mEndButton = mDialogLayout.findViewById(R.id.side_dialog_end_button);
|
|
||||||
mTitleTextview = mDialogLayout.findViewById(R.id.side_dialog_title_textview);
|
|
||||||
mTitleDivider = mDialogLayout.findViewById(R.id.side_dialog_title_divider);
|
|
||||||
|
|
||||||
LayoutInflater.from(context).inflate(layoutId, mScrollView, true);
|
|
||||||
mDialogContent = (T) mScrollView.getChildAt(0);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Attach layouts
|
|
||||||
//mScrollView.addView(mDialogContent);
|
|
||||||
parent.addView(mDialogLayout);
|
|
||||||
|
|
||||||
mMargin = context.getResources().getDimensionPixelOffset(R.dimen._20sdp);
|
mMargin = context.getResources().getDimensionPixelOffset(R.dimen._20sdp);
|
||||||
mSideDialogAnimator = ObjectAnimator.ofFloat(mDialogLayout, "x", 0).setDuration(600);
|
mParent = parent;
|
||||||
Interpolator decelerate = new AccelerateDecelerateInterpolator();
|
mLayoutId = layoutId;
|
||||||
mSideDialogAnimator.setInterpolator(decelerate);
|
|
||||||
|
|
||||||
mDialogLayout.setElevation(10);
|
|
||||||
mDialogLayout.setTranslationZ(10);
|
|
||||||
|
|
||||||
mDialogLayout.setVisibility(View.VISIBLE);
|
|
||||||
mDialogLayout.setBackground(ResourcesCompat.getDrawable(mDialogLayout.getResources(), R.drawable.background_control_editor, null));
|
|
||||||
|
|
||||||
//TODO offset better according to view width
|
|
||||||
mDialogLayout.setX(-mDialogLayout.getResources().getDimensionPixelOffset(R.dimen._280sdp));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTitle(@StringRes int textId) {
|
public void setTitle(@StringRes int textId) {
|
||||||
|
mTitleStringId = textId;
|
||||||
|
if (mIsInstantiated) {
|
||||||
mTitleTextview.setText(textId);
|
mTitleTextview.setText(textId);
|
||||||
mTitleTextview.setVisibility(View.VISIBLE);
|
mTitleTextview.setVisibility(View.VISIBLE);
|
||||||
mTitleDivider.setVisibility(View.VISIBLE);
|
mTitleDivider.setVisibility(View.VISIBLE);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public final void setStartButtonListener(@StringRes int textId, @Nullable View.OnClickListener listener) {
|
public final void setStartButtonListener(@StringRes int textId, @Nullable View.OnClickListener listener) {
|
||||||
setButton(mStartButton, textId, listener);
|
mStartButtonStringId = textId;
|
||||||
|
mStartButtonListener = listener;
|
||||||
|
if (mIsInstantiated) setButton(mStartButton, textId, listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void setEndButtonListener(@StringRes int textId, @Nullable View.OnClickListener listener) {
|
public final void setEndButtonListener(@StringRes int textId, @Nullable View.OnClickListener listener) {
|
||||||
setButton(mEndButton, textId, listener);
|
mEndButtonStringId = textId;
|
||||||
|
mEndButtonListener = listener;
|
||||||
|
if (mIsInstantiated) setButton(mEndButton, textId, listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setButton(@NonNull Button button, @StringRes int textId, @Nullable View.OnClickListener listener) {
|
private void setButton(@NonNull Button button, @StringRes int textId, @Nullable View.OnClickListener listener) {
|
||||||
@ -88,30 +88,94 @@ public class SideDialogView<T extends View> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void inflateLayout() {
|
||||||
|
if(mIsInstantiated) {
|
||||||
|
Log.w("SideDialogView", "Layout already inflated");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inflate layouts
|
||||||
|
mDialogLayout = (ViewGroup) LayoutInflater.from(mParent.getContext()).inflate(R.layout.dialog_side_dialog, mParent, false);
|
||||||
|
mScrollView = mDialogLayout.findViewById(R.id.side_dialog_scrollview);
|
||||||
|
mStartButton = mDialogLayout.findViewById(R.id.side_dialog_start_button);
|
||||||
|
mEndButton = mDialogLayout.findViewById(R.id.side_dialog_end_button);
|
||||||
|
mTitleTextview = mDialogLayout.findViewById(R.id.side_dialog_title_textview);
|
||||||
|
mTitleDivider = mDialogLayout.findViewById(R.id.side_dialog_title_divider);
|
||||||
|
|
||||||
|
LayoutInflater.from(mParent.getContext()).inflate(mLayoutId, mScrollView, true);
|
||||||
|
mDialogContent = (T) mScrollView.getChildAt(0);
|
||||||
|
|
||||||
|
// Attach layouts
|
||||||
|
mParent.addView(mDialogLayout);
|
||||||
|
|
||||||
|
mSideDialogAnimator = ObjectAnimator.ofFloat(mDialogLayout, "x", 0).setDuration(600);
|
||||||
|
mSideDialogAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
|
||||||
|
|
||||||
|
mDialogLayout.setElevation(10);
|
||||||
|
mDialogLayout.setTranslationZ(10);
|
||||||
|
|
||||||
|
mDialogLayout.setVisibility(View.VISIBLE);
|
||||||
|
mDialogLayout.setBackground(ResourcesCompat.getDrawable(mDialogLayout.getResources(), R.drawable.background_control_editor, null));
|
||||||
|
|
||||||
|
//TODO offset better according to view width
|
||||||
|
mDialogLayout.setX(-mDialogLayout.getResources().getDimensionPixelOffset(R.dimen._280sdp));
|
||||||
|
mIsInstantiated = true;
|
||||||
|
|
||||||
|
// Set up UI elements
|
||||||
|
if (mTitleStringId != 0) setTitle(mTitleStringId);
|
||||||
|
if (mStartButtonStringId != 0) setStartButtonListener(mStartButtonStringId, mStartButtonListener);
|
||||||
|
if (mEndButtonStringId != 0) setEndButtonListener(mEndButtonStringId, mEndButtonListener);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Destroy the layout, cleanup variables */
|
||||||
|
private void deflateLayout() {
|
||||||
|
if(!mIsInstantiated) {
|
||||||
|
Log.w("SideDialogView", "Layout not inflated");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
mParent.removeView(mDialogLayout);
|
||||||
|
mIsInstantiated = false;
|
||||||
|
|
||||||
|
mScrollView = null;
|
||||||
|
mSideDialogAnimator = null;
|
||||||
|
mDialogLayout = null;
|
||||||
|
mDialogContent = null;
|
||||||
|
mTitleTextview = null;
|
||||||
|
mTitleDivider = null;
|
||||||
|
mStartButton = null;
|
||||||
|
mEndButton = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Slide the layout into the visible screen area
|
* Slide the layout into the visible screen area
|
||||||
* @return Whether the layout position has changed
|
* @return Whether the layout position has changed
|
||||||
*/
|
*/
|
||||||
@CallSuper
|
@CallSuper
|
||||||
public boolean appear(boolean fromRight) {
|
public final void appear(boolean fromRight) {
|
||||||
boolean hasChanged = false;
|
boolean willBuild = !mIsInstantiated;
|
||||||
|
if (!mIsInstantiated) {
|
||||||
|
inflateLayout();
|
||||||
|
}
|
||||||
|
|
||||||
|
// To avoid UI sizing issue when the dialog is not fully inflated
|
||||||
|
Tools.runOnUiThread(() -> {
|
||||||
|
onAppear(willBuild);
|
||||||
|
|
||||||
if (fromRight) {
|
if (fromRight) {
|
||||||
if (!mDisplaying || !isAtRight()) {
|
if (!mDisplaying || !isAtRight()) {
|
||||||
mSideDialogAnimator.setFloatValues(currentDisplayMetrics.widthPixels, currentDisplayMetrics.widthPixels - mScrollView.getWidth() - mMargin);
|
mSideDialogAnimator.setFloatValues(currentDisplayMetrics.widthPixels, currentDisplayMetrics.widthPixels - mScrollView.getWidth() - mMargin);
|
||||||
mSideDialogAnimator.start();
|
mSideDialogAnimator.start();
|
||||||
hasChanged = true;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!mDisplaying || isAtRight()) {
|
if (!mDisplaying || isAtRight()) {
|
||||||
mSideDialogAnimator.setFloatValues(-mDialogLayout.getWidth(), mMargin);
|
mSideDialogAnimator.setFloatValues(-mDialogLayout.getWidth(), mMargin);
|
||||||
mSideDialogAnimator.start();
|
mSideDialogAnimator.start();
|
||||||
hasChanged = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
mDisplaying = true;
|
mDisplaying = true;
|
||||||
return hasChanged;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isAtRight() {
|
private boolean isAtRight() {
|
||||||
@ -120,10 +184,18 @@ public class SideDialogView<T extends View> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Slide out the layout
|
* Slide out the layout
|
||||||
|
* @param destroy Whether the layout should be destroyed after disappearing.
|
||||||
|
* Recommended to be true if the layout is not going to be used anymore
|
||||||
*/
|
*/
|
||||||
@CallSuper
|
@CallSuper
|
||||||
public void disappear() {
|
public final void disappear(boolean destroy) {
|
||||||
if (!mDisplaying) return;
|
if (!mDisplaying) {
|
||||||
|
if(destroy) {
|
||||||
|
onDisappear(true);
|
||||||
|
deflateLayout();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
mDisplaying = false;
|
mDisplaying = false;
|
||||||
if (isAtRight())
|
if (isAtRight())
|
||||||
@ -131,11 +203,36 @@ public class SideDialogView<T extends View> {
|
|||||||
else
|
else
|
||||||
mSideDialogAnimator.setFloatValues(mMargin, -mDialogLayout.getWidth());
|
mSideDialogAnimator.setFloatValues(mMargin, -mDialogLayout.getWidth());
|
||||||
|
|
||||||
|
if(destroy) {
|
||||||
|
onDisappear(true);
|
||||||
|
mSideDialogAnimator.addListener(new AnimatorListenerAdapter() {
|
||||||
|
@Override
|
||||||
|
public void onAnimationEnd(Animator animation) {
|
||||||
|
mSideDialogAnimator.removeListener(this);
|
||||||
|
deflateLayout();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
mSideDialogAnimator.start();
|
mSideDialogAnimator.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return Whether the dialog is currently displaying */
|
/** @return Whether the dialog is currently displaying */
|
||||||
protected boolean isDisplaying(){
|
protected final boolean isDisplaying(){
|
||||||
return mDisplaying;
|
return mDisplaying;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called after the dialog has appeared with an inflated layout
|
||||||
|
* @param hasBuilt Whether the layout has JUST been built. If false, the layout has been built before
|
||||||
|
*/
|
||||||
|
protected abstract void onAppear(boolean hasBuilt);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called after the dialog has disappeared
|
||||||
|
* @param willDestroy Whether the dialog will be destroyed after disappearing
|
||||||
|
*/
|
||||||
|
protected abstract void onDisappear(boolean willDestroy);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -406,7 +406,7 @@ public class MainActivity extends BaseActivity implements ControlButtonMenuListe
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Tools.runOnUiThread(() -> mQuickSettingSideDialog.appear(true));
|
mQuickSettingSideDialog.appear(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void toggleMouse(Context ctx) {
|
public static void toggleMouse(Context ctx) {
|
||||||
|
@ -41,21 +41,20 @@ public abstract class QuickSettingSideDialog extends com.kdt.SideDialogView<Cons
|
|||||||
public QuickSettingSideDialog(Context context, ViewGroup parent) {
|
public QuickSettingSideDialog(Context context, ViewGroup parent) {
|
||||||
super(context, parent, R.layout.dialog_quick_setting);
|
super(context, parent, R.layout.dialog_quick_setting);
|
||||||
setTitle(R.string.quick_setting_title);
|
setTitle(R.string.quick_setting_title);
|
||||||
bindLayout();
|
|
||||||
setupCancelButton();
|
setupCancelButton();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean appear(boolean fromRight) {
|
protected void onAppear(boolean hasBuilt) {
|
||||||
boolean hasChanged = super.appear(fromRight);
|
if (hasBuilt) {
|
||||||
if (hasChanged) setupListeners();
|
bindLayout();
|
||||||
return hasChanged;
|
setupListeners();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void disappear() {
|
protected void onDisappear(boolean willDestroy) {
|
||||||
super.disappear();
|
if (willDestroy) removeListeners();
|
||||||
removeListeners();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void bindLayout() {
|
private void bindLayout() {
|
||||||
@ -192,7 +191,7 @@ public abstract class QuickSettingSideDialog extends com.kdt.SideDialogView<Cons
|
|||||||
|
|
||||||
private void setupCancelButton() {
|
private void setupCancelButton() {
|
||||||
setStartButtonListener(android.R.string.cancel, v -> cancel());
|
setStartButtonListener(android.R.string.cancel, v -> cancel());
|
||||||
setEndButtonListener(android.R.string.ok, v -> disappear());
|
setEndButtonListener(android.R.string.ok, v -> disappear(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Resets all settings to their original values */
|
/** Resets all settings to their original values */
|
||||||
@ -213,7 +212,7 @@ public abstract class QuickSettingSideDialog extends com.kdt.SideDialogView<Cons
|
|||||||
onResolutionChanged();
|
onResolutionChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
disappear();
|
disappear(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Called when the resolution is changed. Use {@link LauncherPreferences#PREF_SCALE_FACTOR} */
|
/** Called when the resolution is changed. Use {@link LauncherPreferences#PREF_SCALE_FACTOR} */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user