2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-09-05 08:55:59 +00:00

Improved message edition with pre-selected text.

This commit is contained in:
23rd
2024-03-17 03:46:11 +03:00
parent ddaf78828a
commit 2638ee2926
12 changed files with 69 additions and 16 deletions

View File

@@ -1037,3 +1037,28 @@ base::unique_qptr<Ui::RpWidget> PremiumRequiredSendRestriction(
});
return result;
}
void SelectTextInFieldWithMargins(
not_null<Ui::InputField*> field,
const TextSelection &selection) {
if (selection.empty()) {
return;
}
auto textCursor = field->textCursor();
// Try to set equal margins for top and bottom sides.
const auto charsCountInLine = field->width()
/ field->st().font->width('W');
const auto linesCount = (field->height() / field->st().font->height);
const auto selectedLines = (selection.to - selection.from)
/ charsCountInLine;
constexpr auto kMinDiff = ushort(3);
if ((linesCount - selectedLines) > kMinDiff) {
textCursor.setPosition(selection.from
- charsCountInLine * ((linesCount - 1) / 2));
field->setTextCursor(textCursor);
}
textCursor.setPosition(selection.from);
field->setTextCursor(textCursor);
textCursor.setPosition(selection.to, QTextCursor::KeepAnchor);
field->setTextCursor(textCursor);
}