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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/*
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 "export/view/export_view_progress.h"
#include "ui/effects/animations.h"
#include "ui/widgets/labels.h"
#include "ui/widgets/buttons.h"
#include "ui/wrap/fade_wrap.h"
#include "ui/wrap/vertical_layout.h"
#include "lang/lang_keys.h"
#include "styles/style_boxes.h"
#include "styles/style_export.h"
namespace Export {
namespace View {
namespace {
constexpr auto kShowSkipFileTimeout = 5 * crl::time(1000);
} // namespace
class ProgressWidget::Row : public Ui::RpWidget {
public:
Row(QWidget *parent, Content::Row &&data);
void updateData(Content::Row &&data);
protected:
int resizeGetHeight(int newWidth) override;
void paintEvent(QPaintEvent *e) override;
private:
struct Instance {
base::unique_qptr<Ui::FadeWrap<Ui::FlatLabel>> label;
base::unique_qptr<Ui::FadeWrap<Ui::FlatLabel>> info;
float64 value = 0.;
Ui::Animations::Simple progress;
bool hiding = true;
Ui::Animations::Simple opacity;
};
void fillCurrentInstance();
void hideCurrentInstance();
void setInstanceProgress(Instance &instance, float64 progress);
void toggleInstance(Instance &data, bool shown);
void instanceOpacityCallback(QPointer<Ui::FlatLabel> label);
void removeOldInstance(QPointer<Ui::FlatLabel> label);
void paintInstance(QPainter &p, const Instance &data);
void updateControlsGeometry(int newWidth);
void updateInstanceGeometry(const Instance &instance, int newWidth);
Content::Row _data;
Instance _current;
std::vector<Instance> _old;
};
ProgressWidget::Row::Row(QWidget *parent, Content::Row &&data)
: RpWidget(parent)
, _data(std::move(data)) {
fillCurrentInstance();
}
void ProgressWidget::Row::updateData(Content::Row &&data) {
const auto wasId = _data.id;
const auto nowId = data.id;
_data = std::move(data);
if (nowId.isEmpty()) {
hideCurrentInstance();
} else if (wasId.isEmpty()) {
fillCurrentInstance();
} else {
_current.label->entity()->setText(_data.label);
_current.info->entity()->setText(_data.info);
setInstanceProgress(_current, _data.progress);
if (nowId != wasId) {
_current.progress.stop();
}
}
updateControlsGeometry(width());
update();
}
void ProgressWidget::Row::fillCurrentInstance() {
_current.label = base::make_unique_q<Ui::FadeWrap<Ui::FlatLabel>>(
this,
object_ptr<Ui::FlatLabel>(
this,
_data.label,
st::exportProgressLabel));
_current.info = base::make_unique_q<Ui::FadeWrap<Ui::FlatLabel>>(
this,
object_ptr<Ui::FlatLabel>(
this,
_data.info,
st::exportProgressInfoLabel));
_current.label->hide(anim::type::instant);
_current.info->hide(anim::type::instant);
setInstanceProgress(_current, _data.progress);
toggleInstance(_current, true);
if (_data.id == "main") {
_current.opacity.stop();
_current.label->finishAnimating();
_current.info->finishAnimating();
}
}
void ProgressWidget::Row::hideCurrentInstance() {
if (!_current.label) {
return;
}
setInstanceProgress(_current, 1.);
toggleInstance(_current, false);
_old.push_back(std::move(_current));
}
void ProgressWidget::Row::setInstanceProgress(
Instance &instance,
float64 progress) {
if (_current.value < progress) {
_current.progress.start(
[=] { update(); },
_current.value,
progress,
st::exportProgressDuration,
anim::sineInOut);
} else if (_current.value > progress) {
_current.progress.stop();
}
_current.value = progress;
}
void ProgressWidget::Row::toggleInstance(Instance &instance, bool shown) {
Expects(instance.label != nullptr);
if (instance.hiding != shown) {
return;
}
const auto label = Ui::MakeWeak(instance.label->entity());
instance.opacity.start(
[=] { instanceOpacityCallback(label); },
shown ? 0. : 1.,
shown ? 1. : 0.,
st::exportProgressDuration);
instance.hiding = !shown;
_current.label->toggle(shown, anim::type::normal);
_current.info->toggle(shown, anim::type::normal);
}
void ProgressWidget::Row::instanceOpacityCallback(
QPointer<Ui::FlatLabel> label) {
update();
const auto i = ranges::find(_old, label, [](const Instance &instance) {
return Ui::MakeWeak(instance.label->entity());
});
if (i != end(_old) && i->hiding && !i->opacity.animating()) {
crl::on_main(this, [=] {
removeOldInstance(label);
});
}
}
void ProgressWidget::Row::removeOldInstance(QPointer<Ui::FlatLabel> label) {
const auto i = ranges::find(_old, label, [](const Instance &instance) {
return Ui::MakeWeak(instance.label->entity());
});
if (i != end(_old)) {
_old.erase(i);
}
}
int ProgressWidget::Row::resizeGetHeight(int newWidth) {
updateControlsGeometry(newWidth);
return st::exportProgressRowHeight;
}
void ProgressWidget::Row::paintEvent(QPaintEvent *e) {
auto p = QPainter(this);
const auto thickness = st::exportProgressWidth;
const auto top = height() - thickness;
p.fillRect(0, top, width(), thickness, st::shadowFg);
for (const auto &instance : _old) {
paintInstance(p, instance);
}
paintInstance(p, _current);
}
void ProgressWidget::Row::paintInstance(QPainter &p, const Instance &data) {
const auto opacity = data.opacity.value(data.hiding ? 0. : 1.);
if (!opacity) {
return;
}
p.setOpacity(opacity);
const auto thickness = st::exportProgressWidth;
const auto top = height() - thickness;
const auto till = qRound(data.progress.value(data.value) * width());
if (till > 0) {
p.fillRect(0, top, till, thickness, st::exportProgressFg);
}
if (till < width()) {
const auto left = width() - till;
p.fillRect(till, top, left, thickness, st::exportProgressBg);
}
}
void ProgressWidget::Row::updateControlsGeometry(int newWidth) {
updateInstanceGeometry(_current, newWidth);
for (const auto &instance : _old) {
updateInstanceGeometry(instance, newWidth);
}
}
void ProgressWidget::Row::updateInstanceGeometry(
const Instance &instance,
int newWidth) {
if (!instance.label) {
return;
}
instance.info->resizeToNaturalWidth(newWidth);
instance.label->resizeToWidth(newWidth - instance.info->width());
instance.info->moveToRight(0, 0, newWidth);
instance.label->moveToLeft(0, 0, newWidth);
}
ProgressWidget::ProgressWidget(
QWidget *parent,
rpl::producer<Content> content)
: RpWidget(parent)
, _body(this)
, _fileShowSkipTimer([=] { _skipFile->show(anim::type::normal); }) {
widthValue(
) | rpl::start_with_next([=](int width) {
_body->resizeToWidth(width);
_body->moveToLeft(0, 0);
}, _body->lifetime());
auto skipFileWrap = _body->add(object_ptr<Ui::FixedHeightWidget>(
_body.data(),
st::defaultLinkButton.font->height + st::exportProgressRowSkip));
_skipFile = base::make_unique_q<Ui::FadeWrap<Ui::LinkButton>>(
skipFileWrap,
object_ptr<Ui::LinkButton>(
this,
tr::lng_export_skip_file(tr::now),
st::defaultLinkButton));
_skipFile->hide(anim::type::instant);
_skipFile->moveToLeft(st::exportProgressRowPadding.left(), 0);
_about = _body->add(
object_ptr<Ui::FlatLabel>(
this,
tr::lng_export_progress(tr::now),
st::exportAboutLabel),
st::exportAboutPadding);
std::move(
content
) | rpl::start_with_next([=](Content &&content) {
updateState(std::move(content));
}, lifetime());
_cancel = base::make_unique_q<Ui::RoundButton>(
this,
tr::lng_export_stop(),
st::exportCancelButton);
setupBottomButton(_cancel.get());
}
rpl::producer<uint64> ProgressWidget::skipFileClicks() const {
return _skipFile->entity()->clicks(
) | rpl::map([=] { return _fileRandomId; });
}
rpl::producer<> ProgressWidget::cancelClicks() const {
return _cancel
? (_cancel->clicks() | rpl::to_empty)
: (rpl::never<>() | rpl::type_erased());
}
rpl::producer<> ProgressWidget::doneClicks() const {
return _doneClicks.events();
}
void ProgressWidget::setupBottomButton(not_null<Ui::RoundButton*> button) {
button->show();
sizeValue(
) | rpl::start_with_next([=](QSize size) {
button->move(
(size.width() - button->width()) / 2,
(size.height() - st::exportCancelBottom - button->height()));
}, button->lifetime());
}
void ProgressWidget::updateState(Content &&content) {
if (!content.rows.empty() && content.rows[0].id == Content::kDoneId) {
showDone();
}
const auto wasCount = _rows.size();
auto index = 0;
for (auto &row : content.rows) {
if (index < _rows.size()) {
_rows[index]->updateData(std::move(row));
} else {
if (index > 0) {
_body->insert(
index * 2 - 1,
object_ptr<Ui::FixedHeightWidget>(
this,
st::exportProgressRowSkip));
}
_rows.push_back(_body->insert(
index * 2,
object_ptr<Row>(this, std::move(row)),
st::exportProgressRowPadding));
_rows.back()->show();
}
++index;
}
const auto fileRandomId = !content.rows.empty()
? content.rows.back().randomId
: uint64(0);
if (_fileRandomId != fileRandomId) {
_fileShowSkipTimer.cancel();
_skipFile->hide(anim::type::normal);
_fileRandomId = fileRandomId;
if (_fileRandomId) {
_fileShowSkipTimer.callOnce(kShowSkipFileTimeout);
}
}
for (const auto count = _rows.size(); index != count; ++index) {
_rows[index]->updateData(Content::Row());
}
if (_rows.size() != wasCount) {
_body->resizeToWidth(width());
}
}
void ProgressWidget::showDone() {
_cancel = nullptr;
_skipFile->hide(anim::type::instant);
_fileShowSkipTimer.cancel();
_about->setText(tr::lng_export_about_done(tr::now));
_done = base::make_unique_q<Ui::RoundButton>(
this,
tr::lng_export_done(),
st::exportDoneButton);
const auto desired = std::min(
st::exportDoneButton.font->width(tr::lng_export_done(tr::now).toUpper())
+ st::exportDoneButton.height
- st::exportDoneButton.font->height,
st::exportPanelSize.width() - 2 * st::exportCancelBottom);
if (_done->width() < desired) {
_done->setFullWidth(desired);
}
_done->clicks(
) | rpl::to_empty
| rpl::start_to_stream(_doneClicks, _done->lifetime());
setupBottomButton(_done.get());
}
ProgressWidget::~ProgressWidget() = default;
} // namespace View
} // namespace Export
|