include/sfx2/notebookbar/SfxNotebookBar.hxx |    9 --
 sfx2/source/notebookbar/SfxNotebookBar.cxx  |   99 ++++++++++++++++++++++------
 2 files changed, 79 insertions(+), 29 deletions(-)

New commits:
commit da8ebd2c348affbd7fa4975910296f32d3c9d1b0
Author:     Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk>
AuthorDate: Mon Jan 29 16:08:48 2024 +0900
Commit:     Szymon Kłos <szymon.k...@collabora.com>
CommitDate: Mon Jan 29 22:09:30 2024 +0100

    lok: introduce NotebookBarViewManager and NotebookBarViewData
    
    NotebookBarViewManager is a singleton which is responsible to hold
    NotebookBar view specific data, which is kept in NotebookBarViewData
    class. The idea is to have one NotebookBarViewData class instance
    per one view (SfxViewShell instance).
    
    This also refactors the existing code and now moves the
    m_pWeldedWrapper, m_pNotebookBar and the m_pToolbarUnoDispatcher
    into NotebookBarViewData class.
    
    Change-Id: I32f5954fa9f1628acd9f5f9bd5760ac23ca687ae
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/162680
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Szymon Kłos <szymon.k...@collabora.com>

diff --git a/include/sfx2/notebookbar/SfxNotebookBar.hxx 
b/include/sfx2/notebookbar/SfxNotebookBar.hxx
index cd94ddad7b1e..47bd9cc79901 100644
--- a/include/sfx2/notebookbar/SfxNotebookBar.hxx
+++ b/include/sfx2/notebookbar/SfxNotebookBar.hxx
@@ -16,7 +16,6 @@
 #include <vcl/WeldedTabbedNotebookbar.hxx>
 #include <vcl/EnumContext.hxx>
 
-#include <map>
 #include <memory>
 #include <string_view>
 
@@ -33,8 +32,7 @@ class SfxBindings;
 class SfxViewFrame;
 class SfxViewShell;
 class SystemWindow;
