1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
/*
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"
#include "ui/painter.h"
#include <QtGui/QGuiApplication>
namespace Editor {
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
LayerWidget::LayerWidget(
not_null<QWidget*> parent,
base::unique_qptr<Ui::RpWidget> content)
: Ui::LayerWidget(parent)
, _content(std::move(content))
, _backgroundTimer([=] { checkCacheBackground(); }) {
_content->setParent(this);
_content->show();
paintRequest(
) | rpl::start_with_next([=](const QRect &clip) {
auto p = QPainter(this);
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);
}
}, lifetime());
}
bool LayerWidget::eventHook(QEvent *e) {
return RpWidget::eventHook(e);
}
void LayerWidget::start() {
_backgroundNight = IsNightMode();
_background = ProcessBackground(renderBackground(), _backgroundNight);
sizeValue(
) | rpl::start_with_next([=](const QSize &size) {
checkBackgroundStale();
_content->resize(size);
}, lifetime());
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() {
const auto parent = parentWidget();
const auto target = parent->parentWidget();
Ui::SendPendingMoveResizeEvents(target);
const auto ratio = style::DevicePixelRatio();
auto image = QImage(size() * ratio, QImage::Format_ARGB32_Premultiplied);
image.setDevicePixelRatio(ratio);
const auto shown = !parent->isHidden();
const auto focused = shown && Ui::InFocusChain(parent);
if (shown) {
if (focused) {
target->setFocus();
}
parent->hide();
}
auto p = QPainter(&image);
Ui::RenderWidget(p, target, QPoint(), geometry());
p.end();
if (shown) {
parent->show();
if (focused) {
if (isHidden()) {
parent->setFocus();
} else {
setInnerFocus();
}
}
}
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);
}
void LayerWidget::parentResized() {
resizeToWidth(parentWidget()->width());
if (_background.isNull()) {
start();
}
}
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
|