Added simple colorizing of query in found messages.

This commit is contained in:
23rd 2025-08-07 19:22:53 +03:00
parent a28f113105
commit 5c33a2bd5c
5 changed files with 49 additions and 0 deletions

View file

@ -1240,6 +1240,7 @@ void InnerWidget::paintEvent(QPaintEvent *e) {
auto to = ceilclamp(r.y() + r.height() - skip, _st->height, 0, _previewResults.size());
p.translate(0, from * _st->height);
if (from < _previewResults.size()) {
const auto searchLowerText = _searchState.query.toLower();
for (; from < to; ++from) {
const auto &result = _previewResults[from];
const auto active = isSearchResultActive(result.get(), activeEntry);
@ -1257,6 +1258,7 @@ void InnerWidget::paintEvent(QPaintEvent *e) {
.currentBg = currentBg(),
.filter = _filterId,
.now = ms,
.searchLowerText = QStringView(searchLowerText),
.width = fullWidth,
.active = active,
.selected = selected,
@ -1284,6 +1286,7 @@ void InnerWidget::paintEvent(QPaintEvent *e) {
tr::now,
lt_count,
_searchedMigratedCount + _searchedCount);
const auto searchLowerText = _searchState.query.toLower();
p.fillRect(0, 0, fullWidth, st::searchedBarHeight, st::searchedBarBg);
p.setFont(st::searchedBarFont);
p.setPen(st::searchedBarFg);
@ -1331,6 +1334,7 @@ void InnerWidget::paintEvent(QPaintEvent *e) {
.currentBg = currentBg(),
.filter = _filterId,
.now = ms,
.searchLowerText = QStringView(searchLowerText),
.width = fullWidth,
.active = active,
.selected = selected,

View file

@ -1137,6 +1137,7 @@ void RowPainter::Paint(
return {};
}();
previewOptions.ignoreGroup = true;
previewOptions.searchLowerText = context.searchLowerText;
const auto badgesState = context.displayUnreadInfo
? entry->chatListBadgesState()

View file

@ -64,6 +64,7 @@ struct PaintContext {
FilterId filter = 0;
float64 topicsExpanded = 0.;
crl::time now = 0;
QStringView searchLowerText;
int width = 0;
bool active = false;
bool selected = false;

View file

@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "history/history.h"
#include "history/history_item.h"
#include "history/view/history_view_element.h"
#include "history/view/history_view_item_preview.h"
#include "main/main_session.h"
#include "dialogs/dialogs_three_state_icon.h"
@ -207,6 +208,47 @@ void MessageView::prepare(
}
TextUtilities::Trim(preview.text);
auto textToCache = DialogsPreviewText(std::move(preview.text));
if (!options.searchLowerText.isEmpty()) {
static constexpr auto kLeftShift = 15;
auto minFrom = std::numeric_limits<uint16>::max();
const auto words = Ui::Text::Words(options.searchLowerText);
textToCache.entities.reserve(textToCache.entities.size()
+ words.size());
for (const auto &word : words) {
const auto selection = HistoryView::FindSearchQueryHighlight(
textToCache.text,
word);
if (!selection.empty()) {
minFrom = std::min(minFrom, selection.from);
textToCache.entities.push_back(EntityInText{
EntityType::Colorized,
selection.from,
selection.to - selection.from
});
}
}
if (!words.empty() && minFrom != std::numeric_limits<uint16>::max()) {
std::sort(
textToCache.entities.begin(),
textToCache.entities.end(),
[](const auto &a, const auto &b) {
return a.offset() < b.offset();
});
const auto textSize = textToCache.text.size();
minFrom = (minFrom > textSize || minFrom < kLeftShift)
? 0
: minFrom - kLeftShift;
textToCache = TextWithEntities(
minFrom > 0 ? kQEllipsis : QString())
.append(Text::Mid(std::move(textToCache), minFrom));
}
}
_hasPlainLinkAtBegin = !textToCache.entities.empty()
&& (textToCache.entities.front().type() == EntityType::Colorized);
_textCache.setMarkedText(

View file

@ -41,6 +41,7 @@ struct ItemPreview {
struct ToPreviewOptions {
const std::vector<ItemPreviewImage> *existing = nullptr;
QStringView searchLowerText;
bool hideSender = false;
bool hideCaption = false;
bool ignoreMessageText = false;