Description: Fix zoom resetting on each page load.
Author: Soren Stoutner <soren@stoutner.com>
Forwarded: https://bugreports.qt.io/browse/QTBUG-101030
Last-Update: 2023-02-17
--- a/src/core/web_contents_delegate_qt.cpp
+++ b/src/core/web_contents_delegate_qt.cpp
@@ -320,7 +320,6 @@
             const base::Process &process = renderProcessHost->GetProcess();
             if (process.IsValid()) {
                 m_viewClient->renderProcessPidChanged(process.Pid());
-                m_viewClient->zoomUpdateIsNeeded();
             }
         }
     }
@@ -331,7 +330,6 @@
     if (newHost && newHost->GetWidget() && newHost->GetWidget()->GetView()) {
         auto rwhv = static_cast<RenderWidgetHostViewQt *>(newHost->GetWidget()->GetView());
         m_viewClient->widgetChanged(rwhv->delegate());
-        m_viewClient->zoomUpdateIsNeeded();
     }
 }
 
--- a/src/webenginewidgets/api/qwebenginepage.cpp
+++ b/src/webenginewidgets/api/qwebenginepage.cpp
@@ -190,8 +190,6 @@
 #endif
     if (defaultAudioMuted != adapter->isAudioMuted())
         adapter->setAudioMuted(defaultAudioMuted);
-    if (!qFuzzyCompare(adapter->currentZoomFactor(), defaultZoomFactor))
-        adapter->setZoomFactor(defaultZoomFactor);
     if (view)
         adapter->setVisible(view->isVisible());
 
@@ -247,12 +245,6 @@
     });
 }
 
-void QWebEnginePagePrivate::zoomUpdateIsNeeded()
-{
-    Q_Q(QWebEnginePage);
-    q->setZoomFactor(defaultZoomFactor);
-}
-
 void QWebEnginePagePrivate::recentlyAudibleChanged(bool recentlyAudible)
 {
     Q_Q(QWebEnginePage);
--- a/src/webengine/api/qquickwebengineview.cpp
+++ b/src/webengine/api/qquickwebengineview.cpp
@@ -434,12 +434,6 @@
     updateEditActions();
 }
 
-void QQuickWebEngineViewPrivate::zoomUpdateIsNeeded()
-{
-    Q_Q(QQuickWebEngineView);
-    q->setZoomFactor(m_zoomFactor);
-}
-
 void QQuickWebEngineViewPrivate::recentlyAudibleChanged(bool recentlyAudible)
 {
     Q_Q(QQuickWebEngineView);
--- a/src/webenginewidgets/api/qwebenginepage_p.h
+++ b/src/webenginewidgets/api/qwebenginepage_p.h
@@ -100,7 +100,6 @@
     void loadProgressChanged(int progress) override;
     void didUpdateTargetURL(const QUrl&) override;
     void selectionChanged() override;
-    void zoomUpdateIsNeeded() override;
     void recentlyAudibleChanged(bool recentlyAudible) override;
     void renderProcessPidChanged(qint64 pid) override;
     QRectF viewportRect() const override;
--- a/src/webengine/api/qquickwebengineview_p_p.h
+++ b/src/webengine/api/qquickwebengineview_p_p.h
@@ -109,7 +109,6 @@
     void loadProgressChanged(int progress) override;
     void didUpdateTargetURL(const QUrl&) override;
     void selectionChanged() override;
-    void zoomUpdateIsNeeded() override;
     void recentlyAudibleChanged(bool recentlyAudible) override;
     void renderProcessPidChanged(qint64 pid) override;
     QRectF viewportRect() const override;
--- a/src/core/web_contents_adapter_client.h
+++ b/src/core/web_contents_adapter_client.h
@@ -459,7 +459,6 @@
     virtual void loadProgressChanged(int progress) = 0;
     virtual void didUpdateTargetURL(const QUrl&) = 0;
     virtual void selectionChanged() = 0;
-    virtual void zoomUpdateIsNeeded() = 0;
     virtual void recentlyAudibleChanged(bool recentlyAudible) = 0;
     virtual void renderProcessPidChanged(qint64 pid) = 0;
     virtual QRectF viewportRect() const = 0;
--- a/src/core/web_contents_adapter.cpp
+++ b/src/core/web_contents_adapter.cpp
@@ -1012,9 +1012,14 @@
     content::HostZoomMap *zoomMap = content::HostZoomMap::GetForWebContents(m_webContents.get());
 
     if (zoomMap) {
-        int render_process_id = m_webContents->GetMainFrame()->GetProcess()->GetID();
-        int render_view_id = m_webContents->GetRenderViewHost()->GetRoutingID();
-        zoomMap->SetTemporaryZoomLevel(render_process_id, render_view_id, zoomLevel);
+        // Set the default zoom level.  Chromium uses a host map to remember zoom levels for individual hosts.
+        // Qt WebEngine does not make use of this host map, but all the code that utilizes it still remains
+        // underneath.  The desired behavior in Qt WebEngine is that, once a zoom level has been set on a page,
+        // it will remain until it is set again.  To accomplish this, The default zoom level is set below.
+        // This triggers logic which sets the zoom for the current page.  When the URL of the page changes,
+        // Chromium checks the zoom map (which is empty), and then resets the zoom level to the default set
+        // below, causing the zoom to remain constant.  See <https://bugreports.qt.io/browse/QTBUG-101030>.
+        zoomMap->SetDefaultZoomLevel(zoomLevel);
     }
 }
 
