cui/source/options/optcolor.cxx        |   29 ++++++++++++++++-------------
 include/svx/Palette.hxx                |    5 +++++
 include/svx/PaletteManager.hxx         |    4 ++++
 include/svx/colorbox.hxx               |    6 ++++--
 svx/inc/palettes.hxx                   |    9 +++++++++
 svx/source/tbxctrls/Palette.cxx        |   15 +++++++++++++++
 svx/source/tbxctrls/PaletteManager.cxx |   19 +++++++++++++++++++
 svx/source/tbxctrls/tbcontrl.cxx       |   22 ++++++++++++++++++----
 8 files changed, 90 insertions(+), 19 deletions(-)

New commits:
commit d327a3bf45808fc7575b7fffa681314e50b0adf8
Author:     Caolán McNamara <[email protected]>
AuthorDate: Wed Dec 7 16:00:28 2022 +0000
Commit:     Caolán McNamara <[email protected]>
CommitDate: Wed Dec 7 22:58:09 2022 +0000

    Resolves: tdf#152301 allow using an existing ColorListBox to speed init
    
    Change-Id: I31fb350fd69831e68ca7c60ec758126aab086895
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143791
    Tested-by: Jenkins
    Reviewed-by: Caolán McNamara <[email protected]>

diff --git a/cui/source/options/optcolor.cxx b/cui/source/options/optcolor.cxx
index 42c299de3f0b..70345fc5e40f 100644
--- a/cui/source/options/optcolor.cxx
+++ b/cui/source/options/optcolor.cxx
@@ -208,20 +208,18 @@ private:
 
     // Entry -- a color config entry:
     // text (checkbox) + color list box
-    class Entry
+    struct Entry
     {
-    public:
         Entry(weld::Window* pTopLevel, weld::Builder& rBuilder, const char* 
pTextWidget, const char* pColorWidget,
-              const Color& rColor, int nCheckBoxLabelOffset, int* 
pColorWidthRequest, bool bCheckBox, bool bShow);
-    public:
+              const Color& rColor, int nCheckBoxLabelOffset, const 
ColorListBox* pCache, bool bCheckBox, bool bShow);
         void SetText(const OUString& rLabel) { 
dynamic_cast<weld::Label&>(*m_xText).set_label(rLabel); }
         int get_height_request() const
         {
             return std::max(m_xText->get_preferred_size().Height(),
                             
m_xColorList->get_widget().get_preferred_size().Height());
         }
-        void Hide ();
-    public:
+        void Hide();
+
         void SetLinks(Link<weld::Toggleable&,void> const&,
                       Link<ColorListBox&,void> const&,
                       Link<weld::Widget&,void> const&);
@@ -229,10 +227,10 @@ private:
         void Update (ExtendedColorConfigValue const&);
         void ColorChanged (ColorConfigValue&);
         void ColorChanged (ExtendedColorConfigValue&);
-    public:
+
         bool Is(const weld::Toggleable* pBox) const { return m_xText.get() == 
pBox; }
         bool Is(const ColorListBox* pBox) const { return m_xColorList.get() == 
pBox; }
-    private:
+
         // checkbox (CheckBox) or simple text (FixedText)
         std::unique_ptr<weld::Widget> m_xText;
         // color list box
@@ -285,9 +283,9 @@ ColorConfigWindow_Impl::Chapter::Chapter(weld::Builder& 
rBuilder, const char* pL
 ColorConfigWindow_Impl::Entry::Entry(weld::Window* pTopLevel, weld::Builder& 
rBuilder,
                                      const char* pTextWidget, const char* 
pColorWidget,
                                      const Color& rColor, int 
nCheckBoxLabelOffset,
-                                     int* pColorWidthRequestCache, bool 
bCheckBox, bool bShow)
+                                     const ColorListBox* pCache, bool 
bCheckBox, bool bShow)
     : m_xColorList(new ColorListBox(rBuilder.weld_menu_button(pColorWidget),
-                                    [pTopLevel]{ return pTopLevel; }, 
pColorWidthRequestCache))
+                                    [pTopLevel]{ return pTopLevel; }, pCache))
     , m_aDefaultColor(rColor)
 {
     if (bCheckBox)
@@ -405,7 +403,7 @@ void ColorConfigWindow_Impl::CreateEntries()
         m_nCheckBoxLabelOffset = aCheckSize.Width() - aFixedSize.Width();
     }
 
-    int nColorWidthRequestCache = -1;
+    const ColorListBox* pCache = nullptr;
 
     // creating entries
     vEntries.reserve(ColorConfigEntryCount);
@@ -414,9 +412,11 @@ void ColorConfigWindow_Impl::CreateEntries()
         vEntries.push_back(std::make_shared<Entry>(m_pTopLevel, *m_xBuilder,
             vEntryInfo[i].pText, vEntryInfo[i].pColor,
             ColorConfig::GetDefaultColor(static_cast<ColorConfigEntry>(i)),
-            m_nCheckBoxLabelOffset, &nColorWidthRequestCache,
+            m_nCheckBoxLabelOffset, pCache,
             vEntryInfo[i].bCheckBox,
             aModulesInstalled[vEntryInfo[i].eGroup]));
+        if (!pCache)
+            pCache = vEntries.back()->m_xColorList.get();
     }
 
     // extended entries
@@ -448,7 +448,7 @@ void ColorConfigWindow_Impl::CreateEntries()
                 aExtConfig.GetComponentColorConfigValue(sComponentName, i);
             vEntries.push_back(std::make_shared<Entry>(m_pTopLevel, 
*vExtBuilders.back(),
                 "label", "button", aColorEntry.getDefaultColor(),
-                m_nCheckBoxLabelOffset, &nColorWidthRequestCache, false, 
true));
+                m_nCheckBoxLabelOffset, pCache, false, true));
             vEntries.back()->SetText(aColorEntry.getDisplayName());
         }
     }
diff --git a/include/svx/Palette.hxx b/include/svx/Palette.hxx
index c23a09f9cbc8..b24825be3d3c 100644
--- a/include/svx/Palette.hxx
+++ b/include/svx/Palette.hxx
@@ -52,7 +52,10 @@ typedef std::function<void(const OUString&, const 
svx::NamedThemedColor&)> Color
 
 class Palette
 {
+protected:
+    Palette(const Palette&) = default;
 public:
+    Palette() = default;
     virtual ~Palette();
 
     virtual const OUString&     GetName() = 0;
@@ -60,6 +63,8 @@ public:
     virtual void                LoadColorSet(SvxColorValueSet& rColorSet) = 0;
 
     virtual bool                IsValid() = 0;
+
+    virtual Palette*            Clone() const = 0;
 };
 
 #endif // INCLUDED_SVX_PALETTE_HXX
diff --git a/include/svx/PaletteManager.hxx b/include/svx/PaletteManager.hxx
index 4e45df0012e6..a21961ecd0ec 100644
--- a/include/svx/PaletteManager.hxx
+++ b/include/svx/PaletteManager.hxx
@@ -49,6 +49,8 @@ class SVXCORE_DLLPUBLIC PaletteManager
     ColorSelectFunction maColorSelectFunction;
 
     std::unique_ptr<SvColorDialog> m_pColorDlg;
+
+    PaletteManager(const PaletteManager* pClone);
 public:
     PaletteManager();
     ~PaletteManager();
@@ -75,6 +77,8 @@ public:
 
     bool IsThemePaletteSelected() const;
 
+    PaletteManager* Clone() const;
+
     static void GetThemeIndexLumModOff(sal_uInt16 nItemId, sal_Int16& 
rThemeIndex,
                                        sal_Int16& rLumMod, sal_Int16& rLumOff);
 
diff --git a/include/svx/colorbox.hxx b/include/svx/colorbox.hxx
index 887edb92413f..2705833ee37d 100644
--- a/include/svx/colorbox.hxx
+++ b/include/svx/colorbox.hxx
@@ -54,7 +54,8 @@ private:
 public:
     // rTopLevelParentFunction will be used to get parent for any color picker 
