Skip to content

Commit 8341803

Browse files
fix(linux): support themed icons
1 parent a22926a commit 8341803

3 files changed

Lines changed: 44 additions & 2 deletions

File tree

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,12 @@ jobs:
6464
run: |
6565
sudo apt-get update
6666
sudo apt-get install -y \
67+
adwaita-icon-theme \
6768
build-essential \
6869
cmake \
6970
imagemagick \
7071
ninja-build \
72+
qt5-gtk-platformtheme \
7173
qtbase5-dev \
7274
xvfb
7375
@@ -219,6 +221,8 @@ jobs:
219221
# TODO: tests randomly hang on Linux, https://github.com/LizardByte/tray/issues/45
220222
timeout-minutes: 3
221223
working-directory: build/tests
224+
env:
225+
QT_QPA_PLATFORMTHEME: gtk3
222226
run: ./test_tray --gtest_color=yes --gtest_output=xml:test_results.xml
223227

224228
- name: Upload screenshots

src/tray_linux.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ extern "C" {
146146
return;
147147
}
148148

149-
g_tray_icon->setIcon(QIcon(QString::fromUtf8(tray->icon)));
149+
const QString icon_str = QString::fromUtf8(tray->icon);
150+
g_tray_icon->setIcon(
151+
QFileInfo(icon_str).exists() ? QIcon(icon_str) : QIcon::fromTheme(icon_str)
152+
);
150153

151154
if (tray->tooltip != nullptr) {
152155
g_tray_icon->setToolTip(QString::fromUtf8(tray->tooltip));
@@ -168,7 +171,8 @@ extern "C" {
168171
const char *icon_path = tray->notification_icon != nullptr ? tray->notification_icon : tray->icon;
169172
QString icon;
170173
if (icon_path != nullptr) {
171-
icon = QUrl::fromLocalFile(QFileInfo(QString::fromUtf8(icon_path)).absoluteFilePath()).toString();
174+
QFileInfo fi(QString::fromUtf8(icon_path));
175+
icon = fi.exists() ? QUrl::fromLocalFile(fi.absoluteFilePath()).toString() : QString::fromUtf8(icon_path);
172176
}
173177
QVariantMap hints;
174178
if (!icon.isEmpty()) {

tests/unit/test_tray.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#if TRAY_QT
3030
constexpr const char *TRAY_ICON1 = "icon.png";
3131
constexpr const char *TRAY_ICON2 = "icon.png";
32+
constexpr const char *TRAY_ICON_THEMED = "dialog-information";
3233
#elif TRAY_APPKIT
3334
constexpr const char *TRAY_ICON1 = "icon.png";
3435
constexpr const char *TRAY_ICON2 = "icon.png";
@@ -596,3 +597,36 @@ TEST_F(TrayTest, TestTrayShowMenu) {
596597
TEST_F(TrayTest, TestTrayExit) {
597598
tray_exit();
598599
}
600+
601+
#if defined(TRAY_QT)
602+
603+
TEST_F(TrayTest, TestTrayIconThemed) {
604+
testTray.icon = TRAY_ICON_THEMED;
605+
int result = tray_init(&testTray);
606+
trayRunning = (result == 0);
607+
ASSERT_EQ(result, 0);
608+
WaitForTrayReady();
609+
EXPECT_TRUE(captureScreenshot("tray_icon_themed"));
610+
testTray.icon = TRAY_ICON1;
611+
}
612+
613+
TEST_F(TrayTest, TestNotificationWithThemedIcon) {
614+
int initResult = tray_init(&testTray);
615+
trayRunning = (initResult == 0);
616+
ASSERT_EQ(initResult, 0);
617+
618+
testTray.notification_title = "Test Notification";
619+
testTray.notification_text = "This is a test notification message";
620+
testTray.notification_icon = TRAY_ICON_THEMED;
621+
tray_update(&testTray);
622+
623+
WaitForTrayReady();
624+
EXPECT_TRUE(captureScreenshot("tray_notification_themed_icon"));
625+
626+
testTray.notification_title = nullptr;
627+
testTray.notification_text = nullptr;
628+
testTray.notification_icon = nullptr;
629+
tray_update(&testTray);
630+
}
631+
632+
#endif // TRAY_QT

0 commit comments

Comments
 (0)