From aee559fda692474fb7d4a859424dd1ff59f91867 Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 25 Oct 2019 15:12:23 +0300 Subject: [PATCH] Fix '&'-strings in menu items. --- ui/widgets/menu.cpp | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/ui/widgets/menu.cpp b/ui/widgets/menu.cpp index 61e1322..c9ed185 100644 --- a/ui/widgets/menu.cpp +++ b/ui/widgets/menu.cpp @@ -13,6 +13,41 @@ #include namespace Ui { +namespace { + +[[nodiscard]] TextWithEntities ParseMenuItem(const QString &text) { + auto result = TextWithEntities(); + result.text.reserve(text.size()); + auto afterAmpersand = false; + for (const auto ch : text) { + if (afterAmpersand) { + afterAmpersand = false; + if (ch == '&') { + result.text.append(ch); + } else { + result.entities.append(EntityInText{ + EntityType::Underline, + result.text.size(), + 1 }); + result.text.append(ch); + } + } else if (ch == '&') { + afterAmpersand = true; + } else { + result.text.append(ch); + } + } + return result; +} + +TextParseOptions MenuTextOptions = { + TextParseLinks | TextParseRichText, // flags + 0, // maxw + 0, // maxh + Qt::LayoutDirectionAuto, // dir +}; + +} // namespace struct Menu::ActionData { Text::String text; @@ -143,7 +178,7 @@ int Menu::processAction(not_null action, int index, int width) { auto actionTextParts = action->text().split('\t'); auto actionText = actionTextParts.empty() ? QString() : actionTextParts[0]; auto actionShortcut = (actionTextParts.size() > 1) ? actionTextParts[1] : QString(); - data.text.setText(_st.itemStyle, actionText); + data.text.setMarkedText(_st.itemStyle, ParseMenuItem(actionText), MenuTextOptions); const auto textw = data.text.maxWidth(); int goodw = _st.itemPadding.left() + textw + _st.itemPadding.right(); if (data.hasSubmenu) {