From 59015fffeb034d5fef564439e2bd62ba899f24f8 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 16 Jul 2026 11:47:50 +0400 Subject: [PATCH] Auto-detected rtl flag for composed rich messages. --- .../SourceFiles/iv/editor/iv_editor_state.cpp | 1 + .../iv/iv_rich_message_serializer.cpp | 2 +- Telegram/SourceFiles/iv/iv_rich_page.cpp | 33 +++++++++++++++++++ Telegram/SourceFiles/iv/iv_rich_page.h | 1 + 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/Telegram/SourceFiles/iv/editor/iv_editor_state.cpp b/Telegram/SourceFiles/iv/editor/iv_editor_state.cpp index be1aa5496b..3296e856e7 100644 --- a/Telegram/SourceFiles/iv/editor/iv_editor_state.cpp +++ b/Telegram/SourceFiles/iv/editor/iv_editor_state.cpp @@ -8905,6 +8905,7 @@ void State::rebuild() { void State::rebuildPrepared() { _lastPreparedMutationKind = PreparedMutationKind::FullRebuild; + _richPage->rtl = DetermineRichPageRtl(*_richPage); _prepared = Markdown::TryPrepareNativeInstantView({ .richPage = _richPage, .mediaRuntime = _mediaRuntime, diff --git a/Telegram/SourceFiles/iv/iv_rich_message_serializer.cpp b/Telegram/SourceFiles/iv/iv_rich_message_serializer.cpp index 3dde8031d4..ce45324bc7 100644 --- a/Telegram/SourceFiles/iv/iv_rich_message_serializer.cpp +++ b/Telegram/SourceFiles/iv/iv_rich_message_serializer.cpp @@ -1736,7 +1736,7 @@ SerializeInputRichMessageResult SerializeInputRichMessage( } using Flag = MTPDinputRichMessage::Flag; auto flags = MTPDinputRichMessage::Flags(); - if (page.rtl) { + if (DetermineRichPageRtl(page)) { flags |= Flag::f_rtl; } if (!photos.isEmpty()) { diff --git a/Telegram/SourceFiles/iv/iv_rich_page.cpp b/Telegram/SourceFiles/iv/iv_rich_page.cpp index 86316dddd6..d549f168f1 100644 --- a/Telegram/SourceFiles/iv/iv_rich_page.cpp +++ b/Telegram/SourceFiles/iv/iv_rich_page.cpp @@ -2252,6 +2252,35 @@ std::shared_ptr ParsePage( return true; } +[[nodiscard]] std::optional RichTextRtl(const RichText &text) { + const auto &plain = text.text.text; + if (plain.trimmed().isEmpty()) { + return std::nullopt; + } + return plain.isRightToLeft(); +} + +[[nodiscard]] std::optional BlocksTextRtl( + const std::vector &blocks) { + for (const auto &block : blocks) { + if (const auto result = RichTextRtl(block.text)) { + return result; + } + if (const auto result = BlocksTextRtl(block.blocks)) { + return result; + } + for (const auto &item : block.listItems) { + if (const auto result = RichTextRtl(item.text)) { + return result; + } + if (const auto result = BlocksTextRtl(item.blocks)) { + return result; + } + } + } + return std::nullopt; +} + } // namespace bool RichPagesEqual( @@ -2443,6 +2472,10 @@ TextWithEntities FlattenRichPageToSimpleText(const RichPage &page) { return result; } +bool DetermineRichPageRtl(const RichPage &page) { + return BlocksTextRtl(page.blocks).value_or(false); +} + std::optional SerializeAsSimple( const RichPage &page, not_null session) { diff --git a/Telegram/SourceFiles/iv/iv_rich_page.h b/Telegram/SourceFiles/iv/iv_rich_page.h index 5cb20bcae4..e9c5bb5556 100644 --- a/Telegram/SourceFiles/iv/iv_rich_page.h +++ b/Telegram/SourceFiles/iv/iv_rich_page.h @@ -312,5 +312,6 @@ inline constexpr auto kTextDiffDeletedColorIndex = 11; bool emptyFallback = true); [[nodiscard]] TextWithEntities FlattenRichPageToSimpleText( const RichPage &page); +[[nodiscard]] bool DetermineRichPageRtl(const RichPage &page); } // namespace Iv