svx/source/tbxctrls/StylesPreviewWindow.cxx |   40 ++++++++++++++++++++++++----
 1 file changed, 35 insertions(+), 5 deletions(-)

New commits:
commit 9f3ffba983baf924a892d7ec6c102686fd7259c9
Author:     Caolán McNamara <caolan.mcnam...@collabora.com>
AuthorDate: Wed Jun 7 20:43:12 2023 +0100
Commit:     Caolán McNamara <caolan.mcnam...@collabora.com>
CommitDate: Thu Jun 8 09:34:37 2023 +0200

    Related: CollaboraOnline#6511 hold json preview cache longer
    
    due to oddities related to tdf155720 the notebookbar the cache is hooked
    to can be torn down and replaced if the 1st session is interacted with
    while multiple other users join. So count goes to 0 just at the wrong
    time to trigger throwing away the cache and forcing regeneration.
    
    An Idle doesn't suffice here.
    
    https: //github.com/CollaboraOnline/online/issues/6511
    Change-Id: I148c99115fc497e34bf8920b6f59adc47605b8a1
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/152663
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Caolán McNamara <caolan.mcnam...@collabora.com>

diff --git a/svx/source/tbxctrls/StylesPreviewWindow.cxx 
b/svx/source/tbxctrls/StylesPreviewWindow.cxx
index f4dd77dd4fb5..283c19363932 100644
--- a/svx/source/tbxctrls/StylesPreviewWindow.cxx
+++ b/svx/source/tbxctrls/StylesPreviewWindow.cxx
@@ -68,35 +68,65 @@ namespace
 {
 class StylePreviewCache
 {
+private:
+    class JsonStylePreviewCacheClear final : public Timer
+    {
+    public:
+        JsonStylePreviewCacheClear()
+            : Timer("Json Style Preview Cache clear callback")
+        {
+            // a generous 30 secs
+            SetTimeout(30000);
+        }
+        virtual void Invoke() override { 
StylePreviewCache::gJsonStylePreviewCache.clear(); }
+    };
+
     static std::map<OUString, VclPtr<VirtualDevice>> gStylePreviewCache;
     static std::map<OUString, OString> gJsonStylePreviewCache;
     static int gStylePreviewCacheClients;
+    static JsonStylePreviewCacheClear gJsonIdleClear;
 
 public:
     static std::map<OUString, VclPtr<VirtualDevice>>& Get() { return 
gStylePreviewCache; }
     static std::map<OUString, OString>& GetJson() { return 
gJsonStylePreviewCache; }
 
-    static void ClearCache()
+    static void ClearCache(bool bHard)
     {
         for (auto& aPreview : gStylePreviewCache)
             aPreview.second.disposeAndClear();
 
         gStylePreviewCache.clear();
-        gJsonStylePreviewCache.clear();
+        if (bHard)
+        {
+            StylePreviewCache::gJsonStylePreviewCache.clear();
+            gJsonIdleClear.Stop();
+        }
+        else
+        {
+            // tdf#155720 don't immediately clear the json representation
+            gJsonIdleClear.Start();
+        }
+    }
+
+    static void RegisterClient()
+    {
+        if (!gStylePreviewCacheClients)
+            gJsonIdleClear.Stop();
+        gStylePreviewCacheClients++;
     }
 
-    static void RegisterClient() { gStylePreviewCacheClients++; }
     static void UnregisterClient()
     {
         gStylePreviewCacheClients--;
         if (!gStylePreviewCacheClients)
-            ClearCache();
+            ClearCache(false);
     }
 };
 
 std::map<OUString, VclPtr<VirtualDevice>> 
StylePreviewCache::gStylePreviewCache;
 std::map<OUString, OString> StylePreviewCache::gJsonStylePreviewCache;
 int StylePreviewCache::gStylePreviewCacheClients;
+StylePreviewCache::JsonStylePreviewCacheClear 
StylePreviewCache::gJsonIdleClear;
 }
 
 StyleStatusListener::StyleStatusListener(
@@ -143,7 +173,7 @@ StylePoolChangeListener::~StylePoolChangeListener()
 void StylePoolChangeListener::Notify(SfxBroadcaster& /*rBC*/, const SfxHint& 
rHint)
 {
     if (rHint.GetId() == SfxHintId::StyleSheetModified)
-        StylePreviewCache::ClearCache();
+        StylePreviewCache::ClearCache(true);
     m_pPreviewControl->RequestStylesListUpdate();
 }
 

Reply via email to