SystemTrayIcon: Respect device aspect ratio

Fixes #1782
This commit is contained in:
Jonas Kvinge
2025-08-31 02:34:13 +02:00
parent f628914173
commit 3c3480fb84
6 changed files with 103 additions and 37 deletions

View File

@@ -2,7 +2,7 @@
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
* Copyright 2019-2025, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -35,45 +35,54 @@
# include "qtsystemtrayicon.h"
#endif
QPixmap SystemTrayIcon::CreateIcon(const QPixmap &icon, const QPixmap &grey_icon) {
QPixmap SystemTrayIcon::CreateIcon(const QPixmap &pixmap_icon_normal, const QPixmap &pixmap_icon_grey) {
QRect rect(icon.rect());
const qreal dpr = pixmap_icon_normal.devicePixelRatio();
const QRect pixmap_rect = pixmap_icon_normal.rect();
QPixmap ret(icon);
QPainter p(&ret);
QPixmap pixmap_drawn(pixmap_icon_normal.size());
pixmap_drawn.setDevicePixelRatio(dpr);
pixmap_drawn.fill(Qt::transparent);
QPainter p(&pixmap_drawn);
p.setRenderHint(QPainter::SmoothPixmapTransform, true);
p.drawPixmap(0, 0, pixmap_icon_normal);
if (trayicon_progress_) {
// The angle of the line that's used to cover the icon.
// Centered on rect.topLeft()
double angle = static_cast<double>(100 - song_progress_) / 100.0 * M_PI_2;
double length = sqrt(pow(rect.width(), 2.0) + pow(rect.height(), 2.0));
const double angle = static_cast<double>(100 - song_progress_) / 100.0 * M_PI_2;
const double length = std::sqrt(std::pow(pixmap_rect.width(), 2.0) + std::pow(pixmap_rect.height(), 2.0));
QPolygon mask;
mask << rect.topLeft();
mask << rect.topLeft() + QPoint(static_cast<int>(length * sin(angle)), static_cast<int>(length * cos(angle)));
mask << pixmap_rect.topLeft();
mask << pixmap_rect.topLeft() + QPoint(static_cast<int>(length * std::sin(angle)), static_cast<int>(length * std::cos(angle)));
if (song_progress_ > 50) mask << rect.bottomRight();
if (song_progress_ > 50) {
mask << pixmap_rect.bottomRight();
}
mask << rect.topRight();
mask << rect.topLeft();
mask << pixmap_rect.topRight();
mask << pixmap_rect.topLeft();
// Draw the grey bit
p.setClipRegion(mask);
p.drawPixmap(0, 0, grey_icon);
p.drawPixmap(0, 0, pixmap_icon_grey);
p.setClipping(false);
}
// Draw the playing or paused icon in the top-right
if (!current_state_icon_.isNull()) {
int height = rect.height() / 2;
QPixmap scaled(current_state_icon_.scaledToHeight(height, Qt::SmoothTransformation));
const int height = pixmap_rect.height() / 2;
QPixmap current_state_scaled = current_state_icon_.scaledToHeight(height, Qt::SmoothTransformation);
current_state_scaled.setDevicePixelRatio(dpr);
QRect state_rect(rect.width() - scaled.width(), 0, scaled.width(), scaled.height());
p.drawPixmap(state_rect, scaled);
const QRect state_rect(static_cast<int>((pixmap_rect.width() - current_state_scaled.width()) / dpr), 0, static_cast<int>(current_state_scaled.width() / dpr), static_cast<int>(current_state_scaled.height() / dpr));
p.drawPixmap(state_rect, current_state_scaled);
}
p.end();
return ret;
return pixmap_drawn;
}