From 32df03f08d693b7bd4f4e560e3c3c060c66bb4cd Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Mon, 10 Jul 2023 17:29:52 +0300 Subject: [PATCH] Fixed text overlap on y-axis captions when mouse drag is really fast. --- .../SourceFiles/statistics/chart_widget.cpp | 29 ++++++++++++++----- .../SourceFiles/statistics/chart_widget.h | 3 ++ .../SourceFiles/statistics/statistics.style | 3 ++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/Telegram/SourceFiles/statistics/chart_widget.cpp b/Telegram/SourceFiles/statistics/chart_widget.cpp index 909693612..3955a56a2 100644 --- a/Telegram/SourceFiles/statistics/chart_widget.cpp +++ b/Telegram/SourceFiles/statistics/chart_widget.cpp @@ -103,7 +103,7 @@ void PaintBottomLine( const Limits &xPercentageLimits, int fullWidth, int y) { - p.setFont(st::statisticsDetailsPopupStyle.font); + p.setFont(st::statisticsDetailsBottomCaptionStyle.font); const auto startXIndex = chartData.findStartIndex( xPercentageLimits.min); @@ -111,7 +111,9 @@ void PaintBottomLine( startXIndex, xPercentageLimits.max); - for (const auto &date : dates) { + for (auto k = 0; k < dates.size(); k++) { + const auto &date = dates[k]; + const auto isLast = (k == dates.size() - 1); const auto resultAlpha = date.alpha; const auto step = std::max(date.step, 1); @@ -127,6 +129,13 @@ void PaintBottomLine( const auto offset = fullWidth * xPercentageLimits.min; + // 30 ms / 200 ms = 0.15. + constexpr auto kFastAlphaSpeed = 0.85; + const auto hasFastAlpha = (date.stepRaw < dates.back().stepMinFast); + const auto fastAlpha = isLast + ? 1. + : std::max(resultAlpha - kFastAlphaSpeed, 0.); + for (auto i = start; i < end; i += step) { if ((i < 0) || (i >= (chartData.x.size() - 1))) { continue; @@ -134,7 +143,7 @@ void PaintBottomLine( const auto xPercentage = (chartData.x[i] - chartData.x.front()) / (chartData.x.back() - chartData.x.front()); const auto xPoint = xPercentage * fullWidth - offset; - p.setOpacity(resultAlpha); + p.setOpacity(hasFastAlpha ? fastAlpha : resultAlpha); p.drawText(xPoint, y, chartData.getDayString(i)); } } @@ -648,12 +657,12 @@ void ChartWidget::updateBottomDates() { } const auto d = _bottomLine.chartFullWidth * _chartData.oneDayPercentage; const auto k = _chartArea->width() / d; - const auto tempStep = int(k / 6); + const auto stepRaw = int(k / 6); const auto isCurrentNull = (_bottomLine.current.stepMinFast == 0); if (!isCurrentNull - && (tempStep < _bottomLine.current.stepMax) - && (tempStep > _bottomLine.current.stepMin)) { + && (stepRaw < _bottomLine.current.stepMax) + && (stepRaw > _bottomLine.current.stepMin)) { return; } const auto highestOneBit = [](unsigned int v) { @@ -667,19 +676,23 @@ void ChartWidget::updateBottomDates() { } return int(r); }; - const auto step = highestOneBit(tempStep) << 1; + const auto step = highestOneBit(stepRaw) << 1; if (!isCurrentNull && (_bottomLine.current.step == step)) { return; } - constexpr auto kStepRatio = 0.2; + constexpr auto kStepRatio = 0.1; + constexpr auto kFastStepOffset = 4; const auto stepMax = int(step + step * kStepRatio); const auto stepMin = int(step - step * kStepRatio); + const auto stepMinFast = stepMin - kFastStepOffset; auto data = BottomCaptionLineData{ .step = step, .stepMax = stepMax, .stepMin = stepMin, + .stepMinFast = stepMinFast, + .stepRaw = stepRaw, .alpha = 1., }; diff --git a/Telegram/SourceFiles/statistics/chart_widget.h b/Telegram/SourceFiles/statistics/chart_widget.h index 1880a5e7f..1b19a715f 100644 --- a/Telegram/SourceFiles/statistics/chart_widget.h +++ b/Telegram/SourceFiles/statistics/chart_widget.h @@ -30,6 +30,9 @@ public: int step = 0; int stepMax = 0; int stepMin = 0; + int stepMinFast = 0; + int stepRaw = 0; + float64 alpha = 0.; float64 fixedAlpha = 0.; }; diff --git a/Telegram/SourceFiles/statistics/statistics.style b/Telegram/SourceFiles/statistics/statistics.style index 0fc2f9e39..4bf87f664 100644 --- a/Telegram/SourceFiles/statistics/statistics.style +++ b/Telegram/SourceFiles/statistics/statistics.style @@ -23,3 +23,6 @@ statisticsChartBottomCaptionSkip: 15px; statisticsDetailsPopupStyle: TextStyle(defaultTextStyle) { font: font(11px); } +statisticsDetailsBottomCaptionStyle: TextStyle(defaultTextStyle) { + font: font(10px); +}