Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/QtTrayMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -139,8 +139,10 @@ int QtTrayMenu::init(struct tray *tray, const bool notification) {
QApplication::setApplicationName(tray->tooltip);
}

// Create tray icon
trayIcon = std::make_unique<QSystemTrayIcon>(lookupIcon(tray->icon));
#ifdef __APPLE__
useTemplateIcon = (options & TRAY_OPTION_TEMPLATE_ICON) != 0;
#endif
trayIcon = std::make_unique<QSystemTrayIcon>(lookupIcon(tray->icon, useTemplateIcon));
trayIcon->setToolTip(QString::fromUtf8(tray->tooltip));

connect(trayIcon.get(), &QSystemTrayIcon::activated, this, &QtTrayMenu::onTrayActivated);
Expand All @@ -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));
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -417,6 +422,10 @@ void QtTrayMenu::clearMessageCallback() const {
}

#ifdef TRAY_ENABLE_TEST_HOOKS
int QtTrayMenu::iconIsTemplate() const {
return trayIcon ? static_cast<int>(trayIcon->icon().isMask()) : -1;
}

bool QtTrayMenu::positionMouseOverIcon() {
if (!trayIcon) {
return false;
Expand Down
18 changes: 16 additions & 2 deletions src/QtTrayMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<char, 12> defaultArgv0 {'T', 'r', 'a', 'y', 'M', 'e', 'n', 'u', 'A', 'p', 'p', '\0'};
std::array<char *, 2> defaultArgv {defaultArgv0.data(), nullptr};
Expand All @@ -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<void()> notificationCallback = nullptr;
#ifdef TRAY_ENABLE_TEST_HOOKS
Expand Down
24 changes: 24 additions & 0 deletions src/tray.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion src/tray_qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
42 changes: 36 additions & 6 deletions tests/unit/test_tray_qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <array>
#include <atomic>
Expand All @@ -17,6 +13,10 @@
#include <thread>
#include <vector>

// local includes
#include "src/tray.h"
#include "src/tray_qt.h"

#if defined(_WIN32)
// local includes
#include "src/WindowsAppearance.h"
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
Expand Down
Loading