ReenigneArcher e9363c6572 Fix tray menu focus and icon loading on Linux
Replace QApplication::setActiveWindow with menu->activateWindow() to give the menu X11 focus before popup() and clarify the comment. Include QPixmap and change icon resolution logic: when the icon string refers to an existing file, load a QPixmap and add it to a QIcon immediately (avoiding QIcon's lazy loading that leaves availableSizes() empty and can produce a blank tray icon with Qt6 SNI). Fall back to QIcon::fromTheme() when the file doesn't exist. Keep guarding against setting null icons.
2026-03-28 23:22:53 -04:00
2026-03-28 23:22:52 -04:00
2026-03-22 22:22:24 -04:00
2025-06-12 19:10:24 -04:00
2024-08-05 18:45:19 +00:00
2017-01-09 16:40:06 +02:00
2026-03-28 23:22:52 -04:00

Overview

GitHub Workflow Status (CI) Codecov GitHub stars

About

Cross-platform, super tiny C99 implementation of a system tray icon with a popup menu and notifications.

The code is C++ friendly and will compile fine in C++98 and up. This is a fork of dmikushin/tray and is intended to add additional features required for our own Sunshine project.

This fork adds the following features:

  • system tray notifications
  • unit tests
  • code coverage
  • refactored code, e.g., moved source code into the src directory
  • doxygen documentation and readthedocs configuration

Screenshots

  • Linuxlinux
  • macOSmacOS
  • Windowswindows

Supported platforms

  • Linux/Qt (Qt5 or Qt6 Widgets)
  • Windows XP or newer (shellapi.h)
  • MacOS (Cocoa/AppKit)

Prerequisites

  • CMake
  • Ninja, to have the same build commands on all platforms.

Linux Dependencies

  • Arch

    sudo pacman -S qt6-base
    
  • Debian/Ubuntu

    sudo apt install qtbase5-dev
    
  • Fedora

    sudo dnf install qt6-qtbase-devel
    

Building

mkdir -p build
cmake -G Ninja -B build -S .
ninja -C build

Demo

Execute the tray_example application:

./build/tray_example

Tests

Execute the tests application:

./build/tests/test_tray

API

Tray structure defines an icon and a menu. Menu is a NULL-terminated array of items. Menu item defines menu text, menu checked and disabled (grayed) flags and a callback with some optional context pointer.

struct tray {
  char *icon;
  struct tray_menu *menu;
};

struct tray_menu {
  char *text;
  int disabled;
  int checked;

  void (*cb)(struct tray_menu *);
  void *context;

  struct tray_menu *submenu;
};
  • int tray_init(struct tray *) - creates tray icon. Returns -1 if tray icon/menu can't be created.
  • void tray_update(struct tray *) - updates tray icon and menu.
  • int tray_loop(int blocking) - runs one iteration of the UI loop. Returns -1 if tray_exit() has been called.
  • void tray_exit() - terminates UI loop.

All functions are meant to be called from the UI thread only.

Menu arrays must be terminated with a NULL item, e.g. the last item in the array must have text field set to NULL.

License

This software is distributed under MIT license, so feel free to integrate it in your commercial products.

[TOC]
S
Languages
C++ 75.3%
C 17.1%
CMake 7.6%