diff --git a/README.md b/README.md index f261932..0f41d3b 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,65 @@ 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:** +```c +#include +#include + +#define TRAY_WINAPI 1 + +#include "tray.h" +... +``` + +**For Linux:** +```c +#include +#include + +#define TRAY_APPINDICATOR 1 + +#include "tray.h" +... +``` + +**For Mac:** +**For Windows:** +```c +#include +#include + +#define TRAY_APPKIT 1 + +#include "tray.h" +... +``` + +// For Linux: +//#define TRAY_APPINDICATOR 1 +// For Mac: +//#define TRAY_APPKIT 1 + +# Demo + +The included example `.c` files can be compiled based on your environment. + +For example, to compile and run the program on Windows: + +```shell +$> gcc example_windows.c [Enter] +``` + +This will compile and build `a.out`. To run it: + +``` +$> a [Enter] +``` + # Example ```c diff --git a/example_linux.c b/example_linux.c new file mode 100644 index 0000000..fc1bcd9 --- /dev/null +++ b/example_linux.c @@ -0,0 +1,96 @@ +#include +#include + +// For Linux +#define TRAY_APPINDICATOR 1 + +#include "tray.h" + +#if TRAY_APPINDICATOR +#define TRAY_ICON1 "indicator-messages" +#define TRAY_ICON2 "indicator-messages-new" +#elif TRAY_APPKIT +#define TRAY_ICON1 "icon.png" +#define TRAY_ICON2 "icon.png" +#elif TRAY_WINAPI +#define TRAY_ICON1 "icon.ico" +#define TRAY_ICON2 "icon.ico" +#endif + +static struct tray tray; + +static void toggle_cb(struct tray_menu *item) { + printf("toggle cb\n"); + item->checked = !item->checked; + tray_update(&tray); +} + +static void hello_cb(struct tray_menu *item) { + (void)item; + printf("hello cb\n"); + if (strcmp(tray.icon, TRAY_ICON1) == 0) { + tray.icon = TRAY_ICON2; + } else { + tray.icon = TRAY_ICON1; + } + tray_update(&tray); +} + +static void quit_cb(struct tray_menu *item) { + (void)item; + printf("quit cb\n"); + tray_exit(); +} + +static void submenu_cb(struct tray_menu *item) { + (void)item; + printf("submenu: clicked on %s\n", item->text); + tray_update(&tray); +} + +// Test tray init +static struct tray tray = { + .icon = TRAY_ICON1, + .menu = + (struct tray_menu[]){ + {.text = "Hello", .cb = hello_cb}, + {.text = "Checked", .checked = 1, .cb = toggle_cb}, + {.text = "Disabled", .disabled = 1}, + {.text = "-"}, + {.text = "SubMenu", + .submenu = + (struct tray_menu[]){ + {.text = "FIRST", .checked = 1, .cb = submenu_cb}, + {.text = "SECOND", + .submenu = + (struct tray_menu[]){ + {.text = "THIRD", + .submenu = + (struct tray_menu[]){ + {.text = "7", .cb = submenu_cb}, + {.text = "-"}, + {.text = "8", .cb = submenu_cb}, + {.text = NULL}}}, + {.text = "FOUR", + .submenu = + (struct tray_menu[]){ + {.text = "5", .cb = submenu_cb}, + {.text = "6", .cb = submenu_cb}, + {.text = NULL}}}, + {.text = NULL}}}, + {.text = NULL}}}, + {.text = "-"}, + {.text = "Quit", .cb = quit_cb}, + {.text = NULL}}, +}; + +int main() { + if (tray_init(&tray) < 0) { + printf("failed to create tray\n"); + return 1; + } + while (tray_loop(1) == 0) { + printf("iteration\n"); + } + return 0; +} diff --git a/example_mac.c b/example_mac.c new file mode 100644 index 0000000..69b97d8 --- /dev/null +++ b/example_mac.c @@ -0,0 +1,96 @@ +#include +#include + +// For Mac +#define TRAY_APPKIT 1 + +#include "tray.h" + +#if TRAY_APPINDICATOR +#define TRAY_ICON1 "indicator-messages" +#define TRAY_ICON2 "indicator-messages-new" +#elif TRAY_APPKIT +#define TRAY_ICON1 "icon.png" +#define TRAY_ICON2 "icon.png" +#elif TRAY_WINAPI +#define TRAY_ICON1 "icon.ico" +#define TRAY_ICON2 "icon.ico" +#endif + +static struct tray tray; + +static void toggle_cb(struct tray_menu *item) { + printf("toggle cb\n"); + item->checked = !item->checked; + tray_update(&tray); +} + +static void hello_cb(struct tray_menu *item) { + (void)item; + printf("hello cb\n"); + if (strcmp(tray.icon, TRAY_ICON1) == 0) { + tray.icon = TRAY_ICON2; + } else { + tray.icon = TRAY_ICON1; + } + tray_update(&tray); +} + +static void quit_cb(struct tray_menu *item) { + (void)item; + printf("quit cb\n"); + tray_exit(); +} + +static void submenu_cb(struct tray_menu *item) { + (void)item; + printf("submenu: clicked on %s\n", item->text); + tray_update(&tray); +} + +// Test tray init +static struct tray tray = { + .icon = TRAY_ICON1, + .menu = + (struct tray_menu[]){ + {.text = "Hello", .cb = hello_cb}, + {.text = "Checked", .checked = 1, .cb = toggle_cb}, + {.text = "Disabled", .disabled = 1}, + {.text = "-"}, + {.text = "SubMenu", + .submenu = + (struct tray_menu[]){ + {.text = "FIRST", .checked = 1, .cb = submenu_cb}, + {.text = "SECOND", + .submenu = + (struct tray_menu[]){ + {.text = "THIRD", + .submenu = + (struct tray_menu[]){ + {.text = "7", .cb = submenu_cb}, + {.text = "-"}, + {.text = "8", .cb = submenu_cb}, + {.text = NULL}}}, + {.text = "FOUR", + .submenu = + (struct tray_menu[]){ + {.text = "5", .cb = submenu_cb}, + {.text = "6", .cb = submenu_cb}, + {.text = NULL}}}, + {.text = NULL}}}, + {.text = NULL}}}, + {.text = "-"}, + {.text = "Quit", .cb = quit_cb}, + {.text = NULL}}, +}; + +int main() { + if (tray_init(&tray) < 0) { + printf("failed to create tray\n"); + return 1; + } + while (tray_loop(1) == 0) { + printf("iteration\n"); + } + return 0; +} diff --git a/example_windows.c b/example_windows.c new file mode 100644 index 0000000..b10c1af --- /dev/null +++ b/example_windows.c @@ -0,0 +1,96 @@ +#include +#include + +// For Windows: +#define TRAY_WINAPI 1 + +#include "tray.h" + +#if TRAY_APPINDICATOR +#define TRAY_ICON1 "indicator-messages" +#define TRAY_ICON2 "indicator-messages-new" +#elif TRAY_APPKIT +#define TRAY_ICON1 "icon.png" +#define TRAY_ICON2 "icon.png" +#elif TRAY_WINAPI +#define TRAY_ICON1 "icon.ico" +#define TRAY_ICON2 "icon.ico" +#endif + +static struct tray tray; + +static void toggle_cb(struct tray_menu *item) { + printf("toggle cb\n"); + item->checked = !item->checked; + tray_update(&tray); +} + +static void hello_cb(struct tray_menu *item) { + (void)item; + printf("hello cb\n"); + if (strcmp(tray.icon, TRAY_ICON1) == 0) { + tray.icon = TRAY_ICON2; + } else { + tray.icon = TRAY_ICON1; + } + tray_update(&tray); +} + +static void quit_cb(struct tray_menu *item) { + (void)item; + printf("quit cb\n"); + tray_exit(); +} + +static void submenu_cb(struct tray_menu *item) { + (void)item; + printf("submenu: clicked on %s\n", item->text); + tray_update(&tray); +} + +// Test tray init +static struct tray tray = { + .icon = TRAY_ICON1, + .menu = + (struct tray_menu[]){ + {.text = "Hello", .cb = hello_cb}, + {.text = "Checked", .checked = 1, .cb = toggle_cb}, + {.text = "Disabled", .disabled = 1}, + {.text = "-"}, + {.text = "SubMenu", + .submenu = + (struct tray_menu[]){ + {.text = "FIRST", .checked = 1, .cb = submenu_cb}, + {.text = "SECOND", + .submenu = + (struct tray_menu[]){ + {.text = "THIRD", + .submenu = + (struct tray_menu[]){ + {.text = "7", .cb = submenu_cb}, + {.text = "-"}, + {.text = "8", .cb = submenu_cb}, + {.text = NULL}}}, + {.text = "FOUR", + .submenu = + (struct tray_menu[]){ + {.text = "5", .cb = submenu_cb}, + {.text = "6", .cb = submenu_cb}, + {.text = NULL}}}, + {.text = NULL}}}, + {.text = NULL}}}, + {.text = "-"}, + {.text = "Quit", .cb = quit_cb}, + {.text = NULL}}, +}; + +int main() { + if (tray_init(&tray) < 0) { + printf("failed to create tray\n"); + return 1; + } + while (tray_loop(1) == 0) { + printf("iteration\n"); + } + return 0; +} diff --git a/icon.ico b/icon.ico new file mode 100644 index 0000000..bb96b6e Binary files /dev/null and b/icon.ico differ diff --git a/main.c b/main.c new file mode 100644 index 0000000..13fc63a --- /dev/null +++ b/main.c @@ -0,0 +1,35 @@ +#include +#include "tray.h" + +extern void toggle_cb(struct tray_menu *item); +extern void quit_cb(struct tray_menu *item); + +struct tray main_tray = { + .icon = "icon.ico", + .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(&main_tray); +} + +void quit_cb(struct tray_menu *item) { + tray_exit(); +} + + +int main() { + + + + tray_init(&main_tray); + while (tray_loop(1) == 0); + tray_exit(); + + + return 0; +} \ No newline at end of file