dialog created
     ColorListBox(std::unique_ptr<weld::MenuButton> pControl,
-                 TopLevelParentFunction aTopLevelParentFunction, int* 
pWidthRequestCache = nullptr);
+                 TopLevelParentFunction aTopLevelParentFunction,
+                 const ColorListBox* pCache = nullptr);
     ~ColorListBox();
 
     void SetSelectHdl(const Link<ColorListBox&, void>& rLink) { 
m_aSelectedLink = rLink; }
diff --git a/svx/inc/palettes.hxx b/svx/inc/palettes.hxx
index 12fdbb560f1d..abeccf0fe243 100644
--- a/svx/inc/palettes.hxx
+++ b/svx/inc/palettes.hxx
@@ -37,6 +37,7 @@ class PaletteASE final : public Palette
     ColorList   maColors;
 
     void        LoadPalette();
+    PaletteASE(const PaletteASE&) = default;
 public:
     PaletteASE( OUString aFPath, OUString aFName );
     virtual ~PaletteASE() override;
@@ -46,6 +47,8 @@ public:
     virtual void                LoadColorSet(SvxColorValueSet& rColorSet) 
override;
 
     virtual bool                IsValid() override;
+
+    virtual Palette*            Clone() const override;
 };
 
 // GPL - this is *not* GNU Public License, but is the Gimp PaLette
@@ -62,6 +65,7 @@ class PaletteGPL final : public Palette
     bool        ReadPaletteHeader(SvFileStream& rFileStream);
     void        LoadPaletteHeader();
     void        LoadPalette();
+    PaletteGPL(const PaletteGPL&) = default;
 public:
     PaletteGPL( OUString aFPath, OUString aFName );
     virtual ~PaletteGPL() override;
@@ -71,6 +75,8 @@ public:
     virtual void                LoadColorSet(SvxColorValueSet& rColorSet) 
override;
 
     virtual bool                IsValid() override;
+
+    virtual Palette*            Clone() const override;
 };
 
 // SOC - Star Office Color-table
@@ -81,6 +87,7 @@ class PaletteSOC final : public Palette
     OUString        maFPath;
     OUString        maSOCPaletteName;
     XColorListRef   mpColorList;
+    PaletteSOC(const PaletteSOC&) = default;
 public:
     PaletteSOC( OUString aFPath, OUString aFName );
     virtual ~PaletteSOC() override;
@@ -90,6 +97,8 @@ public:
     virtual void                LoadColorSet(SvxColorValueSet& rColorSet) 
override;
 
     virtual bool                IsValid() override;
+
+    virtual Palette*            Clone() const override;
 };
 
 #endif // INCLUDED_SVX_INC_PALETTE_HXX
diff --git a/svx/source/tbxctrls/Palette.cxx b/svx/source/tbxctrls/Palette.cxx
index 64f2b648510e..63d1cf972c38 100644
--- a/svx/source/tbxctrls/Palette.cxx
+++ b/svx/source/tbxctrls/Palette.cxx
@@ -171,6 +171,11 @@ void PaletteASE::LoadPalette()
     mbValidPalette = true;
 }
 
+Palette* PaletteASE::Clone() const
+{
+    return new PaletteASE(*this);
+}
+
 // PaletteGPL 
------------------------------------------------------------------
 
 static OString lcl_getToken(OStringBuffer& rStr, sal_Int32& index);
@@ -286,6 +291,11 @@ void PaletteGPL::LoadPalette()
     } while (aFile.ReadLine(aLine));
 }
 
+Palette* PaletteGPL::Clone() const
+{
+    return new PaletteGPL(*this);
+}
+
 // finds first token in rStr from index, separated by whitespace
 // returns position of next token in index
 static OString lcl_getToken(OStringBuffer& rStr, sal_Int32& index)
@@ -362,6 +372,11 @@ bool PaletteSOC::IsValid()
     return true;
 }
 
