From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: alina sireneva <alina@tei.su>
Date: Thu, 9 Apr 2026 04:17:49 +0300
Subject: disable predictive back, or opt into material3-style one

---
 .../ui/ActionBar/ActionBarLayout.java         | 27 ++++++++++++++-----
 .../java/org/telegram/ui/LaunchActivity.java  | 22 ++++++++++++++-
 2 files changed, 42 insertions(+), 7 deletions(-)

diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ActionBar/ActionBarLayout.java b/TMessagesProj/src/main/java/org/telegram/ui/ActionBar/ActionBarLayout.java
index 0000000..0000000 100644
--- a/TMessagesProj/src/main/java/org/telegram/ui/ActionBar/ActionBarLayout.java
+++ b/TMessagesProj/src/main/java/org/telegram/ui/ActionBar/ActionBarLayout.java
@@ -419,6 +419,9 @@ public class ActionBarLayout extends FrameLayout implements INavigationLayout, F
             if ((passivePreview || transitionAnimationPreviewMode) && (ev.getActionMasked() == MotionEvent.ACTION_DOWN || ev.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN)) {
                 return false;
             }
+            if (inu_m3PredictiveActive && this == containerView) {
+                return false;
+            }
             try {
                 return (!passivePreview || this != containerView) && super.dispatchTouchEvent(ev);
             } catch (Throwable e) {
@@ -966,7 +969,9 @@ public class ActionBarLayout extends FrameLayout implements INavigationLayout, F
                 progress = value / containerView.getMeasuredWidth();
             }
             final BaseFragment prevFragment = fragmentsStack.get(fragmentsStack.size() - 2);