-class ToolbarUnoDispatcher;
-class WeldedTabbedNotebookbar;
+class ViewInstanceManager;
 
 namespace sfx2
 {
@@ -75,11 +73,6 @@ public:
 private:
     static bool m_bLock;
     static bool m_bHide;
-    static std::unique_ptr<ToolbarUnoDispatcher> m_xCalcToolboxDispatcher;
-
-    static std::map<const SfxViewShell*, 
std::shared_ptr<WeldedTabbedNotebookbar>>
-        m_pNotebookBarWeldedWrapper;
-    static std::map<const SfxViewShell*, VclPtr<NotebookBar>> 
m_pNotebookBarInstance;
 
     static void ResetActiveToolbarModeToDefault(vcl::EnumContext::Application 
eApp);
     static void RemoveCurrentLOKWrapper();
diff --git a/sfx2/source/notebookbar/SfxNotebookBar.cxx 
b/sfx2/source/notebookbar/SfxNotebookBar.cxx
index e1a8cfb56bca..657f97840843 100644
--- a/sfx2/source/notebookbar/SfxNotebookBar.cxx
+++ b/sfx2/source/notebookbar/SfxNotebookBar.cxx
@@ -28,7 +28,7 @@
 #include <framework/addonsoptions.hxx>
 #include <vcl/notebookbar/NotebookBarAddonsMerger.hxx>
 #include <vector>
-#include <map>
+#include <unordered_map>
 #include <vcl/WeldedTabbedNotebookbar.hxx>
 
 using namespace sfx2;
@@ -42,9 +42,68 @@ const char MERGE_NOTEBOOKBAR_URL[] = "URL";
 
 bool SfxNotebookBar::m_bLock = false;
 bool SfxNotebookBar::m_bHide = false;
-std::unique_ptr<ToolbarUnoDispatcher> SfxNotebookBar::m_xCalcToolboxDispatcher;
-std::map<const SfxViewShell*, std::shared_ptr<WeldedTabbedNotebookbar>> 
SfxNotebookBar::m_pNotebookBarWeldedWrapper;
-std::map<const SfxViewShell*, VclPtr<NotebookBar>> 
SfxNotebookBar::m_pNotebookBarInstance;
+
+namespace
+{
+
+/** View specific notebook bar data */
+struct NotebookBarViewData
+{
+    std::unique_ptr<WeldedTabbedNotebookbar> m_pWeldedWrapper;
+    VclPtr<NotebookBar> m_pNotebookBar;
+    std::unique_ptr<ToolbarUnoDispatcher> m_pToolbarUnoDispatcher;
+
+    ~NotebookBarViewData()
+    {
+        if (m_pNotebookBar)
+            m_pNotebookBar.disposeAndClear();
+    }
+};
+
+/** Notebookbar instance manager is a singleton that is used for track the
+ *  per-view instances of view specifc data contained in NotebookBarViewData
+ *  class.
+ **/
+class NotebookBarViewManager final
+{
+private:
+    // map contains a view data instance for a view (SfxViewShell pointer)
+    std::unordered_map<const SfxViewShell*, 
std::unique_ptr<NotebookBarViewData>> m_pViewDataList;
+
+    // private constructor to prevent any other instantiation outside of get() 
method
+    NotebookBarViewManager() = default;
+
+    // prevent class copying
+    NotebookBarViewManager(const NotebookBarViewManager&) = delete;
+    NotebookBarViewManager& operator=(const NotebookBarViewManager&) = delete;
+
+public:
+    // Singleton get method - creates an instance on first get() call
+    static NotebookBarViewManager& get()
+    {
+        static NotebookBarViewManager gManager;
+        return gManager;
+    }
+
+    NotebookBarViewData& getViewData(const SfxViewShell* pViewShell)
+    {
+        auto aFound = m_pViewDataList.find(pViewShell);
+        if (aFound != m_pViewDataList.end()) // found
+            return *aFound->second;
+
+        // Create new view data instance
+        NotebookBarViewData* pViewData = new NotebookBarViewData;
+        m_pViewDataList.emplace(pViewShell, 
std::unique_ptr<NotebookBarViewData>(pViewData));
+        return *pViewData;
+    }
+
+    void removeViewData(const SfxViewShell* pViewShell)
+    {
+        m_pViewDataList.erase(pViewShell);
+    }
+};
+
+} // end anonymous namespace
 
 static void NotebookbarAddonValues(
     std::vector<Image>& aImageValues,
@@ -200,13 +259,13 @@ static utl::OConfigurationNode 
lcl_getCurrentImplConfigNode(const Reference<css:
 void SfxNotebookBar::RemoveCurrentLOKWrapper()
 {
     const SfxViewShell* pViewShell = SfxViewShell::Current();
-    auto aFound = m_pNotebookBarInstance.find(pViewShell);
-    if (aFound != m_pNotebookBarInstance.end())
+    auto& rViewData = NotebookBarViewManager::get().getViewData(pViewShell);
+
+    if (rViewData.m_pNotebookBar)
     {
         // Calls STATIC_LINK SfxNotebookBar -> VclDisposeHdl
-        // which clears also m_pNotebookBarWeldedWrapper
-        aFound->second.disposeAndClear();
-        m_pNotebookBarInstance.erase(aFound);
+        // which clears the whole InstanceManager
+        rViewData.m_pNotebookBar.disposeAndClear();
     }
 }
 
@@ -374,7 +433,8 @@ bool SfxNotebookBar::StateMethod(SystemWindow* pSysWindow,
     }
 
     const SfxViewShell* pViewShell = SfxViewShell::Current();
-    bool hasWeldedWrapper = m_pNotebookBarWeldedWrapper.find(pViewShell) != 
m_pNotebookBarWeldedWrapper.end();
+    auto& rViewData = NotebookBarViewManager::get().getViewData(pViewShell);
+    bool hasWeldedWrapper = bool(rViewData.m_pWeldedWrapper);
 
     if (IsActive())
     {
@@ -433,22 +493,19 @@ bool SfxNotebookBar::StateMethod(SystemWindow* pSysWindow,
                 
comphelper::LibreOfficeKit::setLocale(pViewShell->GetLOKLocale());
 
                 pNotebookBar = VclPtr<NotebookBar>::Create(pSysWindow, 
"NotebookBar", aBuf, xFrame, aNotebookBarAddonsItem);
-                m_pNotebookBarInstance.emplace(std::make_pair(pViewShell, 
pNotebookBar));
-
+                rViewData.m_pNotebookBar = pNotebookBar;
                 assert(pNotebookBar->IsWelded());
 
                 sal_uInt64 nWindowId = 
reinterpret_cast<sal_uInt64>(pViewShell);
-                WeldedTabbedNotebookbar* pWrapper = new 
WeldedTabbedNotebookbar(pNotebookBar->GetMainContainer(),
+                rViewData.m_pWeldedWrapper.reset(
+                        new 
WeldedTabbedNotebookbar(pNotebookBar->GetMainContainer(),
                                                     
pNotebookBar->GetUIFilePath(),
-                                                    xFrame,
-                                                    nWindowId);
-                m_pNotebookBarWeldedWrapper.emplace(std::make_pair(pViewShell, 
pWrapper));
+                                                    xFrame, nWindowId));
                 pNotebookBar->SetDisposeCallback(LINK(nullptr, SfxNotebookBar, 
VclDisposeHdl), pViewShell);
 
-                // TODO: this has to be per instance!!! like 
m_pNotebookBarWeldedWrapper
-                // TODO: create LOK Notebookbar Instance manager which will 
encapsulate in single place all of these...
-                SfxNotebookBar::m_xCalcToolboxDispatcher.reset(
-                    new ToolbarUnoDispatcher(pWrapper->getWeldedToolbar(), 
pWrapper->getBuilder(), xFrame));
+                rViewData.m_pToolbarUnoDispatcher.reset(
+                    new 
ToolbarUnoDispatcher(rViewData.m_pWeldedWrapper->getWeldedToolbar(),
+                                             
rViewData.m_pWeldedWrapper->getBuilder(), xFrame));
 
                 return true;
             }
@@ -619,7 +676,7 @@ void SfxNotebookBar::ReloadNotebookBar(std::u16string_view 
sUIPath)
 
 IMPL_STATIC_LINK(SfxNotebookBar, VclDisposeHdl, const SfxViewShell*, 
pViewShell, void)
 {
-    m_pNotebookBarWeldedWrapper.erase(pViewShell);
+    NotebookBarViewManager::get().removeViewData(pViewShell);
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Reply via email to