From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: alina sireneva <alina@tei.su>
Date: Tue, 21 Apr 2026 20:37:24 +0300
Subject: avoid running face detect eagerly for every photo, instead do that right when the filters tab is opened, to avoid overhead for the most common case + lazy chromecast init in photoviewer

---
 .../ui/Components/PhotoFilterView.java        | 25 ++++++++
 .../java/org/telegram/ui/PhotoViewer.java     | 58 ++++++++++++++-----
 2 files changed, 67 insertions(+), 16 deletions(-)

diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Components/PhotoFilterView.java b/TMessagesProj/src/main/java/org/telegram/ui/Components/PhotoFilterView.java
index 0000000..0000000 100644
--- a/TMessagesProj/src/main/java/org/telegram/ui/Components/PhotoFilterView.java
+++ b/TMessagesProj/src/main/java/org/telegram/ui/Components/PhotoFilterView.java
@@ -1218,6 +1218,31 @@ public class PhotoFilterView extends FrameLayout implements FilterShaders.Filter
         }
     }
 
+    public void inu_enableSoftenSkin() {
+        if (softenSkinTool != -1) {
+            return;
+        }
+        softenSkinTool = 0;
+        enhanceTool++;
+        exposureTool++;
+        contrastTool++;
+        saturationTool++;
+        warmthTool++;
+        fadeTool++;
+        highlightsTool++;
+        shadowsTool++;
+        vignetteTool++;
+        if (grainTool != -1) grainTool++;
+        sharpenTool++;
+        tintShadowsTool++;
+        tintHighlightsTool++;
+        rowsCount++;
+        if (recyclerListView != null && recyclerListView.getAdapter() != null) {
+            recyclerListView.getAdapter().notifyItemInserted(0);
+            recyclerListView.scrollToPosition(0);
+        }
+    }
+
     public class ToolsAdapter extends RecyclerListView.SelectionAdapter {
 
         private Context mContext;
diff --git a/TMessagesProj/src/main/java/org/telegram/ui/PhotoViewer.java b/TMessagesProj/src/main/java/org/telegram/ui/PhotoViewer.java
index 0000000..0000000 100644
--- a/TMessagesProj/src/main/java/org/telegram/ui/PhotoViewer.java
+++ b/TMessagesProj/src/main/java/org/telegram/ui/PhotoViewer.java
@@ -914,6 +914,7 @@ public class PhotoViewer implements NotificationCenter.NotificationCenterDelegat
     private ActionBarMenuItem deleteItem;
     private ActionBarMenuSubItem castItem;
     private CastMediaRouteButton castItemButton;
+    private boolean inu_castInitialized;
     private LinearLayout itemsLayout;
     private SpeedButtonsLayout chooseSpeedLayout;
     private ChooseDownloadQualityLayout chooseDownloadQualityLayout;
@@ -1992,6 +1993,7 @@ public class PhotoViewer implements NotificationCenter.NotificationCenterDelegat
     }
 
     private int currentImageHasFace;
+    private boolean inu_waitingForFacesOpen;
     private String currentImageFaceKey;
     private PaintingOverlay paintingOverlay;
     private PaintingOverlay leftPaintingOverlay;
@@ -5107,6 +5109,16 @@ public class PhotoViewer implements NotificationCenter.NotificationCenterDelegat
                         dialog.setTextColor(getThemedColor(Theme.key_voipgroup_actionBarItems));
                     }
                 } else if (id == gallery_menu_chromecast) {
+                    if (!inu_castInitialized) {
+                        try {
+                            castItemButton.setRouteSelector(CastContext.getSharedInstance(activityContext).getMergedSelector());
+                            inu_castInitialized = true;
+                        } catch (Exception e) {
+                            FileLog.e(e);
+                            android.widget.Toast.makeText(activityContext, "Chromecast failed: " + e.getLocalizedMessage(), android.widget.Toast.LENGTH_SHORT);
+                            return;
+                        }
+                    }
                     ChromecastController.getInstance().setCurrentMediaAndCastIfNeeded(getCurrentChromecastMedia());
                     castItemButton.performClick();
                 } else if (id == gallery_menu_showall) {
@@ -5828,20 +5840,11 @@ public class PhotoViewer implements NotificationCenter.NotificationCenterDelegat
                 }
             }
         };
-        boolean castAvailable = true;
-        try {
-            castItemButton.setRouteSelector(CastContext.getSharedInstance(activityContext).getMergedSelector());
-        } catch (Exception e) {
-            FileLog.e(e);
-            castAvailable = false;
-        }
         castItemButton.setVisibility(View.INVISIBLE);
-        if (castAvailable) {
-            castItem = videoItem.addSubItem(gallery_menu_chromecast, R.drawable.menu_video_chromecast, getString(R.string.VideoPlayerChromecast));
-            castItem.setEnabledByColor(false, 0xFFFFFFFF, 0xFF73B4EC);
-            castItem.setSelectorColor(0x0fffffff);
-            castItem.addView(castItemButton, 0, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
-        }
+        castItem = videoItem.addSubItem(gallery_menu_chromecast, R.drawable.menu_video_chromecast, getString(R.string.VideoPlayerChromecast));
+        castItem.setEnabledByColor(false, 0xFFFFFFFF, 0xFF73B4EC);
+        castItem.setSelectorColor(0x0fffffff);
+        castItem.addView(castItemButton, 0, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
 
         videoItem.redrawPopup(0xf9222222);
         videoItem.setOnMenuDismiss(byClick -> checkProgress(0, false, false));
@@ -7686,7 +7689,16 @@ public class PhotoViewer implements NotificationCenter.NotificationCenterDelegat
                     return;
                 }
             }
-            switchToEditMode(EDIT_MODE_FILTER);
+            if (inu_waitingForFacesOpen) {
+                return;
+            }
+            if (currentImageFaceKey != null && currentImageFaceKey.equals(centerImage.getImageKey())) {
+                switchToEditMode(EDIT_MODE_FILTER);
+                return;
+            }
+            inu_waitingForFacesOpen = true;
+            detectFaces();
+            AndroidUtilities.runOnUIThread(this::inu_openFilterModeIfWaiting, 250);
         });
         tuneItem.setContentDescription(getString("AccDescrPhotoAdjust", R.string.AccDescrPhotoAdjust));
 
@@ -7772,7 +7784,6 @@ public class PhotoViewer implements NotificationCenter.NotificationCenterDelegat
                 if (paintingOverlay.getVisibility() == View.VISIBLE) {
                     containerView.requestLayout();
                 }
-                detectFaces();
             }
             if (imageReceiver == centerImage && set && placeProvider != null && placeProvider.scaleToFill() && !ignoreDidSetImage && sendPhotoType != SELECT_TYPE_AVATAR && sendPhotoType != SELECT_TYPE_STICKER) {
                 if (!wasLayout) {
@@ -11879,6 +11890,14 @@ public class PhotoViewer implements NotificationCenter.NotificationCenterDelegat
         }
     }
 
+    private void inu_openFilterModeIfWaiting() {
+        if (!inu_waitingForFacesOpen) {
+            return;
+        }
+        inu_waitingForFacesOpen = false;
+        switchToEditMode(EDIT_MODE_FILTER);
+    }
+
     private void detectFaces() {
         if (centerImage.getAnimation() != null || imagesArrLocals.isEmpty() || sendPhotoType == SELECT_TYPE_AVATAR) {
             return;
@@ -11912,6 +11931,10 @@ public class PhotoViewer implements NotificationCenter.NotificationCenterDelegat
                         if (key.equals(imageKey)) {
                             currentImageHasFace = hasFaces ? 1 : 0;
                             currentImageFaceKey = key;
+                            if (hasFaces && photoFilterView != null) {
+                                photoFilterView.inu_enableSoftenSkin();
+                            }
+                            inu_openFilterModeIfWaiting();
                         }
                     });
                 } else {
@@ -11925,6 +11948,7 @@ public class PhotoViewer implements NotificationCenter.NotificationCenterDelegat
                             currentImageHasFace = 2;
                             currentImageFaceKey = key;
                         }
+                        inu_openFilterModeIfWaiting();
                     });
                 }
             } catch (Exception e) {
@@ -16066,7 +16090,9 @@ public class PhotoViewer implements NotificationCenter.NotificationCenterDelegat
                 videoFrameBitmap = null;
             }
         }
-        detectFaces();
+        currentImageHasFace = 0;
+        currentImageFaceKey = null;
+        inu_waitingForFacesOpen = false;
         if (captionEdit != null) {
             long dialogId = 0;
             if (placeProvider != null)