+Palette* PaletteSOC::Clone() const
+{
+    return new PaletteSOC(*this);
+}
+
 namespace svx
 {
 NamedColor NamedThemedColor::ToNamedColor() const { return { m_aColor, m_aName 
}; }
diff --git a/svx/source/tbxctrls/PaletteManager.cxx 
b/svx/source/tbxctrls/PaletteManager.cxx
index 8f642e779b58..f5cb5d221e37 100644
--- a/svx/source/tbxctrls/PaletteManager.cxx
+++ b/svx/source/tbxctrls/PaletteManager.cxx
@@ -77,6 +77,25 @@ PaletteManager::PaletteManager() :
 
 }
 
+PaletteManager::PaletteManager(const PaletteManager* pClone)
+    : mnMaxRecentColors(pClone->mnMaxRecentColors)
+    , mnNumOfPalettes(pClone->mnNumOfPalettes)
+    , mnCurrentPalette(pClone->mnCurrentPalette)
+    , mnColorCount(pClone->mnColorCount)
+    , mpBtnUpdater(nullptr)
+    , pColorList(pClone->pColorList)
+    , maRecentColors(pClone->maRecentColors)
+    , maColorSelectFunction(PaletteManager::DispatchColorCommand)
+{
+    for (const auto& a : pClone->m_Palettes)
+        m_Palettes.emplace_back(a->Clone());
+}
+
+PaletteManager* PaletteManager::Clone() const
+{
+    return new PaletteManager(this);
+}
+
 PaletteManager::~PaletteManager()
 {
 }
diff --git a/svx/source/tbxctrls/tbcontrl.cxx b/svx/source/tbxctrls/tbcontrl.cxx
index 7dd2e7e55aa2..8df461642233 100644
--- a/svx/source/tbxctrls/tbcontrl.cxx
+++ b/svx/source/tbxctrls/tbcontrl.cxx
@@ -4229,7 +4229,7 @@ void ColorListBox::SetSlotId(sal_uInt16 nSlotId, bool 
bShowNoneButton)
 
 ColorListBox::ColorListBox(std::unique_ptr<weld::MenuButton> pControl,
                            TopLevelParentFunction aTopLevelParentFunction,
-                           int* pWidthRequestCache)
+                           const ColorListBox* pCache)
     : m_xButton(std::move(pControl))
     , m_aColorWrapper(this)
     , 