-            prevFragment.onSlideProgress(false, progress);
+            if (!((startedTracking || animationInProgress) && desu.inugram.helpers.theme.Material3NavigationAnimation.applySlideProgress(this, progress))) {
+                prevFragment.onSlideProgress(false, progress);
+            }
             final BaseFragment currFragment = fragmentsStack.get(fragmentsStack.size() - 1);
             float ratio = MathUtils.clamp(2f * progress, 0f, 1f);
             if (currFragment.isBeginToShow()) {
@@ -1097,11 +1102,13 @@ public class ActionBarLayout extends FrameLayout implements INavigationLayout, F
         int clipRight = width + getPaddingLeft();
         int backOffset = 0;
 
+        if (!inu_m3PredictiveActive) {
         if (child == containerViewBack) {
             clipRight = translationX + dp(1);
         } else if (child == containerView) {
             clipLeft = translationX;
         }
+        }
 
         final int restoreCount2 = canvas.save();
         if (child != bottomSheetTabs && bottomSheetTabsClip != null) {
@@ -1288,7 +1295,8 @@ public class ActionBarLayout extends FrameLayout implements INavigationLayout, F
         delegate = INavigationLayoutDelegate;
     }
 
-    private void onSlideAnimationEnd(final boolean backAnimation) {
+    public void onSlideAnimationEnd(final boolean backAnimation) {
+        desu.inugram.helpers.theme.Material3NavigationAnimation.cleanup(this);
         if (!backAnimation) {
             if (fragmentsStack.size() < 2) {
                 checkBlackScreen("onSlideAnimationEnd exit");
@@ -1518,8 +1526,8 @@ public class ActionBarLayout extends FrameLayout implements INavigationLayout, F
         return false;
     }
 
-    private boolean predictiveInput;
-    private boolean predictiveBackInProgress;
+    public boolean predictiveInput;
+    public boolean predictiveBackInProgress;
     private boolean predictiveBackHasProgress;
     private float predictiveBackY;
     private boolean predictiveBackLeft;
@@ -1592,6 +1600,8 @@ public class ActionBarLayout extends FrameLayout implements INavigationLayout, F
         return predictiveBackInProgress && predictiveBackHasProgress;
     }
 
+    public boolean inu_m3PredictiveActive;
+
     private boolean backAnimatorIsBack;
     private AnimatorSet backAnimator;
     private void animateBackEndAnimation(boolean backAnimation) {
@@ -1828,6 +1838,9 @@ public class ActionBarLayout extends FrameLayout implements INavigationLayout, F
                 }
                 lastFrameTime = newTime;
                 float duration = (preview && open ? 190.0f : 150.0f) / desu.inugram.InuConfig.ANIMATION_SPEED.getValue();
+                if (!preview && desu.inugram.helpers.theme.Material3NavigationAnimation.isEnabled()) {
+                    duration = desu.inugram.helpers.theme.Material3NavigationAnimation.DURATION / desu.inugram.InuConfig.ANIMATION_SPEED.getValue();
+                }
                 animationProgress += dt / duration;
                 if (animationProgress > 1.0f) {
                     animationProgress = 1.0f;
@@ -1872,7 +1885,7 @@ public class ActionBarLayout extends FrameLayout implements INavigationLayout, F
                 } else {
                     interpolated = decelerateInterpolator.getInterpolation(animationProgress);
                 }
-                if (open) {
+                if (open && !desu.inugram.helpers.theme.Material3NavigationAnimation.applyOpenFrame(ActionBarLayout.this, animationProgress, preview)) {
                     float clampedInterpolated = MathUtils.clamp(interpolated, 0, 1);
                     containerView.setAlpha(clampedInterpolated);
                     if (preview) {
@@ -1891,7 +1904,7 @@ public class ActionBarLayout extends FrameLayout implements INavigationLayout, F
                     } else {
                         containerView.setTranslationX(dp(48) * (1.0f - interpolated));
                     }
-                } else {
+                } else if (!open && !desu.inugram.helpers.theme.Material3NavigationAnimation.applyCloseFrame(ActionBarLayout.this, animationProgress, preview)) {
                     float clampedReverseInterpolated = MathUtils.clamp(1f - interpolated, 0, 1);
                     containerViewBack.setAlpha(clampedReverseInterpolated);
                     if (preview) {
@@ -3186,6 +3199,7 @@ public class ActionBarLayout extends FrameLayout implements INavigationLayout, F
             oldFragment = null;
             Runnable endRunnable = onCloseAnimationEndRunnable;
             onCloseAnimationEndRunnable = null;
+            desu.inugram.helpers.theme.Material3NavigationAnimation.cleanup(this);
             if (endRunnable != null) {
                 endRunnable.run();
             }
@@ -3219,6 +3233,7 @@ public class ActionBarLayout extends FrameLayout implements INavigationLayout, F
             oldFragment = null;
             Runnable endRunnable = onOpenAnimationEndRunnable;
             onOpenAnimationEndRunnable = null;
+            desu.inugram.helpers.theme.Material3NavigationAnimation.cleanup(this);
             endRunnable.run();
             checkNeedRebuild();
         }
diff --git a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java
index 0000000..0000000 100644
--- a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java
+++ b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java
@@ -731,7 +731,27 @@ public class LaunchActivity extends BasePermissionsActivity implements INavigati
         BackupAgent.requestBackup();
 
         RestrictedLanguagesSelectActivity.checkRestrictedLanguages(false);
-        if (Build.VERSION.SDK_INT >= 34) {
+        final int inuPredictiveMode = desu.inugram.InuConfig.PREDICTIVE_BACK_MODE.getValue();
+        if (Build.VERSION.SDK_INT >= 34 && inuPredictiveMode == desu.inugram.InuConfig.PredictiveBackModeItem.MATERIAL3 && actionBarLayout != null) {
+            if (onBackAnimationCallback == null) {
+                onBackAnimationCallback = desu.inugram.helpers.theme.Material3PredictiveBack.createCallback(
+                    this,
+                    actionBarLayout,
+                    () -> {
+                        if (AndroidUtilities.isTablet()) {
+                            onBackPressed();
+                            return;
+                        }
+                        if (!onBackPressed(true)) return;
+                        onBackPressed();
+                    }
+                );
+            }
+            getOnBackInvokedDispatcher().registerOnBackInvokedCallback(
+                OnBackInvokedDispatcher.PRIORITY_DEFAULT,
+                (OnBackAnimationCallback) onBackAnimationCallback
+            );
+        } else if (Build.VERSION.SDK_INT >= 34 && inuPredictiveMode == desu.inugram.InuConfig.PredictiveBackModeItem.STOCK) {
             if (onBackAnimationCallback == null) {
                 onBackAnimationCallback =  new OnBackAnimationCallback() {
                     private AnimationNotificationsLocker locker = new AnimationNotificationsLocker();
