Moved animation callback for shake effect to single place.

This commit is contained in:
23rd 2023-11-28 01:57:58 +03:00 committed by John Preston
parent f433d6fbc9
commit 9ef0e5cf83
8 changed files with 67 additions and 53 deletions

View file

@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/abstract_button.h" #include "ui/abstract_button.h"
#include "ui/effects/animations.h" #include "ui/effects/animations.h"
#include "ui/effects/shake_animation.h"
#include "ui/painter.h" #include "ui/painter.h"
#include "ui/rect.h" #include "ui/rect.h"
#include "styles/style_basic.h" #include "styles/style_basic.h"
@ -16,9 +17,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "styles/style_widgets.h" #include "styles/style_widgets.h"
namespace Statistic { namespace Statistic {
namespace {
constexpr auto kShiftDuration = crl::time(300);
} // namespace
class ChartLinesFilterWidget::FlatCheckbox final : public Ui::AbstractButton { class ChartLinesFilterWidget::FlatCheckbox final : public Ui::AbstractButton {
public: public:
@ -83,7 +81,7 @@ void ChartLinesFilterWidget::FlatCheckbox::setChecked(
} else { } else {
const auto from = value ? 0. : 1.; const auto from = value ? 0. : 1.;
const auto to = value ? 1. : 0.; const auto to = value ? 1. : 0.;
_animation.start([=] { update(); }, from, to, kShiftDuration); _animation.start([=] { update(); }, from, to, st::shakeDuration);
} }
} }
@ -95,30 +93,10 @@ void ChartLinesFilterWidget::FlatCheckbox::shake() {
if (_shake.animation.animating()) { if (_shake.animation.animating()) {
return; return;
} }
constexpr auto kShiftProgress = 6; _shake.animation.start(Ui::DefaultShakeCallback([=](int shift) {
constexpr auto kSegmentsCount = 5; _shake.shift = shift;
const auto refresh = [=] {
const auto fullProgress = _shake.animation.value(1.) * kShiftProgress;
const auto segment = std::clamp(
int(std::floor(fullProgress)),
0,
kSegmentsCount);
const auto part = fullProgress - segment;
const auto from = (segment == 0)
? 0.
: (segment == 1 || segment == 3 || segment == 5)
? 1.
: -1.;
const auto to = (segment == 0 || segment == 2 || segment == 4)
? 1.
: (segment == 1 || segment == 3)
? -1.
: 0.;
const auto shift = from * (1. - part) + to * part;
_shake.shift = int(base::SafeRound(shift * st::shakeShift));
update(); update();
}; }), 0., 1., st::shakeDuration);
_shake.animation.start(refresh, 0., 1., kShiftDuration);
} }
void ChartLinesFilterWidget::FlatCheckbox::paintEvent(QPaintEvent *e) { void ChartLinesFilterWidget::FlatCheckbox::paintEvent(QPaintEvent *e) {

View file

@ -8,6 +8,7 @@
#include "base/flat_map.h" #include "base/flat_map.h"
#include "ui/abstract_button.h" #include "ui/abstract_button.h"
#include "ui/effects/shake_animation.h"
#include "ui/paint/blobs.h" #include "ui/paint/blobs.h"
#include "ui/painter.h" #include "ui/painter.h"
#include "ui/power_saving.h" #include "ui/power_saving.h"
@ -48,7 +49,6 @@ constexpr auto kGlowAlpha = 150;
constexpr auto kOverrideColorBgAlpha = 76; constexpr auto kOverrideColorBgAlpha = 76;
constexpr auto kOverrideColorRippleAlpha = 50; constexpr auto kOverrideColorRippleAlpha = 50;
constexpr auto kShiftDuration = crl::time(300);
constexpr auto kSwitchStateDuration = crl::time(120); constexpr auto kSwitchStateDuration = crl::time(120);
constexpr auto kSwitchLabelDuration = crl::time(180); constexpr auto kSwitchLabelDuration = crl::time(180);
@ -996,29 +996,10 @@ void CallMuteButton::shake() {
if (_shakeAnimation.animating()) { if (_shakeAnimation.animating()) {
return; return;
} }
const auto update = [=] { _shakeAnimation.start(DefaultShakeCallback([=](int shift) {
const auto fullProgress = _shakeAnimation.value(1.) * 6; _labelShakeShift = shift;
const auto segment = std::clamp(int(std::floor(fullProgress)), 0, 5);
const auto part = fullProgress - segment;
const auto from = (segment == 0)
? 0.
: (segment == 1 || segment == 3 || segment == 5)
? 1.
: -1.;
const auto to = (segment == 0 || segment == 2 || segment == 4)
? 1.
: (segment == 1 || segment == 3)
? -1.
: 0.;
const auto shift = from * (1. - part) + to * part;
_labelShakeShift = int(base::SafeRound(shift * st::shakeShift));
updateLabelsGeometry(); updateLabelsGeometry();
}; }), 0., 1., st::shakeDuration);
_shakeAnimation.start(
update,
0.,
1.,
kShiftDuration);
} }
CallMuteButton::HandleMouseState CallMuteButton::HandleMouseStateFromType( CallMuteButton::HandleMouseState CallMuteButton::HandleMouseStateFromType(

View file

@ -0,0 +1,39 @@
/*
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 "ui/effects/shake_animation.h"
#include "styles/style_basic.h"
namespace Ui {
Fn<void(float64)> DefaultShakeCallback(Fn<void(int)> applyShift) {
constexpr auto kShiftProgress = 6;
constexpr auto kSegmentsCount = 5;
return [=, applyShift = std::move(applyShift)](float64 value) {
const auto fullProgress = value * kShiftProgress;
const auto segment = std::clamp(
int(std::floor(fullProgress)),
0,
kSegmentsCount);
const auto part = fullProgress - segment;
const auto from = (segment == 0)
? 0.
: (segment == 1 || segment == 3 || segment == 5)
? 1.
: -1.;
const auto to = (segment == 0 || segment == 2 || segment == 4)
? 1.
: (segment == 1 || segment == 3)
? -1.
: 0.;
const auto shift = from * (1. - part) + to * part;
applyShift(int(base::SafeRound(shift * st::shakeShift)));
};
}
} // namespace Ui

View file

@ -0,0 +1,14 @@
/*
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
*/
#pragma once
namespace Ui {
Fn<void(float64)> DefaultShakeCallback(Fn<void(int)> applyShift);
} // namespace Ui

View file

@ -333,6 +333,8 @@ PRIVATE
ui/effects/round_checkbox.h ui/effects/round_checkbox.h
ui/effects/scroll_content_shadow.cpp ui/effects/scroll_content_shadow.cpp
ui/effects/scroll_content_shadow.h ui/effects/scroll_content_shadow.h
ui/effects/shake_animation.cpp
ui/effects/shake_animation.h
ui/effects/snowflakes.cpp ui/effects/snowflakes.cpp
ui/effects/snowflakes.h ui/effects/snowflakes.h
ui/effects/toggle_arrow.cpp ui/effects/toggle_arrow.cpp

@ -1 +1 @@
Subproject commit 299548dcaafc3f75462c78565c3d2ad68a8f7623 Subproject commit f69758da1906b204c156ea6ad254eacd61210a42

@ -1 +1 @@
Subproject commit de731885163bc1b3fe3095413453777ee89a8561 Subproject commit 0971b69ca90f1697ef81276d9820dcd6d26de4ac

@ -1 +1 @@
Subproject commit cd4e9d378cc98f590f814332900ec33863ffb98c Subproject commit 66ca90c1b2c111fe8ae45453c7bf180af2593d8d