m_aAutoDisplayColor(Application::GetSettings().GetStyleSettings().GetDialogColor())
@@ -4239,12 +4239,14 @@ 
ColorListBox::ColorListBox(std::unique_ptr<weld::MenuButton> pControl,
 {
     m_xButton->connect_toggled(LINK(this, ColorListBox, ToggleHdl));
     m_aSelectedColor = 
svx::NamedThemedColor::FromNamedColor(GetAutoColor(m_nSlotId));
-    int nWidthRequest = pWidthRequestCache ? *pWidthRequestCache : -1;
-    if (nWidthRequest == -1)
-        nWidthRequest = CalcBestWidthRequest();
-    LockWidthRequest(nWidthRequest);
-    if (pWidthRequestCache)
-        *pWidthRequestCache = nWidthRequest;
+    if (!pCache)
+        LockWidthRequest(CalcBestWidthRequest());
+    else
+    {
+        LockWidthRequest(pCache->m_xButton->get_size_request().Width());
+        m_xPaletteManager.reset(pCache->m_xPaletteManager->Clone());
+        m_xPaletteManager->SetColorSelectFunction(std::ref(m_aColorWrapper));
+    }
     ShowPreview(m_aSelectedColor.ToNamedColor());
 }
 
commit 62ae7be5aa20499c45e6f1b3f1dd2729483625b0
Author:     Caolán McNamara <[email protected]>
AuthorDate: Wed Dec 7 12:56:10 2022 +0000
Commit:     Caolán McNamara <[email protected]>
CommitDate: Wed Dec 7 22:57:57 2022 +0000

    Related: tdf#152301 cache color widget width
    
    Change-Id: I0030980e2259715aa1fa624eb0ee82d5dfc51810
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143778
    Tested-by: Caolán McNamara <[email protected]>
    Reviewed-by: Caolán McNamara <[email protected]>

diff --git a/cui/source/options/optcolor.cxx b/cui/source/options/optcolor.cxx
index 4b5e4b306fed..42c299de3f0b 100644
--- a/cui/source/options/optcolor.cxx
+++ b/cui/source/options/optcolor.cxx
@@ -212,7 +212,7 @@ private:
     {
     public:
         Entry(weld::Window* pTopLevel, weld::Builder& rBuilder, const char* 
pTextWidget, const char* pColorWidget,
-              const Color& rColor, int nCheckBoxLabelOffset, bool bCheckBox, 
bool bShow);
+              const Color& rColor, int nCheckBoxLabelOffset, int* 
pColorWidthRequest, bool bCheckBox, bool bShow);
     public:
         void SetText(const OUString& rLabel) { 
dynamic_cast<weld::Label&>(*m_xText).set_label(rLabel); }
         int get_height_request() const
@@ -284,9 +284,10 @@ ColorConfigWindow_Impl::Chapter::Chapter(weld::Builder& 
rBuilder, const char* pL
 // ColorConfigWindow_Impl::Entry
 ColorConfigWindow_Impl::Entry::Entry(weld::Window* pTopLevel, weld::Builder& 
rBuilder,
                                      const char* pTextWidget, const char* 
pColorWidget,
-                                     const Color& rColor,
-                                     int nCheckBoxLabelOffset, bool bCheckBox, 
bool bShow)
-    : m_xColorList(new ColorListBox(rBuilder.weld_menu_button(pColorWidget), 
[pTopLevel]{ return pTopLevel; }))
+                                     const Color& rColor, int 
nCheckBoxLabelOffset,
+                                     int* pColorWidthRequestCache, bool 
bCheckBox, bool bShow)
+    : m_xColorList(new ColorListBox(rBuilder.weld_menu_button(pColorWidget),
+                                    [pTopLevel]{ return pTopLevel; }, 
pColorWidthRequestCache))
     , m_aDefaultColor(rColor)
 {
     if (bCheckBox)
@@ -404,6 +405,8 @@ void ColorConfigWindow_Impl::CreateEntries()
         m_nCheckBoxLabelOffset = aCheckSize.Width() - aFixedSize.Width();
     }
 
+    int nColorWidthRequestCache = -1;
+
     // creating entries
     vEntries.reserve(ColorConfigEntryCount);
     for (size_t i = 0; i < std::size(vEntryInfo); ++i)
@@ -411,7 +414,7 @@ void ColorConfigWindow_Impl::CreateEntries()
         vEntries.push_back(std::make_shared<Entry>(m_pTopLevel, *m_xBuilder,
             vEntryInfo[i].pText, vEntryInfo[i].pColor,
             ColorConfig::GetDefaultColor(static_cast<ColorConfigEntry>(i)),
-            m_nCheckBoxLabelOffset,
+            m_nCheckBoxLabelOffset, &nColorWidthRequestCache,
             vEntryInfo[i].bCheckBox,
             aModulesInstalled[vEntryInfo[i].eGroup]));
     }
@@ -445,7 +448,7 @@ void ColorConfigWindow_Impl::CreateEntries()
                 aExtConfig.GetComponentColorConfigValue(sComponentName, i);
             vEntries.push_back(std::make_shared<Entry>(m_pTopLevel, 
*vExtBuilders.back(),
                 "label", "button", aColorEntry.getDefaultColor(),
-                m_nCheckBoxLabelOffset, false, true));
+                m_nCheckBoxLabelOffset, &nColorWidthRequestCache, false, 
true));
             vEntries.back()->SetText(aColorEntry.getDisplayName());
         }
     }
