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

New commits:
commit aae8841e2057d522b3cae7b5792ae75cc8d6b5b4
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: Fri Jun 9 17:52:23 2023 +0200

    Related: cool#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/+/152720
    Tested-by: Caolán McNamara <caolan.mcnam...@collabora.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 53a8f14511a9..c5215cba4a6d 100644
--- a/svx/source/tbxctrls/StylesPreviewWindow.cxx
+++ b/svx/source/tbxctrls/StylesPreviewWindow.cxx
@@ -68,35 +68,66 @@ namespace
 {
 class StylePreviewCache
 {
+private:
+    class JsonStylePreviewCacheClear final : public Timer
+    {
+    public:
+        JsonStylePreviewCacheClear()
+            : Timer("Json Style Preview Cache clear callback")
+        {
+            // a generous 30 secs
+            SetTimeout(30000);
+            SetStatic();
+        }
+        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 +174,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