File tree 4 files changed +42
-4
lines changed
4 files changed +42
-4
lines changed Original file line number Diff line number Diff line change 11
11
QPixmap
12
12
IconImageProvider::requestPixmap (const QString& id, QSize* size, const QSize& requestedSize) {
13
13
QString iconName;
14
+ QString fallbackName;
14
15
QString path;
16
+
15
17
auto splitIdx = id.indexOf (" ?path=" );
16
18
if (splitIdx != -1 ) {
17
19
iconName = id.sliced (0 , splitIdx);
18
20
path = id.sliced (splitIdx + 6 );
19
21
qWarning () << " Searching custom icon paths is not yet supported. Icon path will be ignored for"
20
22
<< id;
21
23
} 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
+ }
23
31
}
24
32
25
33
auto icon = QIcon::fromTheme (iconName);
34
+ if (icon.isNull ()) icon = QIcon::fromTheme (fallbackName);
26
35
27
36
auto targetSize = requestedSize.isValid () ? requestedSize : QSize (100 , 100 );
28
37
if (targetSize.width () == 0 || targetSize.height () == 0 ) targetSize = QSize (2 , 2 );
@@ -55,12 +64,20 @@ QPixmap IconImageProvider::missingPixmap(const QSize& size) {
55
64
return pixmap;
56
65
}
57
66
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
+ ) {
59
72
auto req = " image://icon/" + icon;
60
73
61
74
if (!path.isEmpty ()) {
62
75
req += " ?path=" + path;
63
76
}
64
77
78
+ if (!fallback.isEmpty ()) {
79
+ req += " ?fallback=" + fallback;
80
+ }
81
+
65
82
return req;
66
83
}
Original file line number Diff line number Diff line change @@ -10,5 +10,10 @@ class IconImageProvider: public QQuickImageProvider {
10
10
QPixmap requestPixmap (const QString& id, QSize* size, const QSize& requestedSize) override ;
11
11
12
12
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
+ );
14
19
};
Original file line number Diff line number Diff line change 5
5
#include < qcoreapplication.h>
6
6
#include < qdir.h>
7
7
#include < qguiapplication.h>
8
+ #include < qicon.h>
8
9
#include < qjsengine.h>
9
10
#include < qlogging.h>
10
11
#include < qobject.h>
@@ -196,7 +197,16 @@ QVariant QuickshellGlobal::env(const QString& variable) { // NOLINT
196
197
}
197
198
198
199
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);
200
210
}
201
211
202
212
QuickshellGlobal* QuickshellGlobal::create (QQmlEngine* engine, QJSEngine* /* unused*/ ) {
Original file line number Diff line number Diff line change @@ -137,6 +137,12 @@ class QuickshellGlobal: public QObject {
137
137
// / > at the top of your root config file or set the `QS_ICON_THEME` variable to the name
138
138
// / > of your icon theme.
139
139
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);
140
146
141
147
[[nodiscard]] QString shellRoot () const ;
142
148
You can’t perform that action at this time.
0 commit comments