Skip to content

Commit 68ba500

Browse files
committed
core/icon: ability to specify a fallback or check if an icon exists
1 parent d266736 commit 68ba500

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

src/core/iconimageprovider.cpp

+19-2
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,27 @@
1111
QPixmap
1212
IconImageProvider::requestPixmap(const QString& id, QSize* size, const QSize& requestedSize) {
1313
QString iconName;
14+
QString fallbackName;
1415
QString path;
16+
1517
auto splitIdx = id.indexOf("?path=");
1618
if (splitIdx != -1) {
1719
iconName = id.sliced(0, splitIdx);
1820
path = id.sliced(splitIdx + 6);
1921
qWarning() << "Searching custom icon paths is not yet supported. Icon path will be ignored for"
2022
<< id;
2123
} else {
22-
iconName = id;
24+
splitIdx = id.indexOf("?fallback=");
25+
if (splitIdx != -1) {
26+
iconName = id.sliced(0, splitIdx);
27+
fallbackName = id.sliced(splitIdx + 10);
28+
} else {
29+
iconName = id;
30+
}
2331
}
2432

2533
auto icon = QIcon::fromTheme(iconName);
34+
if (icon.isNull()) icon = QIcon::fromTheme(fallbackName);
2635

2736
auto targetSize = requestedSize.isValid() ? requestedSize : QSize(100, 100);
2837
if (targetSize.width() == 0 || targetSize.height() == 0) targetSize = QSize(2, 2);
@@ -55,12 +64,20 @@ QPixmap IconImageProvider::missingPixmap(const QSize& size) {
5564
return pixmap;
5665
}
5766

58-
QString IconImageProvider::requestString(const QString& icon, const QString& path) {
67+
QString IconImageProvider::requestString(
68+
const QString& icon,
69+
const QString& path,
70+
const QString& fallback
71+
) {
5972
auto req = "image://icon/" + icon;
6073

6174
if (!path.isEmpty()) {
6275
req += "?path=" + path;
6376
}
6477

78+
if (!fallback.isEmpty()) {
79+
req += "?fallback=" + fallback;
80+
}
81+
6582
return req;
6683
}

src/core/iconimageprovider.hpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,10 @@ class IconImageProvider: public QQuickImageProvider {
1010
QPixmap requestPixmap(const QString& id, QSize* size, const QSize& requestedSize) override;
1111

1212
static QPixmap missingPixmap(const QSize& size);
13-
static QString requestString(const QString& icon, const QString& path);
13+
14+
static QString requestString(
15+
const QString& icon,
16+
const QString& path = QString(),
17+
const QString& fallback = QString()
18+
);
1419
};

src/core/qmlglobal.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <qcoreapplication.h>
66
#include <qdir.h>
77
#include <qguiapplication.h>
8+
#include <qicon.h>
89
#include <qjsengine.h>
910
#include <qlogging.h>
1011
#include <qobject.h>
@@ -196,7 +197,16 @@ QVariant QuickshellGlobal::env(const QString& variable) { // NOLINT
196197
}
197198

198199
QString QuickshellGlobal::iconPath(const QString& icon) {
199-
return IconImageProvider::requestString(icon, "");
200+
return IconImageProvider::requestString(icon);
201+
}
202+
203+
QString QuickshellGlobal::iconPath(const QString& icon, bool check) {
204+
if (check && QIcon::fromTheme(icon).isNull()) return "";
205+
return IconImageProvider::requestString(icon);
206+
}
207+
208+
QString QuickshellGlobal::iconPath(const QString& icon, const QString& fallback) {
209+
return IconImageProvider::requestString(icon, "", fallback);
200210
}
201211

202212
QuickshellGlobal* QuickshellGlobal::create(QQmlEngine* engine, QJSEngine* /*unused*/) {

src/core/qmlglobal.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ class QuickshellGlobal: public QObject {
137137
/// > at the top of your root config file or set the `QS_ICON_THEME` variable to the name
138138
/// > of your icon theme.
139139
Q_INVOKABLE static QString iconPath(const QString& icon);
140+
/// Setting the `check` parameter of `iconPath` to true will return an empty string
141+
/// if the icon does not exist, instead of an image showing a missing texture.
142+
Q_INVOKABLE static QString iconPath(const QString& icon, bool check);
143+
/// Setting the `fallback` parameter of `iconPath` will attempt to load the fallback
144+
/// icon if the requested one could not be loaded.
145+
Q_INVOKABLE static QString iconPath(const QString& icon, const QString& fallback);
140146

141147
[[nodiscard]] QString shellRoot() const;
142148

0 commit comments

Comments
 (0)