From a1bb9cbb2b4d6b35387932d9133daabdf6c3f884 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Thu, 9 Jan 2020 18:11:51 +0300 Subject: [PATCH] Added ability to delete and edit files in SendFilesBox. --- Telegram/SourceFiles/boxes/boxes.style | 13 ++- Telegram/SourceFiles/boxes/send_files_box.cpp | 93 +++++++++++++++---- 2 files changed, 82 insertions(+), 24 deletions(-) diff --git a/Telegram/SourceFiles/boxes/boxes.style b/Telegram/SourceFiles/boxes/boxes.style index ed7d04eed..74eb4df04 100644 --- a/Telegram/SourceFiles/boxes/boxes.style +++ b/Telegram/SourceFiles/boxes/boxes.style @@ -522,15 +522,18 @@ sendBoxAlbumGroupSkipTop: 6px; sendBoxAlbumGroupRadius: 12px; sendBoxAlbumGroupHeight: 25px; -sendBoxAlbumGroupEditButtonIcon: icon {{ "settings_edit", msgServiceFg }}; +sendBoxAlbumGroupEditButtonIcon: editMediaButtonIconPhoto; sendBoxAlbumGroupEditButtonIconPosition: point(4px, -1px); -sendBoxAlbumGroupButton: IconButton { - width: sendBoxAlbumGroupHeight; - height: sendBoxAlbumGroupHeight; + +sendBoxAlbumGroupButtonFile: IconButton(editMediaButton) { + ripple: RippleAnimation(defaultRippleAnimation) { + color: windowBgRipple; + } } -sendBoxAlbumGroupDeleteButtonIcon: icon {{ "history_file_cancel", msgServiceFg}}; sendBoxAlbumGroupDeleteButtonIconPosition: point(-3px, 0px); +sendBoxAlbumGroupDeleteButtonIcon: icon {{ "history_file_cancel", msgServiceFg}}; +sendBoxAlbumGroupDeleteButtonIconFile: icon {{ "history_file_cancel", menuIconFg, point(6px, 6px) }}; // End of SendFilesBox diff --git a/Telegram/SourceFiles/boxes/send_files_box.cpp b/Telegram/SourceFiles/boxes/send_files_box.cpp index 6b04369d4..a7b78b697 100644 --- a/Telegram/SourceFiles/boxes/send_files_box.cpp +++ b/Telegram/SourceFiles/boxes/send_files_box.cpp @@ -231,7 +231,10 @@ class AlbumThumb { public: AlbumThumb( const Storage::PreparedFile &file, - const Ui::GroupMediaLayout &layout); + const Ui::GroupMediaLayout &layout, + QWidget *parent, + Fn editCallback, + Fn deleteCallback); void moveToLayout(const Ui::GroupMediaLayout &layout); void animateLayoutToInitial(); @@ -258,10 +261,7 @@ public: void suggestMove(float64 delta, Fn callback); void finishAnimations(); - void addAlbumThumbButtons( - QWidget *parent, - Fn editCallback, - Fn deleteCallback); + void updateFileRow(int row); private: QRect countRealGeometry() const; @@ -297,7 +297,10 @@ private: AlbumThumb::AlbumThumb( const Storage::PreparedFile &file, - const Ui::GroupMediaLayout &layout) + const Ui::GroupMediaLayout &layout, + QWidget *parent, + Fn editCallback, + Fn deleteCallback) : _layout(layout) , _fullPreview(file.preview) , _shrinkSize(int(std::ceil(st::historyMessageRadius / 1.4))) @@ -338,7 +341,11 @@ AlbumThumb::AlbumThumb( const auto availableFileWidth = st::sendMediaPreviewSize - st::sendMediaFileThumbSkip - - st::sendMediaFileThumbSize; + - st::sendMediaFileThumbSize + // Right buttons. + - st::sendBoxAlbumGroupButtonFile.width * 2 + - st::sendBoxAlbumGroupEditInternalSkip + - st::sendBoxAlbumGroupSkipRight; const auto filepath = file.path; if (filepath.isEmpty()) { _name = filedialogDefaultName( @@ -365,17 +372,45 @@ AlbumThumb::AlbumThumb( _nameWidth = st::semiboldFont->width(_name); } _statusWidth = st::normalFont->width(_status); + + _editMedia.create(parent, st::sendBoxAlbumGroupButtonFile); + _deleteMedia.create(parent, st::sendBoxAlbumGroupButtonFile); + + const auto duration = st::historyAttach.ripple.hideDuration; + _editMedia->setClickedCallback(App::LambdaDelayed( + duration, + parent, + std::move(editCallback))); + _deleteMedia->setClickedCallback(App::LambdaDelayed( + duration, + parent, + std::move(deleteCallback))); + + _editMedia->setIconOverride(&st::editMediaButtonIconFile); + _deleteMedia->setIconOverride(&st::sendBoxAlbumGroupDeleteButtonIconFile); + + updateFileRow(-1); } -void AlbumThumb::addAlbumThumbButtons( - QWidget *parent, - Fn editCallback, - Fn deleteCallback) { - _editMedia.create(parent, st::sendBoxAlbumGroupButton); - _deleteMedia.create(parent, st::sendBoxAlbumGroupButton); +void AlbumThumb::updateFileRow(int row) { + if (row < 0) { + _editMedia->hide(); + _deleteMedia->hide(); + return; + } + _editMedia->show(); + _deleteMedia->show(); - _editMedia->setClickedCallback(std::move(editCallback)); - _deleteMedia->setClickedCallback(std::move(deleteCallback)); + const auto fileHeight = st::sendMediaFileThumbSize + + st::sendMediaFileThumbSkip; + + const auto top = row * fileHeight + st::sendBoxAlbumGroupSkipTop; + const auto size = st::editMediaButtonSize; + + auto right = st::sendBoxAlbumGroupSkipRight + size; + _deleteMedia->moveToRight(right, top); + right += st::sendBoxAlbumGroupEditInternalSkip + size; + _editMedia->moveToRight(right, top); } void AlbumThumb::resetLayoutAnimation() { @@ -1136,6 +1171,7 @@ private: void prepareThumbs(); void updateSizeAnimated(const std::vector &layout); void updateSize(); + void updateFileRows(); int thumbIndex(AlbumThumb *thumb); AlbumThumb *thumbUnderCursor(); @@ -1192,6 +1228,7 @@ SendFilesBox::AlbumPreview::AlbumPreview( setMouseTracking(true); prepareThumbs(); updateSize(); + updateFileRows(); } void SendFilesBox::AlbumPreview::setSendWay(SendFilesWay way) { @@ -1200,9 +1237,18 @@ void SendFilesBox::AlbumPreview::setSendWay(SendFilesWay way) { _sendWay = way; } updateSize(); + updateFileRows(); update(); } +void SendFilesBox::AlbumPreview::updateFileRows() { + Expects(_order.size() == _thumbs.size()); + const auto isFile = (_sendWay == SendFilesWay::Files); + for (auto i = 0; i < _order.size(); i++) { + _thumbs[i]->updateFileRow(isFile ? _order[i] : -1); + } +} + std::vector SendFilesBox::AlbumPreview::takeOrder() { auto reordered = std::vector>(); reordered.reserve(_thumbs.size()); @@ -1244,7 +1290,10 @@ void SendFilesBox::AlbumPreview::prepareThumbs() { for (auto i = 0; i != count; ++i) { _thumbs.push_back(std::make_unique( _list.files[i], - layout[i])); + layout[i], + this, + [=] { changeThumbByIndex(thumbIndex(thumbUnderCursor())); }, + [=] { deleteThumbByIndex(thumbIndex(thumbUnderCursor())); })); } _thumbsHeight = countLayoutHeight(layout); _photosHeight = ranges::accumulate(ranges::view::all( @@ -1269,13 +1318,19 @@ AlbumThumb *SendFilesBox::AlbumPreview::findThumb(QPoint position) const { position -= QPoint(contentLeft(), contentTop()); auto top = 0; + const auto isPhotosWay = (_sendWay == SendFilesWay::Photos); + const auto skip = isPhotosWay + ? st::sendMediaPreviewPhotoSkip + : st::sendMediaFileThumbSkip; auto find = [&](const auto &thumb) { if (_sendWay == SendFilesWay::Album) { return thumb->containsPoint(position); - } else if (_sendWay == SendFilesWay::Photos) { - const auto bottom = top + thumb->photoHeight(); + } else if (isPhotosWay || _sendWay == SendFilesWay::Files) { + const auto bottom = top + (isPhotosWay + ? thumb->photoHeight() + : st::sendMediaFileThumbSize); const auto isUnderTop = (position.y() > top); - top = bottom + st::sendMediaPreviewPhotoSkip; + top = bottom + skip; return isUnderTop && (position.y() < bottom); } return false;