tray latest
Cross-platform, super tiny C99 implementation of a system tray icon with a popup menu and notifications.
tray_darwin.m File Reference

System tray implementation for macOS. More...

#include <string.h>
#include <Cocoa/Cocoa.h>
#include "tray.h"
Include dependency graph for tray_darwin.m:

Classes

class  AppDelegate
 The application delegate that handles menu actions. More...
 

Macros

#define QUIT_EVENT_SUBTYPE   0x0DED
 NSEvent subtype used to signal exit.
 

Functions

void tray_exit (void)
 Terminate UI loop.
 
int tray_init (struct tray *tray)
 Create tray icon.
 
int tray_loop (int blocking)
 Run one iteration of the UI loop.
 
void tray_set_app_info (const char *app_name, const char *app_display_name, const char *desktop_name)
 Set application metadata used by the tray library.
 
void tray_set_log_callback (void(*cb)(int level, const char *msg))
 Set a callback for log messages produced by the tray library.
 
void tray_show_menu (void)
 Force show the tray menu (for testing purposes).
 
void tray_simulate_menu_item_click (int index)
 Simulate clicking a top-level menu item by index (for testing purposes).
 
void tray_simulate_notification_click (void)
 Simulate a notification click, invoking the notification callback (for testing purposes).
 
void tray_update (struct tray *tray)
 Update the tray icon and menu.
 

Detailed Description

System tray implementation for macOS.

Function Documentation

◆ tray_init()

int tray_init ( struct tray * tray)

Create tray icon.

Parameters
trayThe tray to initialize.
Returns
0 on success, -1 on error.

◆ tray_loop()

int tray_loop ( int blocking)

Run one iteration of the UI loop.

Parameters
blockingWhether to block the call or not.
Returns
0 on success, -1 if tray_exit() was called.

◆ tray_set_app_info()

void tray_set_app_info ( const char * app_name,
const char * app_display_name,
const char * desktop_name )

Set application metadata used by the tray library.

Must be called before tray_init(). On Linux (Qt), sets the Qt application name, display name, and desktop file name used for D-Bus registration. On other platforms this function is a no-op.

Parameters
app_nameApplication name used as a technical identifier (e.g., for D-Bus registration). Converted to lowercase automatically. NULL uses the default ("tray").
app_display_nameHuman-readable name shown in notifications and UI. NULL derives from the tray tooltip or falls back to app_name.
desktop_nameDesktop file name for D-Bus. NULL appends ".desktop" to app_name.

◆ tray_set_log_callback()

void tray_set_log_callback ( void(* cb )(int level, const char *msg))

Set a callback for log messages produced by the tray library.

On Linux the callback is installed as a Qt message handler so all Qt diagnostic output is routed through it. On other platforms this function is a no-op.

Parameters
cbCallback invoked with level (0=debug, 1=info, 2=warning, 3=error) and the message string. Pass NULL to restore the default logging behaviour.

◆ tray_simulate_menu_item_click()

void tray_simulate_menu_item_click ( int index)

Simulate clicking a top-level menu item by index (for testing purposes).

On Linux (Qt): triggers the QAction associated with the given top-level menu index (separators and submenus are ignored). On other platforms: no-op.

Parameters
indexZero-based index in the top-level tray menu.

◆ tray_simulate_notification_click()

void tray_simulate_notification_click ( void )

Simulate a notification click, invoking the notification callback (for testing purposes).

On Linux (Qt): triggers the stored notification callback as if the user clicked the notification. On other platforms: no-op.

◆ tray_update()

void tray_update ( struct tray * tray)

Update the tray icon and menu.

Parameters
trayThe tray to update.