diff --git a/src/QtTrayMenu.cpp b/src/QtTrayMenu.cpp index fe04988d..3e17f9b2 100644 --- a/src/QtTrayMenu.cpp +++ b/src/QtTrayMenu.cpp @@ -122,7 +122,7 @@ QtTrayMenu::QtTrayMenu(int argc, char **argv, QObject *parent, const bool debug) QtTrayMenu::~QtTrayMenu() = default; -int QtTrayMenu::init(struct tray *tray, const bool notification) { +int QtTrayMenu::init(struct tray *tray, const bool notification, [[maybe_unused]] const unsigned int options) { if (trayIcon) { // Running tray is initialized again. Fail with error. return -1; @@ -139,8 +139,10 @@ int QtTrayMenu::init(struct tray *tray, const bool notification) { QApplication::setApplicationName(tray->tooltip); } - // Create tray icon - trayIcon = std::make_unique(lookupIcon(tray->icon)); +#ifdef __APPLE__ + useTemplateIcon = (options & TRAY_OPTION_TEMPLATE_ICON) != 0; +#endif + trayIcon = std::make_unique(lookupIcon(tray->icon, useTemplateIcon)); trayIcon->setToolTip(QString::fromUtf8(tray->tooltip)); connect(trayIcon.get(), &QSystemTrayIcon::activated, this, &QtTrayMenu::onTrayActivated); @@ -166,7 +168,7 @@ void QtTrayMenu::onUpdate(struct tray *tray, const bool notify) { return; } this->trayStruct = tray; - if (const auto newIcon = lookupIcon(trayStruct->icon); !newIcon.isNull()) { + if (const auto newIcon = lookupIcon(trayStruct->icon, useTemplateIcon); !newIcon.isNull()) { trayIcon->setIcon(newIcon); } trayIcon->setToolTip(QString::fromUtf8(trayStruct->tooltip)); @@ -269,10 +271,13 @@ void QtTrayMenu::createNotification() { } } -QIcon QtTrayMenu::lookupIcon(QString icon) const { +QIcon QtTrayMenu::lookupIcon(QString icon, const bool useTemplate) const { // Find icon for tray if (std::filesystem::exists(icon.toStdString())) { if (auto result = QIcon(icon); !result.isNull()) { + if (useTemplate) { + result.setIsMask(true); + } return result; } } @@ -417,6 +422,10 @@ void QtTrayMenu::clearMessageCallback() const { } #ifdef TRAY_ENABLE_TEST_HOOKS +int QtTrayMenu::iconIsTemplate() const { + return trayIcon ? static_cast(trayIcon->icon().isMask()) : -1; +} + bool QtTrayMenu::positionMouseOverIcon() { if (!trayIcon) { return false; diff --git a/src/QtTrayMenu.h b/src/QtTrayMenu.h index 7ef31c2f..553668f2 100644 --- a/src/QtTrayMenu.h +++ b/src/QtTrayMenu.h @@ -60,9 +60,10 @@ class QtTrayMenu: public QObject { * @brief Initialize tray with given structure * @param tray struct containing tray configuration * @param notification fire tray notification if true + * @param options Rendering options for the tray icon * @return 0 on success */ - int init(struct tray *tray, bool notification = true); + int init(struct tray *tray, bool notification = true, unsigned int options = 0); /** * @brief Process tray loop events @@ -116,6 +117,12 @@ class QtTrayMenu: public QObject { void clearMessageCallback() const; #ifdef TRAY_ENABLE_TEST_HOOKS + /** + * @brief Check whether the active tray icon is marked as a template image. + * @return 1 for a template icon, 0 for a regular icon, or -1 when no icon is active. + */ + int iconIsTemplate() const; + /** * @brief Move the mouse cursor to the center of the tray icon. * @return true if the tray icon has valid screen geometry and the cursor was moved @@ -157,7 +164,13 @@ class QtTrayMenu: public QObject { void createMenu(struct tray_menu *items, QMenu *menu); void createNotification(); void updateMenu(struct tray_menu *items); - QIcon lookupIcon(QString icon) const; + /** + * @brief Find artwork for the tray or a notification. + * @param icon Icon path or theme name. + * @param useTemplate Whether to mark file-backed artwork as a template image. + * @return The requested icon or a fallback icon. + */ + QIcon lookupIcon(QString icon, bool useTemplate = false) const; int defaultArgc = 1; std::array defaultArgv0 {'T', 'r', 'a', 'y', 'M', 'e', 'n', 'u', 'A', 'p', 'p', '\0'}; std::array defaultArgv {defaultArgv0.data(), nullptr}; @@ -167,6 +180,7 @@ class QtTrayMenu: public QObject { struct tray *trayStruct = nullptr; bool running = false; bool blockingEventLoop = false; + bool useTemplateIcon = false; ///< Whether the macOS menu bar icon uses template rendering. struct tray_menu *getTrayMenuItem(const QAction *action); mutable std::function notificationCallback = nullptr; #ifdef TRAY_ENABLE_TEST_HOOKS diff --git a/src/tray.h b/src/tray.h index 8fbfb7ae..805e033b 100644 --- a/src/tray.h +++ b/src/tray.h @@ -52,6 +52,24 @@ extern "C" { */ int tray_init(struct tray *tray); + /** + * @brief Optional rendering behavior for the tray icon. + */ + enum tray_option { + TRAY_OPTION_TEMPLATE_ICON = 1 << 0, ///< Render file-backed macOS menu bar icons as template images. + }; + + /** + * @brief Create a tray icon with the requested options. + * + * Options apply to the tray icon at initialization and on subsequent updates. + * The template icon option applies only to the macOS menu bar. + * @param tray The tray to initialize. + * @param options Bitmask of tray_option values, or zero for default behavior. + * @return 0 on success, -1 on error. + */ + int tray_init_with_options(struct tray *tray, unsigned int options); + /** * @brief Run one iteration of the UI loop. * @param blocking Whether to block the call or not. @@ -71,6 +89,12 @@ extern "C" { void tray_show_menu(void); #ifdef TRAY_ENABLE_TEST_HOOKS + /** + * @brief Check whether the active tray icon is marked as a template image (for testing purposes). + * @return 1 for a template icon, 0 for a regular icon, or -1 when no icon is active. + */ + int tray_icon_is_template(void); + /** * @brief Position the mouse over the tray icon (for testing purposes). * @return 0 on success, -1 if the tray icon geometry is unavailable. diff --git a/src/tray_qt.cpp b/src/tray_qt.cpp index f23fb90c..23c848d0 100644 --- a/src/tray_qt.cpp +++ b/src/tray_qt.cpp @@ -186,6 +186,10 @@ extern "C" { } int tray_init(struct tray *tray) { + return tray_init_with_options(tray, 0); + } + + int tray_init_with_options(struct tray *tray, const unsigned int options) { auto &state = tray_qt::state(); if (state.trayMenu == nullptr) { tray_qt::configure_platform(); @@ -194,7 +198,7 @@ extern "C" { tray_qt::apply_app_info(false); } - if (const auto result = state.trayMenu->init(tray, false); result < 0) { + if (const auto result = state.trayMenu->init(tray, false, options); result < 0) { // Tray init failed. Clean up and return error. tray_exit(); return result; @@ -263,6 +267,11 @@ extern "C" { } #ifdef TRAY_ENABLE_TEST_HOOKS + int tray_icon_is_template(void) { + const auto &tray_menu = tray_qt::state().trayMenu; + return tray_menu != nullptr ? tray_menu->iconIsTemplate() : -1; + } + int tray_position_mouse_over_icon(void) { if (tray_qt::state().trayMenu == nullptr) { return -1; diff --git a/tests/unit/test_tray_qt.cpp b/tests/unit/test_tray_qt.cpp index d4c4c1b0..76e9c507 100644 --- a/tests/unit/test_tray_qt.cpp +++ b/tests/unit/test_tray_qt.cpp @@ -2,10 +2,6 @@ #include "tests/conftest.cpp" #include "tests/notification_utils.h" -// local includes -#include "src/tray.h" -#include "src/tray_qt.h" - // standard includes #include #include @@ -17,6 +13,10 @@ #include #include +// local includes +#include "src/tray.h" +#include "src/tray_qt.h" + #if defined(_WIN32) // local includes #include "src/WindowsAppearance.h" @@ -142,8 +142,12 @@ class TrayQtCoverageTest: public BaseTest { BaseTest::TearDown(); } - void InitTray() { - const int initResult = tray_init(trayData); + /** + * @brief Start the tray, optionally requesting non-default rendering behavior. + * @param options Options passed to the tray API. + */ + void InitTray(const unsigned int options = 0) { + const int initResult = options == 0 ? tray_init(trayData) : tray_init_with_options(trayData, options); trayRunning = (initResult == 0); ASSERT_EQ(initResult, 0); } @@ -155,6 +159,32 @@ class TrayQtCoverageTest: public BaseTest { } }; +/** + * @brief Verify that the default tray API preserves regular artwork. + */ +TEST_F(TrayQtCoverageTest, TrayIconUsesRegularRenderingByDefault) { + EXPECT_EQ(tray_icon_is_template(), -1); + InitTray(); + EXPECT_EQ(tray_icon_is_template(), 0); +} + +/** + * @brief Verify that the template rendering request follows tray icon updates on macOS. + */ +TEST_F(TrayQtCoverageTest, TrayIconTemplateOptionAppliesToInitialAndUpdatedIcons) { + InitTray(TRAY_OPTION_TEMPLATE_ICON); +#ifdef __APPLE__ + constexpr int expected_template = 1; +#else + constexpr int expected_template = 0; +#endif + EXPECT_EQ(tray_icon_is_template(), expected_template); + + trayData->icon = "icon2.png"; + tray_update(trayData); + EXPECT_EQ(tray_icon_is_template(), expected_template); +} + #if defined(_WIN32) TEST(WindowsAppearanceTest, AppsUseLightThemeMapsToColorScheme) { using tray_qt::windows::color_scheme_e;