Added ability to set custom width of section to DiscreteSlider.

This commit is contained in:
23rd 2024-11-02 14:44:29 +03:00
parent de3d7a7774
commit 5a4449f1a2
2 changed files with 22 additions and 10 deletions

View file

@ -57,6 +57,13 @@ void DiscreteSlider::finishAnimating() {
} }
} }
void DiscreteSlider::setAdditionalContentWidthToSection(int index, int w) {
if (index >= 0 && index < _sections.size()) {
auto &section = _sections[index];
section.contentWidth = section.label.maxWidth() + w;
}
}
void DiscreteSlider::setSelectOnPress(bool selectOnPress) { void DiscreteSlider::setSelectOnPress(bool selectOnPress) {
_selectOnPress = selectOnPress; _selectOnPress = selectOnPress;
} }
@ -112,7 +119,7 @@ DiscreteSlider::Range DiscreteSlider::getFinalActiveRange() const {
return { 0, 0 }; return { 0, 0 };
} }
const auto width = _snapToLabel const auto width = _snapToLabel
? std::min(raw->width, raw->label.maxWidth()) ? std::min(raw->width, raw->contentWidth)
: raw->width; : raw->width;
return { raw->left + ((raw->width - width) / 2), width }; return { raw->left + ((raw->width - width) / 2), width };
} }
@ -207,7 +214,8 @@ int DiscreteSlider::getIndexFromPosition(QPoint pos) {
DiscreteSlider::Section::Section( DiscreteSlider::Section::Section(
const QString &label, const QString &label,
const style::TextStyle &st) const style::TextStyle &st)
: label(st, label) { : label(st, label)
, contentWidth(Section::label.maxWidth()) {
} }
DiscreteSlider::Section::Section( DiscreteSlider::Section::Section(
@ -215,6 +223,7 @@ DiscreteSlider::Section::Section(
const style::TextStyle &st, const style::TextStyle &st,
const std::any &context) { const std::any &context) {
this->label.setMarkedText(st, label, kMarkupTextOptions, context); this->label.setMarkedText(st, label, kMarkupTextOptions, context);
contentWidth = Section::label.maxWidth();
} }
SettingsSlider::SettingsSlider( SettingsSlider::SettingsSlider(
@ -274,8 +283,8 @@ std::vector<float64> SettingsSlider::countSectionsWidths(int newWidth) const {
auto labelsWidth = 0; auto labelsWidth = 0;
auto commonWidth = true; auto commonWidth = true;
enumerateSections([&](const Section &section) { enumerateSections([&](const Section &section) {
labelsWidth += section.label.maxWidth(); labelsWidth += section.contentWidth;
if (section.label.maxWidth() >= sectionWidth) { if (section.contentWidth >= sectionWidth) {
commonWidth = false; commonWidth = false;
} }
return true; return true;
@ -289,7 +298,7 @@ std::vector<float64> SettingsSlider::countSectionsWidths(int newWidth) const {
enumerateSections([&](const Section &section) { enumerateSections([&](const Section &section) {
Expects(currentWidth != result.end()); Expects(currentWidth != result.end());
*currentWidth = padding + section.label.maxWidth() + padding; *currentWidth = padding + section.contentWidth + padding;
++currentWidth; ++currentWidth;
return true; return true;
}); });
@ -370,7 +379,7 @@ void SettingsSlider::paintEvent(QPaintEvent *e) {
}; };
enumerateSections([&](Section &section) { enumerateSections([&](Section &section) {
const auto activeWidth = _st.barSnapToLabel const auto activeWidth = _st.barSnapToLabel
? section.label.maxWidth() ? section.contentWidth
: section.width; : section.width;
const auto activeLeft = section.left const auto activeLeft = section.left
+ (section.width - activeWidth) / 2; + (section.width - activeWidth) / 2;
@ -415,11 +424,11 @@ void SettingsSlider::paintEvent(QPaintEvent *e) {
} }
} }
const auto labelLeft = section.left const auto labelLeft = section.left
+ (section.width - section.label.maxWidth()) / 2; + (section.width - section.contentWidth) / 2;
const auto rect = myrtlrect( const auto rect = myrtlrect(
labelLeft, labelLeft,
_st.labelTop, _st.labelTop,
section.label.maxWidth(), section.contentWidth,
_st.labelStyle.font->height); _st.labelStyle.font->height);
if (rect.intersects(clip)) { if (rect.intersects(clip)) {
p.setPen(anim::pen(_st.labelFg, _st.labelFgActive, active)); p.setPen(anim::pen(_st.labelFg, _st.labelFgActive, active));

View file

@ -44,6 +44,8 @@ public:
void setActiveSectionFast(int index); void setActiveSectionFast(int index);
void finishAnimating(); void finishAnimating();
void setAdditionalContentWidthToSection(int index, int width);
[[nodiscard]] rpl::producer<int> sectionActivated() const { [[nodiscard]] rpl::producer<int> sectionActivated() const {
return _sectionActivated.events(); return _sectionActivated.events();
} }
@ -63,10 +65,11 @@ protected:
const style::TextStyle &st, const style::TextStyle &st,
const std::any &context); const std::any &context);
int left = 0;
int width = 0;
Ui::Text::String label; Ui::Text::String label;
std::unique_ptr<RippleAnimation> ripple; std::unique_ptr<RippleAnimation> ripple;
int left = 0;
int width = 0;
int contentWidth = 0;
}; };
struct Range { struct Range {
int left = 0; int left = 0;