diff --git a/compilerplugins/clang/badstatics.cxx b/compilerplugins/clang/badstatics.cxx index 7c604bc5ee66..f22dabac9b48 100644 --- a/compilerplugins/clang/badstatics.cxx +++ b/compilerplugins/clang/badstatics.cxx @@ -199,9 +199,8 @@ public: || name == "g_aWindowList" //vcl/unx/gtk3/a11y/atkutil.cxx, asserted empty at exit || name == "gFontPreviewVirDevs" - || (loplugin::DeclCheck(pVarDecl).Var("aPreviewCache") - .Class("StylesPreviewWindow_Base").GlobalNamespace()) // TODO: temp disable //svtools/source/control/ctrlbox.cxx, empty at exit + || name == "gStylePreviewCache" // svx/source/tbxctrls/StylesPreviewWindow.cxx || name == "aLogger" // FormulaLogger& FormulaLogger::get() in sc/source/core/tool/formulalogger.cxx || name == "s_aUncommittedRegistrations" // sw/source/uibase/dbui/dbmgr.cxx || (loplugin::DeclCheck(pVarDecl).Var("aAllListeners") diff --git a/svx/source/inc/StylesPreviewWindow.hxx b/svx/source/inc/StylesPreviewWindow.hxx index 47e21dcb4ca0..de4984e35968 100644 --- a/svx/source/inc/StylesPreviewWindow.hxx +++ b/svx/source/inc/StylesPreviewWindow.hxx @@ -127,8 +127,6 @@ private: void UpdateStylesList(); void UpdateSelection(); bool Command(const CommandEvent& rEvent); - - static std::map> aPreviewCache; }; class StylesPreviewWindow_Impl final : public InterimItemWindow, public StylesPreviewWindow_Base diff --git a/svx/source/tbxctrls/StylesPreviewWindow.cxx b/svx/source/tbxctrls/StylesPreviewWindow.cxx index c1e3549fb54a..a51283d0d750 100644 --- a/svx/source/tbxctrls/StylesPreviewWindow.cxx +++ b/svx/source/tbxctrls/StylesPreviewWindow.cxx @@ -60,7 +60,36 @@ #include -std::map> StylesPreviewWindow_Base::aPreviewCache; +namespace +{ +class StylePreviewCache +{ + static std::map> gStylePreviewCache; + static int gStylePreviewCacheClients; + +public: + static std::map>& Get() { return gStylePreviewCache; } + + static void ClearCache() + { + for (auto& aPreview : gStylePreviewCache) + aPreview.second.disposeAndClear(); + + gStylePreviewCache.clear(); + } + + static void RegisterClient() { gStylePreviewCacheClients++; } + static void UnregisterClient() + { + gStylePreviewCacheClients--; + if (!gStylePreviewCacheClients) + ClearCache(); + } +}; + +std::map> StylePreviewCache::gStylePreviewCache; +int StylePreviewCache::gStylePreviewCacheClients; +} StyleStatusListener::StyleStatusListener( StylesPreviewWindow_Base* pPreviewControl, @@ -103,8 +132,10 @@ StylePoolChangeListener::~StylePoolChangeListener() EndListening(*m_pStyleSheetPool); } -void StylePoolChangeListener::Notify(SfxBroadcaster& /*rBC*/, const SfxHint& /*rHint*/) +void StylePoolChangeListener::Notify(SfxBroadcaster& /*rBC*/, const SfxHint& rHint) { + if (rHint.GetId() == SfxHintId::StyleSheetModified) + StylePreviewCache::ClearCache(); m_pPreviewControl->RequestStylesListUpdate(); } @@ -378,6 +409,8 @@ StylesPreviewWindow_Base::StylesPreviewWindow_Base( , m_aUpdateTask(*this) , m_aDefaultStyles(std::move(aDefaultStyles)) { + StylePreviewCache::RegisterClient(); + m_xStylesView->connect_selection_changed(LINK(this, StylesPreviewWindow_Base, Selected)); m_xStylesView->connect_item_activated(LINK(this, StylesPreviewWindow_Base, DoubleClick)); m_xStylesView->connect_command(LINK(this, StylesPreviewWindow_Base, DoCommand)); @@ -424,6 +457,8 @@ StylesPreviewWindow_Base::~StylesPreviewWindow_Base() m_aUpdateTask.Stop(); + StylePreviewCache::UnregisterClient(); + try { m_xStatusListener->dispose(); @@ -465,8 +500,9 @@ void StylesListUpdateTask::Invoke() VclPtr StylesPreviewWindow_Base::GetCachedPreview(const std::pair& rStyle) { - if (aPreviewCache.find(rStyle.second) != aPreviewCache.end()) - return aPreviewCache[rStyle.second]; + auto aFound = StylePreviewCache::Get().find(rStyle.second); + if (aFound != StylePreviewCache::Get().end()) + return StylePreviewCache::Get()[rStyle.second]; else { VclPtr pImg = VclPtr::Create(); @@ -475,7 +511,7 @@ StylesPreviewWindow_Base::GetCachedPreview(const std::pair& StyleItemController aStyleController(rStyle); aStyleController.Paint(*pImg); - aPreviewCache[rStyle.second] = pImg; + StylePreviewCache::Get()[rStyle.second] = pImg; return pImg; }