desktop/source/lib/init.cxx              |    5 +++--
 include/sfx2/lokhelper.hxx               |    4 +++-
 libreofficekit/source/gtk/lokdocview.cxx |    4 +---
 sfx2/source/view/lokhelper.cxx           |   21 +++++++++++++++++++++
 4 files changed, 28 insertions(+), 6 deletions(-)

New commits:
commit d14930b30780cecd11f70826e03ddeefb3deb546
Author:     Caolán McNamara <caolan.mcnam...@collabora.com>
AuthorDate: Wed Jul 23 20:20:29 2025 +0100
Commit:     Caolán McNamara <caolan.mcnam...@collabora.com>
CommitDate: Fri Jul 25 14:53:05 2025 +0200

    make doc_getView return the most recent view of the specific document
    
    so we have a viewid associated with the document, which gets key
    events sent to the right one later.
    
    Change-Id: I7005e61de13de8738f56369d385915228a666285
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/188336
    Tested-by: Jenkins
    Reviewed-by: Caolán McNamara <caolan.mcnam...@collabora.com>

diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index bbe6cc7476a7..9b58c752bbfb 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -7038,14 +7038,15 @@ static void doc_setView(SAL_UNUSED_PARAMETER 
LibreOfficeKitDocument* /*pThis*/,
     SfxLokHelper::setView(nId);
 }
 
-static int doc_getView(SAL_UNUSED_PARAMETER LibreOfficeKitDocument* /*pThis*/)
+static int doc_getView(SAL_UNUSED_PARAMETER LibreOfficeKitDocument* pThis)
 {
     comphelper::ProfileZone aZone("doc_getView");
 
     SolarMutexGuard aGuard;
     SetLastExceptionMsg();
 
-    return SfxLokHelper::getCurrentView();
+    LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
+    return SfxLokHelper::getViewId(pDocument->mnDocumentId);
 }
 
 static int doc_getViewsCount(SAL_UNUSED_PARAMETER LibreOfficeKitDocument* 
pThis)
diff --git a/include/sfx2/lokhelper.hxx b/include/sfx2/lokhelper.hxx
index 44099b1858d7..84c7c8e95538 100644
--- a/include/sfx2/lokhelper.hxx
+++ b/include/sfx2/lokhelper.hxx
@@ -109,7 +109,9 @@ public:
     static int getCurrentView();
     /// Get the number of views of the current DocId.
     static std::size_t getViewsCount(int nDocId);
-    /// Get viewIds of views of the current DocId.
+    /// Get the most recently active viewId of the DocId.
+    static int getViewId(int nDocId);
+    /// Get viewIds of views of the DocId.
     static bool getViewIds(int nDocId, int* pArray, size_t nSize);
     /// Set View Blocked for some uno commands
     static void setBlockedCommandList(int nViewId, const char* 
blockedCommandList);
diff --git a/libreofficekit/source/gtk/lokdocview.cxx 
b/libreofficekit/source/gtk/lokdocview.cxx
index 83abc73095ce..fdae962f26a6 100644
--- a/libreofficekit/source/gtk/lokdocview.cxx
+++ b/libreofficekit/source/gtk/lokdocview.cxx
@@ -900,9 +900,7 @@ static gboolean postDocumentLoad(gpointer pData)
 
     std::unique_lock<std::mutex> aGuard(g_aLOKMutex);
     priv->m_pDocument->pClass->initializeForRendering(priv->m_pDocument, 
priv->m_aRenderingArguments.c_str());
-    // This returns the view id of the "current" view, but sadly if you load 
multiple documents that
-    // is apparently not a view showing the most recently loaded document. Not 
much we can do here,
-    // though. If that is fixed, this comment becomes incorrect.
+    // This returns the view id of the most recently used view of the document
     priv->m_nViewId = priv->m_pDocument->pClass->getView(priv->m_pDocument);
     g_aAuthorViews[getAuthorRenderingArgument(priv)] = priv->m_nViewId;
     priv->m_pDocument->pClass->registerCallback(priv->m_pDocument, 
callbackWorker, pLOKDocView);
diff --git a/sfx2/source/view/lokhelper.cxx b/sfx2/source/view/lokhelper.cxx
index 8ba23ebfd091..8a65d172a1fb 100644
--- a/sfx2/source/view/lokhelper.cxx
+++ b/sfx2/source/view/lokhelper.cxx
@@ -260,6 +260,27 @@ std::size_t SfxLokHelper::getViewsCount(int nDocId)
     return n;
 }
 
+// SfxApplication::SetViewFrame_Impl ensures that ViewShells
+// are kept in MRU order
+int SfxLokHelper::getViewId(int nDocId)
+{
+    assert(nDocId != -1 && "Cannot getViewId for invalid DocId -1");
+
+    SfxApplication* pApp = SfxApplication::Get();
+    if (!pApp)
+        return -1;
+
+    SfxViewShell* pViewShell = SfxViewShell::GetFirst();
+    while (pViewShell)
+    {
+        if (pViewShell->GetDocId().get() == nDocId)
+            return pViewShell->GetViewShellId().get();
+        pViewShell = SfxViewShell::GetNext(*pViewShell);
+    }
+
+    return -1;
+}
+
 bool SfxLokHelper::getViewIds(int nDocId, int* pArray, size_t nSize)
 {
     assert(nDocId != -1 && "Cannot getViewsIds for invalid DocId -1");

Reply via email to