diff options
author | Szymon Kłos <szymon.klos@collabora.com> | 2023-06-02 18:31:55 +0200 |
---|---|---|
committer | Caolán McNamara <caolan.mcnamara@collabora.com> | 2023-06-03 17:44:01 +0200 |
commit | d8b834a750a284ecd04cac1f4de4cb9cbf05c42c (patch) | |
tree | 097c967f1881168378459b2d60c691365265361b /svx | |
parent | 5ad502af9cf3d49457082a12d36f8159b6c4ca11 (diff) |
Styles preview cache cleanup on exit
Change-Id: I28929d4137008ebcca1733837d0b2112b6859a89
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/152563
Reviewed-by: Caolán McNamara <caolan.mcnamara@collabora.com>
Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
Reviewed-by: Szymon Kłos <szymon.klos@collabora.com>
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/152576
Tested-by: Jenkins
Diffstat (limited to 'svx')
-rw-r--r-- | svx/source/inc/StylesPreviewWindow.hxx | 2 | ||||
-rw-r--r-- | svx/source/tbxctrls/StylesPreviewWindow.cxx | 46 |
2 files changed, 41 insertions, 7 deletions
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<OUString, VclPtr<VirtualDevice>> 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 <vcl/commandevent.hxx> -std::map<OUString, VclPtr<VirtualDevice>> StylesPreviewWindow_Base::aPreviewCache; +namespace +{ +class StylePreviewCache +{ + static std::map<OUString, VclPtr<VirtualDevice>> gStylePreviewCache; + static int gStylePreviewCacheClients; + +public: + static std::map<OUString, VclPtr<VirtualDevice>>& 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<OUString, VclPtr<VirtualDevice>> 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<VirtualDevice> StylesPreviewWindow_Base::GetCachedPreview(const std::pair<OUString, OUString>& 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<VirtualDevice> pImg = VclPtr<VirtualDevice>::Create(); @@ -475,7 +511,7 @@ StylesPreviewWindow_Base::GetCachedPreview(const std::pair<OUString, OUString>& StyleItemController aStyleController(rStyle); aStyleController.Paint(*pImg); - aPreviewCache[rStyle.second] = pImg; + StylePreviewCache::Get()[rStyle.second] = pImg; return pImg; } |