tdesktop/Telegram/SourceFiles/boxes/background_box.cpp

514 lines
15 KiB
C++
Raw Normal View History

2015-02-03 15:02:46 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
2015-02-03 15:02:46 +00:00
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
2015-02-03 15:02:46 +00:00
*/
2017-04-06 14:38:10 +00:00
#include "boxes/background_box.h"
2015-02-03 15:02:46 +00:00
2017-04-13 08:27:10 +00:00
#include "lang/lang_keys.h"
2016-11-21 17:46:29 +00:00
#include "ui/effects/round_checkbox.h"
#include "ui/image/image.h"
2019-09-13 12:22:54 +00:00
#include "ui/ui_utility.h"
2019-07-24 11:45:24 +00:00
#include "main/main_session.h"
2019-02-09 13:36:07 +00:00
#include "apiwrap.h"
2019-02-07 16:36:30 +00:00
#include "mtproto/sender.h"
2019-01-03 12:36:01 +00:00
#include "data/data_session.h"
2019-09-16 11:14:06 +00:00
#include "data/data_file_origin.h"
#include "data/data_document.h"
#include "data/data_document_media.h"
2019-02-07 16:36:30 +00:00
#include "boxes/background_preview_box.h"
2019-02-09 13:36:07 +00:00
#include "boxes/confirm_box.h"
2020-06-10 18:08:17 +00:00
#include "window/window_session_controller.h"
#include "app.h"
#include "styles/style_overview.h"
2019-09-18 11:19:05 +00:00
#include "styles/style_layers.h"
#include "styles/style_boxes.h"
2019-02-09 13:36:07 +00:00
#include "styles/style_chat_helpers.h"
2015-02-03 15:02:46 +00:00
2019-01-16 12:25:29 +00:00
namespace {
constexpr auto kBackgroundsInRow = 3;
2019-02-05 09:32:54 +00:00
QImage TakeMiddleSample(QImage original, QSize size) {
size *= cIntRetinaFactor();
const auto from = original.size();
if (from.isEmpty()) {
auto result = original.scaled(size);
result.setDevicePixelRatio(cRetinaFactor());
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(cRetinaFactor());
return result;
}
2019-01-16 12:25:29 +00:00
} // namespace
class BackgroundBox::Inner final
: public Ui::RpWidget
, private base::Subscriber {
public:
2019-07-24 14:00:30 +00:00
Inner(
QWidget *parent,
not_null<Main::Session*> session);
~Inner();
2019-02-09 13:36:07 +00:00
rpl::producer<Data::WallPaper> chooseEvents() const;
rpl::producer<Data::WallPaper> removeRequests() const;
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;
2019-02-05 09:32:54 +00:00
struct Paper {
Data::WallPaper data;
mutable std::shared_ptr<Data::DocumentMedia> dataMedia;
2019-02-05 09:32:54 +00:00
mutable QPixmap thumbnail;
};
2019-02-09 13:36:07 +00:00
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 = base::optional_variant<Selected, DeleteSelected>;
int getSelectionIndex(const Selection &selection) const;
void repaintPaper(int index);
void resizeToContentAndPreload();
2019-02-04 18:37:40 +00:00
void updatePapers();
2019-02-09 13:36:07 +00:00
void requestPapers();
2019-02-04 18:37:40 +00:00
void sortPapers();
2019-01-28 13:59:49 +00:00
void paintPaper(
Painter &p,
2019-02-05 09:32:54 +00:00
const Paper &paper,
2019-01-28 13:59:49 +00:00
int column,
int row) const;
2019-02-05 09:32:54 +00:00
void validatePaperThumbnail(const Paper &paper) const;
2019-07-24 14:00:30 +00:00
const not_null<Main::Session*> _session;
2019-11-27 08:02:56 +00:00
MTP::Sender _api;
2019-07-24 14:00:30 +00:00
2019-02-05 09:32:54 +00:00
std::vector<Paper> _papers;
2019-02-09 13:36:07 +00:00
Selection _over;
Selection _overDown;
2019-01-16 12:25:29 +00:00
std::unique_ptr<Ui::RoundCheckbox> _check; // this is not a widget
2019-02-09 13:36:07 +00:00
rpl::event_stream<Data::WallPaper> _backgroundChosen;
rpl::event_stream<Data::WallPaper> _backgroundRemove;
};
2020-06-10 18:08:17 +00:00
BackgroundBox::BackgroundBox(
QWidget*,
not_null<Window::SessionController*> controller)
: _controller(controller) {
}
void BackgroundBox::prepare() {
setTitle(tr::lng_backgrounds_header());
addButton(tr::lng_close(), [=] { closeBox(); });
setDimensions(st::boxWideWidth, st::boxMaxListHeight);
2019-07-24 14:00:30 +00:00
_inner = setInnerWidget(
2020-06-10 18:08:17 +00:00
object_ptr<Inner>(this, &_controller->session()),
2019-07-24 14:00:30 +00:00
st::backgroundScroll);
2019-02-09 13:36:07 +00:00
_inner->chooseEvents(
2019-07-24 14:00:30 +00:00
) | rpl::start_with_next([=](const Data::WallPaper &paper) {
Ui::show(
2020-06-10 18:08:17 +00:00
Box<BackgroundPreviewBox>(_controller, paper),
2019-09-18 11:19:05 +00:00
Ui::LayerOption::KeepOther);
2019-02-09 13:36:07 +00:00
}, _inner->lifetime());
_inner->removeRequests(
) | rpl::start_with_next([=](const Data::WallPaper &paper) {
removePaper(paper);
}, _inner->lifetime());
}
void BackgroundBox::removePaper(const Data::WallPaper &paper) {
2019-09-18 11:19:05 +00:00
const auto box = std::make_shared<QPointer<Ui::BoxContent>>();
2020-06-10 18:08:17 +00:00
const auto session = &_controller->session();
2019-09-13 12:22:54 +00:00
const auto remove = [=, weak = Ui::MakeWeak(this)]{
2019-02-09 13:36:07 +00:00
if (*box) {
(*box)->closeBox();
}
if (weak) {
weak->_inner->removePaper(paper);
}
2019-07-24 14:00:30 +00:00
session->data().removeWallpaper(paper);
session->api().request(MTPaccount_SaveWallPaper(
2020-06-19 15:14:55 +00:00
paper.mtpInput(session),
2019-02-09 13:36:07 +00:00
MTP_bool(true),
paper.mtpSettings()
)).send();
};
*box = Ui::show(
Box<ConfirmBox>(
2019-06-19 15:09:03 +00:00
tr::lng_background_sure_delete(tr::now),
tr::lng_selected_delete(tr::now),
tr::lng_cancel(tr::now),
2019-02-09 13:36:07 +00:00
remove),
2019-09-18 11:19:05 +00:00
Ui::LayerOption::KeepOther);
}
2019-07-24 14:00:30 +00:00
BackgroundBox::Inner::Inner(
QWidget *parent,
not_null<Main::Session*> session)
: RpWidget(parent)
, _session(session)
, _api(&_session->mtp())
2019-01-03 14:39:19 +00:00
, _check(std::make_unique<Ui::RoundCheckbox>(st::overviewCheck, [=] { update(); })) {
2020-03-13 13:05:21 +00:00
_check->setChecked(true, anim::type::instant);
2019-07-24 14:00:30 +00:00
if (_session->data().wallpapers().empty()) {
2019-02-08 16:20:08 +00:00
resize(st::boxWideWidth, 2 * (st::backgroundSize.height() + st::backgroundPadding) + st::backgroundPadding);
2015-02-03 15:02:46 +00:00
} else {
2019-02-04 18:37:40 +00:00
updatePapers();
2015-02-03 15:02:46 +00:00
}
2019-02-09 13:36:07 +00:00
requestPapers();
_session->downloaderTaskFinished(
) | rpl::start_with_next([=] {
update();
}, lifetime());
2019-02-04 18:37:40 +00:00
using Update = Window::Theme::BackgroundUpdate;
subscribe(Window::Theme::Background(), [=](const Update &update) {
if (update.paletteChanged()) {
_check->invalidateCache();
2019-02-04 18:37:40 +00:00
} else if (update.type == Update::Type::New) {
sortPapers();
2019-02-09 13:36:07 +00:00
requestPapers();
2019-02-04 18:37:40 +00:00
this->update();
}
});
2015-02-03 15:02:46 +00:00
setMouseTracking(true);
}
2019-02-09 13:36:07 +00:00
void BackgroundBox::Inner::requestPapers() {
2019-11-27 08:02:56 +00:00
_api.request(MTPaccount_GetWallPapers(
2019-07-24 14:00:30 +00:00
MTP_int(_session->data().wallpapersHash())
2019-02-09 13:36:07 +00:00
)).done([=](const MTPaccount_WallPapers &result) {
2019-07-24 14:00:30 +00:00
if (_session->data().updateWallpapers(result)) {
2019-02-09 13:36:07 +00:00
updatePapers();
}
}).send();
2019-02-04 18:37:40 +00:00
}
void BackgroundBox::Inner::sortPapers() {
const auto current = Window::Theme::Background()->id();
const auto night = Window::Theme::IsNightMode();
ranges::stable_sort(_papers, std::greater<>(), [&](const Paper &paper) {
2019-02-05 09:32:54 +00:00
const auto &data = paper.data;
2019-02-04 18:37:40 +00:00
return std::make_tuple(
2019-02-05 09:32:54 +00:00
data.id() == current,
night ? data.isDark() : !data.isDark(),
!data.isDefault() && !data.isLocal(),
!data.isDefault() && data.isLocal());
2019-02-04 18:37:40 +00:00
});
2019-02-07 16:36:30 +00:00
if (!_papers.empty() && _papers.front().data.id() == current) {
_papers.front().data = _papers.front().data.withParamsFrom(
Window::Theme::Background()->paper());
}
2019-02-04 18:37:40 +00:00
}
void BackgroundBox::Inner::updatePapers() {
2019-02-09 13:36:07 +00:00
_over = _overDown = Selection();
2019-07-24 14:00:30 +00:00
_papers = _session->data().wallpapers(
2019-02-05 09:32:54 +00:00
) | ranges::view::filter([](const Data::WallPaper &paper) {
return !paper.isPattern() || paper.backgroundColor().has_value();
}) | ranges::view::transform([](const Data::WallPaper &paper) {
return Paper{ paper };
}) | ranges::to_vector;
2019-02-04 18:37:40 +00:00
sortPapers();
2019-02-09 13:36:07 +00:00
resizeToContentAndPreload();
}
void BackgroundBox::Inner::resizeToContentAndPreload() {
2019-02-04 18:37:40 +00:00
const auto count = _papers.size();
2019-01-16 12:25:29 +00:00
const auto rows = (count / kBackgroundsInRow)
+ (count % kBackgroundsInRow ? 1 : 0);
2015-02-03 15:02:46 +00:00
resize(
st::boxWideWidth,
(rows * (st::backgroundSize.height() + st::backgroundPadding)
+ st::backgroundPadding));
2015-02-03 15:02:46 +00:00
2019-01-16 12:25:29 +00:00
const auto preload = kBackgroundsInRow * 3;
2019-02-04 18:37:40 +00:00
for (const auto &paper : _papers | ranges::view::take(preload)) {
2020-05-29 14:08:18 +00:00
if (!paper.data.localThumbnail() && !paper.dataMedia) {
if (const auto document = paper.data.document()) {
paper.dataMedia = document->createMediaView();
paper.dataMedia->thumbnailWanted(paper.data.fileOrigin());
}
}
2015-02-03 15:02:46 +00:00
}
2019-02-09 13:36:07 +00:00
update();
2015-02-03 15:02:46 +00:00
}
void BackgroundBox::Inner::paintEvent(QPaintEvent *e) {
2015-02-03 15:02:46 +00:00
QRect r(e->rect());
Painter p(this);
2015-02-03 15:02:46 +00:00
2019-02-04 18:37:40 +00:00
if (_papers.empty()) {
p.setFont(st::noContactsFont);
p.setPen(st::noContactsColor);
2019-06-19 15:09:03 +00:00
p.drawText(QRect(0, 0, width(), st::noContactsHeight), tr::lng_contacts_loading(tr::now), style::al_center);
2019-01-16 12:25:29 +00:00
return;
2015-02-03 15:02:46 +00:00
}
2019-01-16 12:25:29 +00:00
auto row = 0;
auto column = 0;
2019-02-04 18:37:40 +00:00
for (const auto &paper : _papers) {
2019-01-16 12:25:29 +00:00
const auto increment = gsl::finally([&] {
++column;
if (column == kBackgroundsInRow) {
column = 0;
++row;
}
});
if ((st::backgroundSize.height() + st::backgroundPadding) * (row + 1) <= r.top()) {
continue;
2019-01-28 13:59:49 +00:00
} else if ((st::backgroundSize.height() + st::backgroundPadding) * row >= r.top() + r.height()) {
break;
2019-01-16 12:25:29 +00:00
}
2019-01-28 13:59:49 +00:00
paintPaper(p, paper, column, row);
}
}
2015-02-03 15:02:46 +00:00
2019-02-05 09:32:54 +00:00
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 || !paper.dataMedia->thumbnail()) {
return;
}
2019-02-05 09:32:54 +00:00
}
const auto thumbnail = localThumbnail
? localThumbnail
: paper.dataMedia->thumbnail();
2019-02-05 09:32:54 +00:00
auto original = thumbnail->original();
if (paper.data.isPattern()) {
const auto color = *paper.data.backgroundColor();
original = Data::PreparePatternImage(
std::move(original),
color,
Data::PatternColor(color),
paper.data.patternIntensity());
}
paper.thumbnail = App::pixmapFromImageInPlace(TakeMiddleSample(
original,
st::backgroundSize));
paper.thumbnail.setDevicePixelRatio(cRetinaFactor());
}
2019-01-28 13:59:49 +00:00
void BackgroundBox::Inner::paintPaper(
Painter &p,
2019-02-05 09:32:54 +00:00
const Paper &paper,
2019-01-28 13:59:49 +00:00
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);
2019-02-05 09:32:54 +00:00
validatePaperThumbnail(paper);
if (!paper.thumbnail.isNull()) {
p.drawPixmap(x, y, paper.thumbnail);
}
2019-01-28 13:59:49 +00:00
2019-02-09 13:36:07 +00:00
const auto over = _overDown ? _overDown : _over;
2019-02-05 09:32:54 +00:00
if (paper.data.id() == Window::Theme::Background()->id()) {
2019-01-28 13:59:49 +00:00
const auto checkLeft = x + st::backgroundSize.width() - st::overviewCheckSkip - st::overviewCheck.size;
const auto checkTop = y + st::backgroundSize.height() - st::overviewCheckSkip - st::overviewCheck.size;
2019-09-02 12:24:51 +00:00
_check->paint(p, checkLeft, checkTop, width());
2019-02-09 13:36:07 +00:00
} else if (Data::IsCloudWallPaper(paper.data)
&& !Data::IsDefaultWallPaper(paper.data)
&& over.has_value()
&& (&paper == &_papers[getSelectionIndex(over)])) {
const auto deleteSelected = over.is<DeleteSelected>();
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.);
2019-01-16 12:25:29 +00:00
}
}
2015-02-03 15:02:46 +00:00
2019-01-16 12:25:29 +00:00
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));
2019-02-09 13:36:07 +00:00
const auto result = row * kBackgroundsInRow + column;
2019-01-16 12:25:29 +00:00
if (y - row * (height + skip) > skip + height) {
2019-02-09 13:36:07 +00:00
return Selection();
2019-01-16 12:25:29 +00:00
} else if (x - column * (width + skip) > skip + width) {
2019-02-09 13:36:07 +00:00
return Selection();
} else if (result >= _papers.size()) {
return Selection();
2019-01-16 12:25:29 +00:00
}
2019-02-09 13:36:07 +00:00
const auto deleteLeft = (column + 1) * (width + skip)
- st::stickerPanDeleteIconBg.width();
const auto deleteBottom = row * (height + skip) + skip
+ st::stickerPanDeleteIconBg.height();
const auto currentId = Window::Theme::Background()->id();
const auto inDelete = (x >= deleteLeft)
&& (y < deleteBottom)
&& Data::IsCloudWallPaper(_papers[result].data)
&& !Data::IsDefaultWallPaper(_papers[result].data)
&& (currentId != _papers[result].data.id());
return (result >= _papers.size())
? Selection()
: inDelete
? Selection(DeleteSelected{ result })
: Selection(Selected{ result });
2019-01-16 12:25:29 +00:00
}();
if (_over != newOver) {
2019-02-09 13:36:07 +00:00
repaintPaper(getSelectionIndex(_over));
2015-02-03 15:02:46 +00:00
_over = newOver;
2019-02-09 13:36:07 +00:00
repaintPaper(getSelectionIndex(_over));
setCursor((_over.has_value() || _overDown.has_value())
2019-01-16 12:25:29 +00:00
? style::cur_pointer
: style::cur_default);
2015-02-03 15:02:46 +00:00
}
}
2019-02-09 13:36:07 +00:00
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) {
2015-02-03 15:02:46 +00:00
_overDown = _over;
}
2019-02-09 13:36:07 +00:00
int BackgroundBox::Inner::getSelectionIndex(
const Selection &selection) const {
return selection.match([](const Selected &data) {
return data.index;
}, [](const DeleteSelected &data) {
return data.index;
}, [](std::nullopt_t) {
return -1;
});
}
void BackgroundBox::Inner::mouseReleaseEvent(QMouseEvent *e) {
2019-02-09 13:36:07 +00:00
if (base::take(_overDown) == _over && _over.has_value()) {
const auto index = getSelectionIndex(_over);
if (index >= 0 && index < _papers.size()) {
if (base::get_if<DeleteSelected>(&_over)) {
_backgroundRemove.fire_copy(_papers[index].data);
} else if (base::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);
2019-02-09 13:36:07 +00:00
}
}
2019-02-09 13:36:07 +00:00
} else if (!_over.has_value()) {
2015-02-03 15:02:46 +00:00
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;
}
}
}
2019-02-09 13:36:07 +00:00
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;