From 1ee9122660020d7b59cf65b67624a0dadd863b9c Mon Sep 17 00:00:00 2001 From: John Preston Date: Sun, 25 Sep 2022 18:17:38 +0400 Subject: [PATCH] Fix text selection in recent actions. --- .../history/view/history_view_message.cpp | 42 +++++++++++++------ .../history/view/history_view_message.h | 2 + 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/Telegram/SourceFiles/history/view/history_view_message.cpp b/Telegram/SourceFiles/history/view/history_view_message.cpp index d88e674e67..e5afecbb6e 100644 --- a/Telegram/SourceFiles/history/view/history_view_message.cpp +++ b/Telegram/SourceFiles/history/view/history_view_message.cpp @@ -1606,7 +1606,8 @@ TextState Message::textState( result = entry->textState( point - QPoint(entryLeft, entryTop), request); - result.symbol += text().length() + (mediaDisplayed ? media->fullSelectionLength() : 0); + result.symbol += visibleTextLength() + + visibleMediaTextLength(); } } @@ -1624,7 +1625,7 @@ TextState Message::textState( result = bottomInfoResult; } }; - if (inBubble) { + if (!result.symbol && inBubble) { if (mediaDisplayed) { auto mediaHeight = media->height(); auto mediaLeft = trect.x() - st::msgPadding.left(); @@ -1632,18 +1633,18 @@ TextState Message::textState( if (point.y() >= mediaTop && point.y() < mediaTop + mediaHeight) { result = media->textState(point - QPoint(mediaLeft, mediaTop), request); - result.symbol += text().length(); + result.symbol += visibleTextLength(); } else if (getStateText(point, trect, &result, request)) { checkBottomInfoState(); return result; } else if (point.y() >= trect.y() + trect.height()) { - result.symbol = text().length(); + result.symbol = visibleTextLength(); } } else if (getStateText(point, trect, &result, request)) { checkBottomInfoState(); return result; } else if (point.y() >= trect.y() + trect.height()) { - result.symbol = text().length(); + result.symbol = visibleTextLength(); } } checkBottomInfoState(); @@ -1665,7 +1666,7 @@ TextState Message::textState( } } else if (media && media->isDisplayed()) { result = media->textState(point - g.topLeft(), request); - result.symbol += text().length(); + result.symbol += visibleTextLength(); } if (keyboard && item->isHistoryEntry()) { @@ -2002,7 +2003,9 @@ void Message::updatePressed(QPoint point) { TextForMimeData Message::selectedText(TextSelection selection) const { const auto media = this->media(); auto logEntryOriginalResult = TextForMimeData(); - auto textResult = text().toTextForMimeData(selection); + auto textResult = hasVisibleText() + ? text().toTextForMimeData(selection) + : TextForMimeData(); auto skipped = skipTextSelection(selection); auto mediaDisplayed = (media && media->isDisplayed()); auto mediaResult = (mediaDisplayed || isHiddenByGroup()) @@ -2033,8 +2036,10 @@ TextSelection Message::adjustSelection( TextSelectType type) const { const auto media = this->media(); - auto result = text().adjustSelection(selection, type); - auto beforeMediaLength = text().length(); + auto result = hasVisibleText() + ? text().adjustSelection(selection, type) + : selection; + auto beforeMediaLength = visibleTextLength(); if (selection.to <= beforeMediaLength) { return result; } @@ -2048,8 +2053,7 @@ TextSelection Message::adjustSelection( result.to = mediaSelection.to; } } - auto beforeEntryLength = beforeMediaLength - + (mediaDisplayed ? media->fullSelectionLength() : 0); + auto beforeEntryLength = beforeMediaLength + visibleMediaTextLength(); if (selection.to <= beforeEntryLength) { return result; } @@ -2887,13 +2891,16 @@ void Message::fromNameUpdated(int width) const { } TextSelection Message::skipTextSelection(TextSelection selection) const { - if (selection.from == 0xFFFF) { + if (selection.from == 0xFFFF || !hasVisibleText()) { return selection; } return HistoryView::UnshiftItemSelection(selection, text()); } TextSelection Message::unskipTextSelection(TextSelection selection) const { + if (!hasVisibleText()) { + return selection; + } return HistoryView::ShiftItemSelection(selection, text()); } @@ -3160,6 +3167,17 @@ bool Message::hasVisibleText() const { return !media || !media->hideMessageText(); } +int Message::visibleTextLength() const { + return hasVisibleText() ? text().length() : 0; +} + +int Message::visibleMediaTextLength() const { + const auto media = this->media(); + return (media && media->isDisplayed()) + ? media->fullSelectionLength() + : 0; +} + QSize Message::performCountCurrentSize(int newWidth) { const auto newHeight = resizeContentGetHeight(newWidth); diff --git a/Telegram/SourceFiles/history/view/history_view_message.h b/Telegram/SourceFiles/history/view/history_view_message.h index 099e8a476a..c0d626974e 100644 --- a/Telegram/SourceFiles/history/view/history_view_message.h +++ b/Telegram/SourceFiles/history/view/history_view_message.h @@ -226,6 +226,8 @@ private: QSize performCountOptimalSize() override; QSize performCountCurrentSize(int newWidth) override; bool hasVisibleText() const override; + [[nodiscard]] int visibleTextLength() const; + [[nodiscard]] int visibleMediaTextLength() const; [[nodiscard]] bool needInfoDisplay() const; [[nodiscard]] bool isPinnedContext() const;