sfx2/inc/commandpopup/CommandPopup.hxx    |   15 ++++-
 sfx2/source/commandpopup/CommandPopup.cxx |   86 +++++++++++++++++++-----------
 2 files changed, 69 insertions(+), 32 deletions(-)

New commits:
commit 2f9880af7974b9297b48454070c26d2a4c3b63f0
Author:     Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk>
AuthorDate: Wed Aug 4 16:17:46 2021 +0900
Commit:     Christian Lohmaier <lohmaier+libreoff...@googlemail.com>
CommitDate: Tue Aug 10 13:33:36 2021 +0200

    tdf#142532 search string at any position not just start
    
    In the first implementation, we check if the search string is at
    the start of the command string. Mainly this is done to find the
    right command with a better accuracy. The problem with this is
    that it discards other hits where the search word occurs at an
    other position in the command text.
    
    This change adds the command where the search string doesn't occur
    at the start of the command string, but it adds those hits to the
    end of the list, so the best matches are still added at the
    beginning.
    
    Change-Id: I44a15400c84d45b0c8d3b65ec0e1ffee10686e72
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/119962
    Tested-by: Tomaž Vajngerl <qui...@gmail.com>
    Reviewed-by: Tomaž Vajngerl <qui...@gmail.com>
    (cherry picked from commit fc0d2136aee6c1749d780de09df025251703b59e)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/119983
    Reviewed-by: Adolfo Jayme Barrientos <fit...@ubuntu.com>
    Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org>
    Reviewed-by: Christian Lohmaier <lohmaier+libreoff...@googlemail.com>
    Tested-by: Christian Lohmaier <lohmaier+libreoff...@googlemail.com>

diff --git a/sfx2/inc/commandpopup/CommandPopup.hxx 
b/sfx2/inc/commandpopup/CommandPopup.hxx
index 5d1d74aff860..143f0de25adb 100644
--- a/sfx2/inc/commandpopup/CommandPopup.hxx
+++ b/sfx2/inc/commandpopup/CommandPopup.hxx
@@ -23,6 +23,9 @@
 #include <com/sun/star/i18n/XCharacterClassification.hpp>
 #include <com/sun/star/util/XURLTransformer.hpp>
 
+#include <functional>
+#include <unordered_set>
+
 struct CurrentEntry final
 {
     OUString m_aCommandURL;
@@ -56,6 +59,7 @@ private:
     MenuContent m_aMenuContent;
     OUString m_sModuleLongName;
     OUString toLower(OUString const& rString);
+    std::unordered_set<OUString> m_aAdded;
 
 public:
     MenuContentHandler(css::uno::Reference<css::frame::XFrame> const& xFrame);
@@ -67,9 +71,14 @@ public:
                     std::vector<CurrentEntry>& rCommandList);
 
 private:
-    void findInMenuRecursive(MenuContent const& rMenuContent, OUString const& 
rText,
-                             std::unique_ptr<weld::TreeView>& 
rpCommandTreeView,
-                             std::vector<CurrentEntry>& rCommandList);
+    void findInMenuRecursive(
+        MenuContent const& rMenuContent, OUString const& rText,
+        std::unique_ptr<weld::TreeView>& rpCommandTreeView, 
std::vector<CurrentEntry>& rCommandList,
+        std::function<bool(MenuContent const&, OUString const&)> const& 
rSearchCriterium);
+
+    void addCommandIfPossible(MenuContent const& rMenuContent,
+                              std::unique_ptr<weld::TreeView>& 
rpCommandTreeView,
+                              std::vector<CurrentEntry>& rCommandList);
 };
 
 class CommandListBox final
diff --git a/sfx2/source/commandpopup/CommandPopup.cxx 
b/sfx2/source/commandpopup/CommandPopup.cxx
index 518e73a132b1..152e5b9959b4 100644
--- a/sfx2/source/commandpopup/CommandPopup.cxx
+++ b/sfx2/source/commandpopup/CommandPopup.cxx
@@ -107,47 +107,75 @@ void MenuContentHandler::findInMenu(OUString const& rText,
                                     std::unique_ptr<weld::TreeView>& 
rpCommandTreeView,
                                     std::vector<CurrentEntry>& rCommandList)
 {
-    findInMenuRecursive(m_aMenuContent, toLower(rText), rpCommandTreeView, 
rCommandList);
+    m_aAdded.clear();
+
+    OUString aLowerCaseText = toLower(rText);
+
+    auto aTextStartCriterium = [](MenuContent const& rMenuContent, OUString 
const& rSearchText) {
+        return rMenuContent.m_aSearchableMenuLabel.startsWith(rSearchText);
+    };
+
+    findInMenuRecursive(m_aMenuContent, aLowerCaseText, rpCommandTreeView, 
rCommandList,
+                        aTextStartCriterium);
+
+    auto aTextAllCriterium = [](MenuContent const& rMenuContent, OUString 
const& rSearchText) {
+        return rMenuContent.m_aSearchableMenuLabel.indexOf(rSearchText) > 0;
+    };
+
+    findInMenuRecursive(m_aMenuContent, aLowerCaseText, rpCommandTreeView, 
rCommandList,
+                        aTextAllCriterium);
 }
 
