include/vcl/notebookbar/NotebookBarAddonsMerger.hxx |    1 
 vcl/source/window/NotebookBarAddonsMerger.cxx       |   68 +++++++++++++++++++-
 vcl/source/window/builder.cxx                       |    2 
 3 files changed, 69 insertions(+), 2 deletions(-)

New commits:
commit e4063dc67bd79aa34d23de292b6b462d9339304f
Author:     Dev Lo <[email protected]>
AuthorDate: Wed Sep 24 09:01:01 2025 +0800
Commit:     Adolfo Jayme Barrientos <[email protected]>
CommitDate: Fri Oct 10 08:27:20 2025 +0200

    tdf#146101 Filter Context prop in Notebookbar Extension tab and menu
    
    Shows correct icons in Extension tab, and correct extensions in the
    right-hand dropdown menu
    
    Change-Id: I6902fc3fe380841356d4c5753cb326df62305d11
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/191418
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <[email protected]>
    Reviewed-by: Heiko Tietze <[email protected]>
    (cherry picked from commit f44eca0ace11a195b0516a6cadab9be0a4857331)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/191995
    Reviewed-by: Adolfo Jayme Barrientos <[email protected]>

diff --git a/include/vcl/notebookbar/NotebookBarAddonsMerger.hxx 
b/include/vcl/notebookbar/NotebookBarAddonsMerger.hxx
index 30ab04be2b6e..e2266e2db350 100644
--- a/include/vcl/notebookbar/NotebookBarAddonsMerger.hxx
+++ b/include/vcl/notebookbar/NotebookBarAddonsMerger.hxx
@@ -54,6 +54,7 @@ void MergeNotebookBarAddons(vcl::Window* pParent, const 
VclBuilder::customMakeWi
                             const NotebookBarAddonsItem& 
aNotebookBarAddonsItem,
                             VclBuilder::stringmap& rVec);
 void MergeNotebookBarMenuAddons(Menu* pPopupMenu, sal_Int16 nItemId, const 
OUString& sItemIdName,
+                                const css::uno::Reference<css::frame::XFrame>& 
rFrame,
                                 NotebookBarAddonsItem& aNotebookBarAddonsItem);
 };
 
diff --git a/vcl/source/window/NotebookBarAddonsMerger.cxx 
b/vcl/source/window/NotebookBarAddonsMerger.cxx
index a2576887692c..105a5a5e4ba9 100644
--- a/vcl/source/window/NotebookBarAddonsMerger.cxx
+++ b/vcl/source/window/NotebookBarAddonsMerger.cxx
@@ -29,6 +29,14 @@
 #include <IPrioritable.hxx>
 #include <OptionalBox.hxx>
 
+// #tdf146101: For getting the app context
+#include <com/sun/star/frame/XFrame.hpp>
+#include <com/sun/star/frame/XModuleManager.hpp>
+#include <com/sun/star/frame/ModuleManager.hpp>
+#include <comphelper/processfactory.hxx>
+#include <rtl/ustring.hxx>
+#include <o3tl/string_view.hxx>
+
 const char STYLE_TEXT[] = "Text";
 const char STYLE_ICON[] = "Icon";
 
@@ -40,6 +48,14 @@ const char MERGE_NOTEBOOKBAR_CONTROLTYPE[] = "ControlType";
 const char MERGE_NOTEBOOKBAR_WIDTH[] = "Width";
 const char MERGE_NOTEBOOKBAR_STYLE[] = "Style";
 
+// Get the current app context
+static OUString getCurrentDocumentContext(const 
css::uno::Reference<css::frame::XFrame>& xFrame)
+{
+    css::uno::Reference<css::frame::XModuleManager> xModuleManager
+        = 
css::frame::ModuleManager::create(comphelper::getProcessComponentContext());
+    return xModuleManager->identify(xFrame); // e.g. 
"com.sun.star.text.TextDocument"
+}
+
 static void GetAddonNotebookBarItem(const 
css::uno::Sequence<css::beans::PropertyValue>& pExtension,
                                     AddonNotebookBarItem& 
aAddonNotebookBarItem)
 {
@@ -112,6 +128,7 @@ void MergeNotebookBarAddons(vcl::Window* pParent, const 
VclBuilder::customMakeWi
     std::vector<Image> aImageVec = aNotebookBarAddonsItem.aImageValues;
     tools::ULong nIter = 0;
     sal_uInt16 nPriorityIdx = aImageVec.size();
+
     for (const auto& aExtension : aNotebookBarAddonsItem.aAddonValues)
     {
         for (const auto& pExtension : aExtension)
@@ -130,7 +147,30 @@ void MergeNotebookBarAddons(vcl::Window* pParent, const 
VclBuilder::customMakeWi
 
             AddonNotebookBarItem aAddonNotebookBarItem;
             GetAddonNotebookBarItem(pExtension, aAddonNotebookBarItem);
-
+            // #tdf146101: Filter context
+            if (!aAddonNotebookBarItem.sContext.isEmpty())
+            {
+                OUString currentContext = getCurrentDocumentContext(m_xFrame);
+                bool bMatch = false;
+                std::u16string_view contextView = 
aAddonNotebookBarItem.sContext;
+                sal_Int32 nIndex = 0;
+                do
+                {
+                    // might be multiple contexts, separated by comma
+                    std::u16string_view ctx
+                        = o3tl::trim(o3tl::getToken(contextView, 0, ',', 
nIndex));
+                    if (ctx == currentContext)
+                    {
+                        bMatch = true;
+                        break;
+                    }
+                } while (nIndex != -1);
+                if (!bMatch)
+                {
+                    nIter++; // or icons would be wrong
+                    continue;
+                }
+            }
             CreateNotebookBarToolBox(pNotebookbarToolBox, m_xFrame, 
aAddonNotebookBarItem,
                                      aImageVec, nIter);
             nIter++;
@@ -139,6 +179,7 @@ void MergeNotebookBarAddons(vcl::Window* pParent, const 
VclBuilder::customMakeWi
 }
 
 void MergeNotebookBarMenuAddons(Menu* pPopupMenu, sal_Int16 nItemId, const 
OUString& sItemIdName,
+                                const css::uno::Reference<css::frame::XFrame>& 
m_xFrame,
                                 NotebookBarAddonsItem& aNotebookBarAddonsItem)
 {
     std::vector<Image> aImageVec = aNotebookBarAddonsItem.aImageValues;
@@ -154,6 +195,31 @@ void MergeNotebookBarMenuAddons(Menu* pPopupMenu, 
sal_Int16 nItemId, const OUStr
 
             GetAddonNotebookBarItem(pExtension, aAddonNotebookBarItem);
 
+            // #tdf146101: Filter context
+            if (!aAddonNotebookBarItem.sContext.isEmpty())
+            {
+                OUString currentContext = getCurrentDocumentContext(m_xFrame);
+                bool bMatch = false;
+                std::u16string_view contextView = 
aAddonNotebookBarItem.sContext;
+                sal_Int32 nIndex = 0;
+                do
+                {
+                    // might be multiple contexts, separated by comma
+                    std::u16string_view ctx
+                        = o3tl::trim(o3tl::getToken(contextView, 0, ',', 
nIndex));
+                    if (ctx == currentContext)
+                    {
+                        bMatch = true;
+                        break;
+                    }
+                } while (nIndex != -1);
+                if (!bMatch)
+                {
+                    nIter++; // or icons would be wrong
+                    continue;
+                }
+            }
+
             pPopupMenu->InsertItem(nItemId, aAddonNotebookBarItem.sLabel, 
nBits, sItemIdName);
             pPopupMenu->SetItemCommand(nItemId, 
aAddonNotebookBarItem.sCommandURL);
 
diff --git a/vcl/source/window/builder.cxx b/vcl/source/window/builder.cxx
index b9d56812cad8..4e220954840e 100644
--- a/vcl/source/window/builder.cxx
+++ b/vcl/source/window/builder.cxx
@@ -2999,7 +2999,7 @@ void VclBuilder::insertMenuObject(PopupMenu* pParent, 
PopupMenu* pSubMenu, const
 
     if(rClass == "NotebookBarAddonsMenuMergePoint")
     {
-        NotebookBarAddonsMerger::MergeNotebookBarMenuAddons(pParent, nNewId, 
rID, *m_pNotebookBarAddonsItem);
+        NotebookBarAddonsMerger::MergeNotebookBarMenuAddons(pParent, nNewId, 
rID, m_xFrame, *m_pNotebookBarAddonsItem);
         m_pVclParserState->m_nLastMenuItemId = pParent->GetItemCount();
     }
     else if (rClass == "GtkMenuItem")

Reply via email to