mirror of
https://github.com/kotatogram/kotatogram-desktop
synced 2025-09-05 09:05:14 +00:00
Add option to blur chat background.
This commit is contained in:
@@ -8,155 +8,18 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
#include "boxes/background_box.h"
|
||||
|
||||
#include "lang/lang_keys.h"
|
||||
#include "mainwidget.h"
|
||||
#include "mainwindow.h"
|
||||
#include "window/themes/window_theme.h"
|
||||
#include "ui/effects/round_checkbox.h"
|
||||
#include "ui/toast/toast.h"
|
||||
#include "ui/image/image.h"
|
||||
#include "history/history.h"
|
||||
#include "history/history_message.h"
|
||||
#include "history/view/history_view_message.h"
|
||||
#include "auth_session.h"
|
||||
#include "apiwrap.h"
|
||||
#include "mtproto/sender.h"
|
||||
#include "data/data_session.h"
|
||||
#include "data/data_user.h"
|
||||
#include "data/data_document.h"
|
||||
#include "core/application.h"
|
||||
#include "boxes/confirm_box.h"
|
||||
#include "boxes/background_preview_box.h"
|
||||
#include "styles/style_overview.h"
|
||||
#include "styles/style_history.h"
|
||||
#include "styles/style_boxes.h"
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr auto kBackgroundsInRow = 3;
|
||||
constexpr auto kMaxWallPaperSlugLength = 255;
|
||||
|
||||
[[nodiscard]] bool IsValidWallPaperSlug(const QString &slug) {
|
||||
if (slug.isEmpty() || slug.size() > kMaxWallPaperSlugLength) {
|
||||
return false;
|
||||
}
|
||||
return ranges::find_if(slug, [](QChar ch) {
|
||||
return (ch != '.')
|
||||
&& (ch != '_')
|
||||
&& (ch != '-')
|
||||
&& (ch < '0' || ch > '9')
|
||||
&& (ch < 'a' || ch > 'z')
|
||||
&& (ch < 'A' || ch > 'Z');
|
||||
}) == slug.end();
|
||||
}
|
||||
|
||||
AdminLog::OwnedItem GenerateTextItem(
|
||||
not_null<HistoryView::ElementDelegate*> delegate,
|
||||
not_null<History*> history,
|
||||
const QString &text,
|
||||
bool out) {
|
||||
Expects(history->peer->isUser());
|
||||
|
||||
using Flag = MTPDmessage::Flag;
|
||||
static auto id = ServerMaxMsgId + (ServerMaxMsgId / 3);
|
||||
const auto flags = Flag::f_entities
|
||||
| Flag::f_from_id
|
||||
| (out ? Flag::f_out : Flag(0));
|
||||
const auto replyTo = 0;
|
||||
const auto viaBotId = 0;
|
||||
const auto item = new HistoryMessage(
|
||||
history,
|
||||
++id,
|
||||
flags,
|
||||
replyTo,
|
||||
viaBotId,
|
||||
unixtime(),
|
||||
out ? history->session().userId() : peerToUser(history->peer->id),
|
||||
QString(),
|
||||
TextWithEntities{ TextUtilities::Clean(text) });
|
||||
return AdminLog::OwnedItem(delegate, item);
|
||||
}
|
||||
|
||||
QImage PrepareScaledNonPattern(
|
||||
const QImage &image,
|
||||
Images::Option blur) {
|
||||
const auto size = st::boxWideWidth;
|
||||
const auto width = std::max(image.width(), 1);
|
||||
const auto height = std::max(image.height(), 1);
|
||||
const auto takeWidth = (width > height)
|
||||
? (width * size / height)
|
||||
: size;
|
||||
const auto takeHeight = (width > height)
|
||||
? size
|
||||
: (height * size / width);
|
||||
return Images::prepare(
|
||||
image,
|
||||
takeWidth * cIntRetinaFactor(),
|
||||
takeHeight * cIntRetinaFactor(),
|
||||
Images::Option::Smooth
|
||||
| Images::Option::TransparentBackground
|
||||
| blur,
|
||||
size,
|
||||
size);
|
||||
}
|
||||
|
||||
QImage ColorizePattern(QImage image, QColor color) {
|
||||
if (image.format() != QImage::Format_ARGB32_Premultiplied) {
|
||||
image = std::move(image).convertToFormat(
|
||||
QImage::Format_ARGB32_Premultiplied);
|
||||
}
|
||||
// Similar to style::colorizeImage.
|
||||
// But style::colorizeImage takes pattern with all pixels having the
|
||||
// same components value, from (0, 0, 0, 0) to (255, 255, 255, 255).
|
||||
//
|
||||
// While in patterns we have different value ranges, usually they are
|
||||
// from (0, 0, 0, 0) to (0, 0, 0, 255), so we should use only 'alpha'.
|
||||
|
||||
const auto width = image.width();
|
||||
const auto height = image.height();
|
||||
const auto pattern = anim::shifted(color);
|
||||
|
||||
const auto resultBytesPerPixel = (image.depth() >> 3);
|
||||
constexpr auto resultIntsPerPixel = 1;
|
||||
const auto resultIntsPerLine = (image.bytesPerLine() >> 2);
|
||||
const auto resultIntsAdded = resultIntsPerLine - width * resultIntsPerPixel;
|
||||
auto resultInts = reinterpret_cast<uint32*>(image.bits());
|
||||
Assert(resultIntsAdded >= 0);
|
||||
Assert(image.depth() == static_cast<int>((resultIntsPerPixel * sizeof(uint32)) << 3));
|
||||
Assert(image.bytesPerLine() == (resultIntsPerLine << 2));
|
||||
|
||||
const auto maskBytesPerPixel = (image.depth() >> 3);
|
||||
const auto maskBytesPerLine = image.bytesPerLine();
|
||||
const auto maskBytesAdded = maskBytesPerLine - width * maskBytesPerPixel;
|
||||
|
||||
// We want to read the last byte of four available.
|
||||
// This is the difference with style::colorizeImage.
|
||||
auto maskBytes = image.constBits() + (maskBytesPerPixel - 1);
|
||||
Assert(maskBytesAdded >= 0);
|
||||
Assert(image.depth() == (maskBytesPerPixel << 3));
|
||||
for (auto y = 0; y != height; ++y) {
|
||||
for (auto x = 0; x != width; ++x) {
|
||||
auto maskOpacity = static_cast<anim::ShiftedMultiplier>(*maskBytes) + 1;
|
||||
*resultInts = anim::unshifted(pattern * maskOpacity);
|
||||
maskBytes += maskBytesPerPixel;
|
||||
resultInts += resultIntsPerPixel;
|
||||
}
|
||||
maskBytes += maskBytesAdded;
|
||||
resultInts += resultIntsAdded;
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
QImage PrepareScaledFromFull(
|
||||
const QImage &image,
|
||||
std::optional<QColor> patternBackground,
|
||||
Images::Option blur = Images::Option(0)) {
|
||||
auto result = PrepareScaledNonPattern(image, blur);
|
||||
if (patternBackground) {
|
||||
result = ColorizePattern(
|
||||
std::move(result),
|
||||
Data::PatternColor(*patternBackground));
|
||||
}
|
||||
return std::move(result).convertToFormat(
|
||||
QImage::Format_ARGB32_Premultiplied);
|
||||
}
|
||||
|
||||
QImage TakeMiddleSample(QImage original, QSize size) {
|
||||
size *= cIntRetinaFactor();
|
||||
@@ -288,6 +151,10 @@ void BackgroundBox::Inner::sortPapers() {
|
||||
!data.isDefault() && !data.isLocal(),
|
||||
!data.isDefault() && data.isLocal());
|
||||
});
|
||||
if (!_papers.empty() && _papers.front().data.id() == current) {
|
||||
_papers.front().data = _papers.front().data.withParamsFrom(
|
||||
Window::Theme::Background()->paper());
|
||||
}
|
||||
}
|
||||
|
||||
void BackgroundBox::Inner::updatePapers() {
|
||||
@@ -424,318 +291,3 @@ void BackgroundBox::Inner::mouseReleaseEvent(QMouseEvent *e) {
|
||||
}
|
||||
|
||||
BackgroundBox::Inner::~Inner() = default;
|
||||
|
||||
BackgroundPreviewBox::BackgroundPreviewBox(
|
||||
QWidget*,
|
||||
const Data::WallPaper &paper)
|
||||
: _text1(GenerateTextItem(
|
||||
this,
|
||||
Auth().data().history(peerFromUser(ServiceUserId)),
|
||||
lang(lng_background_text1),
|
||||
false))
|
||||
, _text2(GenerateTextItem(
|
||||
this,
|
||||
Auth().data().history(peerFromUser(ServiceUserId)),
|
||||
lang(lng_background_text2),
|
||||
true))
|
||||
, _paper(paper)
|
||||
, _radial(animation(this, &BackgroundPreviewBox::step_radial)) {
|
||||
subscribe(Auth().downloaderTaskFinished(), [=] { update(); });
|
||||
}
|
||||
|
||||
void BackgroundPreviewBox::prepare() {
|
||||
setTitle(langFactory(lng_background_header));
|
||||
|
||||
addButton(langFactory(lng_background_apply), [=] { apply(); });
|
||||
addButton(langFactory(lng_cancel), [=] { closeBox(); });
|
||||
if (_paper.hasShareUrl()) {
|
||||
addLeftButton(langFactory(lng_background_share), [=] { share(); });
|
||||
}
|
||||
updateServiceBg(_paper.backgroundColor());
|
||||
|
||||
_paper.loadThumbnail();
|
||||
_paper.loadDocument();
|
||||
if (_paper.document() && _paper.document()->loading()) {
|
||||
_radial.start(_paper.document()->progress());
|
||||
}
|
||||
setScaledFromThumb();
|
||||
checkLoadedDocument();
|
||||
|
||||
_text1->setDisplayDate(true);
|
||||
_text1->initDimensions();
|
||||
_text1->resizeGetHeight(st::boxWideWidth);
|
||||
_text2->initDimensions();
|
||||
_text2->resizeGetHeight(st::boxWideWidth);
|
||||
|
||||
setDimensions(st::boxWideWidth, st::boxWideWidth);
|
||||
}
|
||||
|
||||
void BackgroundPreviewBox::apply() {
|
||||
App::main()->setChatBackground(_paper, std::move(_full));
|
||||
closeBox();
|
||||
}
|
||||
|
||||
void BackgroundPreviewBox::share() {
|
||||
QApplication::clipboard()->setText(_paper.shareUrl());
|
||||
Ui::Toast::Show(lang(lng_background_link_copied));
|
||||
}
|
||||
|
||||
void BackgroundPreviewBox::paintEvent(QPaintEvent *e) {
|
||||
Painter p(this);
|
||||
|
||||
const auto ms = getms();
|
||||
const auto color = _paper.backgroundColor();
|
||||
if (color) {
|
||||
p.fillRect(e->rect(), *color);
|
||||
}
|
||||
if (!color || _paper.isPattern()) {
|
||||
if (_scaled.isNull() && !setScaledFromThumb()) {
|
||||
p.fillRect(e->rect(), st::boxBg);
|
||||
return;
|
||||
}
|
||||
paintImage(p);
|
||||
paintRadial(p, ms);
|
||||
}
|
||||
paintTexts(p, ms);
|
||||
}
|
||||
|
||||
void BackgroundPreviewBox::paintImage(Painter &p) {
|
||||
Expects(!_scaled.isNull());
|
||||
|
||||
p.setOpacity(_paper.isPattern()
|
||||
? std::clamp(_paper.patternIntensity() / 100., 0., 1.)
|
||||
: 1.);
|
||||
const auto guard = gsl::finally([&] { p.setOpacity(1.); });
|
||||
|
||||
const auto factor = cIntRetinaFactor();
|
||||
const auto size = st::boxWideWidth;
|
||||
const auto from = QRect(
|
||||
0,
|
||||
(size - height()) / 2 * factor,
|
||||
size * factor,
|
||||
height() * factor);
|
||||
p.drawPixmap(
|
||||
rect(),
|
||||
(!_blurred.isNull() && _paper.isBlurred()) ? _blurred : _scaled,
|
||||
from);
|
||||
}
|
||||
|
||||
void BackgroundPreviewBox::paintRadial(Painter &p, TimeMs ms) {
|
||||
bool radial = false;
|
||||
float64 radialOpacity = 0;
|
||||
if (_radial.animating()) {
|
||||
_radial.step(ms);
|
||||
radial = _radial.animating();
|
||||
radialOpacity = _radial.opacity();
|
||||
}
|
||||
if (!radial) {
|
||||
return;
|
||||
}
|
||||
auto inner = radialRect();
|
||||
|
||||
p.setPen(Qt::NoPen);
|
||||
p.setOpacity(radialOpacity);
|
||||
p.setBrush(st::radialBg);
|
||||
|
||||
{
|
||||
PainterHighQualityEnabler hq(p);
|
||||
p.drawEllipse(inner);
|
||||
}
|
||||
|
||||
p.setOpacity(1);
|
||||
QRect arc(inner.marginsRemoved(QMargins(st::radialLine, st::radialLine, st::radialLine, st::radialLine)));
|
||||
_radial.draw(p, arc, st::radialLine, st::radialFg);
|
||||
}
|
||||
|
||||
QRect BackgroundPreviewBox::radialRect() const {
|
||||
const auto available = height()
|
||||
- st::historyPaddingBottom
|
||||
- _text1->height()
|
||||
- _text2->height()
|
||||
- st::historyPaddingBottom;
|
||||
return QRect(
|
||||
QPoint(
|
||||
(width() - st::radialSize.width()) / 2,
|
||||
(available - st::radialSize.height()) / 2),
|
||||
st::radialSize);
|
||||
}
|
||||
|
||||
void BackgroundPreviewBox::paintTexts(Painter &p, TimeMs ms) {
|
||||
const auto height1 = _text1->height();
|
||||
const auto height2 = _text2->height();
|
||||
const auto top = height()
|
||||
- height1
|
||||
- height2
|
||||
- st::historyPaddingBottom;
|
||||
p.translate(0, top);
|
||||
paintDate(p);
|
||||
_text1->draw(p, rect(), TextSelection(), ms);
|
||||
p.translate(0, height1);
|
||||
_text2->draw(p, rect(), TextSelection(), ms);
|
||||
p.translate(0, height2);
|
||||
}
|
||||
|
||||
void BackgroundPreviewBox::paintDate(Painter &p) {
|
||||
const auto date = _text1->Get<HistoryView::DateBadge>();
|
||||
if (!date || !_serviceBg) {
|
||||
return;
|
||||
}
|
||||
const auto text = date->text;
|
||||
const auto bubbleHeight = st::msgServicePadding.top() + st::msgServiceFont->height + st::msgServicePadding.bottom();
|
||||
const auto bubbleTop = st::msgServiceMargin.top();
|
||||
const auto textWidth = st::msgServiceFont->width(text);
|
||||
const auto bubbleWidth = st::msgServicePadding.left() + textWidth + st::msgServicePadding.right();
|
||||
const auto bubbleLeft = (width() - bubbleWidth) / 2;
|
||||
const auto radius = bubbleHeight / 2;
|
||||
p.setPen(Qt::NoPen);
|
||||
p.setBrush(*_serviceBg);
|
||||
p.drawRoundedRect(bubbleLeft, bubbleTop, bubbleWidth, bubbleHeight, radius, radius);
|
||||
p.setPen(st::msgServiceFg);
|
||||
p.setFont(st::msgServiceFont);
|
||||
p.drawText(bubbleLeft + st::msgServicePadding.left(), bubbleTop + st::msgServicePadding.top() + st::msgServiceFont->ascent, text);
|
||||
}
|
||||
|
||||
void BackgroundPreviewBox::step_radial(TimeMs ms, bool timer) {
|
||||
Expects(_paper.document() != nullptr);
|
||||
|
||||
const auto document = _paper.document();
|
||||
const auto wasAnimating = _radial.animating();
|
||||
const auto updated = _radial.update(
|
||||
document->progress(),
|
||||
!document->loading(),
|
||||
ms);
|
||||
if (timer
|
||||
&& (wasAnimating || _radial.animating())
|
||||
&& (!anim::Disabled() || updated)) {
|
||||
update(radialRect());
|
||||
}
|
||||
checkLoadedDocument();
|
||||
}
|
||||
|
||||
bool BackgroundPreviewBox::setScaledFromThumb() {
|
||||
const auto thumbnail = _paper.thumbnail();
|
||||
if (!thumbnail || !thumbnail->loaded()) {
|
||||
return false;
|
||||
}
|
||||
setScaledFromImage(PrepareScaledFromFull(
|
||||
thumbnail->original(),
|
||||
patternBackgroundColor(),
|
||||
_paper.document() ? Images::Option::Blurred : Images::Option(0)));
|
||||
return true;
|
||||
}
|
||||
|
||||
void BackgroundPreviewBox::setScaledFromImage(
|
||||
QImage &&image,
|
||||
QImage &&blurred) {
|
||||
updateServiceBg(Window::Theme::CountAverageColor(image));
|
||||
_scaled = App::pixmapFromImageInPlace(std::move(image));
|
||||
_blurred = App::pixmapFromImageInPlace(std::move(blurred));
|
||||
}
|
||||
|
||||
void BackgroundPreviewBox::updateServiceBg(std::optional<QColor> background) {
|
||||
if (background) {
|
||||
_serviceBg = Window::Theme::AdjustedColor(
|
||||
st::msgServiceBg->c,
|
||||
*background);
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<QColor> BackgroundPreviewBox::patternBackgroundColor() const {
|
||||
return _paper.isPattern() ? _paper.backgroundColor() : std::nullopt;
|
||||
}
|
||||
|
||||
void BackgroundPreviewBox::checkLoadedDocument() {
|
||||
const auto document = _paper.document();
|
||||
if (!document
|
||||
|| !document->loaded(DocumentData::FilePathResolveChecked)
|
||||
|| _generating) {
|
||||
return;
|
||||
}
|
||||
const auto generateCallback = [=](QImage &&image) {
|
||||
auto [left, right] = base::make_binary_guard();
|
||||
_generating = std::move(left);
|
||||
crl::async([
|
||||
this,
|
||||
image = std::move(image),
|
||||
patternBackground = patternBackgroundColor(),
|
||||
guard = std::move(right)
|
||||
]() mutable {
|
||||
auto scaled = PrepareScaledFromFull(image, patternBackground);
|
||||
const auto ms = getms();
|
||||
auto blurred = patternBackground
|
||||
? QImage()
|
||||
: PrepareScaledNonPattern(
|
||||
Data::PrepareBlurredBackground(image),
|
||||
Images::Option(0));
|
||||
crl::on_main([
|
||||
this,
|
||||
image = std::move(image),
|
||||
scaled = std::move(scaled),
|
||||
blurred = std::move(blurred),
|
||||
guard = std::move(guard)
|
||||
]() mutable {
|
||||
if (!guard) {
|
||||
return;
|
||||
}
|
||||
setScaledFromImage(std::move(scaled), std::move(blurred));
|
||||
_full = std::move(image);
|
||||
update();
|
||||
});
|
||||
});
|
||||
};
|
||||
_generating = Data::ReadImageAsync(
|
||||
document,
|
||||
Window::Theme::ProcessBackgroundImage,
|
||||
generateCallback);
|
||||
}
|
||||
|
||||
bool BackgroundPreviewBox::Start(
|
||||
const QString &slug,
|
||||
const QMap<QString, QString> ¶ms) {
|
||||
if (const auto paper = Data::WallPaper::FromColorSlug(slug)) {
|
||||
Ui::show(Box<BackgroundPreviewBox>(paper->withUrlParams(params)));
|
||||
return true;
|
||||
}
|
||||
if (!IsValidWallPaperSlug(slug)) {
|
||||
Ui::show(Box<InformBox>(lang(lng_background_bad_link)));
|
||||
return false;
|
||||
}
|
||||
Auth().api().requestWallPaper(slug, [=](const Data::WallPaper &result) {
|
||||
Ui::show(Box<BackgroundPreviewBox>(result.withUrlParams(params)));
|
||||
}, [](const RPCError &error) {
|
||||
Ui::show(Box<InformBox>(lang(lng_background_bad_link)));
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
HistoryView::Context BackgroundPreviewBox::elementContext() {
|
||||
return HistoryView::Context::ContactPreview;
|
||||
}
|
||||
|
||||
std::unique_ptr<HistoryView::Element> BackgroundPreviewBox::elementCreate(
|
||||
not_null<HistoryMessage*> message) {
|
||||
return std::make_unique<HistoryView::Message>(this, message);
|
||||
}
|
||||
|
||||
std::unique_ptr<HistoryView::Element> BackgroundPreviewBox::elementCreate(
|
||||
not_null<HistoryService*> message) {
|
||||
Unexpected("Service message in BackgroundPreviewBox.");
|
||||
}
|
||||
|
||||
bool BackgroundPreviewBox::elementUnderCursor(
|
||||
not_null<const Element*> view) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void BackgroundPreviewBox::elementAnimationAutoplayAsync(
|
||||
not_null<const Element*> element) {
|
||||
}
|
||||
|
||||
TimeMs BackgroundPreviewBox::elementHighlightTime(
|
||||
not_null<const Element*> element) {
|
||||
return TimeMs();
|
||||
}
|
||||
|
||||
bool BackgroundPreviewBox::elementInSelectionMode() {
|
||||
return false;
|
||||
}
|
||||
|
Reference in New Issue
Block a user