-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.cpp
More file actions
334 lines (291 loc) · 14.9 KB
/
Copy pathmain.cpp
File metadata and controls
334 lines (291 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <iostream>
#include "nativeapi.h"
using nativeapi::Application;
using nativeapi::Display;
using nativeapi::DisplayAddedEvent;
using nativeapi::DisplayManager;
using nativeapi::DisplayRemovedEvent;
using nativeapi::Menu;
using nativeapi::MenuClosedEvent;
using nativeapi::MenuItem;
using nativeapi::MenuItemClickedEvent;
using nativeapi::MenuItemState;
using nativeapi::MenuItemSubmenuClosedEvent;
using nativeapi::MenuItemSubmenuOpenedEvent;
using nativeapi::MenuItemType;
using nativeapi::MenuOpenedEvent;
using nativeapi::TrayIcon;
using nativeapi::TrayIconClickedEvent;
using nativeapi::TrayIconDoubleClickedEvent;
using nativeapi::TrayIconRightClickedEvent;
using nativeapi::TrayManager;
using nativeapi::Window;
using nativeapi::WindowManager;
int main() {
native_accessibility_manager_enable();
bool is_enabled = native_accessibility_manager_is_enabled();
std::cout << "is_enabled: " << is_enabled << std::endl;
DisplayManager& display_manager = DisplayManager::GetInstance();
TrayManager& tray_manager = TrayManager::GetInstance();
WindowManager& window_manager = WindowManager::GetInstance();
// Create a new window (automatically registered)
std::shared_ptr<Window> window_ptr = std::make_shared<Window>();
window_ptr->SetTitle("Window Example");
window_ptr->SetSize({800, 600}, false);
window_ptr->SetMinimumSize({400, 300});
window_ptr->SetMaximumSize({1920, 1080});
window_ptr->Center();
std::shared_ptr<TrayIcon> tray_icon_ptr = std::make_shared<TrayIcon>();
if (tray_icon_ptr != nullptr) {
TrayIcon& tray_icon = *tray_icon_ptr;
auto icon = nativeapi::Image::FromBase64(
"data:image/"
"png;base64,"
"iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABGdBTUEAALGPC/"
"xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAhGVY"
"SWZNTQAqAAAACAAFARIAAwAAAAEAAQAAARoABQAAAAEAAABKARsABQAAAAEAAABSASgAAw"
"AAAAEAAgAAh2kABAAAAAEAAABaAAAAAAAAAEgAAAABAAAASAAAAAEAA6ABAAMAAAABAAEA"
"AKACAAQAAAABAAAAFKADAAQAAAABAAAAFAAAAABB553+"
"AAAACXBIWXMAAAsTAAALEwEAmpwYAAABWWlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPH"
"g6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iWE1QIENvcmUg"
"Ni4wLjAiPgogICA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6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en4gjOuLs0OTlJ"
"KMXy8jLV19cTd1psXmCFzP7p7++XVObm5kQYiUTo+"
"vqahoeHCWPE3ae7uztvXqGUM0IDDa83NzcEYIDy4NPe3p6ADQ4Oypsb4ZUIdrapiQHJ/"
"CI9GDw8PAh7YWGBzs7OhI7H45mqHo2UX82gJ0kTAEVa3d3dNDs7Kw3q6uoSJ6Z5GTYag22"
"GMmt8TPT8XxeAnp4e+Q+jFLnAGFghZaygV+uKN484hZEBBX1/f+/"
"xM6ICqVmOBZGwqqqqPrHRx/z8fPf29tbiJUH8f6XDw0PiVSZdfmOc6+lyFohi47/"
"WVy6ENA/1l/"
"XFg23zDhiRuqUXbBi1whJ9enqSQUWa7x3IcWHH0xDhLfUVYSpsWt6LMfZQwwX/"
"wLVwWPG97osM9Wf7Df6GGOwnsP4BQFiPuOZ8wJUAAAAASUVORK5CYII=");
tray_icon.SetIcon(icon);
std::cout << "Tray ID: " << tray_icon.GetId() << std::endl;
auto title = tray_icon.GetTitle();
std::cout << "Tray Title: " << (title.has_value() ? title.value() : "(no title)") << std::endl;
tray_icon.SetVisible(true);
// Create context menu
auto context_menu = std::make_shared<Menu>();
context_menu->AddListener<MenuOpenedEvent>(
[](const MenuOpenedEvent& event) { std::cout << "Menu opened" << std::endl; });
context_menu->AddListener<MenuClosedEvent>(
[](const MenuClosedEvent& event) { std::cout << "Menu closed" << std::endl; });
// Add menu items
auto show_window_item = std::make_shared<MenuItem>("Show Window", MenuItemType::Normal);
show_window_item->AddListener<MenuItemClickedEvent>(
[window_ptr](const MenuItemClickedEvent& event) {
std::cout << "Show Window clicked from context menu" << std::endl;
if (window_ptr) {
window_ptr->Show();
window_ptr->Focus();
}
});
context_menu->AddItem(show_window_item);
auto hide_window_item = std::make_shared<MenuItem>("Hide Window", MenuItemType::Normal);
hide_window_item->AddListener<MenuItemClickedEvent>(
[window_ptr](const MenuItemClickedEvent& event) {
std::cout << "Hide Window clicked from context menu" << std::endl;
if (window_ptr) {
window_ptr->Hide();
}
});
context_menu->AddItem(hide_window_item);
// Add separator
context_menu->AddSeparator();
// Add about item
auto about_item = std::make_shared<MenuItem>("About", MenuItemType::Normal);
about_item->AddListener<MenuItemClickedEvent>([](const MenuItemClickedEvent& event) {
std::cout << "About clicked from context menu" << std::endl;
std::cout << "Window Example v1.0 - Native API Demo" << std::endl;
});
context_menu->AddItem(about_item);
// Create Tools submenu with submenu event handling
auto tools_submenu = std::make_shared<Menu>();
// Add items to tools submenu
auto clear_cache_item = std::make_shared<MenuItem>("Clear Cache", MenuItemType::Normal);
clear_cache_item->AddListener<MenuItemClickedEvent>([](const MenuItemClickedEvent& event) {
std::cout << "Clear Cache clicked from submenu" << std::endl;
});
tools_submenu->AddItem(clear_cache_item);
auto reset_settings_item = std::make_shared<MenuItem>("Reset Settings", MenuItemType::Normal);
reset_settings_item->AddListener<MenuItemClickedEvent>([](const MenuItemClickedEvent& event) {
std::cout << "Reset Settings clicked from submenu" << std::endl;
});
tools_submenu->AddItem(reset_settings_item);
tools_submenu->AddSeparator();
auto debug_mode_item = std::make_shared<MenuItem>("Debug Mode", MenuItemType::Checkbox);
debug_mode_item->SetState(MenuItemState::Unchecked);
debug_mode_item->AddListener<MenuItemClickedEvent>([debug_mode_item](
const MenuItemClickedEvent& event) {
auto current_state = debug_mode_item->GetState();
MenuItemState new_state = (current_state == MenuItemState::Checked) ? MenuItemState::Unchecked
: MenuItemState::Checked;
debug_mode_item->SetState(new_state);
std::cout << "Debug Mode " << (new_state == MenuItemState::Checked ? "enabled" : "disabled")
<< std::endl;
});
tools_submenu->AddItem(debug_mode_item);
// Create the submenu parent item
auto tools_item = std::make_shared<MenuItem>("Tools", MenuItemType::Submenu);
tools_item->SetSubmenu(tools_submenu);
// Add submenu event listeners
tools_item->AddListener<MenuItemSubmenuOpenedEvent>(
[](const MenuItemSubmenuOpenedEvent& event) {
std::cout << "Tools submenu opened (ID: " << event.GetItemId() << ")" << std::endl;
});
tools_item->AddListener<MenuItemSubmenuClosedEvent>(
[](const MenuItemSubmenuClosedEvent& event) {
std::cout << "Tools submenu closed (ID: " << event.GetItemId() << ")" << std::endl;
});
context_menu->AddItem(tools_item);
// Add separator before preferences
context_menu->AddSeparator();
// Add preferences section (not a submenu, just a label)
auto preferences_item = std::make_shared<MenuItem>("Preferences", MenuItemType::Normal);
context_menu->AddItem(preferences_item);
// Add checkbox menu items
auto auto_start_item = std::make_shared<MenuItem>("Auto Start", MenuItemType::Checkbox);
auto_start_item->SetState(MenuItemState::Checked); // Initially checked
auto_start_item->AddListener<MenuItemClickedEvent>([auto_start_item](
const MenuItemClickedEvent& event) {
auto current_state = auto_start_item->GetState();
MenuItemState new_state = (current_state == MenuItemState::Checked) ? MenuItemState::Unchecked
: MenuItemState::Checked;
auto_start_item->SetState(new_state);
std::cout << "Auto Start " << (new_state == MenuItemState::Checked ? "enabled" : "disabled")
<< std::endl;
});
context_menu->AddItem(auto_start_item);
auto notifications_item =
std::make_shared<MenuItem>("Show Notifications", MenuItemType::Checkbox);
notifications_item->SetState(MenuItemState::Unchecked); // Initially unchecked
notifications_item->AddListener<MenuItemClickedEvent>([notifications_item](
const MenuItemClickedEvent& event) {
auto current_state = notifications_item->GetState();
MenuItemState new_state = (current_state == MenuItemState::Checked) ? MenuItemState::Unchecked
: MenuItemState::Checked;
notifications_item->SetState(new_state);
std::cout << "Notifications "
<< (new_state == MenuItemState::Checked ? "enabled" : "disabled") << std::endl;
});
context_menu->AddItem(notifications_item);
// Add three-state checkbox example
auto sync_item = std::make_shared<MenuItem>("Sync Status", MenuItemType::Checkbox);
sync_item->SetState(MenuItemState::Mixed); // Initially mixed/indeterminate
sync_item->AddListener<MenuItemClickedEvent>([sync_item](const MenuItemClickedEvent& event) {
auto current_state = sync_item->GetState();
MenuItemState next_state;
std::string state_name;
// Cycle through states: Mixed -> Checked -> Unchecked -> Mixed
switch (current_state) {
case MenuItemState::Mixed:
next_state = MenuItemState::Checked;
state_name = "enabled";
break;
case MenuItemState::Checked:
next_state = MenuItemState::Unchecked;
state_name = "disabled";
break;
case MenuItemState::Unchecked:
default:
next_state = MenuItemState::Mixed;
state_name = "partial";
break;
}
sync_item->SetState(next_state);
std::cout << "Sync Status: " << state_name << std::endl;
});
context_menu->AddItem(sync_item);
// Add separator before radio group
context_menu->AddSeparator();
// Add radio button group for theme selection
auto theme_label = std::make_shared<MenuItem>("Theme:", MenuItemType::Normal);
context_menu->AddItem(theme_label);
auto light_theme_item = std::make_shared<MenuItem>("Light Theme", MenuItemType::Radio);
light_theme_item->SetRadioGroup(0); // Group 0
light_theme_item->SetState(MenuItemState::Checked); // Default selection
light_theme_item->AddListener<MenuItemClickedEvent>(
[light_theme_item](const MenuItemClickedEvent& event) {
light_theme_item->SetState(MenuItemState::Checked);
std::cout << "Light theme selected" << std::endl;
});
context_menu->AddItem(light_theme_item);
auto dark_theme_item = std::make_shared<MenuItem>("Dark Theme", MenuItemType::Radio);
dark_theme_item->SetRadioGroup(0); // Same group as light theme
dark_theme_item->AddListener<MenuItemClickedEvent>(
[dark_theme_item](const MenuItemClickedEvent& event) {
dark_theme_item->SetState(MenuItemState::Checked);
std::cout << "Dark theme selected" << std::endl;
});
context_menu->AddItem(dark_theme_item);
auto auto_theme_item = std::make_shared<MenuItem>("Auto Theme", MenuItemType::Radio);
auto_theme_item->SetRadioGroup(0); // Same group
auto_theme_item->AddListener<MenuItemClickedEvent>(
[auto_theme_item](const MenuItemClickedEvent& event) {
auto_theme_item->SetState(MenuItemState::Checked);
std::cout << "Auto theme selected" << std::endl;
});
context_menu->AddItem(auto_theme_item);
// Add another separator
context_menu->AddSeparator();
// Add exit item
auto exit_item = std::make_shared<MenuItem>("Exit", MenuItemType::Normal);
exit_item->AddListener<MenuItemClickedEvent>([window_ptr](const MenuItemClickedEvent& event) {
std::cout << "Exit clicked from context menu" << std::endl;
// Close the window to trigger app exit
if (window_ptr) {
window_ptr->Hide();
}
});
context_menu->AddItem(exit_item);
// Set the context menu to the tray icon
tray_icon.SetContextMenu(context_menu);
// Set up event listeners
tray_icon.AddListener<TrayIconClickedEvent>([&tray_icon](const TrayIconClickedEvent& event) {
std::cout << "*** TRAY ICON LEFT CLICKED! ***" << std::endl;
std::cout << "This is the left click handler working!" << std::endl;
std::cout << "Tray icon ID: " << event.GetTrayIconId() << std::endl;
// Open context menu on left click
tray_icon.OpenContextMenu();
});
tray_icon.AddListener<TrayIconRightClickedEvent>([](const TrayIconRightClickedEvent& event) {
std::cout << "*** TRAY ICON RIGHT CLICKED! ***" << std::endl;
std::cout << "This is the right click handler working!" << std::endl;
std::cout << "Tray icon ID: " << event.GetTrayIconId() << std::endl;
});
tray_icon.AddListener<TrayIconDoubleClickedEvent>([](const TrayIconDoubleClickedEvent& event) {
std::cout << "*** TRAY ICON DOUBLE CLICKED! ***" << std::endl;
std::cout << "This is the double click handler working!" << std::endl;
std::cout << "Tray icon ID: " << event.GetTrayIconId() << std::endl;
});
} else {
std::cerr << "Failed to create tray." << std::endl;
}
display_manager.AddListener<nativeapi::DisplayAddedEvent>(
[](const nativeapi::DisplayAddedEvent& event) {
std::cout << "Display added: " << event.GetDisplay()->GetId() << std::endl;
});
display_manager.AddListener<nativeapi::DisplayRemovedEvent>(
[](const nativeapi::DisplayRemovedEvent& event) {
std::cout << "Display removed: " << event.GetDisplay()->GetId() << std::endl;
});
auto& app = Application::GetInstance();
app.Run(window_ptr);
return 0;
}