From 3ac50cf77f2280d7903ff0aa3dec1e1e7136cbe7 Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Thu, 24 May 2018 17:57:41 +0300
Subject: [PATCH] Move message text to send media box caption field.

---
 Telegram/SourceFiles/boxes/send_files_box.cpp | 22 ++++++++--------
 Telegram/SourceFiles/boxes/send_files_box.h   |  1 +
 .../SourceFiles/history/history_widget.cpp    | 25 +++++++++++++++----
 3 files changed, 31 insertions(+), 17 deletions(-)

diff --git a/Telegram/SourceFiles/boxes/send_files_box.cpp b/Telegram/SourceFiles/boxes/send_files_box.cpp
index 4336a51f1..1782befac 100644
--- a/Telegram/SourceFiles/boxes/send_files_box.cpp
+++ b/Telegram/SourceFiles/boxes/send_files_box.cpp
@@ -1320,8 +1320,15 @@ void SendFilesBox::AlbumPreview::mouseReleaseEvent(QMouseEvent *e) {
 SendFilesBox::SendFilesBox(
 	QWidget*,
 	Storage::PreparedList &&list,
+	const TextWithTags &caption,
 	CompressConfirm compressed)
 : _list(std::move(list))
+, _caption(
+	this,
+	st::confirmCaptionArea,
+	Ui::InputField::Mode::MultiLine,
+	FieldPlaceholder(_list),
+	caption)
 , _compressConfirmInitial(compressed)
 , _compressConfirm(compressed) {
 }
@@ -1418,6 +1425,7 @@ void SendFilesBox::prepare() {
 
 	_send = addButton(langFactory(lng_send_button), [this] { send(); });
 	addButton(langFactory(lng_cancel), [this] { closeBox(); });
+	setupCaption();
 	initSendWay();
 	preparePreview();
 	subscribe(boxClosing, [this] {
@@ -1489,7 +1497,7 @@ void SendFilesBox::preparePreview() {
 void SendFilesBox::setupControls() {
 	setupTitleText();
 	setupSendWayControls();
-	setupCaption();
+	_caption->setPlaceholder(FieldPlaceholder(_list));
 }
 
 void SendFilesBox::setupSendWayControls() {
@@ -1546,23 +1554,13 @@ void SendFilesBox::applyAlbumOrder() {
 }
 
 void SendFilesBox::setupCaption() {
-	if (_caption) {
-		_caption->setPlaceholder(FieldPlaceholder(_list));
-		return;
-	}
-
-	_caption.create(
-		this,
-		st::confirmCaptionArea,
-		Ui::InputField::Mode::MultiLine,
-		FieldPlaceholder(_list));
 	_caption->setMaxLength(MaxPhotoCaption);
 	_caption->setSubmitSettings(Ui::InputField::SubmitSettings::Both);
 	connect(_caption, &Ui::InputField::resized, this, [this] {
 		captionResized();
 	});
 	connect(_caption, &Ui::InputField::submitted, this, [this](
-		bool ctrlShiftEnter) {
+			bool ctrlShiftEnter) {
 		send(ctrlShiftEnter);
 	});
 	connect(_caption, &Ui::InputField::cancelled, this, [this] {
diff --git a/Telegram/SourceFiles/boxes/send_files_box.h b/Telegram/SourceFiles/boxes/send_files_box.h
index 76cee507e..06a7259a6 100644
--- a/Telegram/SourceFiles/boxes/send_files_box.h
+++ b/Telegram/SourceFiles/boxes/send_files_box.h
@@ -33,6 +33,7 @@ public:
 	SendFilesBox(
 		QWidget*,
 		Storage::PreparedList &&list,
+		const TextWithTags &caption,
 		CompressConfirm compressed);
 
 	void setConfirmedCallback(
diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp
index 65272cfea..b2a3bfe4e 100644
--- a/Telegram/SourceFiles/history/history_widget.cpp
+++ b/Telegram/SourceFiles/history/history_widget.cpp
@@ -4163,7 +4163,15 @@ bool HistoryWidget::confirmSendingFiles(
 		? CompressConfirm::None
 		: compressed;
 
-	auto box = Box<SendFilesBox>(std::move(list), boxCompressConfirm);
+	const auto cursor = _field->textCursor();
+	const auto position = cursor.position();
+	const auto anchor = cursor.anchor();
+	const auto text = _field->getTextWithTags();
+	auto box = Box<SendFilesBox>(
+		std::move(list),
+		text,
+		boxCompressConfirm);
+	_field->setTextWithTags({});
 	box->setConfirmedCallback(base::lambda_guarded(this, [=](
 			Storage::PreparedList &&list,
 			SendFilesWay way,
@@ -4185,11 +4193,18 @@ bool HistoryWidget::confirmSendingFiles(
 			replyToId(),
 			album);
 	}));
-	if (!insertTextOnCancel.isEmpty()) {
-		box->setCancelledCallback(base::lambda_guarded(this, [=] {
+	box->setCancelledCallback(base::lambda_guarded(this, [=] {
+		_field->setTextWithTags(text);
+		auto cursor = _field->textCursor();
+		cursor.setPosition(anchor);
+		if (position != anchor) {
+			cursor.setPosition(position, QTextCursor::KeepAnchor);
+		}
+		_field->setTextCursor(cursor);
+		if (!insertTextOnCancel.isEmpty()) {
 			_field->textCursor().insertText(insertTextOnCancel);
-		}));
-	}
+		}
+	}));
 
 	ActivateWindowDelayed(controller());
 	Ui::show(std::move(box));