diff options
author | Justin Luth <justin.luth@collabora.com> | 2024-10-10 20:48:10 -0400 |
---|---|---|
committer | Justin Luth <jluth@mail.com> | 2024-10-17 17:33:49 +0200 |
commit | bc21a85029a6b31cab07a4e3d481e059120ff510 (patch) | |
tree | 7cd9d1e39bce1c0b5c7ebf4536c89b6198999435 | |
parent | 4df8414e919f2ff82429dfa302db9cd2d1261b26 (diff) |
tdf#34804 sw: enable keyboard shortcut for CharColor and CharBackColor
The result of this patch is that a keyboard shortcut can now
set the font fore-/back-ground color using the
color shown in the toolbar/sidebar.
This is now possible thanks to Maxim's work in 7.6.2
tdf#154270 Sync toolbar button recent colors
and Andreas Heinisch's 24.8 commit 8c822b764b35a0116a0865e991a87c8315e0
tdf#72991 - Remember last used color depending in cui
This patch does 3 things:
1. SetRecentColor when the app initializes
2. Uses SID_ATTR_CHAR_COLOR's recentColor for .uno:FontColor
if no pItem was provided (i.e. a keyboard shortcut called it)
3. Uses SID_ATTR_CHAR_BACK_COLOR's recentColor for .uno:CharBackColor
if no pItem was provided
(Note that without a selection,
CharBackColor isn't so useful for a keyboard shortcut,
since it turns the drag-and-drop template on.)
Setting the recent color right away is critical for user acceptance.
Otherwise, it would only function after they first modified the color
in the toolbar/sidebar.
make CppunitTest_sw_uiwriter9 CPPUNIT_TEST_NAME=testTdf34804
Unfortunately, I can't reliably know if this request
came from an awt::KeyEvent or not, because in that case
we could just avoid CharBackColor's template altogether.
[While there is rReq.GetModifiers() as a good hint,
it is not a guarantee (in case assigned to a function key,
or CTRL held while clicking the toolbar, etc.)]
Change-Id: I7377f087dcdf7011205af005cd0d172100bade2b
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/174804
Tested-by: Jenkins
Reviewed-by: Justin Luth <jluth@mail.com>
-rw-r--r-- | include/sfx2/namedcolor.hxx | 2 | ||||
-rw-r--r-- | include/sfx2/objsh.hxx | 2 | ||||
-rw-r--r-- | sfx2/source/doc/objcont.cxx | 5 | ||||
-rw-r--r-- | svx/inc/tbxcolorupdate.hxx | 2 | ||||
-rw-r--r-- | svx/source/tbxctrls/tbxcolorupdate.cxx | 5 | ||||
-rw-r--r-- | sw/qa/extras/uiwriter/uiwriter9.cxx | 16 | ||||
-rw-r--r-- | sw/source/uibase/shells/textsh1.cxx | 31 |
7 files changed, 53 insertions, 10 deletions
diff --git a/include/sfx2/namedcolor.hxx b/include/sfx2/namedcolor.hxx index 1c5fe24b5144..6d82de7b30c3 100644 --- a/include/sfx2/namedcolor.hxx +++ b/include/sfx2/namedcolor.hxx @@ -31,7 +31,7 @@ struct SFX2_DLLPUBLIC NamedColor { } - model::ComplexColor getComplexColor() + model::ComplexColor getComplexColor() const { model::ComplexColor aComplexColor; diff --git a/include/sfx2/objsh.hxx b/include/sfx2/objsh.hxx index bad832977f65..14855e8b62f0 100644 --- a/include/sfx2/objsh.hxx +++ b/include/sfx2/objsh.hxx @@ -582,7 +582,7 @@ public: StarBASIC* GetBasic() const; std::optional<NamedColor> GetRecentColor(sal_uInt16 nSlotId); - void SetRecentColor(sal_uInt16 nSlotId, const NamedColor& rColor); + void SetRecentColor(sal_uInt16 nSlotId, const NamedColor& rColor, bool bBroadcast = true); virtual std::shared_ptr<sfx::IDocumentModelAccessor> GetDocumentModelAccessor() const; virtual std::set<Color> GetDocColors(); diff --git a/sfx2/source/doc/objcont.cxx b/sfx2/source/doc/objcont.cxx index 1507ad6f810d..d2130979c9a6 100644 --- a/sfx2/source/doc/objcont.cxx +++ b/sfx2/source/doc/objcont.cxx @@ -326,10 +326,11 @@ std::optional<NamedColor> SfxObjectShell::GetRecentColor(sal_uInt16 nSlotId) return std::nullopt; } -void SfxObjectShell::SetRecentColor(sal_uInt16 nSlotId, const NamedColor& rColor) +void SfxObjectShell::SetRecentColor(sal_uInt16 nSlotId, const NamedColor& rColor, bool bBroadcast) { pImpl->m_aRecentColors[nSlotId] = rColor; - Broadcast(SfxHint(SfxHintId::ColorsChanged)); + if (bBroadcast) + Broadcast(SfxHint(SfxHintId::ColorsChanged)); } std::set<Color> SfxObjectShell::GetDocColors() diff --git a/svx/inc/tbxcolorupdate.hxx b/svx/inc/tbxcolorupdate.hxx index 6633090e0ee9..7e2170c7b599 100644 --- a/svx/inc/tbxcolorupdate.hxx +++ b/svx/inc/tbxcolorupdate.hxx @@ -59,7 +59,7 @@ namespace svx virtual ~ToolboxButtonColorUpdaterBase(); void Notify(SfxBroadcaster& rBC, const SfxHint& rHint) override; - void SetRecentColor(const NamedColor& rNamedColor); + void SetRecentColor(const NamedColor& rNamedColor, bool bBroadcast = true); void Update( const NamedColor& rNamedColor ); void Update( const Color& rColor, bool bForceUpdate = false ); Color const & GetCurrentColor() const { return maCurColor; } diff --git a/svx/source/tbxctrls/tbxcolorupdate.cxx b/svx/source/tbxctrls/tbxcolorupdate.cxx index be2d13c79b3e..c84d37fe73eb 100644 --- a/svx/source/tbxctrls/tbxcolorupdate.cxx +++ b/svx/source/tbxctrls/tbxcolorupdate.cxx @@ -122,6 +122,7 @@ namespace svx o3tl::toUInt32(o3tl::getToken(aUserData, 0, ';', nIdx))); } } + SetRecentColor(aNamedColor, /*Broadcast=*/false); Update(aNamedColor); } else @@ -149,10 +150,10 @@ namespace svx } } - void ToolboxButtonColorUpdaterBase::SetRecentColor(const NamedColor &rNamedColor) + void ToolboxButtonColorUpdaterBase::SetRecentColor(const NamedColor &rNamedColor, bool bBroadcast) { if (rtl::Reference xModel = dynamic_cast<SfxBaseModel*>(mxFrame->getController()->getModel().get())) - xModel->GetObjectShell()->SetRecentColor(mnSlotId, rNamedColor); + xModel->GetObjectShell()->SetRecentColor(mnSlotId, rNamedColor, bBroadcast); else if (!mbWideButton) Update(rNamedColor); } diff --git a/sw/qa/extras/uiwriter/uiwriter9.cxx b/sw/qa/extras/uiwriter/uiwriter9.cxx index f97d5a572d4d..8f968353bdf8 100644 --- a/sw/qa/extras/uiwriter/uiwriter9.cxx +++ b/sw/qa/extras/uiwriter/uiwriter9.cxx @@ -332,6 +332,22 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest9, testTdf159816) xTransfer->PrivateDrop(*pWrtShell, ptTo, /*bMove=*/true, /*bXSelection=*/true); } +CPPUNIT_TEST_FIXTURE(SwUiWriterTest9, testTdf34804) +{ + createSwDoc(); + SwDoc* pDoc = getSwDoc(); + SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell(); + + // Simulate a keyboard shortcut to SID_ATTR_CHAR_COLOR2 (which must use the shared button color) + dispatchCommand(mxComponent, u".uno:FontColor"_ustr, {}); + pWrtShell->Insert(u"New World!"_ustr); + + const uno::Reference<text::XTextRange> xRun = getRun(getParagraph(1, "New World!"), 1); + // (This test assumes that nothing in the unit tests has modified the app's recent font color) + // COL_DEFAULT_FONT is the default red color for the fontColor button on the toolbar. + CPPUNIT_ASSERT_EQUAL(COL_DEFAULT_FONT, getProperty<Color>(xRun, u"CharColor"_ustr)); +} + CPPUNIT_TEST_FIXTURE(SwUiWriterTest9, testTdf139631) { // Unit test for tdf#139631 diff --git a/sw/source/uibase/shells/textsh1.cxx b/sw/source/uibase/shells/textsh1.cxx index 1c07d8e8f5a3..2ac91aed446c 100644 --- a/sw/source/uibase/shells/textsh1.cxx +++ b/sw/source/uibase/shells/textsh1.cxx @@ -38,6 +38,7 @@ #include <sfx2/htmlmode.hxx> #include <svl/whiter.hxx> #include <sfx2/bindings.hxx> +#include <sfx2/namedcolor.hxx> #include <sfx2/viewfrm.hxx> #include <vcl/unohelp2.hxx> #include <vcl/weld.hxx> @@ -1761,6 +1762,21 @@ void SwTextShell::Execute(SfxRequest &rReq) case SID_ATTR_CHAR_COLOR2: { + std::unique_ptr<const SvxColorItem> pRecentColor; // manage lifecycle scope + if (!pItem) + { + // no color provided: use the pre-selected color shown in the toolbar/sidebar + const std::optional<NamedColor>& oColor + = GetView().GetDocShell()->GetRecentColor(SID_ATTR_CHAR_COLOR); + if (oColor.has_value()) + { + const model::ComplexColor& rCol = (*oColor).getComplexColor(); + pRecentColor = std::make_unique<const SvxColorItem>( + rCol.getFinalColor(), rCol, RES_CHRATR_COLOR); + pItem = pRecentColor.get(); + } + } + if (pItem) { auto* pColorItem = static_cast<const SvxColorItem*>(pItem); @@ -1783,7 +1799,7 @@ void SwTextShell::Execute(SfxRequest &rReq) case SID_ATTR_CHAR_COLOR_BACKGROUND: // deprecated case SID_ATTR_CHAR_COLOR_EXT: { - Color aColor; + Color aColor = COL_TRANSPARENT; model::ComplexColor aComplexColor; if (pItem) @@ -1792,8 +1808,17 @@ void SwTextShell::Execute(SfxRequest &rReq) aColor = pColorItem->GetValue(); aComplexColor = pColorItem->getComplexColor(); } - else - aColor = COL_TRANSPARENT; + else if (nSlot == SID_ATTR_CHAR_BACK_COLOR) + { + // no color provided: use the pre-selected color shown in the toolbar/sidebar + const std::optional<NamedColor>& oColor + = GetView().GetDocShell()->GetRecentColor(nSlot); + if (oColor.has_value()) + { + aComplexColor = (*oColor).getComplexColor(); + aColor = aComplexColor.getFinalColor(); + } + } SwEditWin& rEdtWin = GetView().GetEditWin(); if (nSlot != SID_ATTR_CHAR_COLOR_EXT) |