From be378eaacbf70f7154b7b4b47434663c33b3fca7 Mon Sep 17 00:00:00 2001 From: AlexeyZavar Date: Mon, 19 May 2025 15:44:02 +0300 Subject: [PATCH] feat: simple files reordering in send box --- Telegram/SourceFiles/boxes/send_files_box.cpp | 110 ++++++++++++++++++ Telegram/SourceFiles/boxes/send_files_box.h | 6 + 2 files changed, 116 insertions(+) diff --git a/Telegram/SourceFiles/boxes/send_files_box.cpp b/Telegram/SourceFiles/boxes/send_files_box.cpp index 3e8182692e..d3b9c21035 100644 --- a/Telegram/SourceFiles/boxes/send_files_box.cpp +++ b/Telegram/SourceFiles/boxes/send_files_box.cpp @@ -70,13 +70,17 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/unixtime.h" #include "styles/style_menu_icons.h" #include "ayu/utils/telegram_helpers.h" +#include #include +#include namespace { constexpr auto kMaxMessageLength = 4096; +constexpr auto kDragMime = "application/x-tg-sendfile-index"; + using Ui::SendFilesWay; [[nodiscard]] inline bool CanAddUrls(const QList &urls) { @@ -1123,6 +1127,10 @@ void SendFilesBox::pushBlock(int from, int till) { block.takeWidget(), QMargins(0, _inner->count() ? st::sendMediaRowSkip : 0, 0, 0)); + if ((till - from) == 1 && isFileBlock(from)) { + setupDragForBlock(widget, from); + } + block.itemDeleteRequest( ) | rpl::filter([=] { return !_removingIndex; @@ -2010,3 +2018,105 @@ Fn SendFilesBox::sendCallback() { } SendFilesBox::~SendFilesBox() = default; + +// AyuGram files reordering + +bool SendFilesBox::isFileBlock(int i) const { + using Type = Ui::PreparedFile::Type; + const auto &f = _list.files[i]; + return (f.type == Type::File) + || (f.type == Type::Music) + || (f.type == Type::None) + || (f.type == Type::Photo && !_sendWay.current().sendImagesAsPhotos()); +} + +void SendFilesBox::moveFile(int from, int to) { + if (from < 0 || to < 0 + || from >= _list.files.size() + || to >= _list.files.size()) + return; + + if (from == to) return; + + std::swap(_list.files[from], _list.files[to]); + + refreshAllAfterChanges(std::min(from, to)); +} + +void SendFilesBox::setupDragForBlock(not_null w, int index) { + w->setAcceptDrops(true); + + const auto pressed = w->lifetime().make_state>(false); + const auto pressPos = w->lifetime().make_state>(); + + base::install_event_filter( + w, + [=](not_null e) + { + switch (e->type()) { + case QEvent::MouseButtonPress: { + const auto ev = static_cast(e.get()); + if (ev->button() == Qt::LeftButton) { + pressed->force_assign(true); + pressPos->force_assign(ev->pos()); + } + break; + } + case QEvent::MouseMove: { + if (pressed->current()) { + const auto ev = static_cast(e.get()); + if ((ev->pos() - pressPos->current()).manhattanLength() + >= QApplication::startDragDistance()) { + pressed->force_assign(false); + + const auto drag = std::make_unique(w); + auto mime = std::make_unique(); + mime->setData(kDragMime, QByteArray::number(index)); + drag->setMimeData(mime.release()); + drag->setPixmap(w->grab()); + drag->setHotSpot(ev->pos()); + drag->exec(Qt::MoveAction); + } + } + break; + } + case QEvent::MouseButtonRelease: pressed->force_assign(false); + break; + + case QEvent::DragEnter: + case QEvent::DragMove: { + const auto ev = static_cast(e.get()); + if (ev->mimeData()->hasFormat(kDragMime)) { + const auto from = ev->mimeData() + ->data(kDragMime).toInt(); + if (isFileBlock(from) && isFileBlock(index)) { + ev->acceptProposedAction(); + } + } + break; + } + case QEvent::Drop: { + const auto ev = static_cast(e.get()); + if (ev->mimeData()->hasFormat(kDragMime)) { + const auto from = ev->mimeData() + ->data(kDragMime).toInt(); + if (isFileBlock(from) && isFileBlock(index)) { + const auto dest = index; + QMetaObject::invokeMethod( + this, + [this, from, dest] + { + moveFile(from, dest); + }, + Qt::QueuedConnection); + + ev->acceptProposedAction(); + } + } + break; + } + default: break; + } + return base::EventFilterResult::Continue; + }); +} diff --git a/Telegram/SourceFiles/boxes/send_files_box.h b/Telegram/SourceFiles/boxes/send_files_box.h index 580dd97968..3607ce43ca 100644 --- a/Telegram/SourceFiles/boxes/send_files_box.h +++ b/Telegram/SourceFiles/boxes/send_files_box.h @@ -310,4 +310,10 @@ private: QPointer _send; QPointer _addFile; + // AyuGram files reordering + + [[nodiscard]] bool isFileBlock(int i) const; + void moveFile(int from, int to); + void setupDragForBlock(not_null w, int index); + };