Improve polls design.

This commit is contained in:
John Preston 2018-12-22 14:23:22 +04:00
parent 363f6cb329
commit 8e28a229f2
4 changed files with 71 additions and 47 deletions

View File

@ -1831,8 +1831,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_launch_exe_dont_ask" = "Don't ask me again"; "lng_launch_exe_dont_ask" = "Don't ask me again";
"lng_polls_anonymous" = "Anonymous Poll"; "lng_polls_anonymous" = "Anonymous Poll";
"lng_polls_closed" = "Final results";
"lng_polls_votes_count#one" = "{count} vote"; "lng_polls_votes_count#one" = "{count} vote";
"lng_polls_votes_count#other" = "{count} votes"; "lng_polls_votes_count#other" = "{count} votes";
"lng_polls_votes_none" = "No votes";
"lng_polls_retract" = "Retract vote"; "lng_polls_retract" = "Retract vote";
"lng_polls_stop" = "Stop poll"; "lng_polls_stop" = "Stop poll";
"lng_polls_stop_warning" = "If you stop this poll now, nobody will be able to vote in it anymore. This action cannot be undone."; "lng_polls_stop_warning" = "If you stop this poll now, nobody will be able to vote in it anymore. This action cannot be undone.";

View File

@ -492,28 +492,38 @@ webPageDescriptionStyle: defaultTextStyle;
webPagePhotoSize: 100px; webPagePhotoSize: 100px;
webPagePhotoDelta: 8px; webPagePhotoDelta: 8px;
historyPollQuestionFont: font(boxFontSize semibold); historyPollQuestionFont: semiboldFont;
historyPollQuestionStyle: TextStyle(defaultTextStyle) { historyPollQuestionStyle: TextStyle(defaultTextStyle) {
font: historyPollQuestionFont; font: historyPollQuestionFont;
linkFont: historyPollQuestionFont; linkFont: historyPollQuestionFont;
linkFontOver: historyPollQuestionFont; linkFontOver: historyPollQuestionFont;
} }
historyPollAnswerStyle: TextStyle(defaultTextStyle) { historyPollAnswerStyle: defaultTextStyle;
font: boxTextFont;
linkFont: boxTextFont;
linkFontOver: boxTextFont;
}
historyPollQuestionTop: 7px; historyPollQuestionTop: 7px;
historyPollSubtitleSkip: 5px; historyPollSubtitleSkip: 2px;
historyPollAnswerPadding: margins(30px, 16px, 0px, 16px); historyPollAnswerPadding: margins(31px, 10px, 0px, 10px);
historyPollAnswersSkip: 5px; historyPollAnswersSkip: 3px;
historyPollPercentFont: semiboldFont; historyPollPercentFont: semiboldFont;
historyPollPercentSkip: 5px; historyPollPercentSkip: 6px;
historyPollPercentTop: 3px; historyPollPercentTop: 0px;
historyPollTotalVotesSkip: 5px;
historyPollFillingMin: 8px; historyPollFillingMin: 8px;
historyPollFillingHeight: 6px; historyPollFillingHeight: 4px;
historyPollFillingRadius: 2px; historyPollFillingRadius: 1px;
historyPollFillingBottom: 10px; historyPollFillingBottom: 2px;
historyPollFillingRight: 4px;
historyPollRadio: Radio(defaultRadio) {
bg: transparent;
untoggledFg: checkboxFg;
toggledFg: windowBgActive;
diameter: 18px;
thickness: 2px;
skip: 65px; // * 0.1
duration: 120;
rippleAreaPadding: 8px;
}
historyPollRadioOpacity: 0.7;
historyPollRadioOpacityOver: 1.;
boxAttachEmoji: IconButton(historyAttachEmoji) { boxAttachEmoji: IconButton(historyAttachEmoji) {
width: 30px; width: 30px;

View File

@ -93,10 +93,8 @@ QSize HistoryPoll::countOptimalSize() {
_answers _answers
) | ranges::view::transform([](const Answer &answer) { ) | ranges::view::transform([](const Answer &answer) {
return st::historyPollAnswerPadding.top() return st::historyPollAnswerPadding.top()
+ 2 * st::defaultCheckbox.textPosition.y()
+ answer.text.minHeight() + answer.text.minHeight()
+ st::historyPollAnswerPadding.bottom() + st::historyPollAnswerPadding.bottom();
+ st::lineWidth;
}), 0); }), 0);
auto minHeight = st::historyPollQuestionTop auto minHeight = st::historyPollQuestionTop
@ -121,10 +119,8 @@ int HistoryPoll::countAnswerHeight(
- st::historyPollAnswerPadding.left() - st::historyPollAnswerPadding.left()
- st::historyPollAnswerPadding.right(); - st::historyPollAnswerPadding.right();
return st::historyPollAnswerPadding.top() return st::historyPollAnswerPadding.top()
+ 2 * st::defaultCheckbox.textPosition.y()
+ answer.text.countHeight(answerWidth) + answer.text.countHeight(answerWidth)
+ st::historyPollAnswerPadding.bottom() + st::historyPollAnswerPadding.bottom();
+ st::lineWidth;
} }
QSize HistoryPoll::countCurrentSize(int newWidth) { QSize HistoryPoll::countCurrentSize(int newWidth) {
@ -147,7 +143,7 @@ QSize HistoryPoll::countCurrentSize(int newWidth) {
+ st::msgDateFont->height + st::msgDateFont->height
+ st::historyPollAnswersSkip + st::historyPollAnswersSkip
+ answersHeight + answersHeight
+ st::msgPadding.bottom() + st::historyPollTotalVotesSkip
+ st::msgDateFont->height + st::msgDateFont->height
+ st::msgPadding.bottom(); + st::msgPadding.bottom();
if (!isBubbleTop()) { if (!isBubbleTop()) {
@ -161,14 +157,35 @@ void HistoryPoll::updateTexts() {
return; return;
} }
_pollVersion = _poll->version; _pollVersion = _poll->version;
_closed = _poll->closed;
_question.setText( _question.setText(
st::historyPollQuestionStyle, st::historyPollQuestionStyle,
_poll->question, _poll->question,
Ui::WebpageTextTitleOptions()); Ui::WebpageTextTitleOptions());
_subtitle.setText( _subtitle.setText(
st::msgDateTextStyle, st::msgDateTextStyle,
lang(lng_polls_anonymous)); lang(_closed ? lng_polls_closed : lng_polls_anonymous));
updateAnswers();
}
void HistoryPoll::updateAnswers() {
const auto pairFromAnswer = [](const Answer &a) {
return std::make_pair(a.text.originalText(), a.option);
};
const auto pairFromPollAnswer = [](const PollAnswer &p) {
return std::make_pair(p.text, p.option);
};
const auto changed = !ranges::equal(
_answers,
_poll->answers,
ranges::equal_to(),
pairFromAnswer,
pairFromPollAnswer);
if (!changed) {
return;
}
_answers = ranges::view::all( _answers = ranges::view::all(
_poll->answers _poll->answers
) | ranges::view::transform([](const PollAnswer &answer) { ) | ranges::view::transform([](const PollAnswer &answer) {
@ -184,8 +201,6 @@ void HistoryPoll::updateTexts() {
for (auto &answer : _answers) { for (auto &answer : _answers) {
answer.handler = createAnswerClickHandler(answer); answer.handler = createAnswerClickHandler(answer);
} }
_closed = _poll->closed;
} }
ClickHandlerPtr HistoryPoll::createAnswerClickHandler( ClickHandlerPtr HistoryPoll::createAnswerClickHandler(
@ -208,15 +223,17 @@ void HistoryPoll::updateTotalVotes() const {
return; return;
} }
_totalVotes = _poll->totalVoters; _totalVotes = _poll->totalVoters;
if (!_totalVotes) { const auto string = [&] {
_totalVotesLabel.clear(); if (!_totalVotes) {
return; return lang(lng_polls_votes_none);
} }
const auto formatted = FormatLargeNumber(_totalVotes); const auto format = FormatLargeNumber(_totalVotes);
auto string = lng_polls_votes_count(lt_count, formatted.rounded); auto text = lng_polls_votes_count(lt_count, format.rounded);
if (formatted.shortened) { if (format.shortened) {
string.replace(QString::number(formatted.rounded), formatted.text); text.replace(QString::number(format.rounded), format.text);
} }
return text;
}();
_totalVotesLabel.setText(st::msgDateTextStyle, string); _totalVotesLabel.setText(st::msgDateTextStyle, string);
} }
@ -333,36 +350,30 @@ int HistoryPoll::paintAnswer(
p.drawTextLeft(left, top + st::historyPollPercentTop, outerWidth, answer.votesPercent, answer.votesPercentWidth); p.drawTextLeft(left, top + st::historyPollPercentTop, outerWidth, answer.votesPercent, answer.votesPercentWidth);
} else { } else {
PainterHighQualityEnabler hq(p); PainterHighQualityEnabler hq(p);
const auto &st = st::defaultRadio; const auto &st = st::historyPollRadio;
const auto over = ClickHandler::showAsActive(answer.handler);
auto pen = regular->p; auto pen = regular->p;
pen.setWidth(st.thickness); pen.setWidth(st.thickness);
p.setPen(pen); p.setPen(pen);
p.setBrush(Qt::NoBrush); p.setBrush(Qt::NoBrush);
p.setOpacity(over ? st::historyPollRadioOpacityOver : st::historyPollRadioOpacity);
p.drawEllipse(QRectF(left, top, st.diameter, st.diameter).marginsRemoved(QMarginsF(st.thickness / 2., st.thickness / 2., st.thickness / 2., st.thickness / 2.))); p.drawEllipse(QRectF(left, top, st.diameter, st.diameter).marginsRemoved(QMarginsF(st.thickness / 2., st.thickness / 2., st.thickness / 2., st.thickness / 2.)));
p.setOpacity(1.);
} }
top += st::defaultCheckbox.textPosition.y();
p.setPen(outbg ? st::webPageDescriptionOutFg : st::webPageDescriptionInFg); p.setPen(outbg ? st::webPageDescriptionOutFg : st::webPageDescriptionInFg);
answer.text.drawLeft(p, aleft, top, awidth, outerWidth); answer.text.drawLeft(p, aleft, top, awidth, outerWidth);
if (_voted || _closed) { if (_voted || _closed) {
auto &semibold = selected ? (outbg ? st::msgOutServiceFgSelected : st::msgInServiceFgSelected) : (outbg ? st::msgOutServiceFg : st::msgInServiceFg); const auto bar = outbg ? (selected ? st::msgWaveformOutActiveSelected : st::msgWaveformOutActive) : (selected ? st::msgWaveformInActiveSelected : st::msgWaveformInActive);
PainterHighQualityEnabler hq(p); PainterHighQualityEnabler hq(p);
p.setPen(Qt::NoPen); p.setPen(Qt::NoPen);
p.setBrush(semibold); p.setBrush(bar);
const auto size = anim::interpolate(st::historyPollFillingMin, awidth, answer.filling); const auto max = awidth - st::historyPollFillingRight;
const auto size = anim::interpolate(st::historyPollFillingMin, max, answer.filling);
const auto radius = st::historyPollFillingRadius; const auto radius = st::historyPollFillingRadius;
const auto top = bottom - st::historyPollFillingBottom - st::historyPollFillingHeight; const auto top = bottom - st::historyPollFillingBottom - st::historyPollFillingHeight;
p.drawRoundedRect(aleft, top, size, st::historyPollFillingHeight, radius, radius); p.drawRoundedRect(aleft, top, size, st::historyPollFillingHeight, radius, radius);
} else {
p.setOpacity(0.5);
p.fillRect(
aleft,
bottom - st::lineWidth,
outerWidth - aleft,
st::lineWidth,
regular);
p.setOpacity(1.);
} }
return result; return result;
} }

View File

@ -56,6 +56,7 @@ private:
[[nodiscard]] ClickHandlerPtr createAnswerClickHandler( [[nodiscard]] ClickHandlerPtr createAnswerClickHandler(
const Answer &answer) const; const Answer &answer) const;
void updateTexts(); void updateTexts();
void updateAnswers();
void updateVotes() const; void updateVotes() const;
void updateTotalVotes() const; void updateTotalVotes() const;
void updateAnswerVotes() const; void updateAnswerVotes() const;