Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Qol(settings): energy saving, simplified UI, better perf #6390

Merged
merged 9 commits into from
Dec 27, 2024
138 changes: 82 additions & 56 deletions app_pojavlauncher/src/main/java/com/kdt/CustomSeekbar.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.SeekBar;

import androidx.annotation.Nullable;

import net.kdt.pojavlaunch.R;

/**
* Seekbar with ability to handle ranges and increments
*/
Expand All @@ -14,27 +20,67 @@ public class CustomSeekbar extends SeekBar {
private int mIncrement = 1;
private SeekBar.OnSeekBarChangeListener mListener;

/** When using increments, this flag is used to prevent double calls to the listener */
private boolean mInternalChanges = false;
private final OnSeekBarChangeListener mInternalListener = new OnSeekBarChangeListener() {
/** When using increments, this flag is used to prevent double calls to the listener */
private boolean internalChanges = false;
/** Store the previous progress to prevent double calls with increments */
private int previousProgress = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (internalChanges) return;
internalChanges = true;

progress += mMin;
progress = applyIncrement(progress);

if (progress != previousProgress) {
if (mListener != null) {
previousProgress = progress;
mListener.onProgressChanged(seekBar, progress, fromUser);
}
}

// Forces the thumb to snap to the increment
setProgress(progress);
internalChanges = false;
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
if (internalChanges) return;

if (mListener != null) {
mListener.onStartTrackingTouch(seekBar);
}
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (internalChanges) return;
internalChanges = true;

setProgress(seekBar.getProgress());

if (mListener != null) {
mListener.onStopTrackingTouch(seekBar);
}
internalChanges = false;
}
};

public CustomSeekbar(Context context) {
super(context);
setup();
setup(null);
}

public CustomSeekbar(Context context, AttributeSet attrs) {
super(context, attrs);
setup();
setup(attrs);
}

public CustomSeekbar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setup();
}

public CustomSeekbar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
setup();
setup(attrs);
}

public void setIncrement(int increment) {
Expand Down Expand Up @@ -63,10 +109,15 @@ public synchronized int getProgress() {

@Override
public synchronized void setMin(int min) {
super.setMin(min);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
super.setMin(0);
}
mMin = min;
//todo perform something to update the progress ?
}



/**
* Wrapper to allow for a listener to be set around the internal listener
*/
Expand All @@ -75,52 +126,27 @@ public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) {
mListener = l;
}

public void setup() {
super.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
/** Store the previous progress to prevent double calls with increments */
private int previousProgress = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (mInternalChanges) return;
mInternalChanges = true;

progress += mMin;
progress = applyIncrement(progress);

if (progress != previousProgress) {
if (mListener != null) {
previousProgress = progress;
mListener.onProgressChanged(seekBar, progress, fromUser);
}
}

// Forces the thumb to snap to the increment
setProgress(progress);
mInternalChanges = false;
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
if (mInternalChanges) return;

if (mListener != null) {
mListener.onStartTrackingTouch(seekBar);
}
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (mInternalChanges) return;
mInternalChanges = true;

setProgress(seekBar.getProgress());

if (mListener != null) {
mListener.onStopTrackingTouch(seekBar);
}
mInternalChanges = false;
public void setup(@Nullable AttributeSet attrs) {
try (TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.CustomSeekbar)) {
setIncrement(attributes.getInt(R.styleable.CustomSeekbar_seekBarIncrement, 1));
int min = attributes.getInt(R.styleable.CustomSeekbar_android_min, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
super.setMin(0);
}
});
setRange(min, super.getMax());
}

