FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Added ungrouping of grouped media to selection context menu. · feiyunwill/tdesktop@5f50566 · GitHub

Commit 5f50566

Browse files
committed
Added ungrouping of grouped media to selection context menu.
1 parent 8b54d62 commit 5f50566

3 files changed

Lines changed: 89 additions & 0 deletions

File tree

‎Telegram/SourceFiles/iv/editor/iv_editor_state.cpp‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2350,6 +2350,81 @@ bool State::ungroupGroupedMediaBlock(const BlockPath &path) {
23502350
});
23512351
}
23522352

2353+
bool State::canUngroupGroupedMediaBlocks(
2354+
const PreparedEditSelection &selection) const {
2355+
if (selection.kind != PreparedEditSelectionKind::Blocks) {
2356+
return false;
2357+
}
2358+
const auto range = validateBlockRange(selection.blocks);
2359+
if (!range) {
2360+
return false;
2361+
}
2362+
const auto blocks = blockContainer(range->container);
2363+
if (!blocks) {
2364+
return false;
2365+
}
2366+
for (auto i = range->from; i != range->till; ++i) {
2367+
const auto &block = (*blocks)[i];
2368+
if (block.kind == BlockKind::GroupedMedia
2369+
&& IsGroupableMediaBlock(block)) {
2370+
return true;
2371+
}
2372+
}
2373+
return false;
2374+
}
2375+
2376+
bool State::ungroupGroupedMediaBlocks(
2377+
const PreparedEditSelection &selection) {
2378+
return applyCheckedMutation(false, [selection](State &candidate) {
2379+
if (!candidate.canUngroupGroupedMediaBlocks(selection)) {
2380+
return CheckedMutationResult<bool>{ .result = false };
2381+
}
2382+
const auto range = candidate.validateBlockRange(selection.blocks);
2383+
if (!range) {
2384+
return CheckedMutationResult<bool>{ .result = false };
2385+
}
2386+
auto *blocks = candidate.blockContainer(range->container);
2387+
if (!blocks) {
2388+
return CheckedMutationResult<bool>{ .result = false };
2389+
}
2390+
auto replacement = std::vector<Block>();
2391+
replacement.reserve(range->till - range->from);
2392+
for (auto i = range->from; i != range->till; ++i) {
2393+
auto &block = (*blocks)[i];
2394+
if (block.kind != BlockKind::GroupedMedia
2395+
|| !IsGroupableMediaBlock(block)) {
2396+
replacement.push_back(std::move(block));
2397+
continue;
2398+
}
2399+
auto first = true;
2400+
for (const auto &item : block.mediaItems) {
2401+
auto single = PhotoVideoBlockFromGroupedItem(item);
2402+
if (!single) {
2403+
return CheckedMutationResult<bool>{ .result = false };
2404+
}
2405+
if (first) {
2406+
single->caption = std::move(block.caption);
2407+
single->anchorId = std::move(block.anchorId);
2408+
first = false;
2409+
}
2410+
replacement.push_back(std::move(*single));
2411+
}
2412+
}
2413+
blocks->erase(
2414+
blocks->begin() + range->from,
2415+
blocks->begin() + range->till);
2416+
blocks->insert(
2417+
blocks->begin() + range->from,
2418+
std::make_move_iterator(replacement.begin()),
2419+
std::make_move_iterator(replacement.end()));
2420+
candidate.rebuild();
2421+
return CheckedMutationResult<bool>{
2422+
.apply = true,
2423+
.result = true,
2424+
};
2425+
});
2426+
}
2427+
23532428
bool State::removeGroupedItem(
23542429
const BlockPath &path,
23552430
int itemIndex) {

‎Telegram/SourceFiles/iv/editor/iv_editor_state.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,10 @@ class State final {
540540
const Markdown::PreparedEditSelection &selection,
541541
RichPage::GroupedMediaIntent intent);
542542
[[nodiscard]] bool ungroupGroupedMediaBlock(const BlockPath &path);
543+
[[nodiscard]] bool canUngroupGroupedMediaBlocks(
544+
const Markdown::PreparedEditSelection &selection) const;
545+
[[nodiscard]] bool ungroupGroupedMediaBlocks(
546+
const Markdown::PreparedEditSelection &selection);
543547
[[nodiscard]] bool removeGroupedItem(
544548
const BlockPath &path,
545549
int itemIndex);

‎Telegram/SourceFiles/iv/editor/iv_editor_widget.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6597,6 +6597,16 @@ void Widget::showStructuralPhotoVideoMenu(QPoint globalPos) {
65976597
});
65986598
},
65996599
&st::menuIconPhotoSet);
6600+
if (_state->canUngroupGroupedMediaBlocks(selection)) {
6601+
menu->addAction(
6602+
tr::lng_article_media_ungroup(tr::now),
6603+
[=] {
6604+
[[maybe_unused]] const auto changed = applyMediaBlockChange([=] {
6605+
return _state->ungroupGroupedMediaBlocks(selection);
6606+
});
6607+
},
6608+
&st::menuIconExpand);
6609+
}
66006610
Ui::Menu::CreateAddActionCallback(menu)({
66016611
.text = tr::lng_box_remove(tr::now),
66026612
.handler = [=] {

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL