-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.cpp
More file actions
167 lines (132 loc) · 6.83 KB
/
Copy pathmain.cpp
File metadata and controls
167 lines (132 loc) · 6.83 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
#include <chrono>
#include <iostream>
#include <memory>
#include <thread>
#include "nativeapi.h"
using namespace nativeapi;
int main() {
std::cout << "=== Menu Event System Example ===" << std::endl;
// Get the Application instance to initialize platform
Application& app = Application::GetInstance();
try {
// Create a menu
auto menu = std::make_shared<Menu>();
std::cout << "Created menu with ID: " << menu->GetId() << std::endl;
// Create menu items with different types
auto file_item = std::make_shared<MenuItem>("New File", MenuItemType::Normal);
auto separator_item = std::make_shared<MenuItem>("", MenuItemType::Separator);
auto checkbox_item = std::make_shared<MenuItem>("Word Wrap", MenuItemType::Checkbox);
auto radio_item1 = std::make_shared<MenuItem>("View Mode 1", MenuItemType::Radio);
auto radio_item2 = std::make_shared<MenuItem>("View Mode 2", MenuItemType::Radio);
auto exit_item = std::make_shared<MenuItem>("Exit", MenuItemType::Normal);
// Set up radio group
radio_item1->SetRadioGroup(1);
radio_item2->SetRadioGroup(1);
radio_item1->SetState(MenuItemState::Checked);
// Set keyboard accelerators
file_item->SetAccelerator(KeyboardAccelerator("N", ModifierKey::Ctrl));
exit_item->SetAccelerator(KeyboardAccelerator("Q", ModifierKey::Ctrl));
// Add event listeners using the new event system
std::cout << "Setting up event listeners..." << std::endl;
// Listen to menu item selection events
file_item->AddListener<MenuItemClickedEvent>([](const MenuItemClickedEvent& event) {
std::cout << "[EVENT] Menu item clicked: New File"
<< " (ID: " << event.GetItemId() << ")" << std::endl;
});
checkbox_item->AddListener<MenuItemClickedEvent>([](const MenuItemClickedEvent& event) {
std::cout << "[EVENT] Checkbox item clicked: Word Wrap"
<< " (ID: " << event.GetItemId() << ") - Handle state manually" << std::endl;
});
// Note: State management is now handled by the application
radio_item1->AddListener<MenuItemClickedEvent>([](const MenuItemClickedEvent& event) {
std::cout << "[EVENT] Radio item 1 clicked: ID " << event.GetItemId()
<< " - Handle state manually" << std::endl;
});
radio_item2->AddListener<MenuItemClickedEvent>([](const MenuItemClickedEvent& event) {
std::cout << "[EVENT] Radio item 2 clicked: ID " << event.GetItemId()
<< " - Handle state manually" << std::endl;
});
exit_item->AddListener<MenuItemClickedEvent>([&app](const MenuItemClickedEvent& event) {
std::cout << "[EVENT] Exit item clicked: Exit" << std::endl;
std::cout << "Application exiting..." << std::endl;
app.Quit(0);
});
// Listen to menu events
menu->AddListener<MenuOpenedEvent>([](const MenuOpenedEvent& event) {
std::cout << "[EVENT] Menu opened: ID " << event.GetMenuId() << std::endl;
});
menu->AddListener<MenuClosedEvent>([](const MenuClosedEvent& event) {
std::cout << "[EVENT] Menu closed: ID " << event.GetMenuId() << std::endl;
});
// Add items to menu
menu->AddItem(file_item);
menu->AddItem(separator_item);
menu->AddItem(checkbox_item);
menu->AddSeparator();
menu->AddItem(radio_item1);
menu->AddItem(radio_item2);
menu->AddSeparator();
menu->AddItem(exit_item);
std::cout << "Menu created with " << menu->GetItemCount() << " items" << std::endl;
// Demonstrate submenu
std::cout << "\n=== Testing Submenu ===" << std::endl;
auto submenu = std::make_shared<Menu>();
auto submenu_item1 = std::make_shared<MenuItem>("Submenu Item 1", MenuItemType::Normal);
auto submenu_item2 = std::make_shared<MenuItem>("Submenu Item 2", MenuItemType::Normal);
submenu_item1->AddListener<MenuItemClickedEvent>([](const MenuItemClickedEvent& event) {
std::cout << "[EVENT] Submenu item clicked: Submenu Item 1" << std::endl;
});
submenu_item2->AddListener<MenuItemClickedEvent>([](const MenuItemClickedEvent& event) {
std::cout << "[EVENT] Submenu item clicked: Submenu Item 2" << std::endl;
});
submenu->AddItem(submenu_item1);
submenu->AddItem(submenu_item2);
auto submenu_parent = std::make_shared<MenuItem>("Tools", MenuItemType::Submenu);
submenu_parent->SetSubmenu(submenu);
// Add submenu event listeners
submenu_parent->AddListener<MenuItemSubmenuOpenedEvent>(
[](const MenuItemSubmenuOpenedEvent& event) {
std::cout << "[EVENT] Submenu opened: ID " << event.GetItemId() << std::endl;
});
submenu_parent->AddListener<MenuItemSubmenuClosedEvent>(
[](const MenuItemSubmenuClosedEvent& event) {
std::cout << "[EVENT] Submenu closed: ID " << event.GetItemId() << std::endl;
});
menu->AddItem(submenu_parent);
std::cout << "Added submenu with " << submenu->GetItemCount() << " items" << std::endl;
std::cout << "\n=== Event System Demo Complete ===" << std::endl;
std::cout << "This example demonstrates:" << std::endl;
std::cout << "1. Creating menus and menu items with different types" << std::endl;
std::cout << "2. Using the new event system with AddListener<EventType>()" << std::endl;
std::cout << "3. Handling MenuItemClickedEvent (state managed by application)" << std::endl;
std::cout << "4. Handling MenuOpenedEvent and MenuClosedEvent" << std::endl;
std::cout << "5. Handling MenuItemSubmenuOpenedEvent and "
"MenuItemSubmenuClosedEvent"
<< std::endl;
std::cout << "6. Programmatic event emission using Emit()" << std::endl;
std::cout << "7. Submenu support with event propagation" << std::endl;
std::cout << "\n========================================" << std::endl;
std::cout << "Starting application event loop..." << std::endl;
std::cout << "The menu will open shortly." << std::endl;
std::cout << "Click the Exit menu item to quit the application." << std::endl;
std::cout << "========================================" << std::endl;
// Set up application started listener to open menu after event loop starts
app.AddListener<ApplicationStartedEvent>([menu](const ApplicationStartedEvent& event) {
std::cout << "Application started - opening menu at (100, 100)" << std::endl;
// Open menu as context menu at screen coordinates (100, 100)
if (menu->Open(PositioningStrategy::Absolute({100, 100}))) {
std::cout << "Context menu opened successfully!" << std::endl;
} else {
std::cout << "Failed to open context menu" << std::endl;
}
});
// Run the application event loop - this will block until app.Quit() is called
int exit_code = app.Run();
std::cout << "Exiting Menu Example..." << std::endl;
return exit_code;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}