// Due to issues with negative progress when setting up the seekbar
// We need to set a random progress to force the refresh of the thumb
if(super.getProgress() == 0) {
super.setProgress(super.getProgress() + 1);
post(() -> {
super.setProgress(super.getProgress() - 1);
post(() -> super.setOnSeekBarChangeListener(mInternalListener));
});
} else {
super.setOnSeekBarChangeListener(mInternalListener);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ private void loadControls() {
@Override
public void onAttachedToWindow() {
// Post to get the correct display dimensions after layout.
LauncherPreferences.computeNotchSize(this);
mControlLayout.post(()->{
LauncherPreferences.computeNotchSize(this);
Tools.getDisplayMetrics(this);
loadControls();
});
Expand Down
49 changes: 10 additions & 39 deletions app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Process;
import android.provider.DocumentsContract;
import android.provider.OpenableColumns;
import android.util.ArrayMap;
Expand All @@ -40,7 +41,6 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationManagerCompat;
Expand Down Expand Up @@ -513,33 +513,7 @@ public static DisplayMetrics getDisplayMetrics(Activity activity) {
return displayMetrics;
}

@SuppressWarnings("deprecation")
private static void setFullscreenLegacy(Activity activity, boolean fullscreen) {
final View decorView = activity.getWindow().getDecorView();
View.OnSystemUiVisibilityChangeListener visibilityChangeListener = visibility -> {
boolean multiWindowMode = SDK_INT >= 24 && activity.isInMultiWindowMode();
// When in multi-window mode, asking for fullscreen makes no sense (cause the launcher runs in a window)
// So, ignore the fullscreen setting when activity is in multi window mode
if(fullscreen && !multiWindowMode){
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
}else{
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}

};
decorView.setOnSystemUiVisibilityChangeListener(visibilityChangeListener);
visibilityChangeListener.onSystemUiVisibilityChange(decorView.getSystemUiVisibility()); //call it once since the UI state may not change after the call, so the activity wont become fullscreen
}

@RequiresApi(Build.VERSION_CODES.R)
private static void setFullscreenSdk30(Activity activity, boolean fullscreen) {
public static void setFullscreen(Activity activity, boolean fullscreen) {
WindowInsetsControllerCompat windowInsetsController =
WindowCompat.getInsetsController(activity.getWindow(), activity.getWindow().getDecorView());
if (windowInsetsController == null) {
Expand All @@ -555,27 +529,24 @@ private static void setFullscreenSdk30(Activity activity, boolean fullscreen) {
ViewCompat.setOnApplyWindowInsetsListener(
activity.getWindow().getDecorView(),
(view, windowInsets) -> {
if (fullscreen && !activity.isInMultiWindowMode()) {
boolean fullscreenImpl = fullscreen;
if (SDK_INT >= Build.VERSION_CODES.N && activity.isInMultiWindowMode())
fullscreenImpl = false;

if (fullscreenImpl) {
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
activity.getWindow().setDecorFitsSystemWindows(false);
} else {
windowInsetsController.show(WindowInsetsCompat.Type.systemBars());
activity.getWindow().setDecorFitsSystemWindows(true);
}

if(SDK_INT >= Build.VERSION_CODES.R)
activity.getWindow().setDecorFitsSystemWindows(!fullscreenImpl);

return ViewCompat.onApplyWindowInsets(view, windowInsets);
});

}

public static void setFullscreen(Activity activity, boolean fullscreen) {
if (SDK_INT >= Build.VERSION_CODES.R) {
setFullscreenSdk30(activity, fullscreen);
}else {
setFullscreenLegacy(activity, fullscreen);
}
}

public static DisplayMetrics currentDisplayMetrics;

public static void updateWindowSize(Activity activity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import static net.kdt.pojavlaunch.customcontrols.gamepad.GamepadJoystick.DIRECTION_WEST;
import static net.kdt.pojavlaunch.customcontrols.gamepad.GamepadJoystick.isJoystickEvent;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_DEADZONE_SCALE;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_SCALE_FACTOR;
import static net.kdt.pojavlaunch.utils.MCOptionUtils.getMcScale;
import static org.lwjgl.glfw.CallbackBridge.sendKeyPress;
import static org.lwjgl.glfw.CallbackBridge.sendMouseButton;
Expand All @@ -52,9 +53,6 @@

public class Gamepad implements GrabListener, GamepadHandler {

/* Resolution scaler option, allow downsizing a window */
private final float mScaleFactor = LauncherPreferences.DEFAULT_PREF.getInt("resolutionRatio",100)/100f;

/* Sensitivity, adjusted according to screen size */
private final double mSensitivityFactor = (1.4 * (1080f/ currentDisplayMetrics.heightPixels));

Expand Down Expand Up @@ -116,7 +114,7 @@ public void doFrame(long frameTimeNanos) {
mPointerImageView.setImageDrawable(ResourcesCompat.getDrawable(ctx.getResources(), R.drawable.ic_gamepad_pointer, ctx.getTheme()));
mPointerImageView.getDrawable().setFilterBitmap(false);

int size = (int) ((22 * getMcScale()) / mScaleFactor);
int size = (int) ((22 * getMcScale()) / PREF_SCALE_FACTOR);
mPointerImageView.setLayoutParams(new FrameLayout.LayoutParams(size, size));

mMapProvider = mapProvider;
Expand Down Expand Up @@ -155,7 +153,7 @@ public void updateJoysticks(){

public void notifyGUISizeChange(int newSize){
//Change the pointer size to match UI
int size = (int) ((22 * newSize) / mScaleFactor);
int size = (int) ((22 * newSize) / PREF_SCALE_FACTOR);
mPointerImageView.post(() -> mPointerImageView.setLayoutParams(new FrameLayout.LayoutParams(size, size)));

}
Expand Down Expand Up @@ -228,7 +226,7 @@ private void tick(long frameTimeNanos){
if(!isGrabbing){
CallbackBridge.mouseX = MathUtils.clamp(CallbackBridge.mouseX, 0, CallbackBridge.windowWidth);
CallbackBridge.mouseY = MathUtils.clamp(CallbackBridge.mouseY, 0, CallbackBridge.windowHeight);
placePointerView((int) (CallbackBridge.mouseX / mScaleFactor), (int) (CallbackBridge.mouseY/ mScaleFactor));
placePointerView((int) (CallbackBridge.mouseX / PREF_SCALE_FACTOR), (int) (CallbackBridge.mouseY/ PREF_SCALE_FACTOR));
}

//Send the mouse to the game
Expand Down Expand Up @@ -340,7 +338,7 @@ public void onGrabState(boolean isGrabbing) {
placePointerView(CallbackBridge.physicalWidth/2, CallbackBridge.physicalHeight/2);
mPointerImageView.setVisibility(View.VISIBLE);
// Sensitivity in menu is MC and HARDWARE resolution dependent
mMouseSensitivity = 19 * mScaleFactor / mSensitivityFactor;
mMouseSensitivity = 19 * PREF_SCALE_FACTOR / mSensitivityFactor;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public class CustomSeekBarPreference extends SeekBarPreference {
@SuppressLint("PrivateResource")
public CustomSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.SeekBarPreference, defStyleAttr, defStyleRes);
mMin = a.getInt(R.styleable.SeekBarPreference_min, 0);
a.recycle();
try (TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.SeekBarPreference, defStyleAttr, defStyleRes)) {
mMin = a.getInt(R.styleable.SeekBarPreference_min, 0);
}
}

public CustomSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr) {
Expand Down
Loading
Loading