tdesktop/Telegram/SourceFiles/ui/widgets/discrete_sliders.h

127 lines
2.8 KiB
C
Raw Normal View History

/*
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
2017-09-13 16:57:44 +00:00
#include "ui/rp_widget.h"
#include "ui/round_rect.h"
2019-04-02 09:13:30 +00:00
#include "ui/effects/animations.h"
2016-11-22 09:48:13 +00:00
#include "styles/style_widgets.h"
namespace Ui {
2016-12-02 19:16:35 +00:00
class RippleAnimation;
2017-09-13 16:57:44 +00:00
class DiscreteSlider : public RpWidget {
public:
DiscreteSlider(QWidget *parent);
void addSection(const QString &label);
2021-03-15 10:51:13 +00:00
void setSections(const std::vector<QString> &labels);
int activeSection() const {
return _activeIndex;
}
void setActiveSection(int index);
void setActiveSectionFast(int index);
void finishAnimating();
2017-09-27 12:04:19 +00:00
auto sectionActivated() const {
2017-09-13 16:57:44 +00:00
return _sectionActivated.events();
}
protected:
2016-12-02 19:16:35 +00:00
void timerEvent(QTimerEvent *e) override;
void mousePressEvent(QMouseEvent *e) override;
void mouseMoveEvent(QMouseEvent *e) override;
void mouseReleaseEvent(QMouseEvent *e) override;
2016-11-22 09:48:13 +00:00
int resizeGetHeight(int newWidth) override = 0;
struct Section {
2019-12-14 14:46:31 +00:00
Section(const QString &label, const style::TextStyle &st);
2019-12-14 14:46:31 +00:00
int left = 0;
int width = 0;
Ui::Text::String label;
std::unique_ptr<RippleAnimation> ripple;
};
2016-11-22 09:48:13 +00:00
2019-04-02 09:13:30 +00:00
int getCurrentActiveLeft();
2016-11-22 09:48:13 +00:00
int getSectionsCount() const {
return _sections.size();
}
template <typename Lambda>
void enumerateSections(Lambda callback);
2017-11-12 13:47:11 +00:00
template <typename Lambda>
void enumerateSections(Lambda callback) const;
virtual void startRipple(int sectionIndex) {
2016-12-02 19:16:35 +00:00
}
2016-11-22 09:48:13 +00:00
void stopAnimation() {
2019-04-02 09:13:30 +00:00
_a_left.stop();
2016-11-22 09:48:13 +00:00
}
2016-12-02 19:16:35 +00:00
void setSelectOnPress(bool selectOnPress);
2016-11-22 09:48:13 +00:00
private:
2016-12-02 19:16:35 +00:00
void activateCallback();
2019-12-14 14:46:31 +00:00
virtual const style::TextStyle &getLabelStyle() const = 0;
2016-11-22 09:48:13 +00:00
virtual int getAnimationDuration() const = 0;
int getIndexFromPosition(QPoint pos);
void setSelectedSection(int index);
std::vector<Section> _sections;
int _activeIndex = 0;
2016-11-22 09:48:13 +00:00
bool _selectOnPress = true;
2017-09-13 16:57:44 +00:00
rpl::event_stream<int> _sectionActivated;
int _pressed = -1;
int _selected = 0;
2019-04-02 09:13:30 +00:00
Ui::Animations::Simple _a_left;
2016-11-22 09:48:13 +00:00
2016-12-02 19:16:35 +00:00
int _timerId = -1;
crl::time _callbackAfterMs = 0;
2016-12-02 19:16:35 +00:00
2016-11-22 09:48:13 +00:00
};
class SettingsSlider : public DiscreteSlider {
public:
SettingsSlider(QWidget *parent, const style::SettingsSlider &st = st::defaultSettingsSlider);
void setRippleTopRoundRadius(int radius);
2016-11-22 09:48:13 +00:00
protected:
void paintEvent(QPaintEvent *e) override;
int resizeGetHeight(int newWidth) override;
void startRipple(int sectionIndex) override;
2016-12-02 19:16:35 +00:00
2016-11-22 09:48:13 +00:00
private:
2019-12-14 14:46:31 +00:00
const style::TextStyle &getLabelStyle() const override;
2016-11-22 09:48:13 +00:00
int getAnimationDuration() const override;
QImage prepareRippleMask(int sectionIndex, const Section &section);
2016-11-22 09:48:13 +00:00
void resizeSections(int newWidth);
2017-11-12 13:47:11 +00:00
std::vector<float64> countSectionsWidths(int newWidth) const;
2016-11-22 09:48:13 +00:00
const style::SettingsSlider &_st;
std::optional<Ui::RoundRect> _bar;
std::optional<Ui::RoundRect> _barActive;
int _rippleTopRoundRadius = 0;
2016-12-02 19:16:35 +00:00
};
} // namespace Ui