From e979ec28042466f5bf071d7717a68165064f84fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20K=C5=82os?= Date: Tue, 21 Jan 2025 10:53:16 +0100 Subject: [PATCH] jsdialog: make executor more readable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - no functional change - move executor to separate functions - make factory just for decision making which function to use - we need to use some functions for many types Change-Id: I5b54e90a6feb86bcda97d37c4cfff36a371efb5d Reviewed-on: https://gerrit.libreoffice.org/c/core/+/180540 (cherry picked from commit 7d23b904222ebe82b72dc919bf1aada2d55b35cc) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/182514 Reviewed-by: Szymon Kłos Tested-by: Jenkins --- vcl/jsdialog/executor.cxx | 89 ++++++++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 25 deletions(-) diff --git a/vcl/jsdialog/executor.cxx b/vcl/jsdialog/executor.cxx index 558e9f7302e1..f721a97e13ca 100644 --- a/vcl/jsdialog/executor.cxx +++ b/vcl/jsdialog/executor.cxx @@ -7,13 +7,69 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include +#include +#include +#include #include #include -#include -#include -#include #include -#include +#include +#include +#include + +/// returns true if execution was successful +using JSWidgetExecutor = bool (*)(weld::Widget&, const StringMap&); + +namespace +{ +bool EmptyExecutor(weld::Widget&, const StringMap&) { return false; }; + +bool FocusExecutor(weld::Widget& rWidget, const StringMap&) +{ + rWidget.grab_focus(); + return true; +}; + +bool CustomRendererExecutor(weld::Widget& rWidget, const StringMap& rData) +{ + auto pRenderer = dynamic_cast(&rWidget); + if (!pRenderer) + return false; + + // pos;dpix;dpiy + const OUString& sParams = rData.at(u"data"_ustr); + const OUString aPos = sParams.getToken(0, ';'); + const OUString aDpiScaleX = sParams.getToken(1, ';'); + const OUString aDpiScaleY = sParams.getToken(2, ';'); + + pRenderer->render_entry(o3tl::toInt32(aPos), o3tl::toInt32(aDpiScaleX), + o3tl::toInt32(aDpiScaleY)); + + return true; +}; + +constexpr auto ActionExecutors + = frozen::make_unordered_map({ + { u"grab_focus", FocusExecutor }, + { u"render_entry", CustomRendererExecutor }, + }); + +} // end of namespace + +namespace JSWidgetExecutorSelector +{ +static JSWidgetExecutor get(const std::u16string_view& /*sControlType*/, + const std::u16string_view& sAction) +{ + auto aFound = ActionExecutors.find(sAction); + if (aFound != ActionExecutors.end()) + return aFound->second; + + return EmptyExecutor; +} + +} // end of namespace JSWidgetExecutorSelector namespace jsdialog { @@ -94,30 +150,13 @@ bool ExecuteAction(const OUString& nWindowId, const OUString& rWidget, StringMap } else { - // shared actions + assert(pWidget); - if (sAction == "grab_focus") - { - pWidget->grab_focus(); + const JSWidgetExecutor rExecutor = JSWidgetExecutorSelector::get(sControlType, sAction); + if (rExecutor(*pWidget, rData)) return true; - } - else if (sAction == "render_entry") - { - auto pRenderer = dynamic_cast(pWidget); - if (pRenderer) - { - // pos;dpix;dpiy - const OUString& sParams = rData[u"data"_ustr]; - const OUString aPos = sParams.getToken(0, ';'); - const OUString aDpiScaleX = sParams.getToken(1, ';'); - const OUString aDpiScaleY = sParams.getToken(2, ';'); - - pRenderer->render_entry(o3tl::toInt32(aPos), o3tl::toInt32(aDpiScaleX), - o3tl::toInt32(aDpiScaleY)); - } - return true; - } + // TODO: convert to executors like above // depends on type if (sControlType == "tabcontrol")