mirror of
https://github.com/LizardByte/tray.git
synced 2026-12-06 15:12:48 +09:00
fix(linux): migrate to qt tray
This commit is contained in:
7 files changed
+197
-328
No files matched your search
@@ -36,7 +36,7 @@ jobs:
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
build:
|
||||
name: Build (${{ matrix.os }} - ${{ matrix.appindicator || 'default' }})
|
||||
name: Build (${{ matrix.os }})
|
||||
defaults:
|
||||
run:
|
||||
shell: ${{ matrix.shell }}
|
||||
@@ -50,12 +50,6 @@ jobs:
|
||||
- os: macos-latest
|
||||
shell: "bash"
|
||||
- os: ubuntu-latest
|
||||
appindicator: "libayatana-appindicator3-dev"
|
||||
appindicator_type: "ayatana"
|
||||
shell: "bash"
|
||||
- os: ubuntu-latest
|
||||
appindicator: "libappindicator3-dev"
|
||||
appindicator_type: "legacy"
|
||||
shell: "bash"
|
||||
- os: windows-latest
|
||||
shell: "msys2 {0}"
|
||||
@@ -72,18 +66,15 @@ jobs:
|
||||
sudo apt-get install -y \
|
||||
build-essential \
|
||||
cmake \
|
||||
${{ matrix.appindicator }} \
|
||||
imagemagick \
|
||||
libglib2.0-dev \
|
||||
libnotify-dev \
|
||||
ninja-build \
|
||||
qtbase5-dev \
|
||||
xvfb
|
||||
|
||||
- name: Setup virtual desktop
|
||||
if: runner.os == 'Linux'
|
||||
uses: LizardByte/actions/actions/virtual_desktop@0affa4f7bcb27562658960eee840eff8ff844578 # v2026.328.161128
|
||||
with:
|
||||
appindicator-version: ${{ matrix.appindicator_type }}
|
||||
environment: mate
|
||||
|
||||
- name: Setup Dependencies macOS
|
||||
@@ -236,7 +227,7 @@ jobs:
|
||||
(steps.test.outcome == 'success' || steps.test.outcome == 'failure')
|
||||
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
|
||||
with:
|
||||
name: tray-screenshots-${{ runner.os }}${{ matrix.appindicator && format('-{0}', matrix.appindicator) || '' }}
|
||||
name: tray-screenshots-${{ runner.os }}
|
||||
path: build/tests/screenshots
|
||||
if-no-files-found: error
|
||||
|
||||
@@ -264,9 +255,6 @@ jobs:
|
||||
id: codecov_flags
|
||||
run: |
|
||||
flags="${{ runner.os }}"
|
||||
if [ -n "${{ matrix.appindicator }}" ]; then
|
||||
flags="${flags},${{ matrix.appindicator }}"
|
||||
fi
|
||||
echo "flags=${flags}" >> "${GITHUB_OUTPUT}"
|
||||
|
||||
- name: Upload coverage
|
||||
|
||||
+17
-18
@@ -5,7 +5,7 @@ cmake_minimum_required(VERSION 3.13 FATAL_ERROR) # target_link_directories
|
||||
project(tray VERSION 0.0.0
|
||||
DESCRIPTION "A cross-platform system tray library"
|
||||
HOMEPAGE_URL "https://app.lizardbyte.dev"
|
||||
LANGUAGES C)
|
||||
LANGUAGES C CXX)
|
||||
|
||||
set(PROJECT_LICENSE "MIT")
|
||||
|
||||
@@ -29,7 +29,7 @@ endif()
|
||||
set(CMAKE_COLOR_MAKEFILE ON)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
|
||||
find_package (PkgConfig REQUIRED)
|
||||
find_package(PkgConfig)
|
||||
|
||||
file(GLOB TRAY_SOURCES
|
||||
"${CMAKE_SOURCE_DIR}/src/*.h"
|
||||
@@ -44,15 +44,22 @@ else()
|
||||
find_library(COCOA Cocoa REQUIRED)
|
||||
list(APPEND TRAY_SOURCES "${CMAKE_SOURCE_DIR}/src/tray_darwin.m")
|
||||
else()
|
||||
find_package(APPINDICATOR REQUIRED)
|
||||
find_package(LibNotify REQUIRED)
|
||||
list(APPEND TRAY_SOURCES "${CMAKE_SOURCE_DIR}/src/tray_linux.c")
|
||||
find_package(Qt6 QUIET COMPONENTS Widgets)
|
||||
if(Qt6_FOUND)
|
||||
set(TRAY_QT_VERSION 6)
|
||||
else()
|
||||
find_package(Qt5 REQUIRED COMPONENTS Widgets)
|
||||
set(TRAY_QT_VERSION 5)
|
||||
endif()
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
list(APPEND TRAY_SOURCES "${CMAKE_SOURCE_DIR}/src/tray_linux.cpp")
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
add_library(${PROJECT_NAME} STATIC ${TRAY_SOURCES})
|
||||
set_property(TARGET ${PROJECT_NAME} PROPERTY C_STANDARD 99)
|
||||
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
|
||||
|
||||
if(WIN32)
|
||||
list(APPEND TRAY_DEFINITIONS TRAY_WINAPI=1 WIN32_LEAN_AND_MEAN NOMINMAX)
|
||||
@@ -65,20 +72,12 @@ else()
|
||||
list(APPEND TRAY_DEFINITIONS TRAY_APPKIT=1)
|
||||
list(APPEND TRAY_EXTERNAL_LIBRARIES ${COCOA})
|
||||
else()
|
||||
list(APPEND TRAY_COMPILE_OPTIONS ${APPINDICATOR_CFLAGS})
|
||||
list(APPEND TRAY_EXTERNAL_DIRECTORIES ${APPINDICATOR_LIBRARY_DIRS})
|
||||
list(APPEND TRAY_DEFINITIONS TRAY_APPINDICATOR=1)
|
||||
if(APPINDICATOR_AYATANA)
|
||||
list(APPEND TRAY_DEFINITIONS TRAY_AYATANA_APPINDICATOR=1)
|
||||
list(APPEND TRAY_DEFINITIONS TRAY_QT=1)
|
||||
if(TRAY_QT_VERSION EQUAL 6)
|
||||
list(APPEND TRAY_EXTERNAL_LIBRARIES Qt6::Widgets)
|
||||
else()
|
||||
list(APPEND TRAY_EXTERNAL_LIBRARIES Qt5::Widgets)
|
||||
endif()
|
||||
if(APPINDICATOR_LEGACY)
|
||||
list(APPEND TRAY_DEFINITIONS TRAY_LEGACY_APPINDICATOR=1)
|
||||
endif()
|
||||
list(APPEND TRAY_LIBNOTIFY=1)
|
||||
list(APPEND TRAY_EXTERNAL_LIBRARIES ${APPINDICATOR_LIBRARIES} ${LIBNOTIFY_LIBRARIES})
|
||||
|
||||
include_directories(SYSTEM ${APPINDICATOR_INCLUDE_DIRS} ${LIBNOTIFY_INCLUDE_DIRS})
|
||||
link_directories(${APPINDICATOR_LIBRARY_DIRS} ${LIBNOTIFY_LIBRARY_DIRS})
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
@@ -15,11 +15,10 @@ The code is C++ friendly and will compile fine in C++98 and up. This is a fork o
|
||||
This fork adds the following features:
|
||||
|
||||
- system tray notifications
|
||||
- support for both linux appindicator versions
|
||||
- unit tests
|
||||
- code coverage
|
||||
- refactored code, e.g. moved source code into the `src` directory
|
||||
- doxygen documentation, and readthedocs configuration
|
||||
- refactored code, e.g., moved source code into the `src` directory
|
||||
- doxygen documentation and readthedocs configuration
|
||||
|
||||
## Screenshots
|
||||
|
||||
@@ -33,14 +32,14 @@ This fork adds the following features:
|
||||
|
||||
## Supported platforms
|
||||
|
||||
* Linux/Gtk (libayatana-appindicator3 or libappindicator3)
|
||||
* Linux/Qt (Qt5 or Qt6 Widgets)
|
||||
* Windows XP or newer (shellapi.h)
|
||||
* MacOS (Cocoa/AppKit)
|
||||
|
||||
## Prerequisites
|
||||
|
||||
* CMake
|
||||
* [Ninja](https://ninja-build.org/), in order to have the same build commands on all platforms
|
||||
* [Ninja](https://ninja-build.org/), to have the same build commands on all platforms
|
||||
|
||||
### Linux Dependencies
|
||||
|
||||
@@ -48,17 +47,17 @@ This fork adds the following features:
|
||||
|
||||
- <b class="tab-title">Arch</b>
|
||||
```bash
|
||||
sudo pacman -S libayatana-appindicator
|
||||
sudo pacman -S qt6-base
|
||||
```
|
||||
|
||||
- <b class="tab-title">Debian/Ubuntu</b>
|
||||
```bash
|
||||
sudo apt install libappindicator3-dev
|
||||
sudo apt install qtbase5-dev
|
||||
```
|
||||
|
||||
- <b class="tab-title">Fedora</b>
|
||||
```bash
|
||||
sudo dnf install libappindicator-gtk3-devel
|
||||
sudo dnf install qt6-qtbase-devel
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
+4
-4
@@ -9,7 +9,7 @@
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
#define TRAY_WINAPI 1 ///< Use WinAPI.
|
||||
#elif defined(__linux__) || defined(linux) || defined(__linux)
|
||||
#define TRAY_APPINDICATOR 1
|
||||
#define TRAY_QT 1
|
||||
#elif defined(__APPLE__) || defined(__MACH__)
|
||||
#define TRAY_APPKIT 1
|
||||
#endif
|
||||
@@ -17,9 +17,9 @@
|
||||
// local includes
|
||||
#include "tray.h"
|
||||
|
||||
#if TRAY_APPINDICATOR
|
||||
#define TRAY_ICON1 "mail-message-new"
|
||||
#define TRAY_ICON2 "mail-message-new"
|
||||
#if TRAY_QT
|
||||
#define TRAY_ICON1 "icon.png"
|
||||
#define TRAY_ICON2 "icon.png"
|
||||
#elif TRAY_APPKIT
|
||||
#define TRAY_ICON1 "icon.png"
|
||||
#define TRAY_ICON2 "icon.png"
|
||||
|
||||
@@ -1,275 +0,0 @@
|
||||
/**
|
||||
* @file src/tray_linux.c
|
||||
* @brief System tray implementation for Linux.
|
||||
*/
|
||||
// standard includes
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// local includes
|
||||
#include "tray.h"
|
||||
|
||||
// lib includes
|
||||
#ifdef TRAY_AYATANA_APPINDICATOR
|
||||
#include <libayatana-appindicator/app-indicator.h>
|
||||
#elif TRAY_LEGACY_APPINDICATOR
|
||||
#include <libappindicator/app-indicator.h>
|
||||
#endif
|
||||
#ifndef IS_APP_INDICATOR
|
||||
#define IS_APP_INDICATOR APP_IS_INDICATOR ///< Define IS_APP_INDICATOR for app-indicator compatibility.
|
||||
#endif
|
||||
#include <libnotify/notify.h>
|
||||
|
||||
// Use a per-process AppIndicator id to avoid DBus collisions when tests create multiple
|
||||
// tray instances in the same desktop/session.
|
||||
static unsigned long tray_appindicator_seq = 0;
|
||||
|
||||
static bool async_update_pending = false;
|
||||
static pthread_cond_t async_update_cv = PTHREAD_COND_INITIALIZER;
|
||||
static pthread_mutex_t async_update_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
static AppIndicator *indicator = NULL;
|
||||
static int loop_result = 0;
|
||||
static NotifyNotification *currentNotification = NULL;
|
||||
static GtkMenu *current_menu = NULL;
|
||||
static GtkMenu *current_popup = NULL;
|
||||
static GtkWidget *menu_anchor_window = NULL;
|
||||
|
||||
static void _tray_menu_cb(GtkMenuItem *item, gpointer data) {
|
||||
(void) item;
|
||||
struct tray_menu *m = (struct tray_menu *) data;
|
||||
m->cb(m);
|
||||
}
|
||||
|
||||
static GtkMenuShell *_tray_menu(struct tray_menu *m) {
|
||||
GtkMenuShell *menu = (GtkMenuShell *) gtk_menu_new();
|
||||
for (; m != NULL && m->text != NULL; m++) {
|
||||
GtkWidget *item;
|
||||
if (strcmp(m->text, "-") == 0) {
|
||||
item = gtk_separator_menu_item_new();
|
||||
} else {
|
||||
if (m->submenu != NULL) {
|
||||
item = gtk_menu_item_new_with_label(m->text);
|
||||
gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), GTK_WIDGET(_tray_menu(m->submenu)));
|
||||
} else if (m->checkbox) {
|
||||
item = gtk_check_menu_item_new_with_label(m->text);
|
||||
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), !!m->checked);
|
||||
} else {
|
||||
item = gtk_menu_item_new_with_label(m->text);
|
||||
}
|
||||
gtk_widget_set_sensitive(item, !m->disabled);
|
||||
if (m->cb != NULL) {
|
||||
g_signal_connect(item, "activate", G_CALLBACK(_tray_menu_cb), m);
|
||||
}
|
||||
}
|
||||
gtk_widget_show(item);
|
||||
gtk_menu_shell_append(menu, item);
|
||||
}
|
||||
return menu;
|
||||
}
|
||||
|
||||
int tray_init(struct tray *tray) {
|
||||
if (gtk_init_check(0, NULL) == FALSE) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// If a previous tray instance wasn't fully torn down (common in unit tests),
|
||||
// drop our references before creating a new indicator.
|
||||
if (indicator != NULL) {
|
||||
g_object_unref(G_OBJECT(indicator));
|
||||
indicator = NULL;
|
||||
}
|
||||
loop_result = 0;
|
||||
notify_init("tray-icon");
|
||||
// The id is used as part of the exported DBus object path.
|
||||
// Make it unique per *tray instance* to prevent collisions inside a single test process.
|
||||
// Avoid underscores and other characters that may be normalized/stripped.
|
||||
char appindicator_id[64];
|
||||
tray_appindicator_seq++;
|
||||
snprintf(appindicator_id, sizeof(appindicator_id), "trayid%ld%lu", (long) getpid(), tray_appindicator_seq);
|
||||
|
||||
indicator = app_indicator_new(appindicator_id, tray->icon, APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
|
||||
if (indicator == NULL || !IS_APP_INDICATOR(indicator)) {
|
||||
return -1;
|
||||
}
|
||||
app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
|
||||
tray_update(tray);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int tray_loop(int blocking) {
|
||||
gtk_main_iteration_do(blocking);
|
||||
return loop_result;
|
||||
}
|
||||
|
||||
static gboolean tray_update_internal(gpointer user_data) {
|
||||
struct tray *tray = user_data;
|
||||
|
||||
if (indicator != NULL && IS_APP_INDICATOR(indicator)) {
|
||||
app_indicator_set_icon_full(indicator, tray->icon, tray->icon);
|
||||
// GTK is all about reference counting, so previous menu should be destroyed
|
||||
// here
|
||||
GtkMenu *menu = GTK_MENU(_tray_menu(tray->menu));
|
||||
app_indicator_set_menu(indicator, menu);
|
||||
if (current_menu != NULL) {
|
||||
g_object_unref(current_menu);
|
||||
}
|
||||
current_menu = menu;
|
||||
g_object_ref(current_menu); // Keep a reference for showing
|
||||
}
|
||||
if (tray->notification_text != 0 && strlen(tray->notification_text) > 0 && notify_is_initted()) {
|
||||
if (currentNotification != NULL && NOTIFY_IS_NOTIFICATION(currentNotification)) {
|
||||
notify_notification_close(currentNotification, NULL);
|
||||
g_object_unref(G_OBJECT(currentNotification));
|
||||
}
|
||||
const char *notification_icon = tray->notification_icon != NULL ? tray->notification_icon : tray->icon;
|
||||
currentNotification = notify_notification_new(tray->notification_title, tray->notification_text, notification_icon);
|
||||
if (currentNotification != NULL && NOTIFY_IS_NOTIFICATION(currentNotification)) {
|
||||
if (tray->notification_cb != NULL) {
|
||||
notify_notification_add_action(currentNotification, "default", "Default", NOTIFY_ACTION_CALLBACK(tray->notification_cb), NULL, NULL);
|
||||
}
|
||||
notify_notification_show(currentNotification, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
// Unwait any pending tray_update() calls
|
||||
pthread_mutex_lock(&async_update_mutex);
|
||||
async_update_pending = false;
|
||||
pthread_cond_broadcast(&async_update_cv);
|
||||
pthread_mutex_unlock(&async_update_mutex);
|
||||
return G_SOURCE_REMOVE;
|
||||
}
|
||||
|
||||
void tray_update(struct tray *tray) {
|
||||
// Perform the tray update on the tray loop thread, but block
|
||||
// in this thread to ensure none of the strings stored in the
|
||||
// tray icon struct go out of scope before the callback runs.
|
||||
|
||||
if (g_main_context_is_owner(g_main_context_default())) {
|
||||
// Invoke the callback directly if we're on the loop thread
|
||||
tray_update_internal(tray);
|
||||
} else {
|
||||
// If there's already an update pending, wait for it to complete
|
||||
// and claim the next pending update slot.
|
||||
pthread_mutex_lock(&async_update_mutex);
|
||||
while (async_update_pending) {
|
||||
pthread_cond_wait(&async_update_cv, &async_update_mutex);
|
||||
}
|
||||
async_update_pending = true;
|
||||
pthread_mutex_unlock(&async_update_mutex);
|
||||
|
||||
// Queue the update callback to the tray thread
|
||||
g_main_context_invoke(NULL, tray_update_internal, tray);
|
||||
|
||||
// Wait for the callback to run
|
||||
pthread_mutex_lock(&async_update_mutex);
|
||||
while (async_update_pending) {
|
||||
pthread_cond_wait(&async_update_cv, &async_update_mutex);
|
||||
}
|
||||
pthread_mutex_unlock(&async_update_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
static void _tray_popup(GtkMenu *menu) {
|
||||
if (menu == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Dismiss any previously shown popup
|
||||
if (current_popup != NULL) {
|
||||
gtk_menu_popdown(current_popup);
|
||||
current_popup = NULL;
|
||||
}
|
||||
if (menu_anchor_window != NULL) {
|
||||
gtk_widget_destroy(menu_anchor_window);
|
||||
menu_anchor_window = NULL;
|
||||
}
|
||||
|
||||
GtkWidget *anchor_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
||||
if (anchor_window != NULL) {
|
||||
gtk_window_set_type_hint(GTK_WINDOW(anchor_window), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
|
||||
gtk_window_set_decorated(GTK_WINDOW(anchor_window), FALSE);
|
||||
gtk_window_set_skip_taskbar_hint(GTK_WINDOW(anchor_window), TRUE);
|
||||
gtk_window_set_skip_pager_hint(GTK_WINDOW(anchor_window), TRUE);
|
||||
gtk_window_move(GTK_WINDOW(anchor_window), 100, 100);
|
||||
gtk_window_resize(GTK_WINDOW(anchor_window), 1, 1);
|
||||
gtk_widget_show(anchor_window);
|
||||
menu_anchor_window = anchor_window;
|
||||
|
||||
while (gtk_events_pending()) {
|
||||
gtk_main_iteration();
|
||||
}
|
||||
|
||||
if (gtk_check_version(3, 22, 0) == NULL) {
|
||||
GdkWindow *gdk_window = gtk_widget_get_window(anchor_window);
|
||||
if (gdk_window != NULL) {
|
||||
GdkRectangle rect = {0, 0, 1, 1};
|
||||
gtk_menu_popup_at_rect(menu, gdk_window, &rect, GDK_GRAVITY_NORTH_WEST, GDK_GRAVITY_NORTH_WEST, NULL);
|
||||
} else {
|
||||
gtk_menu_popup(menu, NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
|
||||
}
|
||||
} else {
|
||||
gtk_menu_popup(menu, NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
|
||||
}
|
||||
current_popup = menu;
|
||||
} else {
|
||||
gtk_menu_popup(menu, NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
|
||||
current_popup = menu;
|
||||
}
|
||||
}
|
||||
|
||||
void tray_show_menu(void) {
|
||||
_tray_popup(current_menu);
|
||||
}
|
||||
|
||||
static gboolean tray_exit_internal(gpointer user_data) {
|
||||
(void) user_data;
|
||||
|
||||
if (currentNotification != NULL && NOTIFY_IS_NOTIFICATION(currentNotification)) {
|
||||
int v = notify_notification_close(currentNotification, NULL);
|
||||
if (v == TRUE) {
|
||||
g_object_unref(G_OBJECT(currentNotification));
|
||||
}
|
||||
currentNotification = NULL;
|
||||
}
|
||||
|
||||
if (current_popup != NULL) {
|
||||
gtk_menu_popdown(current_popup);
|
||||
current_popup = NULL;
|
||||
}
|
||||
|
||||
if (current_menu != NULL) {
|
||||
g_object_unref(current_menu);
|
||||
current_menu = NULL;
|
||||
}
|
||||
|
||||
if (menu_anchor_window != NULL) {
|
||||
gtk_widget_destroy(menu_anchor_window);
|
||||
menu_anchor_window = NULL;
|
||||
}
|
||||
|
||||
if (indicator != NULL) {
|
||||
// Make the indicator passive before unref to encourage a clean DBus unexport.
|
||||
app_indicator_set_status(indicator, APP_INDICATOR_STATUS_PASSIVE);
|
||||
g_object_unref(G_OBJECT(indicator));
|
||||
indicator = NULL;
|
||||
}
|
||||
notify_uninit();
|
||||
return G_SOURCE_REMOVE;
|
||||
}
|
||||
|
||||
void tray_exit(void) {
|
||||
// Wait for any pending callbacks to complete
|
||||
pthread_mutex_lock(&async_update_mutex);
|
||||
while (async_update_pending) {
|
||||
pthread_cond_wait(&async_update_cv, &async_update_mutex);
|
||||
}
|
||||
pthread_mutex_unlock(&async_update_mutex);
|
||||
|
||||
// Perform cleanup on the main thread
|
||||
loop_result = -1;
|
||||
g_main_context_invoke(NULL, tray_exit_internal, NULL);
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
/**
|
||||
* @file src/tray_linux.cpp
|
||||
* @brief System tray implementation for Linux using Qt.
|
||||
*/
|
||||
// standard includes
|
||||
#include <cstring>
|
||||
|
||||
// local includes
|
||||
#include "tray.h"
|
||||
|
||||
// Qt includes
|
||||
#include <QApplication>
|
||||
#include <QIcon>
|
||||
#include <QMenu>
|
||||
#include <QSystemTrayIcon>
|
||||
|
||||
static QApplication *app = nullptr;
|
||||
static QSystemTrayIcon *tray_icon = nullptr;
|
||||
static QMenu *tray_menu = nullptr;
|
||||
static int loop_result = 0;
|
||||
static bool app_owned = false;
|
||||
|
||||
static QMenu *_tray_menu(struct tray_menu *m) {
|
||||
auto *menu = new QMenu();
|
||||
for (; m != nullptr && m->text != nullptr; m++) {
|
||||
if (std::strcmp(m->text, "-") == 0) {
|
||||
menu->addSeparator();
|
||||
} else if (m->submenu != nullptr) {
|
||||
QMenu *sub = _tray_menu(m->submenu);
|
||||
sub->setTitle(QString::fromUtf8(m->text));
|
||||
menu->addMenu(sub);
|
||||
} else if (m->checkbox) {
|
||||
auto *action = menu->addAction(QString::fromUtf8(m->text));
|
||||
action->setCheckable(true);
|
||||
action->setChecked(m->checked != 0);
|
||||
action->setEnabled(m->disabled == 0);
|
||||
if (m->cb != nullptr) {
|
||||
struct tray_menu *item = m;
|
||||
QObject::connect(action, &QAction::triggered, [item]() {
|
||||
item->cb(item);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
auto *action = menu->addAction(QString::fromUtf8(m->text));
|
||||
action->setEnabled(m->disabled == 0);
|
||||
if (m->cb != nullptr) {
|
||||
struct tray_menu *item = m;
|
||||
QObject::connect(action, &QAction::triggered, [item]() {
|
||||
item->cb(item);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
return menu;
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
int tray_init(struct tray *tray) {
|
||||
if (QApplication::instance() == nullptr) {
|
||||
static int argc = 0;
|
||||
app = new QApplication(argc, nullptr);
|
||||
app_owned = true;
|
||||
}
|
||||
|
||||
if (tray_icon != nullptr) {
|
||||
delete tray_icon;
|
||||
tray_icon = nullptr;
|
||||
}
|
||||
if (tray_menu != nullptr) {
|
||||
delete tray_menu;
|
||||
tray_menu = nullptr;
|
||||
}
|
||||
|
||||
loop_result = 0;
|
||||
|
||||
tray_icon = new QSystemTrayIcon();
|
||||
|
||||
if (!QSystemTrayIcon::isSystemTrayAvailable()) {
|
||||
delete tray_icon;
|
||||
tray_icon = nullptr;
|
||||
return -1;
|
||||
}
|
||||
|
||||
tray_update(tray);
|
||||
tray_icon->show();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int tray_loop(int blocking) {
|
||||
if (blocking) {
|
||||
QApplication::exec();
|
||||
} else {
|
||||
QApplication::processEvents();
|
||||
}
|
||||
return loop_result;
|
||||
}
|
||||
|
||||
void tray_update(struct tray *tray) {
|
||||
if (tray_icon == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
tray_icon->setIcon(QIcon(QString::fromUtf8(tray->icon)));
|
||||
|
||||
if (tray->tooltip != nullptr) {
|
||||
tray_icon->setToolTip(QString::fromUtf8(tray->tooltip));
|
||||
}
|
||||
|
||||
if (tray->menu != nullptr) {
|
||||
QMenu *new_menu = _tray_menu(tray->menu);
|
||||
tray_icon->setContextMenu(new_menu);
|
||||
if (tray_menu != nullptr) {
|
||||
delete tray_menu;
|
||||
}
|
||||
tray_menu = new_menu;
|
||||
}
|
||||
|
||||
if (tray->notification_text != nullptr && std::strlen(tray->notification_text) > 0) {
|
||||
QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information;
|
||||
const QString title = tray->notification_title != nullptr ? QString::fromUtf8(tray->notification_title) : QString();
|
||||
const QString text = QString::fromUtf8(tray->notification_text);
|
||||
tray_icon->showMessage(title, text, icon, 5000);
|
||||
|
||||
if (tray->notification_cb != nullptr) {
|
||||
void (*cb)() = tray->notification_cb;
|
||||
QObject::connect(tray_icon, &QSystemTrayIcon::messageClicked, [cb]() {
|
||||
cb();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void tray_show_menu(void) {
|
||||
if (tray_icon != nullptr && tray_menu != nullptr) {
|
||||
tray_menu->popup(QCursor::pos());
|
||||
QApplication::processEvents();
|
||||
}
|
||||
}
|
||||
|
||||
void tray_exit(void) {
|
||||
loop_result = -1;
|
||||
|
||||
if (tray_icon != nullptr) {
|
||||
tray_icon->hide();
|
||||
delete tray_icon;
|
||||
tray_icon = nullptr;
|
||||
}
|
||||
|
||||
if (tray_menu != nullptr) {
|
||||
delete tray_menu;
|
||||
tray_menu = nullptr;
|
||||
}
|
||||
|
||||
if (app_owned && app != nullptr) {
|
||||
app->quit();
|
||||
}
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -16,7 +16,7 @@
|
||||
// clang-format on
|
||||
#define TRAY_WINAPI 1
|
||||
#elif defined(__linux__) || defined(linux) || defined(__linux)
|
||||
#define TRAY_APPINDICATOR 1
|
||||
#define TRAY_QT 1
|
||||
#elif defined(__APPLE__) || defined(__MACH__)
|
||||
#include <Carbon/Carbon.h>
|
||||
#define TRAY_APPKIT 1
|
||||
@@ -26,9 +26,9 @@
|
||||
#include "src/tray.h"
|
||||
#include "tests/screenshot_utils.h"
|
||||
|
||||
#if TRAY_APPINDICATOR
|
||||
constexpr const char *TRAY_ICON1 = "mail-message-new";
|
||||
constexpr const char *TRAY_ICON2 = "mail-message-new";
|
||||
#if TRAY_QT
|
||||
constexpr const char *TRAY_ICON1 = "icon.png";
|
||||
constexpr const char *TRAY_ICON2 = "icon.png";
|
||||
#elif TRAY_APPKIT
|
||||
constexpr const char *TRAY_ICON1 = "icon.png";
|
||||
constexpr const char *TRAY_ICON2 = "icon.png";
|
||||
@@ -159,7 +159,6 @@ protected: // NOSONAR(cpp:S3656) - TEST_F generates subclasses that need access
|
||||
GTEST_SKIP() << "Screenshot output path not initialized";
|
||||
}
|
||||
|
||||
#if defined(TRAY_WINAPI) || defined(TRAY_APPKIT)
|
||||
// Ensure icon files exist in test binary directory
|
||||
std::filesystem::path projectRoot = testBinaryDir.parent_path();
|
||||
std::filesystem::path iconSource;
|
||||
@@ -182,7 +181,6 @@ protected: // NOSONAR(cpp:S3656) - TEST_F generates subclasses that need access
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
trayRunning = false;
|
||||
testTray.icon = TRAY_ICON1;
|
||||
@@ -199,7 +197,7 @@ protected: // NOSONAR(cpp:S3656) - TEST_F generates subclasses that need access
|
||||
// Process pending events to allow tray icon to appear.
|
||||
// Call this ONLY before screenshots to ensure the icon is visible.
|
||||
void WaitForTrayReady() {
|
||||
#if defined(TRAY_APPINDICATOR)
|
||||
#if defined(TRAY_QT)
|
||||
for (int i = 0; i < 100; i++) {
|
||||
tray_loop(0);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||
@@ -405,7 +403,7 @@ TEST_F(TrayTest, TestNotificationCallback) {
|
||||
|
||||
tray_update(&testTray);
|
||||
|
||||
// Note: callback would be invoked by user interaction in real scenario
|
||||
// Note: callback would be invoked by user interaction in a real scenario
|
||||
// In test environment, we verify it's set correctly
|
||||
EXPECT_NE(testTray.notification_cb, nullptr);
|
||||
|
||||
|
||||
Reference in new issue
Block a user