presentation/TrayIconRenderer.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "TrayIconRenderer.h" | ||
| 2 | |||
| 3 | #include <QFont> | ||
| 4 | #include <QFontMetricsF> | ||
| 5 | #include <QPainter> | ||
| 6 | #include <QPixmap> | ||
| 7 | #include <QSvgRenderer> | ||
| 8 | |||
| 9 | #include <algorithm> | ||
| 10 | #include <array> | ||
| 11 | #include <cmath> | ||
| 12 | |||
| 13 | namespace { | ||
| 14 | |||
| 15 | constexpr std::array<int, 11> kNativeIconSizes = { | ||
| 16 | 16, 20, 22, 24, 32, 40, 48, 64, 96, 128, 256, | ||
| 17 | }; | ||
| 18 | |||
| 19 | 1914 | void drawBadge(QPainter &painter, const QRectF &badgeRect, | |
| 20 | const QString &text, const QColor &badgeColor) { | ||
| 21 |
1/1✓ Branch 4 → 5 taken 1914 times.
|
1914 | painter.setRenderHints(QPainter::Antialiasing | |
| 22 | 1914 | QPainter::TextAntialiasing | | |
| 23 | QPainter::SmoothPixmapTransform); | ||
| 24 | |||
| 25 | 1914 | const qreal outlineWidth = std::max(1.0, badgeRect.width() / 26.0); | |
| 26 |
3/3✓ Branch 7 → 8 taken 1914 times.
✓ Branch 8 → 9 taken 1914 times.
✓ Branch 9 → 10 taken 1914 times.
|
1914 | painter.setPen(QPen(Qt::white, outlineWidth)); |
| 27 |
2/2✓ Branch 12 → 13 taken 1914 times.
✓ Branch 13 → 14 taken 1914 times.
|
1914 | painter.setBrush(badgeColor); |
| 28 |
1/1✓ Branch 16 → 17 taken 1914 times.
|
1914 | painter.drawEllipse(badgeRect.adjusted(outlineWidth / 2.0, |
| 29 | outlineWidth / 2.0, | ||
| 30 | 1914 | -outlineWidth / 2.0, | |
| 31 | 1914 | -outlineWidth / 2.0)); | |
| 32 | |||
| 33 |
1/1✓ Branch 17 → 18 taken 1914 times.
|
1914 | QFont font; |
| 34 |
1/1✓ Branch 18 → 19 taken 1914 times.
|
1914 | font.setBold(true); |
| 35 |
1/1✓ Branch 21 → 22 taken 1914 times.
|
1914 | font.setPixelSize( |
| 36 | 1914 | std::max(5, static_cast<int>(std::lround(badgeRect.height() * 0.52)))); | |
| 37 | |||
| 38 |
1/1✓ Branch 22 → 23 taken 1914 times.
|
1914 | QFontMetricsF metrics(font); |
| 39 | 1914 | const qreal maximumTextWidth = badgeRect.width() * 0.78; | |
| 40 |
1/1✓ Branch 24 → 25 taken 1914 times.
|
1914 | const qreal textWidth = metrics.horizontalAdvance(text); |
| 41 |
2/2✓ Branch 25 → 26 taken 616 times.
✓ Branch 25 → 29 taken 1298 times.
|
1914 | if (textWidth > maximumTextWidth) { |
| 42 | 616 | const int fittedSize = std::max( | |
| 43 |
1/1✓ Branch 26 → 27 taken 616 times.
|
616 | 4, static_cast<int>(std::floor(font.pixelSize() * maximumTextWidth / |
| 44 | 616 | textWidth))); | |
| 45 |
1/1✓ Branch 28 → 29 taken 616 times.
|
616 | font.setPixelSize(fittedSize); |
| 46 | } | ||
| 47 | |||
| 48 |
1/1✓ Branch 29 → 30 taken 1914 times.
|
1914 | painter.setFont(font); |
| 49 | 5742 | const auto linear = [](qreal channel) { | |
| 50 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 5742 times.
|
5742 | return channel <= 0.04045 ? channel / 12.92 |
| 51 | 5742 | : std::pow((channel + 0.055) / 1.055, 2.4); | |
| 52 | }; | ||
| 53 | 1914 | const qreal luminance = 0.2126 * linear(badgeColor.redF()) + | |
| 54 | 1914 | 0.7152 * linear(badgeColor.greenF()) + 0.0722 * linear(badgeColor.blueF()); | |
| 55 | // Choose the higher-contrast label for both dark and light danger tokens. | ||
| 56 |
3/3✓ Branch 36 → 37 taken 154 times.
✓ Branch 36 → 38 taken 1760 times.
✓ Branch 40 → 41 taken 1914 times.
|
1914 | painter.setPen(luminance > 0.179 ? Qt::black : Qt::white); |
| 57 |
1/1✓ Branch 41 → 42 taken 1914 times.
|
1914 | painter.drawText(badgeRect, Qt::AlignCenter, text); |
| 58 | 1914 | } | |
| 59 | |||
| 60 | } // namespace | ||
| 61 | |||
| 62 | namespace TrayIconRenderer { | ||
| 63 | |||
| 64 | 1602 | QString badgeText(int unreadCount) { | |
| 65 |
0/2✗ Branch 14 → 15 not taken.
✗ Branch 14 → 16 not taken.
|
1602 | return unreadCount > 99 ? QStringLiteral("99+") |
| 66 |
5/5✓ Branch 2 → 3 taken 57 times.
✓ Branch 2 → 6 taken 1545 times.
✓ Branch 7 → 8 taken 1545 times.
✓ Branch 9 → 10 taken 57 times.
✓ Branch 9 → 11 taken 1545 times.
|
4863 | : QString::number(std::max(0, unreadCount)); |
| 67 | } | ||
| 68 | |||
| 69 | 1600 | QIcon badgedIcon(int unreadCount, const QColor &badgeColor) { | |
| 70 | // Render the bundled vector directly. Minimal installations can have QtSvg | ||
| 71 | // without the optional SVG QIcon plugin; a numeric badge must never replace | ||
| 72 | // a missing application icon with an otherwise transparent square. | ||
| 73 |
1/1✓ Branch 5 → 6 taken 1600 times.
|
1600 | QSvgRenderer baseIcon(QStringLiteral(":/icons/mailjd.svg")); |
| 74 | |||
| 75 | 1600 | QIcon result; | |
| 76 |
1/1✓ Branch 9 → 10 taken 1600 times.
|
1600 | const QString text = badgeText(unreadCount); |
| 77 |
2/2✓ Branch 25 → 11 taken 17600 times.
✓ Branch 25 → 26 taken 1600 times.
|
19200 | for (const int size : kNativeIconSizes) { |
| 78 |
1/1✓ Branch 11 → 12 taken 17600 times.
|
17600 | QPixmap pixmap(size, size); |
| 79 |
1/1✓ Branch 13 → 14 taken 17600 times.
|
17600 | pixmap.fill(Qt::transparent); |
| 80 | |||
| 81 |
1/1✓ Branch 14 → 15 taken 17600 times.
|
17600 | QPainter painter(&pixmap); |
| 82 |
1/1✓ Branch 16 → 17 taken 17600 times.
|
17600 | baseIcon.render(&painter, QRectF(0, 0, size, size)); |
| 83 | |||
| 84 | 17600 | const qreal badgeSize = size * 0.61; | |
| 85 | const QRectF badgeRect(size - badgeSize, size - badgeSize, badgeSize, | ||
| 86 | 17600 | badgeSize); | |
| 87 |
3/3✓ Branch 18 → 19 taken 1914 times.
✓ Branch 18 → 20 taken 15686 times.
✓ Branch 19 → 20 taken 1914 times.
|
17600 | if (unreadCount > 0) drawBadge(painter, badgeRect, text, badgeColor); |
| 88 |
1/1✓ Branch 20 → 21 taken 17600 times.
|
17600 | painter.end(); |
| 89 | |||
| 90 |
1/1✓ Branch 21 → 22 taken 17600 times.
|
17600 | result.addPixmap(pixmap, QIcon::Normal, QIcon::Off); |
| 91 | 17600 | } | |
| 92 | 1600 | return result; | |
| 93 | 1600 | } | |
| 94 | |||
| 95 | } // namespace TrayIconRenderer | ||
| 96 |