-void MenuContentHandler::findInMenuRecursive(MenuContent const& rMenuContent, 
OUString const& rText,
-                                             std::unique_ptr<weld::TreeView>& 
rpCommandTreeView,
-                                             std::vector<CurrentEntry>& 
rCommandList)
+void MenuContentHandler::findInMenuRecursive(
+    MenuContent const& rMenuContent, OUString const& rText,
+    std::unique_ptr<weld::TreeView>& rpCommandTreeView, 
std::vector<CurrentEntry>& rCommandList,
+    std::function<bool(MenuContent const&, OUString const&)> const& 
rSearchCriterium)
 {
     for (MenuContent const& aSubContent : rMenuContent.m_aSubMenuContent)
     {
-        if (aSubContent.m_aSearchableMenuLabel.startsWith(rText))
+        if (rSearchCriterium(aSubContent, rText))
         {
-            OUString sCommandURL = aSubContent.m_aCommandURL;
-            util::URL aCommandURL;
-            aCommandURL.Complete = sCommandURL;
+            addCommandIfPossible(aSubContent, rpCommandTreeView, rCommandList);
+        }
+        findInMenuRecursive(aSubContent, rText, rpCommandTreeView, 
rCommandList, rSearchCriterium);
+    }
+}
+
+void MenuContentHandler::addCommandIfPossible(MenuContent const& rMenuContent,
+                                              std::unique_ptr<weld::TreeView>& 
rpCommandTreeView,
+                                              std::vector<CurrentEntry>& 
rCommandList)
+{
+    if (m_aAdded.find(rMenuContent.m_aFullLabelWithPath) != m_aAdded.end())
+        return;
 
-            m_xURLTransformer->parseStrict(aCommandURL);
+    OUString sCommandURL = rMenuContent.m_aCommandURL;
+    util::URL aCommandURL;
+    aCommandURL.Complete = sCommandURL;
 
-            auto* pViewFrame = SfxViewFrame::Current();
+    if (m_xURLTransformer->parseStrict(aCommandURL))
+    {
+        auto* pViewFrame = SfxViewFrame::Current();
+
+        SfxSlotPool& rSlotPool = SfxSlotPool::GetSlotPool(pViewFrame);
+        const SfxSlot* pSlot = rSlotPool.GetUnoSlot(aCommandURL.Path);
+        if (pSlot)
+        {
+            std::unique_ptr<SfxPoolItem> pState;
+            SfxItemState eState = 
pViewFrame->GetBindings().QueryState(pSlot->GetSlotId(), pState);
 
-            SfxSlotPool& rSlotPool = SfxSlotPool::GetSlotPool(pViewFrame);
-            const SfxSlot* pSlot = rSlotPool.GetUnoSlot(aCommandURL.Path);
-            if (pSlot)
+            if (eState != SfxItemState::DISABLED)
             {
-                std::unique_ptr<SfxPoolItem> pState;
-                SfxItemState eState
-                    = pViewFrame->GetBindings().QueryState(pSlot->GetSlotId(), 
pState);
-
-                if (eState != SfxItemState::DISABLED)
-                {
-                    auto xGraphic
-                        = 
vcl::CommandInfoProvider::GetXGraphicForCommand(sCommandURL, m_xFrame);
-                    rCommandList.emplace_back(sCommandURL, 
aSubContent.m_aTooltip);
-
-                    auto pIter = rpCommandTreeView->make_iterator();
-                    rpCommandTreeView->insert(nullptr, -1, 
&aSubContent.m_aFullLabelWithPath,
-                                              nullptr, nullptr, nullptr, 
false, pIter.get());
-                    rpCommandTreeView->set_image(*pIter, xGraphic);
-                }
+                auto xGraphic
+                    = 
vcl::CommandInfoProvider::GetXGraphicForCommand(sCommandURL, m_xFrame);
+                rCommandList.emplace_back(sCommandURL, 
rMenuContent.m_aTooltip);
+
+                auto pIter = rpCommandTreeView->make_iterator();
+                rpCommandTreeView->insert(nullptr, -1, 
&rMenuContent.m_aFullLabelWithPath, nullptr,
+                                          nullptr, nullptr, false, 
pIter.get());
+                rpCommandTreeView->set_image(*pIter, xGraphic);
+                m_aAdded.insert(rMenuContent.m_aFullLabelWithPath);
             }
         }
-        findInMenuRecursive(aSubContent, rText, rpCommandTreeView, 
rCommandList);
     }
 }
 

Reply via email to