2022-12-14 08:20:01 +00:00
|
|
|
/*
|
|
|
|
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 "editor/editor_layer_widget.h"
|
|
|
|
|
2022-12-21 15:12:58 +00:00
|
|
|
#include "ui/painter.h"
|
|
|
|
|
2022-12-14 08:20:01 +00:00
|
|
|
#include <QtGui/QGuiApplication>
|
|
|
|
|
|
|
|
namespace Editor {
|
2022-12-21 15:12:58 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kCacheBackgroundFastTimeout = crl::time(200);
|
|
|
|
constexpr auto kCacheBackgroundFullTimeout = crl::time(1000);
|
|
|
|
constexpr auto kFadeBackgroundDuration = crl::time(200);
|
|
|
|
|
|
|
|
// Thread: Main.
|
|
|
|
[[nodiscard]] bool IsNightMode() {
|
|
|
|
return (st::windowBg->c.lightnessF() < 0.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] QColor BlurOverlayColor(bool night) {
|
|
|
|
return QColor(16, 16, 16, night ? 128 : 192);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] QImage ProcessBackground(QImage image, bool night) {
|
|
|
|
const auto size = image.size();
|
|
|
|
auto p = QPainter(&image);
|
|
|
|
p.fillRect(
|
|
|
|
QRect(QPoint(), image.size() / image.devicePixelRatio()),
|
|
|
|
BlurOverlayColor(night));
|
|
|
|
p.end();
|
|
|
|
return Images::DitherImage(
|
|
|
|
Images::BlurLargeImage(
|
|
|
|
std::move(image).scaled(
|
|
|
|
size / style::ConvertScale(4),
|
|
|
|
Qt::IgnoreAspectRatio,
|
|
|
|
Qt::SmoothTransformation),
|
|
|
|
24).scaled(
|
|
|
|
size,
|
|
|
|
Qt::IgnoreAspectRatio,
|
|
|
|
Qt::SmoothTransformation));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2022-12-14 08:20:01 +00:00
|
|
|
|
|
|
|
LayerWidget::LayerWidget(
|
|
|
|
not_null<QWidget*> parent,
|
|
|
|
base::unique_qptr<Ui::RpWidget> content)
|
|
|
|
: Ui::LayerWidget(parent)
|
2022-12-21 15:12:58 +00:00
|
|
|
, _content(std::move(content))
|
|
|
|
, _backgroundTimer([=] { checkCacheBackground(); }) {
|
2022-12-14 08:20:01 +00:00
|
|
|
_content->setParent(this);
|
|
|
|
_content->show();
|
|
|
|
|
|
|
|
paintRequest(
|
|
|
|
) | rpl::start_with_next([=](const QRect &clip) {
|
|
|
|
auto p = QPainter(this);
|
2022-12-21 15:12:58 +00:00
|
|
|
const auto faded = _backgroundFade.value(1.);
|
|
|
|
if (faded < 1.) {
|
|
|
|
p.drawImage(rect(), _backgroundBack);
|
|
|
|
if (faded > 0.) {
|
|
|
|
p.setOpacity(faded);
|
|
|
|
p.drawImage(rect(), _background);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p.drawImage(rect(), _background);
|
|
|
|
}
|
2022-12-14 08:20:01 +00:00
|
|
|
}, lifetime());
|
2022-12-21 15:12:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LayerWidget::eventHook(QEvent *e) {
|
|
|
|
return RpWidget::eventHook(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerWidget::start() {
|
|
|
|
_backgroundNight = IsNightMode();
|
|
|
|
_background = ProcessBackground(renderBackground(), _backgroundNight);
|
2022-12-14 08:20:01 +00:00
|
|
|
|
|
|
|
sizeValue(
|
|
|
|
) | rpl::start_with_next([=](const QSize &size) {
|
2022-12-21 15:12:58 +00:00
|
|
|
checkBackgroundStale();
|
2022-12-14 08:20:01 +00:00
|
|
|
_content->resize(size);
|
|
|
|
}, lifetime());
|
2022-12-21 15:12:58 +00:00
|
|
|
|
|
|
|
style::PaletteChanged() | rpl::start_with_next([=] {
|
|
|
|
checkBackgroundStale();
|
|
|
|
}, lifetime());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerWidget::checkBackgroundStale() {
|
|
|
|
const auto ratio = style::DevicePixelRatio();
|
|
|
|
const auto &ready = _backgroundNext.isNull()
|
|
|
|
? _background
|
|
|
|
: _backgroundNext;
|
|
|
|
if (ready.size() == size() * ratio
|
|
|
|
&& _backgroundNight == IsNightMode()) {
|
|
|
|
_backgroundTimer.cancel();
|
|
|
|
} else if (!_backgroundCaching && !_backgroundTimer.isActive()) {
|
|
|
|
_lastAreaChangeTime = crl::now();
|
|
|
|
_backgroundTimer.callOnce(kCacheBackgroundFastTimeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage LayerWidget::renderBackground() {
|
2022-12-28 09:05:23 +00:00
|
|
|
const auto parent = parentWidget();
|
|
|
|
const auto target = parent->parentWidget();
|
2022-12-21 15:12:58 +00:00
|
|
|
Ui::SendPendingMoveResizeEvents(target);
|
|
|
|
|
|
|
|
const auto ratio = style::DevicePixelRatio();
|
|
|
|
auto image = QImage(size() * ratio, QImage::Format_ARGB32_Premultiplied);
|
|
|
|
image.setDevicePixelRatio(ratio);
|
|
|
|
|
2022-12-28 09:05:23 +00:00
|
|
|
const auto shown = !parent->isHidden();
|
|
|
|
const auto focused = shown && Ui::InFocusChain(parent);
|
|
|
|
if (shown) {
|
|
|
|
if (focused) {
|
|
|
|
target->setFocus();
|
|
|
|
}
|
|
|
|
parent->hide();
|
|
|
|
}
|
2022-12-21 15:12:58 +00:00
|
|
|
auto p = QPainter(&image);
|
|
|
|
Ui::RenderWidget(p, target, QPoint(), geometry());
|
|
|
|
p.end();
|
|
|
|
|
2022-12-28 09:05:23 +00:00
|
|
|
if (shown) {
|
|
|
|
parent->show();
|
|
|
|
if (focused) {
|
|
|
|
if (isHidden()) {
|
|
|
|
parent->setFocus();
|
|
|
|
} else {
|
|
|
|
setInnerFocus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-21 15:12:58 +00:00
|
|
|
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerWidget::checkCacheBackground() {
|
|
|
|
if (_backgroundCaching || _backgroundTimer.isActive()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto now = crl::now();
|
|
|
|
if (now - _lastAreaChangeTime < kCacheBackgroundFullTimeout
|
|
|
|
&& QGuiApplication::mouseButtons() != 0) {
|
|
|
|
_backgroundTimer.callOnce(kCacheBackgroundFastTimeout);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cacheBackground();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerWidget::cacheBackground() {
|
|
|
|
_backgroundCaching = true;
|
|
|
|
const auto weak = Ui::MakeWeak(this);
|
|
|
|
const auto night = IsNightMode();
|
|
|
|
crl::async([weak, night, image = renderBackground()]() mutable {
|
|
|
|
auto result = ProcessBackground(image, night);
|
|
|
|
crl::on_main([weak, night, result = std::move(result)]() mutable {
|
|
|
|
if (const auto strong = weak.data()) {
|
|
|
|
strong->backgroundReady(std::move(result), night);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerWidget::backgroundReady(QImage background, bool night) {
|
|
|
|
_backgroundCaching = false;
|
|
|
|
|
|
|
|
const auto required = size() * style::DevicePixelRatio();
|
|
|
|
if (background.size() == required && night == IsNightMode()) {
|
|
|
|
_backgroundNext = std::move(background);
|
|
|
|
_backgroundNight = night;
|
|
|
|
if (!_backgroundFade.animating()) {
|
|
|
|
startBackgroundFade();
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
} else if (_background.size() != required) {
|
|
|
|
_backgroundTimer.callOnce(kCacheBackgroundFastTimeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerWidget::startBackgroundFade() {
|
|
|
|
if (_backgroundNext.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_backgroundBack = std::move(_background);
|
|
|
|
_background = base::take(_backgroundNext);
|
|
|
|
_backgroundFade.start([=] {
|
|
|
|
update();
|
|
|
|
if (!_backgroundFade.animating()) {
|
|
|
|
_backgroundBack = QImage();
|
|
|
|
startBackgroundFade();
|
|
|
|
}
|
|
|
|
}, 0., 1., kFadeBackgroundDuration);
|
2022-12-14 08:20:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LayerWidget::parentResized() {
|
|
|
|
resizeToWidth(parentWidget()->width());
|
2022-12-21 15:12:58 +00:00
|
|
|
if (_background.isNull()) {
|
|
|
|
start();
|
|
|
|
}
|
2022-12-14 08:20:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LayerWidget::keyPressEvent(QKeyEvent *e) {
|
|
|
|
QGuiApplication::sendEvent(_content.get(), e);
|
|
|
|
}
|
|
|
|
|
|
|
|
int LayerWidget::resizeGetHeight(int newWidth) {
|
|
|
|
return parentWidget()->height();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LayerWidget::closeByOutsideClick() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Editor
|