Skip to content

Commit

Permalink
Improved message edition with pre-selected text.
Browse files Browse the repository at this point in the history
  • Loading branch information
23rd committed Mar 17, 2024
1 parent ddaf788 commit 2638ee2
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 16 deletions.
25 changes: 25 additions & 0 deletions Telegram/SourceFiles/chat_helpers/message_field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 4 additions & 0 deletions Telegram/SourceFiles/chat_helpers/message_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,7 @@ class MessageLinksParser final : private QObject {
QWidget *parent,
not_null<UserData*> user,
not_null<Window::SessionController*> controller);

void SelectTextInFieldWithMargins(
not_null<Ui::InputField*> field,
const TextSelection &selection);
12 changes: 11 additions & 1 deletion Telegram/SourceFiles/history/history_inner_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2131,7 +2131,17 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) {
if (editItem) {
const auto editItemId = editItem->fullId();
_menu->addAction(tr::lng_context_edit_msg(tr::now), [=] {
_widget->editMessage(editItemId);
if (const auto item = session->data().message(editItemId)) {
auto it = _selected.find(item);
const auto selection = ((it != _selected.end())
&& (it->second != FullSelection))
? it->second
: TextSelection();
if (!selection.empty()) {
clearSelected(true);
}
_widget->editMessage(item, selection);
}
}, &st::menuIconEdit);
}
const auto pinItem = (item->canPin() && item->isPinned())
Expand Down
13 changes: 5 additions & 8 deletions Telegram/SourceFiles/history/history_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6606,7 +6606,7 @@ void HistoryWidget::keyPressEvent(QKeyEvent *e) {
&& _field->empty()
&& !_editMsgId
&& !_replyTo) {
editMessage(item);
editMessage(item, {});
return;
}
_scroll->keyPressEvent(e);
Expand Down Expand Up @@ -7570,13 +7570,9 @@ void HistoryWidget::setReplyFieldsFromProcessing() {
setInnerFocus();
}

void HistoryWidget::editMessage(FullMsgId itemId) {
if (const auto item = session().data().message(itemId)) {
editMessage(item);
}
}

void HistoryWidget::editMessage(not_null<HistoryItem*> item) {
void HistoryWidget::editMessage(
not_null<HistoryItem*> item,
const TextSelection &selection) {
if (_chooseTheme) {
toggleChooseChatTheme(_peer);
} else if (_voiceRecordBar->isActive()) {
Expand Down Expand Up @@ -7625,6 +7621,7 @@ void HistoryWidget::editMessage(not_null<HistoryItem*> item) {
updateReplyToName();
updateControlsGeometry();
updateField();
SelectTextInFieldWithMargins(_field, selection);

_saveDraftText = true;
_saveDraftStart = crl::now();
Expand Down
5 changes: 3 additions & 2 deletions Telegram/SourceFiles/history/history_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,9 @@ class HistoryWidget final
not_null<HistoryItem*> item,
TextWithEntities quote = {},
int quoteOffset = 0);
void editMessage(FullMsgId itemId);
void editMessage(not_null<HistoryItem*> item);
void editMessage(
not_null<HistoryItem*> item,
const TextSelection &selection);

[[nodiscard]] FullReplyTo replyTo() const;
bool lastForceReplyReplied(const FullMsgId &replyTo) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2857,9 +2857,12 @@ void ComposeControls::updateHeight() {
}
}

void ComposeControls::editMessage(FullMsgId id) {
void ComposeControls::editMessage(
FullMsgId id,
const TextSelection &selection) {
if (const auto item = session().data().message(id)) {
editMessage(item);
SelectTextInFieldWithMargins(_field, selection);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class ComposeControls final {
void showFinished();
void raisePanels();

void editMessage(FullMsgId id);
void editMessage(FullMsgId id, const TextSelection &selection);
void cancelEditMessage();
void maybeCancelEditMessage(); // Confirm if changed and cancel.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2425,6 +2425,11 @@ SelectedItems ListWidget::getSelectedItems() const {
return collectSelectedItems();
}

const TextSelection &ListWidget::getSelectedTextRange(
not_null<HistoryItem*> item) const {
return _selectedTextRange;
}

int ListWidget::findItemIndexByY(int y) const {
Expects(!_items.empty());

Expand Down
2 changes: 2 additions & 0 deletions Telegram/SourceFiles/history/view/history_view_list_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ class ListWidget final
[[nodiscard]] TextForMimeData getSelectedText() const;
[[nodiscard]] MessageIdsList getSelectedIds() const;
[[nodiscard]] SelectedItems getSelectedItems() const;
[[nodiscard]] const TextSelection &getSelectedTextRange(
not_null<HistoryItem*> item) const;
void cancelSelection();
void selectItem(not_null<HistoryItem*> item);
void selectItemAsGroup(not_null<HistoryItem*> item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,9 @@ RepliesWidget::RepliesWidget(
if (const auto item = session().data().message(fullId)) {
const auto media = item->media();
if (!media || media->webpage() || media->allowsEditCaption()) {
_composeControls->editMessage(fullId);
_composeControls->editMessage(
fullId,
_inner->getSelectedTextRange(item));
}
}
}, _inner->lifetime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ ScheduledWidget::ScheduledWidget(
if (const auto item = session().data().message(fullId)) {
const auto media = item->media();
if (!media || media->webpage() || media->allowsEditCaption()) {
_composeControls->editMessage(fullId);
_composeControls->editMessage(
fullId,
_inner->getSelectedTextRange(item));
}
}
}, _inner->lifetime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,9 @@ ShortcutMessages::ShortcutMessages(
if (const auto item = _session->data().message(fullId)) {
const auto media = item->media();
if (!media || media->webpage() || media->allowsEditCaption()) {
_composeControls->editMessage(fullId);
_composeControls->editMessage(
fullId,
_inner->getSelectedTextRange(item));
}
}
}, _inner->lifetime());
Expand Down

0 comments on commit 2638ee2

Please sign in to comment.