| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860 |
- /*
- This file is part of Telegram Desktop,
- the official desktop application for the Telegram messaging service.
- For license and copyright information please follow this link:
- https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
- */
- #include "boxes/background_box.h"
- #include "lang/lang_keys.h"
- #include "ui/effects/round_checkbox.h"
- #include "ui/image/image.h"
- #include "ui/chat/attach/attach_extensions.h"
- #include "ui/chat/chat_theme.h"
- #include "ui/ui_utility.h"
- #include "ui/vertical_list.h"
- #include "main/main_session.h"
- #include "apiwrap.h"
- #include "mtproto/sender.h"
- #include "core/file_utilities.h"
- #include "data/data_peer.h"
- #include "data/data_session.h"
- #include "data/data_file_origin.h"
- #include "data/data_document.h"
- #include "data/data_document_media.h"
- #include "boxes/background_preview_box.h"
- #include "info/profile/info_profile_icon.h"
- #include "ui/boxes/confirm_box.h"
- #include "ui/widgets/buttons.h"
- #include "window/window_session_controller.h"
- #include "window/themes/window_theme.h"
- #include "styles/style_overview.h"
- #include "styles/style_layers.h"
- #include "styles/style_boxes.h"
- #include "styles/style_chat_helpers.h"
- #include "styles/style_info.h"
- namespace {
- constexpr auto kBackgroundsInRow = 3;
- QImage TakeMiddleSample(QImage original, QSize size) {
- size *= style::DevicePixelRatio();
- const auto from = original.size();
- if (from.isEmpty()) {
- auto result = original.scaled(size);
- result.setDevicePixelRatio(style::DevicePixelRatio());
- return result;
- }
- const auto take = (from.width() * size.height()
- > from.height() * size.width())
- ? QSize(size.width() * from.height() / size.height(), from.height())
- : QSize(from.width(), size.height() * from.width() / size.width());
- auto result = original.copy(
- (from.width() - take.width()) / 2,
- (from.height() - take.height()) / 2,
- take.width(),
- take.height()
- ).scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
- result.setDevicePixelRatio(style::DevicePixelRatio());
- return result;
- }
- } // namespace
- class BackgroundBox::Inner final : public Ui::RpWidget {
- public:
- Inner(
- QWidget *parent,
- not_null<Main::Session*> session,
- PeerData *forPeer);
- ~Inner();
- [[nodiscard]] rpl::producer<Data::WallPaper> chooseEvents() const;
- [[nodiscard]] rpl::producer<Data::WallPaper> removeRequests() const;
- [[nodiscard]] auto resolveResetCustomPaper() const
- ->std::optional<Data::WallPaper>;
- void removePaper(const Data::WallPaper &data);
- private:
- void paintEvent(QPaintEvent *e) override;
- void mouseMoveEvent(QMouseEvent *e) override;
- void mousePressEvent(QMouseEvent *e) override;
- void mouseReleaseEvent(QMouseEvent *e) override;
- void visibleTopBottomUpdated(
- int visibleTop,
- int visibleBottom) override;
- struct Paper {
- Data::WallPaper data;
- mutable std::shared_ptr<Data::DocumentMedia> dataMedia;
- mutable QPixmap thumbnail;
- };
- struct Selected {
- int index = 0;
- inline bool operator==(const Selected &other) const {
- return index == other.index;
- }
- inline bool operator!=(const Selected &other) const {
- return !(*this == other);
- }
- };
- struct DeleteSelected {
- int index = 0;
- inline bool operator==(const DeleteSelected &other) const {
- return index == other.index;
- }
- inline bool operator!=(const DeleteSelected &other) const {
- return !(*this == other);
- }
- };
- using Selection = std::variant<v::null_t, Selected, DeleteSelected>;
- int getSelectionIndex(const Selection &selection) const;
- void repaintPaper(int index);
- void resizeToContentAndPreload();
- void updatePapers();
- void requestPapers();
- void pushCustomPapers();
- void sortPapers();
- void paintPaper(
- QPainter &p,
- const Paper &paper,
- int column,
- int row) const;
- void validatePaperThumbnail(const Paper &paper) const;
- [[nodiscard]] bool forChannel() const;
- const not_null<Main::Session*> _session;
- PeerData * const _forPeer = nullptr;
- MTP::Sender _api;
- std::vector<Paper> _papers;
- uint64 _currentId = 0;
- uint64 _insertedResetId = 0;
- Selection _over;
- Selection _overDown;
- std::unique_ptr<Ui::RoundCheckbox> _check; // this is not a widget
- rpl::event_stream<Data::WallPaper> _backgroundChosen;
- rpl::event_stream<Data::WallPaper> _backgroundRemove;
- };
- BackgroundBox::BackgroundBox(
- QWidget*,
- not_null<Window::SessionController*> controller,
- PeerData *forPeer)
- : _controller(controller)
- , _forPeer(forPeer) {
- }
- void BackgroundBox::prepare() {
- setTitle(tr::lng_backgrounds_header());
- addButton(tr::lng_close(), [=] { closeBox(); });
- setDimensions(st::boxWideWidth, st::boxMaxListHeight);
- auto wrap = object_ptr<Ui::VerticalLayout>(this);
- const auto container = wrap.data();
- Ui::AddSkip(container);
- const auto button = container->add(object_ptr<Ui::SettingsButton>(
- container,
- tr::lng_settings_bg_from_file(),
- st::infoProfileButton));
- object_ptr<Info::Profile::FloatingIcon>(
- button,
- st::infoIconMediaPhoto,
- st::infoSharedMediaButtonIconPosition);
- if (forChannel() && _forPeer->wallPaper()) {
- const auto remove = container->add(object_ptr<Ui::SettingsButton>(
- container,
- tr::lng_settings_bg_remove(),
- st::infoBlockButton));
- object_ptr<Info::Profile::FloatingIcon>(
- remove,
- st::infoIconDeleteRed,
- st::infoSharedMediaButtonIconPosition);
- remove->setClickedCallback([=] {
- if (const auto resolved = _inner->resolveResetCustomPaper()) {
- chosen(*resolved);
- }
- });
- }
- button->setClickedCallback([=] {
- chooseFromFile();
- });
- Ui::AddSkip(container);
- Ui::AddDivider(container);
- _inner = container->add(
- object_ptr<Inner>(this, &_controller->session(), _forPeer));
- container->resizeToWidth(st::boxWideWidth);
- setInnerWidget(std::move(wrap), st::backgroundScroll);
- setInnerTopSkip(st::lineWidth);
- _inner->chooseEvents(
- ) | rpl::start_with_next([=](const Data::WallPaper &paper) {
- chosen(paper);
- }, _inner->lifetime());
- _inner->removeRequests(
- ) | rpl::start_with_next([=](const Data::WallPaper &paper) {
- removePaper(paper);
- }, _inner->lifetime());
- }
- void BackgroundBox::chooseFromFile() {
- const auto filterStart = _forPeer
- ? u"Image files (*"_q
- : u"Theme files (*.tdesktop-theme *.tdesktop-palette *"_q;
- auto filters = QStringList(
- filterStart
- + Ui::ImageExtensions().join(u" *"_q)
- + u")"_q);
- filters.push_back(FileDialog::AllFilesFilter());
- const auto callback = [=](const FileDialog::OpenResult &result) {
- if (result.paths.isEmpty() && result.remoteContent.isEmpty()) {
- return;
- }
- if (!_forPeer && !result.paths.isEmpty()) {
- const auto filePath = result.paths.front();
- const auto hasExtension = [&](QLatin1String extension) {
- return filePath.endsWith(extension, Qt::CaseInsensitive);
- };
- if (hasExtension(qstr(".tdesktop-theme"))
- || hasExtension(qstr(".tdesktop-palette"))) {
- Window::Theme::Apply(filePath);
- return;
- }
- }
- auto image = Images::Read({
- .path = result.paths.isEmpty() ? QString() : result.paths.front(),
- .content = result.remoteContent,
- .forceOpaque = true,
- }).image;
- if (image.isNull() || image.width() <= 0 || image.height() <= 0) {
- return;
- }
- auto local = Data::CustomWallPaper();
- local.setLocalImageAsThumbnail(std::make_shared<Image>(
- std::move(image)));
- _controller->show(Box<BackgroundPreviewBox>(
- _controller,
- local,
- BackgroundPreviewArgs{ _forPeer }));
- };
- FileDialog::GetOpenPath(
- this,
- tr::lng_choose_image(tr::now),
- filters.join(u";;"_q),
- crl::guard(this, callback));
- }
- bool BackgroundBox::hasDefaultForPeer() const {
- Expects(_forPeer != nullptr);
- const auto paper = _forPeer->wallPaper();
- if (!paper) {
- return true;
- }
- const auto reset = _inner->resolveResetCustomPaper();
- Assert(reset.has_value());
- return (paper->id() == reset->id());
- }
- bool BackgroundBox::chosenDefaultForPeer(
- const Data::WallPaper &paper) const {
- if (!_forPeer) {
- return false;
- }
- const auto reset = _inner->resolveResetCustomPaper();
- Assert(reset.has_value());
- return (paper.id() == reset->id());
- }
- void BackgroundBox::chosen(const Data::WallPaper &paper) {
- if (chosenDefaultForPeer(paper)) {
- if (!hasDefaultForPeer()) {
- const auto reset = crl::guard(this, [=](Fn<void()> close) {
- resetForPeer();
- close();
- });
- _controller->show(Ui::MakeConfirmBox({
- .text = tr::lng_background_sure_reset_default(),
- .confirmed = reset,
- .confirmText = tr::lng_background_reset_default(),
- }));
- } else {
- closeBox();
- }
- return;
- } else if (forChannel()) {
- if (_forPeer->wallPaper() && _forPeer->wallPaper()->equals(paper)) {
- closeBox();
- return;
- }
- const auto &themes = _forPeer->owner().cloudThemes();
- for (const auto &theme : themes.chatThemes()) {
- for (const auto &[type, themed] : theme.settings) {
- if (themed.paper && themed.paper->equals(paper)) {
- _controller->show(Box<BackgroundPreviewBox>(
- _controller,
- Data::WallPaper::FromEmojiId(theme.emoticon),
- BackgroundPreviewArgs{ _forPeer }));
- return;
- }
- }
- }
- }
- _controller->show(Box<BackgroundPreviewBox>(
- _controller,
- paper,
- BackgroundPreviewArgs{ _forPeer }));
- }
- void BackgroundBox::resetForPeer() {
- const auto api = &_controller->session().api();
- api->request(MTPmessages_SetChatWallPaper(
- MTP_flags(0),
- _forPeer->input,
- MTPInputWallPaper(),
- MTPWallPaperSettings(),
- MTPint()
- )).done([=](const MTPUpdates &result) {
- api->applyUpdates(result);
- }).send();
- const auto weak = Ui::MakeWeak(this);
- _forPeer->setWallPaper({});
- if (weak) {
- _controller->finishChatThemeEdit(_forPeer);
- }
- }
- bool BackgroundBox::forChannel() const {
- return _forPeer && _forPeer->isChannel();
- }
- void BackgroundBox::removePaper(const Data::WallPaper &paper) {
- const auto session = &_controller->session();
- const auto remove = [=, weak = Ui::MakeWeak(this)](Fn<void()> &&close) {
- close();
- if (weak) {
- weak->_inner->removePaper(paper);
- }
- session->data().removeWallpaper(paper);
- session->api().request(MTPaccount_SaveWallPaper(
- paper.mtpInput(session),
- MTP_bool(true),
- paper.mtpSettings()
- )).send();
- };
- _controller->show(Ui::MakeConfirmBox({
- .text = tr::lng_background_sure_delete(),
- .confirmed = remove,
- .confirmText = tr::lng_selected_delete(),
- }));
- }
- BackgroundBox::Inner::Inner(
- QWidget *parent,
- not_null<Main::Session*> session,
- PeerData *forPeer)
- : RpWidget(parent)
- , _session(session)
- , _forPeer(forPeer)
- , _api(&_session->mtp())
- , _check(
- std::make_unique<Ui::RoundCheckbox>(
- st::overviewCheck,
- [=] { update(); })) {
- _check->setChecked(true, anim::type::instant);
- resize(
- st::boxWideWidth,
- (2 * (st::backgroundSize.height() + st::backgroundPadding)
- + st::backgroundPadding));
- Window::Theme::IsNightModeValue(
- ) | rpl::start_with_next([=] {
- updatePapers();
- }, lifetime());
- requestPapers();
- _session->downloaderTaskFinished(
- ) | rpl::start_with_next([=] {
- update();
- }, lifetime());
- style::PaletteChanged(
- ) | rpl::start_with_next([=] {
- _check->invalidateCache();
- }, lifetime());
- if (forChannel()) {
- _session->data().cloudThemes().chatThemesUpdated(
- ) | rpl::start_with_next([=] {
- updatePapers();
- }, lifetime());
- } else {
- using Update = Window::Theme::BackgroundUpdate;
- Window::Theme::Background()->updates(
- ) | rpl::start_with_next([=](const Update &update) {
- if (update.type == Update::Type::New) {
- sortPapers();
- requestPapers();
- this->update();
- }
- }, lifetime());
- }
- setMouseTracking(true);
- }
- void BackgroundBox::Inner::requestPapers() {
- if (forChannel()) {
- _session->data().cloudThemes().refreshChatThemes();
- return;
- }
- _api.request(MTPaccount_GetWallPapers(
- MTP_long(_session->data().wallpapersHash())
- )).done([=](const MTPaccount_WallPapers &result) {
- if (_session->data().updateWallpapers(result)) {
- updatePapers();
- }
- }).send();
- }
- auto BackgroundBox::Inner::resolveResetCustomPaper() const
- -> std::optional<Data::WallPaper> {
- if (!_forPeer) {
- return {};
- }
- const auto nonCustom = Window::Theme::Background()->paper();
- const auto themeEmoji = _forPeer->themeEmoji();
- if (forChannel() || themeEmoji.isEmpty()) {
- return nonCustom;
- }
- const auto &themes = _forPeer->owner().cloudThemes();
- const auto theme = themes.themeForEmoji(themeEmoji);
- if (!theme) {
- return nonCustom;
- }
- using Type = Data::CloudTheme::Type;
- const auto dark = Window::Theme::IsNightMode();
- const auto i = theme->settings.find(dark ? Type::Dark : Type::Light);
- if (i != end(theme->settings) && i->second.paper) {
- return *i->second.paper;
- }
- return nonCustom;
- }
- void BackgroundBox::Inner::pushCustomPapers() {
- auto customId = uint64();
- if (const auto custom = _forPeer ? _forPeer->wallPaper() : nullptr) {
- customId = custom->id();
- const auto j = ranges::find(
- _papers,
- custom->id(),
- [](const Paper &paper) { return paper.data.id(); });
- if (j != end(_papers)) {
- j->data = j->data.withParamsFrom(*custom);
- } else {
- _papers.insert(begin(_papers), Paper{ *custom });
- }
- }
- if (const auto reset = resolveResetCustomPaper()) {
- _insertedResetId = reset->id();
- const auto j = ranges::find(
- _papers,
- _insertedResetId,
- [](const Paper &paper) { return paper.data.id(); });
- if (j != end(_papers)) {
- if (_insertedResetId != customId) {
- j->data = j->data.withParamsFrom(*reset);
- }
- } else {
- _papers.insert(begin(_papers), Paper{ *reset });
- }
- }
- }
- void BackgroundBox::Inner::sortPapers() {
- Expects(!forChannel());
- const auto currentCustom = _forPeer ? _forPeer->wallPaper() : nullptr;
- _currentId = currentCustom
- ? currentCustom->id()
- : _insertedResetId
- ? _insertedResetId
- : Window::Theme::Background()->id();
- const auto dark = Window::Theme::IsNightMode();
- ranges::stable_sort(_papers, std::greater<>(), [&](const Paper &paper) {
- const auto &data = paper.data;
- return std::make_tuple(
- _insertedResetId && (_insertedResetId == data.id()),
- data.id() == _currentId,
- dark ? data.isDark() : !data.isDark(),
- Data::IsDefaultWallPaper(data),
- !data.isDefault() && !Data::IsLegacy1DefaultWallPaper(data),
- Data::IsLegacy3DefaultWallPaper(data),
- Data::IsLegacy2DefaultWallPaper(data),
- Data::IsLegacy1DefaultWallPaper(data));
- });
- if (!_papers.empty()
- && _papers.front().data.id() == _currentId
- && !currentCustom
- && !_insertedResetId) {
- _papers.front().data = _papers.front().data.withParamsFrom(
- Window::Theme::Background()->paper());
- }
- }
- void BackgroundBox::Inner::updatePapers() {
- if (forChannel()) {
- if (_session->data().cloudThemes().chatThemes().empty()) {
- return;
- }
- } else {
- if (_session->data().wallpapers().empty()) {
- return;
- }
- }
- _over = _overDown = Selection();
- const auto was = base::take(_papers);
- if (forChannel()) {
- const auto now = _forPeer->wallPaper();
- const auto &list = _session->data().cloudThemes().chatThemes();
- if (list.empty()) {
- return;
- }
- using Type = Data::CloudThemeType;
- const auto type = Window::Theme::IsNightMode()
- ? Type::Dark
- : Type::Light;
- _papers.reserve(list.size() + 1);
- const auto nowEmojiId = now ? now->emojiId() : QString();
- if (!now || !now->emojiId().isEmpty()) {
- _papers.push_back({ Window::Theme::Background()->paper() });
- _currentId = _papers.back().data.id();
- } else {
- _papers.push_back({ *now });
- _currentId = now->id();
- }
- for (const auto &theme : list) {
- const auto i = theme.settings.find(type);
- if (i != end(theme.settings) && i->second.paper) {
- _papers.push_back({ *i->second.paper });
- if (nowEmojiId == theme.emoticon) {
- _currentId = _papers.back().data.id();
- }
- }
- }
- } else {
- _papers = _session->data().wallpapers(
- ) | ranges::views::filter([&](const Data::WallPaper &paper) {
- return (!paper.isPattern() || !paper.backgroundColors().empty())
- && (!_forPeer
- || (!Data::IsDefaultWallPaper(paper)
- && (Data::IsCloudWallPaper(paper)
- || Data::IsCustomWallPaper(paper))));
- }) | ranges::views::transform([](const Data::WallPaper &paper) {
- return Paper{ paper };
- }) | ranges::to_vector;
- pushCustomPapers();
- sortPapers();
- }
- resizeToContentAndPreload();
- }
- void BackgroundBox::Inner::resizeToContentAndPreload() {
- const auto count = _papers.size();
- const auto rows = (count / kBackgroundsInRow)
- + (count % kBackgroundsInRow ? 1 : 0);
- resize(
- st::boxWideWidth,
- (rows * (st::backgroundSize.height() + st::backgroundPadding)
- + st::backgroundPadding));
- const auto preload = kBackgroundsInRow * 3;
- for (const auto &paper : _papers | ranges::views::take(preload)) {
- if (!paper.data.localThumbnail() && !paper.dataMedia) {
- if (const auto document = paper.data.document()) {
- paper.dataMedia = document->createMediaView();
- paper.dataMedia->thumbnailWanted(paper.data.fileOrigin());
- }
- }
- }
- update();
- }
- void BackgroundBox::Inner::paintEvent(QPaintEvent *e) {
- QRect r(e->rect());
- auto p = QPainter(this);
- if (_papers.empty()) {
- p.setFont(st::noContactsFont);
- p.setPen(st::noContactsColor);
- p.drawText(QRect(0, 0, width(), st::noContactsHeight), tr::lng_contacts_loading(tr::now), style::al_center);
- return;
- }
- auto row = 0;
- auto column = 0;
- for (const auto &paper : _papers) {
- const auto increment = gsl::finally([&] {
- ++column;
- if (column == kBackgroundsInRow) {
- column = 0;
- ++row;
- }
- });
- if ((st::backgroundSize.height() + st::backgroundPadding) * (row + 1) <= r.top()) {
- continue;
- } else if ((st::backgroundSize.height() + st::backgroundPadding) * row >= r.top() + r.height()) {
- break;
- }
- paintPaper(p, paper, column, row);
- }
- }
- void BackgroundBox::Inner::validatePaperThumbnail(
- const Paper &paper) const {
- if (!paper.thumbnail.isNull()) {
- return;
- }
- const auto localThumbnail = paper.data.localThumbnail();
- if (!localThumbnail) {
- if (const auto document = paper.data.document()) {
- if (!paper.dataMedia) {
- paper.dataMedia = document->createMediaView();
- paper.dataMedia->thumbnailWanted(paper.data.fileOrigin());
- }
- if (!paper.dataMedia->thumbnail()) {
- return;
- }
- } else if (!paper.data.backgroundColors().empty()) {
- paper.thumbnail = Ui::PixmapFromImage(
- Ui::GenerateBackgroundImage(
- st::backgroundSize * style::DevicePixelRatio(),
- paper.data.backgroundColors(),
- paper.data.gradientRotation()));
- paper.thumbnail.setDevicePixelRatio(style::DevicePixelRatio());
- return;
- } else {
- return;
- }
- }
- const auto thumbnail = localThumbnail
- ? localThumbnail
- : paper.dataMedia->thumbnail();
- auto original = thumbnail->original();
- if (paper.data.isPattern()) {
- original = Ui::PreparePatternImage(
- std::move(original),
- paper.data.backgroundColors(),
- paper.data.gradientRotation(),
- paper.data.patternOpacity());
- }
- paper.thumbnail = Ui::PixmapFromImage(TakeMiddleSample(
- original,
- st::backgroundSize));
- paper.thumbnail.setDevicePixelRatio(style::DevicePixelRatio());
- }
- bool BackgroundBox::Inner::forChannel() const {
- return _forPeer && _forPeer->isChannel();
- }
- void BackgroundBox::Inner::paintPaper(
- QPainter &p,
- const Paper &paper,
- int column,
- int row) const {
- const auto x = st::backgroundPadding + column * (st::backgroundSize.width() + st::backgroundPadding);
- const auto y = st::backgroundPadding + row * (st::backgroundSize.height() + st::backgroundPadding);
- validatePaperThumbnail(paper);
- if (!paper.thumbnail.isNull()) {
- p.drawPixmap(x, y, paper.thumbnail);
- }
- const auto over = !v::is_null(_overDown) ? _overDown : _over;
- if (paper.data.id() == _currentId) {
- const auto checkLeft = x + st::backgroundSize.width() - st::overviewCheckSkip - st::overviewCheck.size;
- const auto checkTop = y + st::backgroundSize.height() - st::overviewCheckSkip - st::overviewCheck.size;
- _check->paint(p, checkLeft, checkTop, width());
- } else if (!forChannel()
- && Data::IsCloudWallPaper(paper.data)
- && !Data::IsDefaultWallPaper(paper.data)
- && !Data::IsLegacy2DefaultWallPaper(paper.data)
- && !Data::IsLegacy3DefaultWallPaper(paper.data)
- && !v::is_null(over)
- && (&paper == &_papers[getSelectionIndex(over)])) {
- const auto deleteSelected = v::is<DeleteSelected>(over);
- const auto deletePos = QPoint(x + st::backgroundSize.width() - st::stickerPanDeleteIconBg.width(), y);
- p.setOpacity(deleteSelected ? st::stickerPanDeleteOpacityBgOver : st::stickerPanDeleteOpacityBg);
- st::stickerPanDeleteIconBg.paint(p, deletePos, width());
- p.setOpacity(deleteSelected ? st::stickerPanDeleteOpacityFgOver : st::stickerPanDeleteOpacityFg);
- st::stickerPanDeleteIconFg.paint(p, deletePos, width());
- p.setOpacity(1.);
- }
- }
- void BackgroundBox::Inner::mouseMoveEvent(QMouseEvent *e) {
- const auto newOver = [&] {
- const auto x = e->pos().x();
- const auto y = e->pos().y();
- const auto width = st::backgroundSize.width();
- const auto height = st::backgroundSize.height();
- const auto skip = st::backgroundPadding;
- const auto row = int((y - skip) / (height + skip));
- const auto column = int((x - skip) / (width + skip));
- const auto result = row * kBackgroundsInRow + column;
- if (y - row * (height + skip) > skip + height) {
- return Selection();
- } else if (x - column * (width + skip) > skip + width) {
- return Selection();
- } else if (result >= _papers.size()) {
- return Selection();
- }
- auto &data = _papers[result].data;
- const auto deleteLeft = (column + 1) * (width + skip)
- - st::stickerPanDeleteIconBg.width();
- const auto deleteBottom = row * (height + skip) + skip
- + st::stickerPanDeleteIconBg.height();
- const auto inDelete = !forChannel()
- && (x >= deleteLeft)
- && (y < deleteBottom)
- && Data::IsCloudWallPaper(data)
- && !Data::IsDefaultWallPaper(data)
- && !Data::IsLegacy2DefaultWallPaper(data)
- && !Data::IsLegacy3DefaultWallPaper(data)
- && (_currentId != data.id());
- return (result >= _papers.size())
- ? Selection()
- : inDelete
- ? Selection(DeleteSelected{ result })
- : Selection(Selected{ result });
- }();
- if (_over != newOver) {
- repaintPaper(getSelectionIndex(_over));
- _over = newOver;
- repaintPaper(getSelectionIndex(_over));
- setCursor((!v::is_null(_over) || !v::is_null(_overDown))
- ? style::cur_pointer
- : style::cur_default);
- }
- }
- void BackgroundBox::Inner::repaintPaper(int index) {
- if (index < 0 || index >= _papers.size()) {
- return;
- }
- const auto row = (index / kBackgroundsInRow);
- const auto column = (index % kBackgroundsInRow);
- const auto width = st::backgroundSize.width();
- const auto height = st::backgroundSize.height();
- const auto skip = st::backgroundPadding;
- update(
- (width + skip) * column + skip,
- (height + skip) * row + skip,
- width,
- height);
- }
- void BackgroundBox::Inner::mousePressEvent(QMouseEvent *e) {
- _overDown = _over;
- }
- int BackgroundBox::Inner::getSelectionIndex(
- const Selection &selection) const {
- return v::match(selection, [](const Selected &data) {
- return data.index;
- }, [](const DeleteSelected &data) {
- return data.index;
- }, [](v::null_t) {
- return -1;
- });
- }
- void BackgroundBox::Inner::mouseReleaseEvent(QMouseEvent *e) {
- if (base::take(_overDown) == _over && !v::is_null(_over)) {
- const auto index = getSelectionIndex(_over);
- if (index >= 0 && index < _papers.size()) {
- if (std::get_if<DeleteSelected>(&_over)) {
- _backgroundRemove.fire_copy(_papers[index].data);
- } else if (std::get_if<Selected>(&_over)) {
- auto &paper = _papers[index];
- if (!paper.dataMedia) {
- if (const auto document = paper.data.document()) {
- // Keep it alive while it is on the screen.
- paper.dataMedia = document->createMediaView();
- }
- }
- _backgroundChosen.fire_copy(paper.data);
- }
- }
- } else if (v::is_null(_over)) {
- setCursor(style::cur_default);
- }
- }
- void BackgroundBox::Inner::visibleTopBottomUpdated(
- int visibleTop,
- int visibleBottom) {
- for (auto i = 0, count = int(_papers.size()); i != count; ++i) {
- const auto row = (i / kBackgroundsInRow);
- const auto height = st::backgroundSize.height();
- const auto skip = st::backgroundPadding;
- const auto top = skip + row * (height + skip);
- const auto bottom = top + height;
- if ((bottom <= visibleTop || top >= visibleBottom)
- && !_papers[i].thumbnail.isNull()) {
- _papers[i].dataMedia = nullptr;
- }
- }
- }
- rpl::producer<Data::WallPaper> BackgroundBox::Inner::chooseEvents() const {
- return _backgroundChosen.events();
- }
- auto BackgroundBox::Inner::removeRequests() const
- -> rpl::producer<Data::WallPaper> {
- return _backgroundRemove.events();
- }
- void BackgroundBox::Inner::removePaper(const Data::WallPaper &data) {
- const auto i = ranges::find(
- _papers,
- data.id(),
- [](const Paper &paper) { return paper.data.id(); });
- if (i != end(_papers)) {
- _papers.erase(i);
- _over = _overDown = Selection();
- resizeToContentAndPreload();
- }
- }
- BackgroundBox::Inner::~Inner() = default;
|