2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-31 06:26:18 +00:00

Fix sending many files with a comment.

This commit is contained in:
John Preston
2020-10-17 12:51:55 +03:00
parent 85d08c8f52
commit bb4fdde616
15 changed files with 246 additions and 295 deletions

View File

@@ -137,7 +137,7 @@ AlbumThumbnail *AlbumPreview::findThumb(QPoint position) const {
? st::sendMediaPreviewPhotoSkip
: st::sendMediaFileThumbSkip;
auto find = [&](const auto &thumb) {
if (_sendWay.groupMediaInAlbums()) {
if (_sendWay.groupFiles() && _sendWay.sendImagesAsPhotos()) {
return thumb->containsPoint(position);
} else {
const auto bottom = top + (isPhotosWay
@@ -258,13 +258,13 @@ void AlbumPreview::updateSizeAnimated(
void AlbumPreview::updateSize() {
const auto newHeight = [&] {
if (_sendWay.groupMediaInAlbums()) {
return int(std::round(_thumbsHeightAnimation.value(
_thumbsHeight)));
} else if (_sendWay.sendImagesAsPhotos()) {
if (!_sendWay.sendImagesAsPhotos()) {
return _filesHeight;
} else if (!_sendWay.groupFiles()) {
return _photosHeight;
} else {
return _filesHeight;
return int(std::round(_thumbsHeightAnimation.value(
_thumbsHeight)));
}
}();
if (height() != newHeight) {
@@ -275,12 +275,12 @@ void AlbumPreview::updateSize() {
void AlbumPreview::paintEvent(QPaintEvent *e) {
Painter p(this);
if (_sendWay.groupMediaInAlbums()) {
paintAlbum(p);
} else if (_sendWay.sendImagesAsPhotos()) {
if (!_sendWay.sendImagesAsPhotos()) {
paintFiles(p, e->rect());
} else if (!_sendWay.groupFiles()) {
paintPhotos(p, e->rect());
} else {
paintFiles(p, e->rect());
paintAlbum(p);
}
}
@@ -409,7 +409,8 @@ void AlbumPreview::mouseMoveEvent(QMouseEvent *e) {
applyCursor(style::cur_default);
return;
}
const auto isAlbum = _sendWay.groupMediaInAlbums();
const auto isAlbum = _sendWay.sendImagesAsPhotos()
&& _sendWay.groupFiles();
if (isAlbum && _draggedThumb) {
const auto position = e->pos();
_draggedThumb->moveInAlbum(position - _draggedStartPosition);

View File

@@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "ui/chat/attach/attach_prepare.h"
#include "ui/chat/attach/attach_send_files_way.h"
#include "core/mime_type.h"
namespace Ui {
@@ -21,7 +22,7 @@ PreparedFile::PreparedFile(const QString &path) : path(path) {
PreparedFile::PreparedFile(PreparedFile &&other) = default;
PreparedFile &PreparedFile::operator=(PreparedFile && other) = default;
PreparedFile &PreparedFile::operator=(PreparedFile &&other) = default;
PreparedFile::~PreparedFile() = default;
@@ -90,7 +91,7 @@ bool PreparedList::canBeSentInSlowmodeWith(const PreparedList &other) const {
return !hasNonGrouping && (!hasFiles || !hasVideos);
}
bool PreparedList::canAddCaption(bool groupMediaInAlbums) const {
bool PreparedList::canAddCaption(bool sendingAlbum) const {
if (!filesToProcess.empty()
|| files.empty()
|| files.size() > kMaxAlbumCount) {
@@ -104,14 +105,18 @@ bool PreparedList::canAddCaption(bool groupMediaInAlbums) const {
qstr(".tgs"),
Qt::CaseInsensitive);
return !isSticker;
} else if (!groupMediaInAlbums) {
} else if (!sendingAlbum) {
return false;
}
const auto hasFiles = ranges::contains(
files,
PreparedFile::AlbumType::File,
&PreparedFile::type);
return !hasFiles;
const auto hasNotGrouped = ranges::contains(
files,
PreparedFile::AlbumType::None,
&PreparedFile::type);
return !hasFiles && !hasNotGrouped;
}
int MaxAlbumItems() {
@@ -125,4 +130,53 @@ bool ValidateThumbDimensions(int width, int height) {
&& (height < 20 * width);
}
std::vector<PreparedGroup> DivideByGroups(
PreparedList &&list,
SendFilesWay way,
bool slowmode) {
const auto sendImagesAsPhotos = way.sendImagesAsPhotos();
const auto groupFiles = way.groupFiles() || slowmode;
auto group = Ui::PreparedList();
// For groupType Type::Video means media album,
// Type::File means file album,
// Type::None means no grouping.
using Type = Ui::PreparedFile::AlbumType;
auto groupType = Type::None;
auto result = std::vector<PreparedGroup>();
auto pushGroup = [&] {
result.push_back(PreparedGroup{
.list = base::take(group),
.grouped = (groupType != Type::None)
});
};
for (auto i = 0; i != list.files.size(); ++i) {
auto &file = list.files[i];
const auto fileGroupType = (file.type == Type::Video)
? (groupFiles ? Type::Video : Type::None)
: (file.type == Type::Photo)
? ((groupFiles && sendImagesAsPhotos)
? Type::Video
: (groupFiles && !sendImagesAsPhotos)
? Type::File
: Type::None)
: (file.type == Type::File)
? (groupFiles ? Type::File : Type::None)
: Type::None;
if ((!group.files.empty() && groupType != fileGroupType)
|| ((groupType != Type::None)
&& (group.files.size() == Ui::MaxAlbumItems()))) {
pushGroup();
}
group.files.push_back(std::move(file));
groupType = fileGroupType;
}
if (!group.files.empty()) {
pushGroup();
}
return result;
}
} // namespace Ui

View File

@@ -12,6 +12,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
namespace Ui {
class SendFilesWay;
struct PreparedFileInformation {
struct Image {
QImage data;
@@ -37,11 +39,9 @@ struct PreparedFileInformation {
struct PreparedFile {
// File-s can be grouped if 'groupFiles'.
// File-s + Photo-s can be grouped if 'groupFiles && !sendImagesAsPhotos'.
// Photo-s can be grouped if '(groupFiles && !sendImagesAsPhotos)
// || (groupMediaInAlbums && sendImagesAsPhotos)'.
// Photo-s + Video-s can be grouped if 'groupMediaInAlbums
// && sendImagesAsPhotos'.
// Video-s can be grouped if 'groupMediaInAlbums'.
// Photo-s can be grouped if 'groupFiles'.
// Photo-s + Video-s can be grouped if 'groupFiles && sendImagesAsPhotos'.
// Video-s can be grouped if 'groupFiles'.
enum class AlbumType {
File,
Photo,
@@ -77,12 +77,15 @@ struct PreparedList {
: error(error)
, errorData(errorData) {
}
PreparedList(PreparedList &&other) = default;
PreparedList &operator=(PreparedList &&other) = default;
[[nodiscard]] static PreparedList Reordered(
PreparedList &&list,
std::vector<int> order);
void mergeToEnd(PreparedList &&other, bool cutToAlbumSize = false);
[[nodiscard]] bool canAddCaption(bool groupMediaInAlbums) const;
[[nodiscard]] bool canAddCaption(bool sendingAlbum) const;
[[nodiscard]] bool canBeSentInSlowmode() const;
[[nodiscard]] bool canBeSentInSlowmodeWith(
const PreparedList &other) const;
@@ -94,6 +97,16 @@ struct PreparedList {
std::optional<bool> overrideSendImagesAsPhotos;
};
struct PreparedGroup {
PreparedList list;
bool grouped = false;
};
[[nodiscard]] std::vector<PreparedGroup> DivideByGroups(
PreparedList &&list,
SendFilesWay way,
bool slowmode);
[[nodiscard]] int MaxAlbumItems();
[[nodiscard]] bool ValidateThumbDimensions(int width, int height);

View File

@@ -9,19 +9,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
namespace Ui {
void SendFilesWay::setGroupMediaInAlbums(bool value) {
if (value) {
_flags |= (Flag::GroupMediaInAlbums | Flag::SendImagesAsPhotos);
} else {
_flags &= ~Flag::GroupMediaInAlbums;
}
}
void SendFilesWay::setSendImagesAsPhotos(bool value) {
if (value) {
_flags |= Flag::SendImagesAsPhotos;
} else {
_flags &= ~(Flag::SendImagesAsPhotos | Flag::GroupMediaInAlbums);
_flags &= ~Flag::SendImagesAsPhotos;
}
}
@@ -40,36 +32,23 @@ void SendFilesWay::setGroupFiles(bool value) {
//};
int32 SendFilesWay::serialize() const {
auto result = groupMediaInAlbums()
auto result = (sendImagesAsPhotos() && groupFiles())
? int32(0)
: sendImagesAsPhotos()
? int32(1)
: groupFiles()
? int32(3)
: int32(2);
if (!groupFiles()) {
result |= 0x04;
}
return result;
}
std::optional<SendFilesWay> SendFilesWay::FromSerialized(int32 value) {
auto result = SendFilesWay();
result.setGroupFiles(!(value & 0x04));
value &= ~0x04;
switch (value) {
case 0:
result.setGroupMediaInAlbums(true);
result.setSendImagesAsPhotos(true);
break;
case 1:
result.setGroupMediaInAlbums(false);
result.setSendImagesAsPhotos(true);
break;
case 2:
result.setGroupMediaInAlbums(false);
result.setSendImagesAsPhotos(false);
break;
default: return std::nullopt;
if (value < 0 || value > 3) {
return std::nullopt;
}
auto result = SendFilesWay();
result.setGroupFiles((value == 0) || (value == 3));
result.setSendImagesAsPhotos((value == 0) || (value == 1));
return result;
}

View File

@@ -19,18 +19,14 @@ enum class AttachButtonType {
class SendFilesWay final {
public:
[[nodiscard]] bool groupMediaInAlbums() const {
return (_flags & Flag::GroupMediaInAlbums) != 0;
[[nodiscard]] bool groupFiles() const {
return (_flags & Flag::GroupFiles) != 0;
}
[[nodiscard]] bool sendImagesAsPhotos() const {
return (_flags & Flag::SendImagesAsPhotos) != 0;
}
[[nodiscard]] bool groupFiles() const {
return (_flags & Flag::GroupFiles);
}
void setGroupMediaInAlbums(bool value);
void setSendImagesAsPhotos(bool value);
void setGroupFiles(bool value);
void setSendImagesAsPhotos(bool value);
[[nodiscard]] inline bool operator<(const SendFilesWay &other) const {
return _flags < other._flags;
@@ -57,11 +53,10 @@ public:
private:
enum class Flag : uchar {
GroupMediaInAlbums = (1 << 0),
GroupFiles = (1 << 0),
SendImagesAsPhotos = (1 << 1),
GroupFiles = (1 << 2),
Default = GroupMediaInAlbums | SendImagesAsPhotos | GroupFiles,
Default = GroupFiles | SendImagesAsPhotos,
};
friend inline constexpr bool is_flag_type(Flag) { return true; };