zserge / tray

Cross-platform, super tiny C99 implementation of a system tray icon with a popup menu.
MIT License
493 stars 84 forks source link
appindicator cross-platform gtk tray-icon tray-menu

Tray

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

Works well on:

There is also a stub implementation that returns errors on attempt to create a tray menu.

Setup

Before you can compile tray, you'll need to add an environment definition before the line where you include tray.h.

For Windows:

#include <stdio.h>
#include <string.h>

#define TRAY_WINAPI 1

#include "tray.h"
...

For Linux:

#include <stdio.h>
#include <string.h>

#define TRAY_APPINDICATOR 1

#include "tray.h"
...

For Mac:

#include <stdio.h>
#include <string.h>

#define TRAY_APPKIT 1

#include "tray.h"
...

Demo

The included example .c files can be compiled based on your environment.

For example, to compile and run the program on Windows:

$> gcc example_windows.c [Enter]

This will compile and build a.out. To run it:

$> a [Enter]

Example

struct tray tray = {
    .icon = "icon.png",
    .menu = (struct tray_menu[]){{"Toggle me", 0, 0, toggle_cb, NULL},
                                 {"-", 0, 0, NULL, NULL},
                                 {"Quit", 0, 0, quit_cb, NULL},
                                 {NULL, 0, 0, NULL, NULL}},
};

void toggle_cb(struct tray_menu *item) {
    item->checked = !item->checked;
    tray_update(&tray);
}

void quit_cb(struct tray_menu *item) {
  tray_exit();
}

...

tray_init(&tray);
while (tray_loop(1) == 0);
tray_exit();

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;
};

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.

Roadmap

License

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