diff --git a/include/svx/colorbox.hxx b/include/svx/colorbox.hxx
index ff635c908565..887edb92413f 100644
--- a/include/svx/colorbox.hxx
+++ b/include/svx/colorbox.hxx
@@ -45,7 +45,8 @@ private:
 
     void Selected(const svx::NamedThemedColor& rNamedColor);
     void createColorWindow();
-    void LockWidthRequest();
+    void LockWidthRequest(int nWidthRequest);
+    int CalcBestWidthRequest();
     ColorWindow* getColorWindow() const;
 
     DECL_DLLPRIVATE_LINK(ToggleHdl, weld::Toggleable&, void);
@@ -53,7 +54,7 @@ private:
 public:
     // rTopLevelParentFunction will be used to get parent for any color picker 
dialog created
     ColorListBox(std::unique_ptr<weld::MenuButton> pControl,
-                 TopLevelParentFunction aTopLevelParentFunction);
+                 TopLevelParentFunction aTopLevelParentFunction, int* 
pWidthRequestCache = nullptr);
     ~ColorListBox();
 
     void SetSelectHdl(const Link<ColorListBox&, void>& rLink) { 
m_aSelectedLink = rLink; }
diff --git a/svx/source/tbxctrls/tbcontrl.cxx b/svx/source/tbxctrls/tbcontrl.cxx
index 173d6b3e4f32..7dd2e7e55aa2 100644
--- a/svx/source/tbxctrls/tbcontrl.cxx
+++ b/svx/source/tbxctrls/tbcontrl.cxx
@@ -4227,7 +4227,9 @@ void ColorListBox::SetSlotId(sal_uInt16 nSlotId, bool 
bShowNoneButton)
     createColorWindow();
 }
 
-ColorListBox::ColorListBox(std::unique_ptr<weld::MenuButton> pControl, 
TopLevelParentFunction aTopLevelParentFunction)
+ColorListBox::ColorListBox(std::unique_ptr<weld::MenuButton> pControl,
+                           TopLevelParentFunction aTopLevelParentFunction,
+                           int* pWidthRequestCache)
     : m_xButton(std::move(pControl))
     , m_aColorWrapper(this)
     , 
m_aAutoDisplayColor(Application::GetSettings().GetStyleSettings().GetDialogColor())
@@ -4237,7 +4239,12 @@ 
ColorListBox::ColorListBox(std::unique_ptr<weld::MenuButton> pControl, TopLevelP
 {
     m_xButton->connect_toggled(LINK(this, ColorListBox, ToggleHdl));
     m_aSelectedColor = 
svx::NamedThemedColor::FromNamedColor(GetAutoColor(m_nSlotId));
-    LockWidthRequest();
+    int nWidthRequest = pWidthRequestCache ? *pWidthRequestCache : -1;
+    if (nWidthRequest == -1)
+        nWidthRequest = CalcBestWidthRequest();
+    LockWidthRequest(nWidthRequest);
+    if (pWidthRequestCache)
+        *pWidthRequestCache = nWidthRequest;
     ShowPreview(m_aSelectedColor.ToNamedColor());
 }
 
@@ -4319,7 +4326,7 @@ void ColorListBox::Selected(const svx::NamedThemedColor& 
rColor)
 //to avoid the box resizing every time the color is changed to
 //the optimal size of the individual color, get the longest
 //standard color and stick with that as the size for all
-void ColorListBox::LockWidthRequest()
+int ColorListBox::CalcBestWidthRequest()
 {
     NamedColor aLongestColor;
     tools::Long nMaxStandardColorTextWidth = 0;
@@ -4335,7 +4342,12 @@ void ColorListBox::LockWidthRequest()
         }
     }
     ShowPreview(aLongestColor);
-    m_xButton->set_size_request(m_xButton->get_preferred_size().Width(), -1);
+    return m_xButton->get_preferred_size().Width();
+}
+
+void ColorListBox::LockWidthRequest(int nWidth)
+{
+    m_xButton->set_size_request(nWidth, -1);
 }
 
 void ColorListBox::ShowPreview(const NamedColor &rColor)

Reply via email to