From e0750f7b87d0e3cd05a06f010fa4b4a87bd90ed8 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sat, 13 Jun 2020 17:13:56 +0300 Subject: [PATCH] Added drag'n'drop area to SendFilesBox for images. --- Telegram/SourceFiles/boxes/send_files_box.cpp | 38 ++++++++++++++++++- Telegram/SourceFiles/boxes/send_files_box.h | 2 + 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/Telegram/SourceFiles/boxes/send_files_box.cpp b/Telegram/SourceFiles/boxes/send_files_box.cpp index 57f9faa2e8..32ff2af777 100644 --- a/Telegram/SourceFiles/boxes/send_files_box.cpp +++ b/Telegram/SourceFiles/boxes/send_files_box.cpp @@ -19,6 +19,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "chat_helpers/tabbed_panel.h" #include "chat_helpers/tabbed_selector.h" #include "confirm_box.h" +#include "history/history_drag_area.h" #include "history/view/history_view_schedule_box.h" #include "core/file_utilities.h" #include "core/mime_type.h" @@ -333,7 +334,7 @@ AlbumThumb::AlbumThumb( - st::sendMediaFileThumbSize // Right buttons. - st::sendBoxAlbumGroupButtonFile.width * 2 - - st::sendBoxAlbumGroupEditInternalSkip + - st::sendBoxAlbumGroupEditInternalSkip * 2 - st::sendBoxAlbumGroupSkipRight; const auto filepath = file.path; if (filepath.isEmpty()) { @@ -1858,6 +1859,40 @@ void SendFilesBox::prepare() { })); updateLeftButtonVisibility(); + setupDragArea(); +} + +void SendFilesBox::setupDragArea() { + // 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 == Storage::MimeDataState::Files) + // Temporary enable drag'n'drop only for images. TODO. + ? Storage::MimeDataState::None + : state; + }; + const auto areas = DragArea::SetupDragAreaToContainer( + this, + [=](not_null d) { return canAddFiles(d); }, + [=](bool f) { _caption->setAcceptDrops(f); }, + [=] { updateControlsGeometry(); }, + std::move(computeState)); + + const auto droppedCallback = [=](bool compress) { + return [=](const QMimeData *data) { + addFiles(data); + Window::ActivateWindow(_controller); + }; + }; + areas.document->setDroppedCallback(droppedCallback(false)); + areas.photo->setDroppedCallback(droppedCallback(true)); + _albumChanged.events( + ) | rpl::start_with_next([=] { + areas.document->raise(); + areas.photo->raise(); + }, lifetime()); } void SendFilesBox::updateLeftButtonVisibility() { @@ -1875,6 +1910,7 @@ void SendFilesBox::refreshAllAfterAlbumChanges() { preparePreview(); captionResized(); updateLeftButtonVisibility(); + _albumChanged.fire({}); } void SendFilesBox::openDialogToAddFileToAlbum() { diff --git a/Telegram/SourceFiles/boxes/send_files_box.h b/Telegram/SourceFiles/boxes/send_files_box.h index 956d9d2c9f..84fe80ad8e 100644 --- a/Telegram/SourceFiles/boxes/send_files_box.h +++ b/Telegram/SourceFiles/boxes/send_files_box.h @@ -115,6 +115,7 @@ private: void sendScheduled(); void captionResized(); + void setupDragArea(); void setupTitleText(); void updateBoxSize(); void updateControlsGeometry(); @@ -163,6 +164,7 @@ private: std::shared_ptr> _sendWay; rpl::variable _footerHeight = 0; + rpl::event_stream<> _albumChanged; QWidget *_preview = nullptr; AlbumPreview *_albumPreview = nullptr;