2017-12-22 05:16:23 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-12-22 05:16:23 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-12-22 05:16:23 +00:00
|
|
|
*/
|
|
|
|
#include "boxes/edit_caption_box.h"
|
|
|
|
|
2020-05-28 14:57:21 +00:00
|
|
|
#include "api/api_editing.h"
|
2019-09-16 11:14:06 +00:00
|
|
|
#include "api/api_text_entities.h"
|
2021-07-09 11:59:48 +00:00
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "base/event_filter.h"
|
2022-05-11 09:59:38 +00:00
|
|
|
#include "boxes/premium_limits_box.h"
|
2019-03-26 21:14:51 +00:00
|
|
|
#include "chat_helpers/emoji_suggestions_widget.h"
|
|
|
|
#include "chat_helpers/message_field.h"
|
|
|
|
#include "chat_helpers/tabbed_panel.h"
|
|
|
|
#include "chat_helpers/tabbed_selector.h"
|
2020-06-18 18:04:16 +00:00
|
|
|
#include "core/application.h"
|
|
|
|
#include "core/core_settings.h"
|
2019-03-26 21:14:51 +00:00
|
|
|
#include "core/file_utilities.h"
|
|
|
|
#include "core/mime_type.h"
|
|
|
|
#include "data/data_document.h"
|
2020-05-25 14:16:04 +00:00
|
|
|
#include "data/data_photo_media.h"
|
2021-07-09 11:59:48 +00:00
|
|
|
#include "data/data_session.h"
|
2022-05-10 14:22:28 +00:00
|
|
|
#include "data/data_user.h"
|
2021-07-09 11:59:48 +00:00
|
|
|
#include "editor/photo_editor_layer_widget.h"
|
2020-06-13 13:08:55 +00:00
|
|
|
#include "history/history_drag_area.h"
|
2019-03-26 21:14:51 +00:00
|
|
|
#include "history/history_item.h"
|
2017-12-22 05:16:23 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2021-07-09 11:59:48 +00:00
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "main/main_session_settings.h"
|
2020-06-17 09:36:25 +00:00
|
|
|
#include "mtproto/mtproto_config.h"
|
2021-07-09 11:59:48 +00:00
|
|
|
#include "platform/platform_specific.h"
|
|
|
|
#include "storage/localimageloader.h" // SendMediaType
|
|
|
|
#include "storage/storage_media_prepare.h"
|
2022-05-11 09:59:38 +00:00
|
|
|
#include "ui/boxes/confirm_box.h"
|
2021-07-09 11:59:48 +00:00
|
|
|
#include "ui/chat/attach/attach_item_single_file_preview.h"
|
|
|
|
#include "ui/chat/attach/attach_item_single_media_preview.h"
|
|
|
|
#include "ui/chat/attach/attach_single_file_preview.h"
|
|
|
|
#include "ui/chat/attach/attach_single_media_preview.h"
|
2020-10-13 10:00:35 +00:00
|
|
|
#include "ui/controls/emoji_button.h"
|
2022-04-13 10:22:10 +00:00
|
|
|
#include "ui/effects/scroll_content_shadow.h"
|
2021-07-09 11:59:48 +00:00
|
|
|
#include "ui/image/image.h"
|
2020-10-16 14:11:23 +00:00
|
|
|
#include "ui/toast/toast.h"
|
2021-05-07 14:33:53 +00:00
|
|
|
#include "ui/ui_utility.h"
|
2021-07-09 11:59:48 +00:00
|
|
|
#include "ui/widgets/checkbox.h"
|
|
|
|
#include "ui/widgets/input_fields.h"
|
|
|
|
#include "ui/widgets/scroll_area.h"
|
|
|
|
#include "ui/wrap/slide_wrap.h"
|
|
|
|
#include "ui/wrap/vertical_layout.h"
|
2019-06-06 10:21:40 +00:00
|
|
|
#include "window/window_session_controller.h"
|
2019-09-18 11:19:05 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2020-10-10 09:15:37 +00:00
|
|
|
#include "styles/style_chat.h"
|
2021-07-09 11:59:48 +00:00
|
|
|
#include "styles/style_chat_helpers.h"
|
|
|
|
#include "styles/style_layers.h"
|
2021-02-20 07:52:48 +00:00
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtCore/QMimeData>
|
|
|
|
|
2020-05-20 11:04:56 +00:00
|
|
|
namespace {
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
auto ListFromMimeData(not_null<const QMimeData*> data, bool premium) {
|
2020-10-13 12:07:53 +00:00
|
|
|
using Error = Ui::PreparedList::Error;
|
2020-06-13 13:08:55 +00:00
|
|
|
auto result = data->hasUrls()
|
|
|
|
? Storage::PrepareMediaList(
|
|
|
|
// When we edit media, we need only 1 file.
|
|
|
|
data->urls().mid(0, 1),
|
2022-05-10 14:22:28 +00:00
|
|
|
st::sendMediaPreviewSize,
|
|
|
|
premium)
|
2020-10-13 12:07:53 +00:00
|
|
|
: Ui::PreparedList(Error::EmptyFile, QString());
|
2020-06-13 13:08:55 +00:00
|
|
|
if (result.error == Error::None) {
|
|
|
|
return result;
|
|
|
|
} else if (data->hasImage()) {
|
2021-08-10 13:15:05 +00:00
|
|
|
auto image = qvariant_cast<QImage>(data->imageData());
|
2020-06-13 13:08:55 +00:00
|
|
|
if (!image.isNull()) {
|
|
|
|
return Storage::PrepareMediaFromImage(
|
|
|
|
std::move(image),
|
|
|
|
QByteArray(),
|
|
|
|
st::sendMediaPreviewSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
Ui::AlbumType ComputeAlbumType(not_null<HistoryItem*> item) {
|
|
|
|
if (item->groupId().empty()) {
|
|
|
|
return Ui::AlbumType();
|
|
|
|
}
|
|
|
|
const auto media = item->media();
|
|
|
|
|
|
|
|
if (media->photo()) {
|
|
|
|
return Ui::AlbumType::PhotoVideo;
|
|
|
|
} else if (const auto document = media->document()) {
|
|
|
|
if (document->isVideoFile()) {
|
|
|
|
return Ui::AlbumType::PhotoVideo;
|
|
|
|
} else if (document->isSong()) {
|
|
|
|
return Ui::AlbumType::Music;
|
|
|
|
} else {
|
|
|
|
return Ui::AlbumType::File;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Ui::AlbumType();
|
|
|
|
}
|
|
|
|
|
2021-07-20 15:42:05 +00:00
|
|
|
bool CanBeCompressed(Ui::AlbumType type) {
|
|
|
|
return (type == Ui::AlbumType::None)
|
|
|
|
|| (type == Ui::AlbumType::PhotoVideo);
|
|
|
|
}
|
|
|
|
|
2020-05-20 11:04:56 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-12-22 05:16:23 +00:00
|
|
|
EditCaptionBox::EditCaptionBox(
|
|
|
|
QWidget*,
|
2019-06-06 10:21:40 +00:00
|
|
|
not_null<Window::SessionController*> controller,
|
2018-01-18 13:59:22 +00:00
|
|
|
not_null<HistoryItem*> item)
|
2018-05-26 14:58:21 +00:00
|
|
|
: _controller(controller)
|
2021-07-09 11:59:48 +00:00
|
|
|
, _historyItem(item)
|
|
|
|
, _isAllowedEditMedia(item->media()
|
|
|
|
? item->media()->allowsEditMedia()
|
|
|
|
: false)
|
|
|
|
, _albumType(ComputeAlbumType(item))
|
|
|
|
, _controls(base::make_unique_q<Ui::VerticalLayout>(this))
|
|
|
|
, _scroll(base::make_unique_q<Ui::ScrollArea>(this, st::boxScroll))
|
|
|
|
, _field(base::make_unique_q<Ui::InputField>(
|
|
|
|
this,
|
|
|
|
st::confirmCaptionArea,
|
|
|
|
Ui::InputField::Mode::MultiLine,
|
|
|
|
tr::lng_photo_caption(),
|
|
|
|
PrepareEditText(item)))
|
|
|
|
, _emojiToggle(base::make_unique_q<Ui::EmojiButton>(
|
|
|
|
this,
|
2022-04-13 10:22:10 +00:00
|
|
|
st::boxAttachEmoji)) {
|
2018-01-18 13:59:22 +00:00
|
|
|
Expects(item->media() != nullptr);
|
|
|
|
Expects(item->media()->allowsEditCaption());
|
2019-04-02 10:38:53 +00:00
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
_controller->session().data().itemRemoved(
|
|
|
|
_historyItem->fullId()
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
closeBox();
|
|
|
|
}, lifetime());
|
|
|
|
}
|
2020-10-20 08:19:48 +00:00
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
EditCaptionBox::~EditCaptionBox() = default;
|
2020-10-20 08:19:48 +00:00
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
void EditCaptionBox::prepare() {
|
|
|
|
addButton(tr::lng_settings_save(), [=] { save(); });
|
|
|
|
addButton(tr::lng_cancel(), [=] { closeBox(); });
|
|
|
|
|
|
|
|
updateBoxSize();
|
|
|
|
|
|
|
|
setupField();
|
|
|
|
setupEmojiPanel();
|
|
|
|
|
|
|
|
rebuildPreview();
|
|
|
|
setupEditEventHandler();
|
2022-04-13 10:22:10 +00:00
|
|
|
SetupShadowsToScrollContent(this, _scroll, _contentHeight.events());
|
2021-07-09 11:59:48 +00:00
|
|
|
|
|
|
|
setupControls();
|
|
|
|
setupPhotoEditorEventHandler();
|
|
|
|
|
|
|
|
setupDragArea();
|
|
|
|
|
|
|
|
captionResized();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditCaptionBox::rebuildPreview() {
|
|
|
|
const auto gifPaused = [controller = _controller] {
|
|
|
|
return controller->isGifPausedAtLeastFor(
|
|
|
|
Window::GifPauseReason::Layer);
|
2020-06-04 12:35:26 +00:00
|
|
|
};
|
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
if (_preparedList.files.empty()) {
|
|
|
|
const auto media = _historyItem->media();
|
|
|
|
const auto photo = media->photo();
|
|
|
|
const auto document = media->document();
|
|
|
|
if (photo || document->isVideoFile() || document->isAnimation()) {
|
2021-07-20 15:42:05 +00:00
|
|
|
_isPhoto = (photo != nullptr);
|
2021-07-09 11:59:48 +00:00
|
|
|
const auto media = Ui::CreateChild<Ui::ItemSingleMediaPreview>(
|
|
|
|
this,
|
|
|
|
gifPaused,
|
|
|
|
_historyItem,
|
|
|
|
Ui::AttachControls::Type::EditOnly);
|
|
|
|
_photoMedia = media->sharedPhotoMedia();
|
|
|
|
_content.reset(media);
|
2017-12-22 05:16:23 +00:00
|
|
|
} else {
|
2021-07-09 11:59:48 +00:00
|
|
|
_isPhoto = false;
|
|
|
|
_content.reset(Ui::CreateChild<Ui::ItemSingleFilePreview>(
|
|
|
|
this,
|
|
|
|
_historyItem,
|
|
|
|
Ui::AttachControls::Type::EditOnly));
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-07-09 11:59:48 +00:00
|
|
|
const auto &file = _preparedList.files.front();
|
|
|
|
|
|
|
|
const auto media = Ui::SingleMediaPreview::Create(
|
|
|
|
this,
|
|
|
|
gifPaused,
|
|
|
|
file,
|
|
|
|
Ui::AttachControls::Type::EditOnly);
|
|
|
|
if (media) {
|
|
|
|
_isPhoto = media->isPhoto();
|
|
|
|
_content.reset(media);
|
2017-12-22 05:16:23 +00:00
|
|
|
} else {
|
2021-07-09 11:59:48 +00:00
|
|
|
_isPhoto = false;
|
|
|
|
_content.reset(Ui::CreateChild<Ui::SingleFilePreview>(
|
|
|
|
this,
|
|
|
|
file,
|
|
|
|
Ui::AttachControls::Type::EditOnly));
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
2021-07-09 11:59:48 +00:00
|
|
|
}
|
|
|
|
Assert(_content != nullptr);
|
2018-08-02 14:31:54 +00:00
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
rpl::combine(
|
|
|
|
_content->heightValue(),
|
|
|
|
_footerHeight.value(),
|
|
|
|
rpl::single(st::boxPhotoPadding.top()),
|
|
|
|
rpl::mappers::_1 + rpl::mappers::_2 + rpl::mappers::_3
|
|
|
|
) | rpl::start_with_next([=](int height) {
|
|
|
|
setDimensions(
|
|
|
|
st::boxWideWidth,
|
|
|
|
std::min(st::sendMediaPreviewHeightMax, height),
|
|
|
|
true);
|
|
|
|
}, _content->lifetime());
|
2019-04-04 16:31:34 +00:00
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
_content->editRequests(
|
|
|
|
) | rpl::start_to_stream(_editMediaClicks, _content->lifetime());
|
2019-03-21 12:17:18 +00:00
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
_content->modifyRequests(
|
|
|
|
) | rpl::start_to_stream(_photoEditorOpens, _content->lifetime());
|
2017-12-22 05:16:23 +00:00
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
_content->heightValue(
|
|
|
|
) | rpl::start_to_stream(_contentHeight, _content->lifetime());
|
|
|
|
|
|
|
|
_scroll->setOwnedWidget(
|
|
|
|
object_ptr<Ui::RpWidget>::fromRaw(_content.get()));
|
|
|
|
|
2021-07-20 15:42:05 +00:00
|
|
|
_previewRebuilds.fire({});
|
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
captionResized();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditCaptionBox::setupField() {
|
2022-03-03 16:44:42 +00:00
|
|
|
const auto show = std::make_shared<Window::Show>(_controller);
|
|
|
|
const auto session = &_controller->session();
|
2020-06-17 09:36:25 +00:00
|
|
|
_field->setSubmitSettings(
|
2020-06-18 18:04:16 +00:00
|
|
|
Core::App().settings().sendSubmitWay());
|
2018-05-20 17:42:30 +00:00
|
|
|
_field->setInstantReplaces(Ui::InstantReplaces::Default());
|
2019-08-02 10:40:35 +00:00
|
|
|
_field->setInstantReplacesEnabled(
|
2020-06-18 18:04:16 +00:00
|
|
|
Core::App().settings().replaceEmojiValue());
|
2018-05-25 17:47:29 +00:00
|
|
|
_field->setMarkdownReplacesEnabled(rpl::single(true));
|
2019-08-02 10:40:35 +00:00
|
|
|
_field->setEditLinkCallback(
|
2022-03-03 16:44:42 +00:00
|
|
|
DefaultEditLinkCallback(show, session, _field));
|
2021-07-09 11:59:48 +00:00
|
|
|
_field->setMaxHeight(st::confirmCaptionArea.heightMax);
|
2019-03-27 10:44:57 +00:00
|
|
|
|
2022-03-03 16:44:42 +00:00
|
|
|
InitSpellchecker(show, session, _field);
|
2019-10-18 13:54:26 +00:00
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
connect(_field, &Ui::InputField::submitted, [=] { save(); });
|
|
|
|
connect(_field, &Ui::InputField::cancelled, [=] { closeBox(); });
|
|
|
|
connect(_field, &Ui::InputField::resized, [=] { captionResized(); });
|
|
|
|
_field->setMimeDataHook([=](
|
|
|
|
not_null<const QMimeData*> data,
|
|
|
|
Ui::InputField::MimeAction action) {
|
|
|
|
if (action == Ui::InputField::MimeAction::Check) {
|
|
|
|
if (!data->hasText() && !_isAllowedEditMedia) {
|
|
|
|
return false;
|
|
|
|
} else if (Storage::ValidateEditMediaDragData(data, _albumType)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return data->hasText();
|
|
|
|
} else if (action == Ui::InputField::MimeAction::Insert) {
|
|
|
|
return fileFromClipboard(data);
|
|
|
|
}
|
|
|
|
Unexpected("Action in MimeData hook.");
|
|
|
|
});
|
|
|
|
Ui::Emoji::SuggestionsController::Init(
|
|
|
|
getDelegate()->outerContainer(),
|
|
|
|
_field,
|
|
|
|
&_controller->session());
|
|
|
|
|
|
|
|
auto cursor = _field->textCursor();
|
|
|
|
cursor.movePosition(QTextCursor::End);
|
|
|
|
_field->setTextCursor(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditCaptionBox::setupControls() {
|
2021-07-20 15:42:05 +00:00
|
|
|
auto hintLabelToggleOn = _previewRebuilds.events_starting_with(
|
2022-03-11 05:55:21 +00:00
|
|
|
{}
|
2021-07-20 15:42:05 +00:00
|
|
|
) | rpl::map([=] {
|
2021-07-09 11:59:48 +00:00
|
|
|
return _controller->session().settings().photoEditorHintShown()
|
2021-07-20 15:42:05 +00:00
|
|
|
? _isPhoto
|
2021-07-09 11:59:48 +00:00
|
|
|
: false;
|
|
|
|
});
|
|
|
|
|
|
|
|
_controls->add(object_ptr<Ui::SlideWrap<Ui::FlatLabel>>(
|
2021-04-23 15:54:50 +00:00
|
|
|
this,
|
|
|
|
object_ptr<Ui::FlatLabel>(
|
|
|
|
this,
|
|
|
|
tr::lng_edit_photo_editor_hint(tr::now),
|
|
|
|
st::editMediaHintLabel),
|
2021-07-09 11:59:48 +00:00
|
|
|
st::editMediaLabelMargins)
|
|
|
|
)->toggleOn(std::move(hintLabelToggleOn), anim::type::instant);
|
|
|
|
|
|
|
|
_controls->add(object_ptr<Ui::SlideWrap<Ui::Checkbox>>(
|
2019-03-27 10:44:57 +00:00
|
|
|
this,
|
|
|
|
object_ptr<Ui::Checkbox>(
|
|
|
|
this,
|
2020-10-19 15:37:59 +00:00
|
|
|
tr::lng_send_compressed(tr::now),
|
|
|
|
true,
|
2019-03-27 10:44:57 +00:00
|
|
|
st::defaultBoxCheckbox),
|
2021-07-09 11:59:48 +00:00
|
|
|
st::editMediaCheckboxMargins)
|
|
|
|
)->toggleOn(
|
2022-03-11 05:55:21 +00:00
|
|
|
_previewRebuilds.events_starting_with({}) | rpl::map([=] {
|
2021-07-20 15:42:05 +00:00
|
|
|
return _isPhoto
|
|
|
|
&& CanBeCompressed(_albumType)
|
|
|
|
&& !_preparedList.files.empty();
|
2021-07-09 11:59:48 +00:00
|
|
|
}),
|
|
|
|
anim::type::instant
|
|
|
|
)->entity()->checkedChanges(
|
2019-03-27 10:44:57 +00:00
|
|
|
) | rpl::start_with_next([&](bool checked) {
|
2020-10-19 15:37:59 +00:00
|
|
|
_asFile = !checked;
|
2021-07-09 11:59:48 +00:00
|
|
|
}, _controls->lifetime());
|
2021-02-20 07:52:48 +00:00
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
_controls->resizeToWidth(st::sendMediaPreviewSize);
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
void EditCaptionBox::setupEditEventHandler() {
|
2022-06-14 09:48:25 +00:00
|
|
|
const auto toastParent = Ui::BoxShow(this).toastParent();
|
2019-04-01 19:09:14 +00:00
|
|
|
const auto callback = [=](FileDialog::OpenResult &&result) {
|
2022-06-14 09:48:25 +00:00
|
|
|
auto showError = [toastParent](tr::phrase<> t) {
|
|
|
|
Ui::Toast::Show(toastParent, t(tr::now));
|
2019-04-02 16:24:34 +00:00
|
|
|
};
|
|
|
|
|
2020-10-16 14:11:23 +00:00
|
|
|
const auto checkResult = [=](const Ui::PreparedList &list) {
|
|
|
|
if (list.files.size() != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto &file = list.files.front();
|
|
|
|
const auto mime = file.information->filemime;
|
|
|
|
if (Core::IsMimeSticker(mime)) {
|
|
|
|
showError(tr::lng_edit_media_invalid_file);
|
|
|
|
return false;
|
2020-10-20 08:19:48 +00:00
|
|
|
} else if (_albumType != Ui::AlbumType::None
|
|
|
|
&& !file.canBeInAlbumType(_albumType)) {
|
|
|
|
showError(tr::lng_edit_media_album_error);
|
|
|
|
return false;
|
2020-10-16 14:11:23 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2022-05-20 14:57:01 +00:00
|
|
|
const auto premium = _controller->session().premium();
|
2020-10-13 12:07:53 +00:00
|
|
|
auto list = Storage::PreparedFileFromFilesDialog(
|
2020-01-03 21:01:09 +00:00
|
|
|
std::move(result),
|
2020-10-16 14:11:23 +00:00
|
|
|
checkResult,
|
|
|
|
showError,
|
2022-05-10 14:22:28 +00:00
|
|
|
st::sendMediaPreviewSize,
|
|
|
|
premium);
|
2019-04-02 16:24:34 +00:00
|
|
|
|
2020-01-03 21:01:09 +00:00
|
|
|
if (list) {
|
2020-10-20 08:19:48 +00:00
|
|
|
setPreparedList(std::move(*list));
|
2019-03-29 10:51:19 +00:00
|
|
|
}
|
|
|
|
};
|
2019-03-24 12:39:19 +00:00
|
|
|
|
2019-03-30 08:53:28 +00:00
|
|
|
const auto buttonCallback = [=] {
|
2020-10-20 08:19:48 +00:00
|
|
|
const auto filters = (_albumType == Ui::AlbumType::PhotoVideo)
|
|
|
|
? FileDialog::PhotoVideoFilesFilter()
|
2020-01-05 20:37:41 +00:00
|
|
|
: FileDialog::AllFilesFilter();
|
2019-03-24 12:39:19 +00:00
|
|
|
FileDialog::GetOpenPath(
|
|
|
|
this,
|
2019-06-19 15:09:03 +00:00
|
|
|
tr::lng_choose_file(tr::now),
|
2020-01-05 20:37:41 +00:00
|
|
|
filters,
|
2019-03-24 12:39:19 +00:00
|
|
|
crl::guard(this, callback));
|
2019-03-30 08:53:28 +00:00
|
|
|
};
|
|
|
|
|
2019-04-04 16:31:34 +00:00
|
|
|
_editMediaClicks.events(
|
|
|
|
) | rpl::start_with_next(
|
|
|
|
buttonCallback,
|
|
|
|
lifetime());
|
2019-03-29 10:51:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
void EditCaptionBox::setupPhotoEditorEventHandler() {
|
2021-09-05 09:37:30 +00:00
|
|
|
const auto openedOnce = lifetime().make_state<bool>(false);
|
2021-07-09 11:59:48 +00:00
|
|
|
_photoEditorOpens.events(
|
|
|
|
) | rpl::start_with_next([=, controller = _controller] {
|
2021-09-05 09:37:30 +00:00
|
|
|
const auto increment = [=] {
|
|
|
|
if (*openedOnce) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*openedOnce = true;
|
|
|
|
controller->session().settings().incrementPhotoEditorHintShown();
|
|
|
|
controller->session().saveSettings();
|
|
|
|
};
|
2022-03-04 15:53:32 +00:00
|
|
|
const auto clearError = [=] {
|
|
|
|
_error = QString();
|
|
|
|
update();
|
|
|
|
};
|
2021-07-09 11:59:48 +00:00
|
|
|
const auto previewWidth = st::sendMediaPreviewSize;
|
|
|
|
if (!_preparedList.files.empty()) {
|
2021-09-05 09:37:30 +00:00
|
|
|
increment();
|
2022-03-04 15:53:32 +00:00
|
|
|
clearError();
|
2021-07-09 11:59:48 +00:00
|
|
|
Editor::OpenWithPreparedFile(
|
|
|
|
this,
|
|
|
|
controller,
|
|
|
|
&_preparedList.files.front(),
|
|
|
|
previewWidth,
|
|
|
|
[=] { rebuildPreview(); });
|
|
|
|
} else if (_photoMedia) {
|
|
|
|
const auto large = _photoMedia->image(Data::PhotoSize::Large);
|
|
|
|
if (!large) {
|
|
|
|
return;
|
2019-04-01 19:09:14 +00:00
|
|
|
}
|
2021-09-05 09:37:30 +00:00
|
|
|
increment();
|
2022-03-04 15:53:32 +00:00
|
|
|
clearError();
|
2021-07-09 11:59:48 +00:00
|
|
|
auto callback = [=](const Editor::PhotoModifications &mods) {
|
|
|
|
if (!mods || !_photoMedia) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto large = _photoMedia->image(Data::PhotoSize::Large);
|
|
|
|
if (!large) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto copy = large->original();
|
|
|
|
_preparedList = Storage::PrepareMediaFromImage(
|
|
|
|
std::move(copy),
|
|
|
|
QByteArray(),
|
|
|
|
previewWidth);
|
2017-12-22 05:16:23 +00:00
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
using ImageInfo = Ui::PreparedFileInformation::Image;
|
|
|
|
auto &file = _preparedList.files.front();
|
|
|
|
const auto image = std::get_if<ImageInfo>(
|
|
|
|
&file.information->media);
|
2020-10-20 08:19:48 +00:00
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
image->modifications = mods;
|
|
|
|
Storage::UpdateImageDetails(file, previewWidth);
|
|
|
|
rebuildPreview();
|
|
|
|
};
|
|
|
|
const auto fileImage = std::make_shared<Image>(*large);
|
|
|
|
controller->showLayer(
|
|
|
|
std::make_unique<Editor::LayerWidget>(
|
|
|
|
this,
|
|
|
|
&controller->window(),
|
|
|
|
fileImage,
|
|
|
|
Editor::PhotoModifications(),
|
|
|
|
std::move(callback)),
|
|
|
|
Ui::LayerOption::KeepOther);
|
2020-06-13 13:08:55 +00:00
|
|
|
}
|
2021-07-09 11:59:48 +00:00
|
|
|
}, lifetime());
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
void EditCaptionBox::setupDragArea() {
|
|
|
|
auto enterFilter = [=](not_null<const QMimeData*> data) {
|
|
|
|
return !_isAllowedEditMedia
|
|
|
|
? false
|
|
|
|
: Storage::ValidateEditMediaDragData(data, _albumType);
|
|
|
|
};
|
|
|
|
// Avoid both drag areas appearing at one time.
|
|
|
|
auto computeState = [=](const QMimeData *data) {
|
|
|
|
const auto state = Storage::ComputeMimeDataState(data);
|
|
|
|
return (state == Storage::MimeDataState::PhotoFiles)
|
|
|
|
? Storage::MimeDataState::Image
|
|
|
|
: state;
|
|
|
|
};
|
|
|
|
const auto areas = DragArea::SetupDragAreaToContainer(
|
|
|
|
this,
|
|
|
|
std::move(enterFilter),
|
|
|
|
[=](bool f) { _field->setAcceptDrops(f); },
|
|
|
|
nullptr,
|
|
|
|
std::move(computeState));
|
2021-04-22 20:39:41 +00:00
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
const auto droppedCallback = [=](bool compress) {
|
|
|
|
return [=](const QMimeData *data) {
|
|
|
|
fileFromClipboard(data);
|
|
|
|
Window::ActivateWindow(_controller);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
areas.document->setDroppedCallback(droppedCallback(false));
|
|
|
|
areas.photo->setDroppedCallback(droppedCallback(true));
|
2021-04-22 20:39:41 +00:00
|
|
|
}
|
|
|
|
|
2018-11-22 12:48:50 +00:00
|
|
|
void EditCaptionBox::setupEmojiPanel() {
|
|
|
|
const auto container = getDelegate()->outerContainer();
|
|
|
|
_emojiPanel = base::make_unique_q<ChatHelpers::TabbedPanel>(
|
|
|
|
container,
|
|
|
|
_controller,
|
|
|
|
object_ptr<ChatHelpers::TabbedSelector>(
|
|
|
|
nullptr,
|
|
|
|
_controller,
|
|
|
|
ChatHelpers::TabbedSelector::Mode::EmojiOnly));
|
|
|
|
_emojiPanel->setDesiredHeightValues(
|
|
|
|
1.,
|
|
|
|
st::emojiPanMinHeight / 2,
|
|
|
|
st::emojiPanMinHeight);
|
|
|
|
_emojiPanel->hide();
|
2019-08-16 12:44:20 +00:00
|
|
|
_emojiPanel->selector()->emojiChosen(
|
2018-11-22 12:48:50 +00:00
|
|
|
) | rpl::start_with_next([=](EmojiPtr emoji) {
|
|
|
|
Ui::InsertEmojiAtCursor(_field->textCursor(), emoji);
|
|
|
|
}, lifetime());
|
|
|
|
|
2019-09-05 10:51:36 +00:00
|
|
|
const auto filterCallback = [=](not_null<QEvent*> event) {
|
|
|
|
emojiFilterForGeometry(event);
|
2019-09-19 09:28:36 +00:00
|
|
|
return base::EventFilterResult::Continue;
|
2019-09-05 10:51:36 +00:00
|
|
|
};
|
2019-09-19 09:28:36 +00:00
|
|
|
_emojiFilter.reset(base::install_event_filter(container, filterCallback));
|
2018-11-22 12:48:50 +00:00
|
|
|
|
|
|
|
_emojiToggle->installEventFilter(_emojiPanel);
|
|
|
|
_emojiToggle->addClickHandler([=] {
|
|
|
|
_emojiPanel->toggleAnimated();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
void EditCaptionBox::emojiFilterForGeometry(not_null<QEvent*> event) {
|
|
|
|
const auto type = event->type();
|
|
|
|
if (type == QEvent::Move || type == QEvent::Resize) {
|
|
|
|
// updateEmojiPanelGeometry uses not only container geometry, but
|
|
|
|
// also container children geometries that will be updated later.
|
|
|
|
crl::on_main(this, [=] { updateEmojiPanelGeometry(); });
|
|
|
|
}
|
|
|
|
}
|
2020-06-13 13:08:55 +00:00
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
void EditCaptionBox::updateEmojiPanelGeometry() {
|
|
|
|
const auto parent = _emojiPanel->parentWidget();
|
|
|
|
const auto global = _emojiToggle->mapToGlobal({ 0, 0 });
|
|
|
|
const auto local = parent->mapFromGlobal(global);
|
|
|
|
_emojiPanel->moveBottomRight(
|
|
|
|
local.y(),
|
|
|
|
local.x() + _emojiToggle->width() * 3);
|
2020-06-13 13:08:55 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
bool EditCaptionBox::fileFromClipboard(not_null<const QMimeData*> data) {
|
2022-05-20 14:57:01 +00:00
|
|
|
const auto premium = _controller->session().premium();
|
2022-05-10 14:22:28 +00:00
|
|
|
return setPreparedList(ListFromMimeData(data, premium));
|
2021-01-09 23:04:03 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
bool EditCaptionBox::setPreparedList(Ui::PreparedList &&list) {
|
|
|
|
if (!_isAllowedEditMedia) {
|
|
|
|
return false;
|
2019-03-27 10:44:57 +00:00
|
|
|
}
|
2021-07-09 11:59:48 +00:00
|
|
|
using Error = Ui::PreparedList::Error;
|
|
|
|
if (list.error != Error::None || list.files.empty()) {
|
|
|
|
return false;
|
2021-04-23 15:54:50 +00:00
|
|
|
}
|
2021-07-09 11:59:48 +00:00
|
|
|
auto file = &list.files.front();
|
|
|
|
const auto invalidForAlbum = (_albumType != Ui::AlbumType::None)
|
|
|
|
&& !file->canBeInAlbumType(_albumType);
|
|
|
|
if (_albumType == Ui::AlbumType::PhotoVideo) {
|
|
|
|
using Video = Ui::PreparedFileInformation::Video;
|
|
|
|
if (const auto video = std::get_if<Video>(
|
|
|
|
&file->information->media)) {
|
|
|
|
video->isGifv = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (invalidForAlbum) {
|
2022-06-14 09:48:25 +00:00
|
|
|
Ui::Toast::Show(
|
|
|
|
Ui::BoxShow(this).toastParent(),
|
|
|
|
tr::lng_edit_media_album_error(tr::now));
|
2021-07-09 11:59:48 +00:00
|
|
|
return false;
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
2021-07-09 11:59:48 +00:00
|
|
|
_preparedList = std::move(list);
|
|
|
|
rebuildPreview();
|
|
|
|
return true;
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
void EditCaptionBox::captionResized() {
|
|
|
|
updateBoxSize();
|
|
|
|
resizeEvent(0);
|
|
|
|
updateEmojiPanelGeometry();
|
|
|
|
update();
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
void EditCaptionBox::updateBoxSize() {
|
|
|
|
auto footerHeight = 0;
|
2022-03-04 15:48:11 +00:00
|
|
|
footerHeight += st::normalFont->height + errorTopSkip();
|
2021-07-09 11:59:48 +00:00
|
|
|
if (_field) {
|
|
|
|
footerHeight += st::boxPhotoCaptionSkip + _field->height();
|
2020-05-20 11:04:56 +00:00
|
|
|
}
|
2021-07-09 11:59:48 +00:00
|
|
|
if (_controls && !_controls->isHidden()) {
|
|
|
|
footerHeight += _controls->heightNoMargins();
|
2020-05-20 11:04:56 +00:00
|
|
|
}
|
2021-07-09 11:59:48 +00:00
|
|
|
_footerHeight = footerHeight;
|
2020-05-20 11:04:56 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
int EditCaptionBox::errorTopSkip() const {
|
|
|
|
return (st::defaultBox.buttonPadding.top() / 2);
|
2020-05-20 11:04:56 +00:00
|
|
|
}
|
|
|
|
|
2017-12-22 05:16:23 +00:00
|
|
|
void EditCaptionBox::paintEvent(QPaintEvent *e) {
|
|
|
|
BoxContent::paintEvent(e);
|
|
|
|
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
if (!_error.isEmpty()) {
|
|
|
|
p.setFont(st::normalFont);
|
|
|
|
p.setPen(st::boxTextFgError);
|
2021-05-01 17:04:24 +00:00
|
|
|
p.drawTextLeft(
|
|
|
|
_field->x(),
|
|
|
|
_field->y() + _field->height() + errorTopSkip(),
|
|
|
|
width(),
|
|
|
|
_error);
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
2019-04-04 16:31:34 +00:00
|
|
|
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditCaptionBox::resizeEvent(QResizeEvent *e) {
|
|
|
|
BoxContent::resizeEvent(e);
|
2019-03-27 10:44:57 +00:00
|
|
|
|
2022-03-04 15:48:11 +00:00
|
|
|
const auto errorHeight = st::normalFont->height + errorTopSkip();
|
2021-07-09 11:59:48 +00:00
|
|
|
auto bottom = height();
|
2022-03-04 14:22:19 +00:00
|
|
|
{
|
|
|
|
const auto resultScrollHeight = bottom
|
|
|
|
- _field->height()
|
|
|
|
- st::boxPhotoCaptionSkip
|
|
|
|
- (_controls->isHidden() ? 0 : _controls->heightNoMargins())
|
2022-03-04 15:48:11 +00:00
|
|
|
- st::boxPhotoPadding.top()
|
|
|
|
- errorHeight;
|
2022-03-04 14:22:19 +00:00
|
|
|
const auto minThumbH = st::sendBoxAlbumGroupSize.height()
|
|
|
|
+ st::sendBoxAlbumGroupSkipTop * 2;
|
|
|
|
const auto diff = resultScrollHeight - minThumbH;
|
|
|
|
if (diff < 0) {
|
|
|
|
bottom -= diff;
|
|
|
|
}
|
|
|
|
}
|
2019-03-27 10:44:57 +00:00
|
|
|
|
2022-03-04 15:48:11 +00:00
|
|
|
bottom -= errorHeight;
|
2017-12-22 05:16:23 +00:00
|
|
|
_field->resize(st::sendMediaPreviewSize, _field->height());
|
2021-04-23 15:54:50 +00:00
|
|
|
_field->moveToLeft(
|
|
|
|
st::boxPhotoPadding.left(),
|
2021-07-09 11:59:48 +00:00
|
|
|
bottom - _field->height());
|
|
|
|
bottom -= st::boxPhotoCaptionSkip + _field->height();
|
2021-04-23 15:54:50 +00:00
|
|
|
|
2018-11-22 12:48:50 +00:00
|
|
|
_emojiToggle->moveToLeft(
|
|
|
|
(st::boxPhotoPadding.left()
|
|
|
|
+ st::sendMediaPreviewSize
|
|
|
|
- _emojiToggle->width()),
|
|
|
|
_field->y() + st::boxAttachEmojiTop);
|
2021-07-09 11:59:48 +00:00
|
|
|
_emojiToggle->update();
|
|
|
|
|
|
|
|
if (!_controls->isHidden()) {
|
2021-07-20 15:42:05 +00:00
|
|
|
_controls->resizeToWidth(width());
|
2021-07-09 11:59:48 +00:00
|
|
|
_controls->moveToLeft(
|
|
|
|
st::boxPhotoPadding.left(),
|
|
|
|
bottom - _controls->heightNoMargins());
|
|
|
|
bottom -= _controls->heightNoMargins();
|
|
|
|
}
|
|
|
|
_scroll->resize(width(), bottom - st::boxPhotoPadding.top());
|
|
|
|
_scroll->move(0, st::boxPhotoPadding.top());
|
|
|
|
|
|
|
|
if (_content) {
|
|
|
|
_content->resize(_scroll->width(), _content->height());
|
|
|
|
}
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditCaptionBox::setInnerFocus() {
|
|
|
|
_field->setFocusFast();
|
|
|
|
}
|
|
|
|
|
2022-05-11 09:59:38 +00:00
|
|
|
bool EditCaptionBox::validateLength(const QString &text) const {
|
|
|
|
const auto session = &_controller->session();
|
|
|
|
const auto limit = CurrentPremiumLimit(
|
|
|
|
session,
|
|
|
|
"caption_length_limit_default",
|
|
|
|
1024,
|
|
|
|
"caption_length_limit_premium",
|
|
|
|
2048);
|
|
|
|
const auto remove = int(text.size()) - limit;
|
|
|
|
if (remove <= 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
_controller->show(Box(CaptionLimitReachedBox, session, remove));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-22 05:16:23 +00:00
|
|
|
void EditCaptionBox::save() {
|
2021-07-09 11:59:48 +00:00
|
|
|
if (_saveRequestId) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-22 05:16:23 +00:00
|
|
|
|
2021-07-09 11:59:48 +00:00
|
|
|
const auto item = _controller->session().data().message(
|
|
|
|
_historyItem->fullId());
|
2017-12-22 05:16:23 +00:00
|
|
|
if (!item) {
|
2019-06-19 15:09:03 +00:00
|
|
|
_error = tr::lng_edit_deleted(tr::now);
|
2017-12-22 05:16:23 +00:00
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-31 18:28:37 +00:00
|
|
|
const auto textWithTags = _field->getTextWithAppliedMarkdown();
|
2022-05-11 09:59:38 +00:00
|
|
|
if (!validateLength(textWithTags.text)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-28 14:57:21 +00:00
|
|
|
const auto sending = TextWithEntities{
|
2018-05-24 13:03:21 +00:00
|
|
|
textWithTags.text,
|
2019-09-16 11:14:06 +00:00
|
|
|
TextUtilities::ConvertTextTagsToEntities(textWithTags.tags)
|
2018-05-24 13:03:21 +00:00
|
|
|
};
|
2019-03-24 12:39:19 +00:00
|
|
|
|
2020-05-30 08:47:32 +00:00
|
|
|
auto options = Api::SendOptions();
|
|
|
|
options.scheduled = item->isScheduled() ? item->date() : 0;
|
|
|
|
|
2019-04-01 19:09:14 +00:00
|
|
|
if (!_preparedList.files.empty()) {
|
2022-03-04 15:53:32 +00:00
|
|
|
if ((_albumType != Ui::AlbumType::None)
|
|
|
|
&& !_preparedList.files.front().canBeInAlbumType(
|
|
|
|
_albumType)) {
|
|
|
|
_error = tr::lng_edit_media_album_error(tr::now);
|
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
2021-11-09 15:24:13 +00:00
|
|
|
auto action = Api::SendAction(item->history(), options);
|
2021-03-09 09:11:40 +00:00
|
|
|
action.replaceMediaOf = item->fullId().msg;
|
2020-05-30 08:47:32 +00:00
|
|
|
|
2021-09-05 09:37:30 +00:00
|
|
|
Storage::ApplyModifications(_preparedList);
|
2021-02-20 07:52:48 +00:00
|
|
|
|
2019-07-24 14:00:30 +00:00
|
|
|
_controller->session().api().editMedia(
|
2019-03-26 21:14:51 +00:00
|
|
|
std::move(_preparedList),
|
2021-07-20 15:42:05 +00:00
|
|
|
(!_asFile && _isPhoto && CanBeCompressed(_albumType))
|
2021-07-09 11:59:48 +00:00
|
|
|
? SendMediaType::Photo
|
|
|
|
: SendMediaType::File,
|
2019-03-24 12:39:19 +00:00
|
|
|
_field->getTextWithAppliedMarkdown(),
|
2021-03-09 09:11:40 +00:00
|
|
|
action);
|
2019-03-24 12:39:19 +00:00
|
|
|
closeBox();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-28 14:57:21 +00:00
|
|
|
const auto done = crl::guard(this, [=](const MTPUpdates &updates) {
|
|
|
|
_saveRequestId = 0;
|
|
|
|
closeBox();
|
|
|
|
});
|
2017-12-22 05:16:23 +00:00
|
|
|
|
2021-03-12 12:48:00 +00:00
|
|
|
const auto fail = crl::guard(this, [=](const MTP::Error &error) {
|
2020-05-28 14:57:21 +00:00
|
|
|
_saveRequestId = 0;
|
|
|
|
const auto &type = error.type();
|
2020-05-30 07:10:54 +00:00
|
|
|
if (ranges::contains(Api::kDefaultEditMessagesErrors, type)) {
|
2020-05-28 14:57:21 +00:00
|
|
|
_error = tr::lng_edit_error(tr::now);
|
|
|
|
update();
|
|
|
|
} else if (type == u"MESSAGE_NOT_MODIFIED"_q) {
|
|
|
|
closeBox();
|
|
|
|
} else if (type == u"MESSAGE_EMPTY"_q) {
|
|
|
|
_field->setFocus();
|
|
|
|
_field->showError();
|
|
|
|
update();
|
|
|
|
} else {
|
|
|
|
_error = tr::lng_edit_error(tr::now);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
lifetime().add([=] {
|
|
|
|
if (_saveRequestId) {
|
|
|
|
auto &session = _controller->session();
|
|
|
|
session.api().request(base::take(_saveRequestId)).cancel();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-30 08:47:32 +00:00
|
|
|
_saveRequestId = Api::EditCaption(item, sending, options, done, fail);
|
2017-12-22 05:16:23 +00:00
|
|
|
}
|
2019-03-26 21:14:51 +00:00
|
|
|
|
2019-03-30 08:53:28 +00:00
|
|
|
void EditCaptionBox::keyPressEvent(QKeyEvent *e) {
|
2021-04-12 17:01:11 +00:00
|
|
|
const auto ctrl = e->modifiers().testFlag(Qt::ControlModifier);
|
|
|
|
if ((e->key() == Qt::Key_E) && ctrl) {
|
|
|
|
_photoEditorOpens.fire({});
|
|
|
|
} else if ((e->key() == Qt::Key_O) && ctrl) {
|
2019-03-30 08:53:28 +00:00
|
|
|
_editMediaClicks.fire({});
|
|
|
|
} else {
|
|
|
|
e->ignore();
|
|
|
|
}
|
|
|
|
}
|