|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +#ifdef _WIN32 |
| 6 | +#include <windows.h> |
| 7 | +#else |
| 8 | +#include <unistd.h> |
| 9 | +#endif |
| 10 | + |
| 11 | +// Include individual C API headers instead of the full nativeapi.h |
| 12 | +#include "../../src/capi/menu_c.h" |
| 13 | +#include "../../src/capi/run_example_app_c.h" |
| 14 | +#include "../../src/capi/tray_icon_c.h" |
| 15 | +#include "../../src/capi/tray_manager_c.h" |
| 16 | + |
| 17 | +// Callback functions |
| 18 | +void on_menu_item_clicked(const native_menu_item_selected_event_t* event, |
| 19 | + void* user_data) { |
| 20 | + printf("Menu item clicked: ID=%ld, Text='%s'\n", event->item_id, |
| 21 | + event->item_text); |
| 22 | + |
| 23 | + if (strcmp(event->item_text, "Exit") == 0) { |
| 24 | + printf("Exiting application...\n"); |
| 25 | + exit(0); |
| 26 | + } else if (strcmp(event->item_text, "Show Message") == 0) { |
| 27 | + printf("Hello from tray menu!\n"); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +void on_checkbox_state_changed( |
| 32 | + const native_menu_item_state_changed_event_t* event, |
| 33 | + void* user_data) { |
| 34 | + printf("Checkbox state changed: ID=%ld, Checked=%s\n", event->item_id, |
| 35 | + event->checked ? "true" : "false"); |
| 36 | +} |
| 37 | + |
| 38 | +void on_tray_left_click(void* user_data) { |
| 39 | + printf("Tray icon left clicked!\n"); |
| 40 | +} |
| 41 | + |
| 42 | +void on_tray_right_click(void* user_data) { |
| 43 | + printf("Tray icon right clicked!\n"); |
| 44 | +} |
| 45 | + |
| 46 | +void on_tray_double_click(void* user_data) { |
| 47 | + printf("Tray icon double clicked!\n"); |
| 48 | +} |
| 49 | + |
| 50 | +void on_menu_will_show(native_menu_id_t menu_id, void* user_data) { |
| 51 | + printf("Menu will show: ID=%ld\n", menu_id); |
| 52 | +} |
| 53 | + |
| 54 | +void on_menu_did_hide(native_menu_id_t menu_id, void* user_data) { |
| 55 | + printf("Menu did hide: ID=%ld\n", menu_id); |
| 56 | +} |
| 57 | + |
| 58 | +int main() { |
| 59 | + printf("=== Tray Menu C API Example ===\n"); |
| 60 | + |
| 61 | + // Check if system tray is supported |
| 62 | + if (!native_tray_manager_is_supported()) { |
| 63 | + printf("Error: System tray is not supported on this platform!\n"); |
| 64 | + return 1; |
| 65 | + } |
| 66 | + |
| 67 | + printf("System tray is supported.\n"); |
| 68 | + |
| 69 | + // Create a menu |
| 70 | + native_menu_t menu = native_menu_create(); |
| 71 | + if (!menu) { |
| 72 | + printf("Error: Failed to create menu!\n"); |
| 73 | + return 1; |
| 74 | + } |
| 75 | + |
| 76 | + printf("Created menu with ID: %ld\n", native_menu_get_id(menu)); |
| 77 | + |
| 78 | + // Create menu items |
| 79 | + native_menu_item_t item1 = |
| 80 | + native_menu_item_create("Show Message", NATIVE_MENU_ITEM_TYPE_NORMAL); |
| 81 | + native_menu_item_t item2 = |
| 82 | + native_menu_item_create("Settings", NATIVE_MENU_ITEM_TYPE_NORMAL); |
| 83 | + native_menu_item_t checkbox = native_menu_item_create( |
| 84 | + "Enable Notifications", NATIVE_MENU_ITEM_TYPE_CHECKBOX); |
| 85 | + native_menu_item_t separator = native_menu_item_create_separator(); |
| 86 | + native_menu_item_t exit_item = |
| 87 | + native_menu_item_create("Exit", NATIVE_MENU_ITEM_TYPE_NORMAL); |
| 88 | + |
| 89 | + if (!item1 || !item2 || !checkbox || !separator || !exit_item) { |
| 90 | + printf("Error: Failed to create menu items!\n"); |
| 91 | + native_menu_destroy(menu); |
| 92 | + return 1; |
| 93 | + } |
| 94 | + |
| 95 | + // Set up menu item properties |
| 96 | + native_menu_item_set_enabled(item1, true); |
| 97 | + native_menu_item_set_tooltip(item1, "Click to show a message"); |
| 98 | + |
| 99 | + // Set up keyboard accelerator for exit item |
| 100 | + native_keyboard_accelerator_t exit_accel = { |
| 101 | + .modifiers = NATIVE_ACCELERATOR_MODIFIER_CTRL, .key = "Q"}; |
| 102 | + native_menu_item_set_accelerator(exit_item, &exit_accel); |
| 103 | + |
| 104 | + // Set checkbox state |
| 105 | + native_menu_item_set_checked(checkbox, true); |
| 106 | + |
| 107 | + // Set up event handlers |
| 108 | + native_menu_item_set_on_click(item1, on_menu_item_clicked, NULL); |
| 109 | + native_menu_item_set_on_click(item2, on_menu_item_clicked, NULL); |
| 110 | + native_menu_item_set_on_click(exit_item, on_menu_item_clicked, NULL); |
| 111 | + native_menu_item_set_on_state_changed(checkbox, on_checkbox_state_changed, |
| 112 | + NULL); |
| 113 | + |
| 114 | + // Add items to menu |
| 115 | + native_menu_add_item(menu, item1); |
| 116 | + native_menu_add_item(menu, item2); |
| 117 | + native_menu_add_item(menu, checkbox); |
| 118 | + native_menu_add_item(menu, separator); |
| 119 | + native_menu_add_item(menu, exit_item); |
| 120 | + |
| 121 | + printf("Added %zu items to menu\n", native_menu_get_item_count(menu)); |
| 122 | + |
| 123 | + // Set menu callbacks |
| 124 | + native_menu_set_on_will_show(menu, on_menu_will_show, NULL); |
| 125 | + native_menu_set_on_did_hide(menu, on_menu_did_hide, NULL); |
| 126 | + |
| 127 | + // Create a submenu example |
| 128 | + native_menu_t submenu = native_menu_create(); |
| 129 | + if (submenu) { |
| 130 | + native_menu_item_t sub_item1 = |
| 131 | + native_menu_item_create("Sub Item 1", NATIVE_MENU_ITEM_TYPE_NORMAL); |
| 132 | + native_menu_item_t sub_item2 = |
| 133 | + native_menu_item_create("Sub Item 2", NATIVE_MENU_ITEM_TYPE_NORMAL); |
| 134 | + |
| 135 | + if (sub_item1 && sub_item2) { |
| 136 | + native_menu_add_item(submenu, sub_item1); |
| 137 | + native_menu_add_item(submenu, sub_item2); |
| 138 | + |
| 139 | + // Create submenu item and add to main menu |
| 140 | + native_menu_item_t submenu_item = |
| 141 | + native_menu_create_and_add_submenu(menu, "More Options", submenu); |
| 142 | + if (submenu_item) { |
| 143 | + printf("Created submenu with %zu items\n", |
| 144 | + native_menu_get_item_count(submenu)); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // Create tray icon |
| 150 | + native_tray_icon_t tray_icon = native_tray_manager_create(); |
| 151 | + if (!tray_icon) { |
| 152 | + printf("Error: Failed to create tray icon!\n"); |
| 153 | + native_menu_destroy(menu); |
| 154 | + return 1; |
| 155 | + } |
| 156 | + |
| 157 | + printf("Created tray icon with ID: %ld\n", |
| 158 | + native_tray_icon_get_id(tray_icon)); |
| 159 | + |
| 160 | + // Set up tray icon properties |
| 161 | + native_tray_icon_set_title(tray_icon, "My App"); |
| 162 | + native_tray_icon_set_tooltip(tray_icon, |
| 163 | + "My Application - Right click for menu"); |
| 164 | + |
| 165 | + // Set the context menu |
| 166 | + native_tray_icon_set_context_menu(tray_icon, menu); |
| 167 | + |
| 168 | + // Set up tray icon event handlers |
| 169 | + native_tray_icon_set_on_left_click(tray_icon, on_tray_left_click, NULL); |
| 170 | + native_tray_icon_set_on_right_click(tray_icon, on_tray_right_click, NULL); |
| 171 | + native_tray_icon_set_on_double_click(tray_icon, on_tray_double_click, NULL); |
| 172 | + |
| 173 | + // Show the tray icon |
| 174 | + if (native_tray_icon_show(tray_icon)) { |
| 175 | + printf("Tray icon is now visible\n"); |
| 176 | + } else { |
| 177 | + printf("Warning: Failed to show tray icon\n"); |
| 178 | + } |
| 179 | + |
| 180 | + // Get tray icon bounds |
| 181 | + native_rectangle_t bounds; |
| 182 | + if (native_tray_icon_get_bounds(tray_icon, &bounds)) { |
| 183 | + printf("Tray icon bounds: x=%.1f, y=%.1f, width=%.1f, height=%.1f\n", |
| 184 | + bounds.x, bounds.y, bounds.width, bounds.height); |
| 185 | + } |
| 186 | + |
| 187 | + // Demonstrate menu item lookup |
| 188 | + native_menu_item_t found_item = native_menu_find_item_by_text(menu, "Exit"); |
| 189 | + if (found_item) { |
| 190 | + printf("Found 'Exit' menu item with ID: %ld\n", |
| 191 | + native_menu_item_get_id(found_item)); |
| 192 | + |
| 193 | + // Get accelerator info |
| 194 | + native_keyboard_accelerator_t accel; |
| 195 | + if (native_menu_item_get_accelerator(found_item, &accel)) { |
| 196 | + char accel_str[64]; |
| 197 | + if (native_keyboard_accelerator_to_string(&accel, accel_str, |
| 198 | + sizeof(accel_str)) > 0) { |
| 199 | + printf("Exit item accelerator: %s\n", accel_str); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + // Show all managed tray icons |
| 205 | + native_tray_icon_list_t tray_list = native_tray_manager_get_all(); |
| 206 | + printf("Total managed tray icons: %zu\n", tray_list.count); |
| 207 | + native_tray_icon_list_free(tray_list); |
| 208 | + |
| 209 | + printf("\n=== Tray icon and menu are now active ===\n"); |
| 210 | + printf("- Left click the tray icon to see left click message\n"); |
| 211 | + printf("- Right click the tray icon to open context menu\n"); |
| 212 | + printf("- Double click the tray icon to see double click message\n"); |
| 213 | + printf("- Use menu items to interact with the application\n"); |
| 214 | + printf("- Click 'Exit' to quit\n"); |
| 215 | + printf("\nRunning... (Press Ctrl+C to force quit)\n"); |
| 216 | + |
| 217 | + int exit_code = native_run_example_app(); |
| 218 | + |
| 219 | + return 0; |
| 220 | +} |
0 commit comments