| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- /*
- 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 "window/themes/window_theme_warning.h"
- #include "ui/widgets/buttons.h"
- #include "ui/widgets/shadow.h"
- #include "ui/painter.h"
- #include "ui/ui_utility.h"
- #include "ui/cached_round_corners.h"
- #include "window/themes/window_theme.h"
- #include "lang/lang_keys.h"
- #include "styles/style_layers.h"
- #include "styles/style_boxes.h"
- namespace Window {
- namespace Theme {
- namespace {
- constexpr int kWaitBeforeRevertMs = 15999;
- } // namespace
- WarningWidget::WarningWidget(QWidget *parent)
- : TWidget(parent)
- , _timer([=] { handleTimer(); })
- , _secondsLeft(kWaitBeforeRevertMs / 1000)
- , _keepChanges(this, tr::lng_theme_keep_changes(), st::defaultBoxButton)
- , _revert(this, tr::lng_theme_revert(), st::defaultBoxButton) {
- using TextTransform = Ui::RoundButton::TextTransform;
- _keepChanges->setTextTransform(TextTransform::NoTransform);
- _keepChanges->setClickedCallback([] { KeepApplied(); });
- _revert->setTextTransform(TextTransform::NoTransform);
- _revert->setClickedCallback([] { Revert(); });
- updateText();
- }
- void WarningWidget::keyPressEvent(QKeyEvent *e) {
- if (e->key() == Qt::Key_Escape) {
- Window::Theme::Revert();
- }
- }
- void WarningWidget::paintEvent(QPaintEvent *e) {
- Painter p(this);
- if (!_cache.isNull()) {
- if (!_animation.animating()) {
- if (isHidden()) {
- return;
- }
- }
- p.setOpacity(_animation.value(_hiding ? 0. : 1.));
- p.drawPixmap(_outer.topLeft(), _cache);
- if (!_animation.animating()) {
- _cache = QPixmap();
- showChildren();
- _started = crl::now();
- _timer.callOnce(100);
- }
- return;
- }
- Ui::Shadow::paint(p, _inner, width(), st::boxRoundShadow);
- Ui::FillRoundRect(p, _inner, st::boxBg, Ui::BoxCorners);
- p.setFont(st::boxTitleFont);
- p.setPen(st::boxTitleFg);
- p.drawTextLeft(_inner.x() + st::boxTitlePosition.x(), _inner.y() + st::boxTitlePosition.y(), width(), tr::lng_theme_sure_keep(tr::now));
- p.setFont(st::boxTextFont);
- p.setPen(st::boxTextFg);
- p.drawTextLeft(_inner.x() + st::boxTitlePosition.x(), _inner.y() + st::themeWarningTextTop, width(), _text);
- }
- void WarningWidget::resizeEvent(QResizeEvent *e) {
- _inner = QRect((width() - st::themeWarningWidth) / 2, (height() - st::themeWarningHeight) / 2, st::themeWarningWidth, st::themeWarningHeight);
- _outer = _inner.marginsAdded(st::boxRoundShadow.extend);
- updateControlsGeometry();
- update();
- }
- void WarningWidget::updateControlsGeometry() {
- auto left = _inner.x() + _inner.width() - st::defaultBox.buttonPadding.right() - _keepChanges->width();
- _keepChanges->moveToLeft(left, _inner.y() + _inner.height() - st::defaultBox.buttonPadding.bottom() - _keepChanges->height());
- _revert->moveToLeft(left - st::defaultBox.buttonPadding.left() - _revert->width(), _keepChanges->y());
- }
- void WarningWidget::refreshLang() {
- InvokeQueued(this, [this] { updateControlsGeometry(); });
- }
- void WarningWidget::handleTimer() {
- auto msPassed = crl::now() - _started;
- setSecondsLeft((kWaitBeforeRevertMs - msPassed) / 1000);
- }
- void WarningWidget::setSecondsLeft(int secondsLeft) {
- if (secondsLeft <= 0) {
- Window::Theme::Revert();
- } else {
- if (_secondsLeft != secondsLeft) {
- _secondsLeft = secondsLeft;
- updateText();
- update();
- }
- _timer.callOnce(100);
- }
- }
- void WarningWidget::updateText() {
- _text = tr::lng_theme_reverting(tr::now, lt_count, _secondsLeft);
- }
- void WarningWidget::showAnimated() {
- startAnimation(false);
- show();
- setFocus();
- }
- void WarningWidget::hideAnimated() {
- startAnimation(true);
- }
- void WarningWidget::startAnimation(bool hiding) {
- _timer.cancel();
- _hiding = hiding;
- if (_cache.isNull()) {
- showChildren();
- Ui::SendPendingMoveResizeEvents(this);
- _cache = Ui::GrabWidget(this, _outer);
- }
- hideChildren();
- _animation.start([this] {
- update();
- if (_hiding) {
- hide();
- if (_hiddenCallback) {
- _hiddenCallback();
- }
- }
- }, _hiding ? 1. : 0., _hiding ? 0. : 1., st::boxDuration);
- }
- } // namespace Theme
- } // namespace Window
|