forked from quickshell/quickshell
service/tray: account for more edge cases and add placeholder img
This commit is contained in:
parent
54bf485101
commit
61812343f5
7 changed files with 117 additions and 35 deletions
|
@ -1,6 +1,9 @@
|
|||
#include "iconimageprovider.hpp"
|
||||
|
||||
#include <qcolor.h>
|
||||
#include <qicon.h>
|
||||
#include <qlogging.h>
|
||||
#include <qpainter.h>
|
||||
#include <qpixmap.h>
|
||||
#include <qsize.h>
|
||||
#include <qstring.h>
|
||||
|
@ -10,8 +13,32 @@ IconImageProvider::requestPixmap(const QString& id, QSize* size, const QSize& re
|
|||
auto icon = QIcon::fromTheme(id);
|
||||
|
||||
auto targetSize = requestedSize.isValid() ? requestedSize : QSize(100, 100);
|
||||
if (targetSize.width() == 0 || targetSize.height() == 0) targetSize = QSize(2, 2);
|
||||
auto pixmap = icon.pixmap(targetSize.width(), targetSize.height());
|
||||
|
||||
if (pixmap.isNull()) {
|
||||
qWarning() << "Could not load icon" << id << "at size" << targetSize << "from request";
|
||||
pixmap = IconImageProvider::missingPixmap(targetSize);
|
||||
}
|
||||
|
||||
if (size != nullptr) *size = pixmap.size();
|
||||
return pixmap;
|
||||
}
|
||||
|
||||
QPixmap IconImageProvider::missingPixmap(const QSize& size) {
|
||||
auto width = size.width() % 2 == 0 ? size.width() : size.width() + 1;
|
||||
auto height = size.height() % 2 == 0 ? size.height() : size.height() + 1;
|
||||
if (width < 2) width = 2;
|
||||
if (height < 2) height = 2;
|
||||
|
||||
auto pixmap = QPixmap(width, height);
|
||||
pixmap.fill(QColorConstants::Black);
|
||||
auto painter = QPainter(&pixmap);
|
||||
|
||||
auto halfWidth = width / 2;
|
||||
auto halfHeight = height / 2;
|
||||
auto purple = QColor(0xd900d8);
|
||||
painter.fillRect(halfWidth, 0, halfWidth, halfHeight, purple);
|
||||
painter.fillRect(0, halfHeight, halfWidth, halfHeight, purple);
|
||||
return pixmap;
|
||||
}
|
||||
|
|
|
@ -8,4 +8,6 @@ public:
|
|||
explicit IconImageProvider(): QQuickImageProvider(QQuickImageProvider::Pixmap) {}
|
||||
|
||||
QPixmap requestPixmap(const QString& id, QSize* size, const QSize& requestedSize) override;
|
||||
|
||||
static QPixmap missingPixmap(const QSize& size);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue