mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Removed redundant templates from DiscreteSlider.
This commit is contained in:
parent
b06dbd1c00
commit
de3d7a7774
2 changed files with 80 additions and 51 deletions
|
@ -8,7 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include "ui/widgets/discrete_sliders.h"
|
#include "ui/widgets/discrete_sliders.h"
|
||||||
|
|
||||||
#include "ui/effects/ripple_animation.h"
|
#include "ui/effects/ripple_animation.h"
|
||||||
#include "ui/painter.h"
|
#include "styles/style_widgets.h"
|
||||||
|
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
|
|
||||||
|
@ -125,8 +125,7 @@ DiscreteSlider::Range DiscreteSlider::getCurrentActiveRange() const {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Lambda>
|
void DiscreteSlider::enumerateSections(Fn<bool(Section&)> callback) {
|
||||||
void DiscreteSlider::enumerateSections(Lambda callback) {
|
|
||||||
for (auto §ion : _sections) {
|
for (auto §ion : _sections) {
|
||||||
if (!callback(section)) {
|
if (!callback(section)) {
|
||||||
return;
|
return;
|
||||||
|
@ -134,9 +133,9 @@ void DiscreteSlider::enumerateSections(Lambda callback) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Lambda>
|
void DiscreteSlider::enumerateSections(
|
||||||
void DiscreteSlider::enumerateSections(Lambda callback) const {
|
Fn<bool(const Section&)> callback) const {
|
||||||
for (auto §ion : _sections) {
|
for (const auto §ion : _sections) {
|
||||||
if (!callback(section)) {
|
if (!callback(section)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -144,7 +143,7 @@ void DiscreteSlider::enumerateSections(Lambda callback) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiscreteSlider::mousePressEvent(QMouseEvent *e) {
|
void DiscreteSlider::mousePressEvent(QMouseEvent *e) {
|
||||||
auto index = getIndexFromPosition(e->pos());
|
const auto index = getIndexFromPosition(e->pos());
|
||||||
if (_selectOnPress) {
|
if (_selectOnPress) {
|
||||||
setSelectedSection(index);
|
setSelectedSection(index);
|
||||||
}
|
}
|
||||||
|
@ -153,17 +152,21 @@ void DiscreteSlider::mousePressEvent(QMouseEvent *e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiscreteSlider::mouseMoveEvent(QMouseEvent *e) {
|
void DiscreteSlider::mouseMoveEvent(QMouseEvent *e) {
|
||||||
if (_pressed < 0) return;
|
if (_pressed < 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (_selectOnPress) {
|
if (_selectOnPress) {
|
||||||
setSelectedSection(getIndexFromPosition(e->pos()));
|
setSelectedSection(getIndexFromPosition(e->pos()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiscreteSlider::mouseReleaseEvent(QMouseEvent *e) {
|
void DiscreteSlider::mouseReleaseEvent(QMouseEvent *e) {
|
||||||
auto pressed = std::exchange(_pressed, -1);
|
const auto pressed = std::exchange(_pressed, -1);
|
||||||
if (pressed < 0) return;
|
if (pressed < 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
auto index = getIndexFromPosition(e->pos());
|
const auto index = getIndexFromPosition(e->pos());
|
||||||
if (pressed < _sections.size()) {
|
if (pressed < _sections.size()) {
|
||||||
if (_sections[pressed].ripple) {
|
if (_sections[pressed].ripple) {
|
||||||
_sections[pressed].ripple->lastStop();
|
_sections[pressed].ripple->lastStop();
|
||||||
|
@ -175,14 +178,16 @@ void DiscreteSlider::mouseReleaseEvent(QMouseEvent *e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiscreteSlider::setSelectedSection(int index) {
|
void DiscreteSlider::setSelectedSection(int index) {
|
||||||
if (index < 0 || index >= _sections.size()) return;
|
if (index < 0 || index >= _sections.size()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (_selected != index) {
|
if (_selected != index) {
|
||||||
const auto from = getFinalActiveRange();
|
const auto from = getFinalActiveRange();
|
||||||
_selected = index;
|
_selected = index;
|
||||||
const auto to = getFinalActiveRange();
|
const auto to = getFinalActiveRange();
|
||||||
const auto duration = getAnimationDuration();
|
const auto duration = getAnimationDuration();
|
||||||
const auto updater = [=] { update(); };
|
const auto updater = [this] { update(); };
|
||||||
_a_left.start(updater, from.left, to.left, duration);
|
_a_left.start(updater, from.left, to.left, duration);
|
||||||
_a_width.start(updater, from.width, to.width, duration);
|
_a_width.start(updater, from.width, to.width, duration);
|
||||||
_callbackAfterMs = crl::now() + duration;
|
_callbackAfterMs = crl::now() + duration;
|
||||||
|
@ -190,8 +195,8 @@ void DiscreteSlider::setSelectedSection(int index) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int DiscreteSlider::getIndexFromPosition(QPoint pos) {
|
int DiscreteSlider::getIndexFromPosition(QPoint pos) {
|
||||||
int count = _sections.size();
|
const auto count = _sections.size();
|
||||||
for (int i = 0; i != count; ++i) {
|
for (auto i = 0; i != count; ++i) {
|
||||||
if (_sections[i].left + _sections[i].width > pos.x()) {
|
if (_sections[i].left + _sections[i].width > pos.x()) {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
@ -237,10 +242,12 @@ int SettingsSlider::getAnimationDuration() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
void SettingsSlider::resizeSections(int newWidth) {
|
void SettingsSlider::resizeSections(int newWidth) {
|
||||||
auto count = getSectionsCount();
|
const auto count = getSectionsCount();
|
||||||
if (!count) return;
|
if (!count) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
auto sectionWidths = countSectionsWidths(newWidth);
|
const auto sectionWidths = countSectionsWidths(newWidth);
|
||||||
|
|
||||||
auto skip = 0;
|
auto skip = 0;
|
||||||
auto x = 0.;
|
auto x = 0.;
|
||||||
|
@ -258,11 +265,10 @@ void SettingsSlider::resizeSections(int newWidth) {
|
||||||
stopAnimation();
|
stopAnimation();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<float64> SettingsSlider::countSectionsWidths(
|
std::vector<float64> SettingsSlider::countSectionsWidths(int newWidth) const {
|
||||||
int newWidth) const {
|
const auto count = getSectionsCount();
|
||||||
auto count = getSectionsCount();
|
const auto sectionsWidth = newWidth - (count - 1) * _st.barSkip;
|
||||||
auto sectionsWidth = newWidth - (count - 1) * _st.barSkip;
|
const auto sectionWidth = sectionsWidth / float64(count);
|
||||||
auto sectionWidth = sectionsWidth / float64(count);
|
|
||||||
|
|
||||||
auto result = std::vector<float64>(count, sectionWidth);
|
auto result = std::vector<float64>(count, sectionWidth);
|
||||||
auto labelsWidth = 0;
|
auto labelsWidth = 0;
|
||||||
|
@ -297,7 +303,9 @@ int SettingsSlider::resizeGetHeight(int newWidth) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void SettingsSlider::startRipple(int sectionIndex) {
|
void SettingsSlider::startRipple(int sectionIndex) {
|
||||||
if (!_st.ripple.showDuration) return;
|
if (!_st.ripple.showDuration) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
auto index = 0;
|
auto index = 0;
|
||||||
enumerateSections([this, &index, sectionIndex](Section §ion) {
|
enumerateSections([this, &index, sectionIndex](Section §ion) {
|
||||||
if (index++ == sectionIndex) {
|
if (index++ == sectionIndex) {
|
||||||
|
@ -319,13 +327,13 @@ void SettingsSlider::startRipple(int sectionIndex) {
|
||||||
QImage SettingsSlider::prepareRippleMask(
|
QImage SettingsSlider::prepareRippleMask(
|
||||||
int sectionIndex,
|
int sectionIndex,
|
||||||
const Section §ion) {
|
const Section §ion) {
|
||||||
auto size = QSize(section.width, height() - _st.rippleBottomSkip);
|
const auto size = QSize(section.width, height() - _st.rippleBottomSkip);
|
||||||
if (!_rippleTopRoundRadius
|
if (!_rippleTopRoundRadius
|
||||||
|| (sectionIndex > 0 && sectionIndex + 1 < getSectionsCount())) {
|
|| (sectionIndex > 0 && sectionIndex + 1 < getSectionsCount())) {
|
||||||
return RippleAnimation::RectMask(size);
|
return RippleAnimation::RectMask(size);
|
||||||
}
|
}
|
||||||
return RippleAnimation::MaskByDrawer(size, false, [&](QPainter &p) {
|
return RippleAnimation::MaskByDrawer(size, false, [&](QPainter &p) {
|
||||||
auto plusRadius = _rippleTopRoundRadius + 1;
|
const auto plusRadius = _rippleTopRoundRadius + 1;
|
||||||
p.drawRoundedRect(
|
p.drawRoundedRect(
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
|
@ -347,10 +355,10 @@ QImage SettingsSlider::prepareRippleMask(
|
||||||
}
|
}
|
||||||
|
|
||||||
void SettingsSlider::paintEvent(QPaintEvent *e) {
|
void SettingsSlider::paintEvent(QPaintEvent *e) {
|
||||||
Painter p(this);
|
auto p = QPainter(this);
|
||||||
|
|
||||||
auto clip = e->rect();
|
const auto clip = e->rect();
|
||||||
auto range = getCurrentActiveRange();
|
const auto range = DiscreteSlider::getCurrentActiveRange();
|
||||||
|
|
||||||
const auto drawRect = [&](QRect rect, bool active = false) {
|
const auto drawRect = [&](QRect rect, bool active = false) {
|
||||||
const auto &bar = active ? _barActive : _bar;
|
const auto &bar = active ? _barActive : _bar;
|
||||||
|
@ -366,28 +374,35 @@ void SettingsSlider::paintEvent(QPaintEvent *e) {
|
||||||
: section.width;
|
: section.width;
|
||||||
const auto activeLeft = section.left
|
const auto activeLeft = section.left
|
||||||
+ (section.width - activeWidth) / 2;
|
+ (section.width - activeWidth) / 2;
|
||||||
auto active = 1.
|
const auto active = 1.
|
||||||
- std::clamp(
|
- std::clamp(
|
||||||
qAbs(range.left - activeLeft) / float64(range.width),
|
std::abs(range.left - activeLeft) / float64(range.width),
|
||||||
0.,
|
0.,
|
||||||
1.);
|
1.);
|
||||||
if (section.ripple) {
|
if (section.ripple) {
|
||||||
auto color = anim::color(_st.rippleBg, _st.rippleBgActive, active);
|
const auto color = anim::color(
|
||||||
|
_st.rippleBg,
|
||||||
|
_st.rippleBgActive,
|
||||||
|
active);
|
||||||
section.ripple->paint(p, section.left, 0, width(), &color);
|
section.ripple->paint(p, section.left, 0, width(), &color);
|
||||||
if (section.ripple->empty()) {
|
if (section.ripple->empty()) {
|
||||||
section.ripple.reset();
|
section.ripple.reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!_st.barSnapToLabel) {
|
if (!_st.barSnapToLabel) {
|
||||||
auto from = activeLeft, tofill = activeWidth;
|
auto from = activeLeft;
|
||||||
|
auto tofill = activeWidth;
|
||||||
if (range.left > from) {
|
if (range.left > from) {
|
||||||
auto fill = qMin(tofill, range.left - from);
|
const auto fill = std::min(tofill, range.left - from);
|
||||||
drawRect(myrtlrect(from, _st.barTop, fill, _st.barStroke));
|
drawRect(myrtlrect(from, _st.barTop, fill, _st.barStroke));
|
||||||
from += fill;
|
from += fill;
|
||||||
tofill -= fill;
|
tofill -= fill;
|
||||||
}
|
}
|
||||||
if (range.left + activeWidth > from) {
|
if (range.left + activeWidth > from) {
|
||||||
if (auto fill = qMin(tofill, range.left + activeWidth - from)) {
|
const auto fill = std::min(
|
||||||
|
tofill,
|
||||||
|
range.left + activeWidth - from);
|
||||||
|
if (fill) {
|
||||||
drawRect(
|
drawRect(
|
||||||
myrtlrect(from, _st.barTop, fill, _st.barStroke),
|
myrtlrect(from, _st.barTop, fill, _st.barStroke),
|
||||||
true);
|
true);
|
||||||
|
@ -399,15 +414,20 @@ void SettingsSlider::paintEvent(QPaintEvent *e) {
|
||||||
drawRect(myrtlrect(from, _st.barTop, tofill, _st.barStroke));
|
drawRect(myrtlrect(from, _st.barTop, tofill, _st.barStroke));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const auto labelLeft = section.left + (section.width - section.label.maxWidth()) / 2;
|
const auto labelLeft = section.left
|
||||||
if (myrtlrect(labelLeft, _st.labelTop, section.label.maxWidth(), _st.labelStyle.font->height).intersects(clip)) {
|
+ (section.width - section.label.maxWidth()) / 2;
|
||||||
|
const auto rect = myrtlrect(
|
||||||
|
labelLeft,
|
||||||
|
_st.labelTop,
|
||||||
|
section.label.maxWidth(),
|
||||||
|
_st.labelStyle.font->height);
|
||||||
|
if (rect.intersects(clip)) {
|
||||||
p.setPen(anim::pen(_st.labelFg, _st.labelFgActive, active));
|
p.setPen(anim::pen(_st.labelFg, _st.labelFgActive, active));
|
||||||
section.label.drawLeft(
|
section.label.draw(p, {
|
||||||
p,
|
.position = QPoint(labelLeft, _st.labelTop),
|
||||||
labelLeft,
|
.outerWidth = width(),
|
||||||
_st.labelTop,
|
.availableWidth = section.label.maxWidth(),
|
||||||
section.label.maxWidth(),
|
});
|
||||||
width());
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
@ -416,7 +436,9 @@ void SettingsSlider::paintEvent(QPaintEvent *e) {
|
||||||
const auto from = std::max(range.left - add, 0);
|
const auto from = std::max(range.left - add, 0);
|
||||||
const auto till = std::min(range.left + range.width + add, width());
|
const auto till = std::min(range.left + range.width + add, width());
|
||||||
if (from < till) {
|
if (from < till) {
|
||||||
drawRect(myrtlrect(from, _st.barTop, till - from, _st.barStroke), true);
|
drawRect(
|
||||||
|
myrtlrect(from, _st.barTop, till - from, _st.barStroke),
|
||||||
|
true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,15 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include "ui/rp_widget.h"
|
#include "ui/rp_widget.h"
|
||||||
#include "ui/round_rect.h"
|
#include "ui/round_rect.h"
|
||||||
#include "ui/effects/animations.h"
|
#include "ui/effects/animations.h"
|
||||||
#include "styles/style_widgets.h"
|
|
||||||
|
namespace style {
|
||||||
|
struct TextStyle;
|
||||||
|
struct SettingsSlider;
|
||||||
|
} // namespace style
|
||||||
|
|
||||||
|
namespace st {
|
||||||
|
extern const style::SettingsSlider &defaultSettingsSlider;
|
||||||
|
} // namespace st
|
||||||
|
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
|
|
||||||
|
@ -72,11 +80,8 @@ protected:
|
||||||
return _sections.size();
|
return _sections.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Lambda>
|
void enumerateSections(Fn<bool(Section&)> callback);
|
||||||
void enumerateSections(Lambda callback);
|
void enumerateSections(Fn<bool(const Section&)> callback) const;
|
||||||
|
|
||||||
template <typename Lambda>
|
|
||||||
void enumerateSections(Lambda callback) const;
|
|
||||||
|
|
||||||
virtual void startRipple(int sectionIndex) {
|
virtual void startRipple(int sectionIndex) {
|
||||||
}
|
}
|
||||||
|
@ -116,7 +121,9 @@ private:
|
||||||
|
|
||||||
class SettingsSlider : public DiscreteSlider {
|
class SettingsSlider : public DiscreteSlider {
|
||||||
public:
|
public:
|
||||||
SettingsSlider(QWidget *parent, const style::SettingsSlider &st = st::defaultSettingsSlider);
|
SettingsSlider(
|
||||||
|
QWidget *parent,
|
||||||
|
const style::SettingsSlider &st = st::defaultSettingsSlider);
|
||||||
|
|
||||||
void setRippleTopRoundRadius(int radius);
|
void setRippleTopRoundRadius(int radius);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue