* @brief Draws a window control button with an SVG icon. */
| 595 | * @brief Draws a window control button with an SVG icon. |
| 596 | */ |
| 597 | void Titlebar::drawButton(QPainter* painter, Button button, const QString& svgPath) |
| 598 | { |
| 599 | const QRectF iconRect = buttonRect(button); |
| 600 | const bool hovered = (m_hoveredButton == button); |
| 601 | const bool pressed = (m_pressedButton == button); |
| 602 | |
| 603 | drawButtonHoverBackground(painter, button, hovered, pressed); |
| 604 | const QColor iconColor = buttonIconColor(button, hovered, pressed); |
| 605 | |
| 606 | const qreal dpr = qApp->devicePixelRatio(); |
| 607 | const QSize pixelSize(qRound(iconRect.width() * dpr), qRound(iconRect.height() * dpr)); |
| 608 | const QRectF logicalRect(0, 0, iconRect.width(), iconRect.height()); |
| 609 | |
| 610 | if (pixelSize.isEmpty()) |
| 611 | return; |
| 612 | |
| 613 | const QString cacheKey = QStringLiteral("%1|%2|%3x%4|%5") |
| 614 | .arg(svgPath) |
| 615 | .arg(QString::number(iconColor.rgba(), 16)) |
| 616 | .arg(pixelSize.width()) |
| 617 | .arg(pixelSize.height()) |
| 618 | .arg(QString::number(dpr, 'f', 3)); |
| 619 | auto cacheIt = m_iconCache.constFind(cacheKey); |
| 620 | if (cacheIt != m_iconCache.cend()) { |
| 621 | painter->drawPixmap(iconRect.topLeft(), cacheIt.value()); |
| 622 | return; |
| 623 | } |
| 624 | |
| 625 | const QPixmap colorized = renderColorizedSvg(svgPath, pixelSize, logicalRect, dpr, iconColor); |
| 626 | if (colorized.isNull()) |
| 627 | return; |
| 628 | |
| 629 | m_iconCache.insert(cacheKey, colorized); |
| 630 | painter->drawPixmap(iconRect.topLeft(), colorized); |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * @brief Handles loss of mouse grab. |