Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/PojavLauncher
Path: blob/v3_openjdk/app_pojavlauncher/src/main/java/com/kdt/SideDialogView.java
2129 views
1
package com.kdt;
2
3
import static net.kdt.pojavlaunch.Tools.currentDisplayMetrics;
4
5
import android.animation.Animator;
6
import android.animation.AnimatorListenerAdapter;
7
import android.animation.ObjectAnimator;
8
import android.content.Context;
9
import android.util.Log;
10
import android.view.LayoutInflater;
11
import android.view.View;
12
import android.view.ViewGroup;
13
import android.view.animation.AccelerateDecelerateInterpolator;
14
import android.widget.Button;
15
import android.widget.TextView;
16
17
import androidx.annotation.CallSuper;
18
import androidx.annotation.LayoutRes;
19
import androidx.annotation.NonNull;
20
import androidx.annotation.Nullable;
21
import androidx.annotation.StringRes;
22
import androidx.core.content.res.ResourcesCompat;
23
24
import net.kdt.pojavlaunch.R;
25
import net.kdt.pojavlaunch.Tools;
26
27
/**
28
* The base class for side dialog views
29
* A side dialog is a dialog appearing from one side of the screen
30
*/
31
public abstract class SideDialogView {
32
33
private final ViewGroup mParent;
34
private final @LayoutRes int mLayoutId;
35
private ViewGroup mDialogLayout;
36
private DefocusableScrollView mScrollView;
37
protected View mDialogContent;
38
39
protected final int mMargin;
40
private ObjectAnimator mSideDialogAnimator;
41
protected boolean mDisplaying = false;
42
/* Whether the layout is built */
43
private boolean mIsInstantiated = false;
44
45
/* UI elements */
46
private Button mStartButton, mEndButton;
47
private TextView mTitleTextview;
48
private View mTitleDivider;
49
50
/* Data to store when the UI element has yet to be inflated */
51
private @StringRes int mStartButtonStringId, mEndButtonStringId, mTitleStringId;
52
private View.OnClickListener mStartButtonListener, mEndButtonListener;
53
54
55
public SideDialogView(Context context, ViewGroup parent, @LayoutRes int layoutId) {
56
mMargin = context.getResources().getDimensionPixelOffset(R.dimen._20sdp);
57
mParent = parent;
58
mLayoutId = layoutId;
59
}
60
61
public void setTitle(@StringRes int textId) {
62
mTitleStringId = textId;
63
if (mIsInstantiated) {
64
mTitleTextview.setText(textId);
65
mTitleTextview.setVisibility(View.VISIBLE);
66
mTitleDivider.setVisibility(View.VISIBLE);
67
}
68
}
69
70
public final void setStartButtonListener(@StringRes int textId, @Nullable View.OnClickListener listener) {
71
mStartButtonStringId = textId;
72
mStartButtonListener = listener;
73
if (mIsInstantiated) setButton(mStartButton, textId, listener);
74
}
75
76
public final void setEndButtonListener(@StringRes int textId, @Nullable View.OnClickListener listener) {
77
mEndButtonStringId = textId;
78
mEndButtonListener = listener;
79
if (mIsInstantiated) setButton(mEndButton, textId, listener);
80
}
81
82
private void setButton(@NonNull Button button, @StringRes int textId, @Nullable View.OnClickListener listener) {
83
button.setText(textId);
84
button.setOnClickListener(listener);
85
button.setVisibility(View.VISIBLE);
86
}
87
88
89
private void inflateLayout() {
90
if(mIsInstantiated) {
91
Log.w("SideDialogView", "Layout already inflated");
92
return;
93
}
94
95
// Inflate layouts
96
mDialogLayout = (ViewGroup) LayoutInflater.from(mParent.getContext()).inflate(R.layout.dialog_side_dialog, mParent, false);
97
mScrollView = mDialogLayout.findViewById(R.id.side_dialog_scrollview);
98
mStartButton = mDialogLayout.findViewById(R.id.side_dialog_start_button);
99
mEndButton = mDialogLayout.findViewById(R.id.side_dialog_end_button);
100
mTitleTextview = mDialogLayout.findViewById(R.id.side_dialog_title_textview);
101
mTitleDivider = mDialogLayout.findViewById(R.id.side_dialog_title_divider);
102
103
LayoutInflater.from(mParent.getContext()).inflate(mLayoutId, mScrollView, true);
104
mDialogContent = mScrollView.getChildAt(0);
105
106
// Attach layouts
107
mParent.addView(mDialogLayout);
108
109
mSideDialogAnimator = ObjectAnimator.ofFloat(mDialogLayout, "x", 0).setDuration(600);
110
mSideDialogAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
111
112
mDialogLayout.setElevation(10);
113
mDialogLayout.setTranslationZ(10);
114
115
mDialogLayout.setVisibility(View.VISIBLE);
116
mDialogLayout.setBackground(ResourcesCompat.getDrawable(mDialogLayout.getResources(), R.drawable.background_control_editor, null));
117
118
//TODO offset better according to view width
119
mDialogLayout.setX(-mDialogLayout.getResources().getDimensionPixelOffset(R.dimen._280sdp));
120
mIsInstantiated = true;
121
122
// Set up UI elements
123
if (mTitleStringId != 0) setTitle(mTitleStringId);
124
if (mStartButtonStringId != 0) setStartButtonListener(mStartButtonStringId, mStartButtonListener);
125
if (mEndButtonStringId != 0) setEndButtonListener(mEndButtonStringId, mEndButtonListener);
126
}
127
128
/** Destroy the layout, cleanup variables */
129
private void deflateLayout() {
130
if(!mIsInstantiated) {
131
Log.w("SideDialogView", "Layout not inflated");
132
return;
133
}
134
135
mSideDialogAnimator.removeAllUpdateListeners();
136
mSideDialogAnimator.removeAllListeners();
137
138
mParent.removeView(mDialogLayout);
139
mIsInstantiated = false;
140
141
mScrollView = null;
142
mSideDialogAnimator = null;
143
mDialogLayout = null;
144
mDialogContent = null;
145
mTitleTextview = null;
146
mTitleDivider = null;
147
mStartButton = null;
148
mEndButton = null;
149
}
150
151
152
/**
153
* Slide the layout into the visible screen area
154
*/
155
@CallSuper
156
public final void appear(boolean fromRight) {
157
if (!mIsInstantiated) {
158
inflateLayout();
159
onInflate();
160
}
161
162
// To avoid UI sizing issue when the dialog is not fully inflated
163
onAppear();
164
Tools.runOnUiThread(() -> {
165
if (fromRight) {
166
if (!mDisplaying || !isAtRight()) {
167
mSideDialogAnimator.setFloatValues(currentDisplayMetrics.widthPixels, currentDisplayMetrics.widthPixels - mScrollView.getWidth() - mMargin);
168
mSideDialogAnimator.start();
169
mDisplaying = true;
170
}
171
} else {
172
if (!mDisplaying || isAtRight()) {
173
mSideDialogAnimator.setFloatValues(-mDialogLayout.getWidth(), mMargin);
174
mSideDialogAnimator.start();
175
mDisplaying = true;
176
}
177
}
178
});
179
}
180
181
protected final boolean isAtRight() {
182
return mDialogLayout.getX() > currentDisplayMetrics.widthPixels / 2f;
183
}
184
185
/**
186
* Slide out the layout
187
* @param destroy Whether the layout should be destroyed after disappearing.
188
* Recommended to be true if the layout is not going to be used anymore
189
*/
190
@CallSuper
191
public final void disappear(boolean destroy) {
192
if(!mIsInstantiated) {
193
Log.w("SideDialogView", "Layout not inflated");
194
return;
195
}
196
197
if (!mDisplaying) {
198
if(destroy) {
199
onDisappear();
200
onDestroy();
201
deflateLayout();
202
}
203
return;
204
}
205
206
mDisplaying = false;
207
if (isAtRight())
208
mSideDialogAnimator.setFloatValues(currentDisplayMetrics.widthPixels - mDialogLayout.getWidth() - mMargin, currentDisplayMetrics.widthPixels);
209
else
210
mSideDialogAnimator.setFloatValues(mMargin, -mDialogLayout.getWidth());
211
212
if(destroy) {
213
onDisappear();
214
onDestroy();
215
mSideDialogAnimator.addListener(new AnimatorListenerAdapter() {
216
@Override
217
public void onAnimationEnd(Animator animation) {
218
deflateLayout();
219
}
220
});
221
}
222
223
mSideDialogAnimator.start();
224
}
225
226
/** @return Whether the dialog is currently displaying */
227
public final boolean isDisplaying(){
228
return mDisplaying;
229
}
230
231
/**
232
* Called when the dialog is inflated, ideal for setting up UI elements bindings
233
*/
234
protected void onInflate() {}
235
236
/**
237
* Called after the dialog has appeared
238
*/
239
protected void onAppear() {}
240
241
/**
242
* Called after the dialog has disappeared
243
*/
244
protected void onDisappear() {}
245
246
/**
247
* Called before the dialog gets destroyed (removing views from parent)
248
* Ideal for cleaning up resources
249
*/
250
protected void onDestroy() {}
251
252
253
}
254
255