mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-07 15:43:55 +02:00
feat: simple files reordering in send box
This commit is contained in:
parent
631f5a1043
commit
be378eaacb
2 changed files with 116 additions and 0 deletions
|
@ -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 <QApplication>
|
||||
#include <QBuffer>
|
||||
#include <QDrag>
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr auto kMaxMessageLength = 4096;
|
||||
|
||||
constexpr auto kDragMime = "application/x-tg-sendfile-index";
|
||||
|
||||
using Ui::SendFilesWay;
|
||||
|
||||
[[nodiscard]] inline bool CanAddUrls(const QList<QUrl> &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<void(Api::SendOptions)> 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<Ui::RpWidget*> w, int index) {
|
||||
w->setAcceptDrops(true);
|
||||
|
||||
const auto pressed = w->lifetime().make_state<rpl::variable<bool>>(false);
|
||||
const auto pressPos = w->lifetime().make_state<rpl::variable<QPoint>>();
|
||||
|
||||
base::install_event_filter(
|
||||
w,
|
||||
[=](not_null<QEvent*> e)
|
||||
{
|
||||
switch (e->type()) {
|
||||
case QEvent::MouseButtonPress: {
|
||||
const auto ev = static_cast<QMouseEvent*>(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<QMouseEvent*>(e.get());
|
||||
if ((ev->pos() - pressPos->current()).manhattanLength()
|
||||
>= QApplication::startDragDistance()) {
|
||||
pressed->force_assign(false);
|
||||
|
||||
const auto drag = std::make_unique<QDrag>(w);
|
||||
auto mime = std::make_unique<QMimeData>();
|
||||
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<QDragMoveEvent*>(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<QDropEvent*>(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;
|
||||
});
|
||||
}
|
||||
|
|
|
@ -310,4 +310,10 @@ private:
|
|||
QPointer<Ui::RoundButton> _send;
|
||||
QPointer<Ui::RoundButton> _addFile;
|
||||
|
||||
// AyuGram files reordering
|
||||
|
||||
[[nodiscard]] bool isFileBlock(int i) const;
|
||||
void moveFile(int from, int to);
|
||||
void setupDragForBlock(not_null<Ui::RpWidget*> w, int index);
|
||||
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue