compilerplugins/clang/badstatics.cxx        |    3 
 include/sfx2/notebookbar/SfxNotebookBar.hxx |    9 --
 sfx2/source/notebookbar/SfxNotebookBar.cxx  |   99 ++++++++++++++++++++++------
 3 files changed, 80 insertions(+), 31 deletions(-)

New commits:
commit 74ee046a04d6893db5b772f8f4219f7db413fbb8
Author:     Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk>
AuthorDate: Mon Jan 29 16:08:48 2024 +0900
Commit:     Tomaž Vajngerl <qui...@gmail.com>
CommitDate: Tue Jan 30 10:10:07 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/+/162706
    Tested-by: Jenkins
    Reviewed-by: Szymon Kłos <szymon.k...@collabora.com>

diff --git a/compilerplugins/clang/badstatics.cxx 
b/compilerplugins/clang/badstatics.cxx
index 328218086453..18638695bbeb 100644
--- a/compilerplugins/clang/badstatics.cxx
+++ b/compilerplugins/clang/badstatics.cxx
@@ -210,8 +210,7 @@ public:
                 || name == "s_aLOKWindowsMap" // LOK only, guarded by assert, 
and LOK never tries to perform a VCL cleanup
                 || name == "s_aLOKWeldBuildersMap" // LOK only, similar case 
as above
                 || name == "s_aLOKPopupsMap" // LOK only, similar case as above
-                || name == "m_pNotebookBarWeldedWrapper" // LOK only, warning 
about map's key, no VCL cleanup performed
-                || name == "m_pNotebookBarInstance" // LOK only case, when 
notebookbar is closed - VclPtr instance is removed
+                || name == "gNotebookBarManager" // LOK only case, when 
notebookbar is closed - VclPtr instance is removed
                 || name == "gStaticManager" // vcl/source/graphic/Manager.cxx 
- stores non-owning pointers
                 || name == "aThreadedInterpreterPool"    // 
ScInterpreterContext(Pool), not owning
                 || name == "aNonThreadedInterpreterPool" // 
ScInterpreterContext(Pool), not owning
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 9c33b3ef5fb9..c6b1cddb34ad 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 gNotebookBarManager;
+        return gNotebookBarManager;
+    }
+
+    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();
     }
 }
 
@@ -378,7 +437,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())
     {
@@ -437,22 +497,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;
             }
@@ -624,7 +681,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