From bb03cb889eb513826d6feb143cf4b02002da21de Mon Sep 17 00:00:00 2001 From: artdeell Date: Fri, 22 Nov 2024 12:17:13 +0300 Subject: [PATCH] Feat[color_selector]: rewrite to be a SideDialogView --- .../src/main/java/com/kdt/SideDialogView.java | 3 +- .../colorselector/ColorSelector.java | 73 ++++++++++--------- .../handleview/EditControlSideDialog.java | 53 ++------------ 3 files changed, 45 insertions(+), 84 deletions(-) diff --git a/app_pojavlauncher/src/main/java/com/kdt/SideDialogView.java b/app_pojavlauncher/src/main/java/com/kdt/SideDialogView.java index 6b7a2ce06d..5570295105 100644 --- a/app_pojavlauncher/src/main/java/com/kdt/SideDialogView.java +++ b/app_pojavlauncher/src/main/java/com/kdt/SideDialogView.java @@ -11,7 +11,6 @@ import android.view.View; import android.view.ViewGroup; import android.view.animation.AccelerateDecelerateInterpolator; -import android.view.animation.Interpolator; import android.widget.Button; import android.widget.TextView; @@ -218,7 +217,7 @@ public void onAnimationEnd(Animator animation) { } /** @return Whether the dialog is currently displaying */ - protected final boolean isDisplaying(){ + public final boolean isDisplaying(){ return mDisplaying; } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/ColorSelector.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/ColorSelector.java index dd614af164..f94b8351ad 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/ColorSelector.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/ColorSelector.java @@ -5,76 +5,79 @@ import android.graphics.Color; import android.text.Editable; import android.text.TextWatcher; -import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import androidx.annotation.Nullable; +import com.kdt.SideDialogView; + import net.kdt.pojavlaunch.R; -public class ColorSelector implements HueSelectionListener, RectangleSelectionListener, AlphaSelectionListener, TextWatcher{ +public class ColorSelector extends SideDialogView implements HueSelectionListener, RectangleSelectionListener, AlphaSelectionListener, TextWatcher{ private static final int ALPHA_MASK = ~(0xFF << 24); - private final View mRootView; - private final HueView mHueView; - private final SVRectangleView mLuminosityIntensityView; - private final AlphaView mAlphaView; - private final ColorSideBySideView mColorView; - private final EditText mTextView; + private HueView mHueView; + private SVRectangleView mLuminosityIntensityView; + private AlphaView mAlphaView; + private ColorSideBySideView mColorView; + private EditText mTextView; private ColorSelectionListener mColorSelectionListener; private final float[] mHueTemplate = new float[] {0,1,1}; private final float[] mHsvSelected = new float[] {360,1,1}; private int mAlphaSelected = 0xff; - private final ColorStateList mTextColors; + private ColorStateList mTextColors; private boolean mWatch = true; private boolean mAlphaEnabled = true; - /** - * Creates a color selector dialog for this Context. - * @param context Context used for this ColorSelector dialog - * @param colorSelectionListener Color selection listener to which the events will be sent to. Can be null. - */ public ColorSelector(Context context, ViewGroup parent, @Nullable ColorSelectionListener colorSelectionListener) { + super(context, parent, R.layout.dialog_color_selector); + mColorSelectionListener = colorSelectionListener; + } - mRootView = LayoutInflater.from(context).inflate(R.layout.dialog_color_selector,parent, false); - mHueView = mRootView.findViewById(R.id.color_selector_hue_view); - mLuminosityIntensityView = mRootView.findViewById(R.id.color_selector_rectangle_view); - mAlphaView = mRootView.findViewById(R.id.color_selector_alpha_view); - mColorView = mRootView.findViewById(R.id.color_selector_color_view); - mTextView = mRootView.findViewById(R.id.color_selector_hex_edit); + @Override + protected void onInflate() { + super.onInflate(); + // Initialize the view contents + mHueView = mDialogContent.findViewById(R.id.color_selector_hue_view); + mLuminosityIntensityView = mDialogContent.findViewById(R.id.color_selector_rectangle_view); + mAlphaView = mDialogContent.findViewById(R.id.color_selector_alpha_view); + mColorView = mDialogContent.findViewById(R.id.color_selector_color_view); + mTextView = mDialogContent.findViewById(R.id.color_selector_hex_edit); runColor(Color.RED); mHueView.setHueSelectionListener(this); mLuminosityIntensityView.setRectSelectionListener(this); mAlphaView.setAlphaSelectionListener(this); mTextView.addTextChangedListener(this); mTextColors = mTextView.getTextColors(); - - mColorSelectionListener = colorSelectionListener; - - parent.addView(mRootView); - } - - /** @return The root view, mainly for position manipulation purposes */ - public View getRootView(){ - return mRootView; + mAlphaView.setVisibility(mAlphaEnabled ? View.VISIBLE : View.GONE); + + // Set elevation to show above other side dialogs. + // Jank, should be done better + View contentParent = mDialogContent.findViewById(R.id.side_dialog_scrollview); + if(contentParent != null) { + ViewGroup dialogLayout = (ViewGroup) mDialogContent.getParent(); + dialogLayout.setElevation(11); + dialogLayout.setTranslationZ(11); + } } /** * Shows the color selector with the default (red) color selected. */ - public void show() { - show(Color.RED); + public void show(boolean fromRight) { + show(fromRight, Color.RED); } /** * Shows the color selector with the desired ARGB color selected * @param previousColor the desired ARGB color */ - public void show(int previousColor) { + public void show(boolean fromRight, int previousColor) { + appear(fromRight); runColor(previousColor); // initialize dispatchColorChange(); // set the hex text } @@ -157,8 +160,10 @@ public void setColorSelectionListener(ColorSelectionListener listener){ public void setAlphaEnabled(boolean alphaEnabled){ mAlphaEnabled = alphaEnabled; - mAlphaView.setVisibility(alphaEnabled ? View.VISIBLE : View.GONE); - mAlphaView.setAlpha(255); + if(mAlphaView != null) { + mAlphaView.setVisibility(alphaEnabled ? View.VISIBLE : View.GONE); + mAlphaView.setAlpha(255); + } } private void notifyColorSelector(int color){ diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/handleview/EditControlSideDialog.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/handleview/EditControlSideDialog.java index f2b9c684ff..14bf4dde4f 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/handleview/EditControlSideDialog.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/handleview/EditControlSideDialog.java @@ -5,14 +5,12 @@ import static net.kdt.pojavlaunch.Tools.currentDisplayMetrics; -import android.animation.ObjectAnimator; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Color; import android.util.Log; import android.view.View; import android.view.ViewGroup; -import android.view.animation.AccelerateDecelerateInterpolator; import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.EditText; @@ -21,8 +19,6 @@ import android.widget.Switch; import android.widget.TextView; -import androidx.constraintlayout.widget.ConstraintLayout; - import com.kdt.SideDialogView; import net.kdt.pojavlaunch.EfficientAndroidLWJGLKeycode; @@ -82,8 +78,6 @@ public void onLayoutChange(View v, int left, int top, int right, int bottom, int // Color selector related stuff private ColorSelector mColorSelector; private final ViewGroup mParent; - private boolean mDisplayingColor = false; - private ObjectAnimator mColorEditorAnimator; public EditControlSideDialog(Context context, ViewGroup parent) { super(context, parent, R.layout.dialog_control_button_setting); @@ -100,58 +94,25 @@ protected void onInflate() { @Override protected void onDestroy() { - mParent.removeView(mColorSelector.getRootView()); + mColorSelector.disappear(true); } - /* While the selector could be retrofitted to side dialog, it's not worth the effort */ private void buildColorSelector() { mColorSelector = new ColorSelector(mParent.getContext(), mParent, null); - mColorSelector.getRootView().setElevation(11); - mColorSelector.getRootView().setTranslationZ(11); - mColorSelector.getRootView().setX(-mParent.getResources().getDimensionPixelOffset(R.dimen._280sdp)); - - mColorEditorAnimator = ObjectAnimator.ofFloat(mColorSelector.getRootView(), "x", 0).setDuration(600); - mColorEditorAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); } /** * Slide the layout into the visible screen area */ public void appearColor(boolean fromRight, int color) { - if (fromRight) { - if (!mDisplayingColor || !isAtRightColor()) { - mColorEditorAnimator.setFloatValues(currentDisplayMetrics.widthPixels, currentDisplayMetrics.widthPixels - mDialogContent.getWidth() - mMargin); - mColorEditorAnimator.start(); - } - } else { - if (!mDisplayingColor || isAtRightColor()) { - mColorEditorAnimator.setFloatValues(-mDialogContent.getWidth(), mMargin); - mColorEditorAnimator.start(); - } - } - - // Adjust the color selector to have the same size as the control settings - ViewGroup.LayoutParams params = mColorSelector.getRootView().getLayoutParams(); - params.height = ((ViewGroup)mDialogContent.getParent()).getHeight(); - mColorSelector.getRootView().setLayoutParams(params); - - mDisplayingColor = true; - mColorSelector.show(color == -1 ? Color.WHITE : color); + mColorSelector.show(fromRight, color == -1 ? Color.WHITE : color); } /** * Slide out the layout */ public void disappearColor() { - if (!mDisplayingColor) return; - - mDisplayingColor = false; - if (isAtRight()) - mColorEditorAnimator.setFloatValues(currentDisplayMetrics.widthPixels - mDialogContent.getWidth() - mMargin, currentDisplayMetrics.widthPixels); - else - mColorEditorAnimator.setFloatValues(mMargin, -mDialogContent.getWidth()); - - mColorEditorAnimator.start(); + mColorSelector.disappear(false); } /** @@ -160,7 +121,7 @@ public void disappearColor() { * @return True if the last layer is disappearing */ public boolean disappearLayer() { - if (mDisplayingColor) { + if (mColorSelector.isDisplaying()) { disappearColor(); return false; } else { @@ -176,7 +137,7 @@ public void adaptPanelPosition() { if (mDisplaying) { boolean isAtRight = mCurrentlyEditedButton.getControlView().getX() + mCurrentlyEditedButton.getControlView().getWidth() / 2f < currentDisplayMetrics.widthPixels / 2f; appear(isAtRight); - if (mDisplayingColor) { + if (mColorSelector.isDisplaying()) { Tools.runOnUiThread(() -> appearColor(isAtRight, mCurrentlyEditedButton.getProperties().bgColor)); } } @@ -515,10 +476,6 @@ private float safeParseFloat(String string) { return out; } - private boolean isAtRightColor() { - return mColorSelector.getRootView().getX() > currentDisplayMetrics.widthPixels / 2f; - } - public void setCurrentlyEditedButton(ControlInterface button) { if (mCurrentlyEditedButton != null) mCurrentlyEditedButton.getControlView().removeOnLayoutChangeListener(mLayoutChangedListener);