44// Include C++ STD
55#include < iostream>
66
7- // Making this object global so show_second_window() can access it.
8- webui::window my_second_window;
9-
10- // Example of a simple Class
11- class MyClass {
7+ class ServeAFolderApp {
128 public:
13- // This method gets called every time the
14- // user clicks on "OpenNewWindow"
9+ ServeAFolderApp () {
10+ // Bind HTML element IDs with class methods
11+ my_window.bind (" SwitchToSecondPage" , this , &ServeAFolderApp::switch_to_second_page);
12+ my_window.bind (" OpenNewWindow" , this , &ServeAFolderApp::show_second_window);
13+ my_window.bind (" Exit" , this , &ServeAFolderApp::exit_app);
14+ my_second_window.bind (" Exit" , this , &ServeAFolderApp::exit_app);
15+
16+ // Bind all events
17+ my_window.bind (" " , this , &ServeAFolderApp::events);
18+ my_second_window.bind (" " , this , &ServeAFolderApp::events);
19+ }
20+
21+ void run () {
22+ // Print logs (debug build only)
23+ std::cout << " Starting..." << std::endl;
24+
25+ // Show a new window
26+ my_window.show (" index.html" ); // my_window.show_browser("index.html", Chrome);
27+
28+ // Wait until all windows get closed
29+ webui::wait ();
30+
31+ // Print logs (debug build only)
32+ std::cout << " Thank you." << std::endl;
33+ }
34+
35+ private:
36+ webui::window my_window;
37+ webui::window my_second_window;
38+
39+ const char * event_type_to_string (size_t type) const {
40+ switch (type) {
41+ case webui::DISCONNECTED: return " DISCONNECTED" ;
42+ case webui::CONNECTED: return " CONNECTED" ;
43+ case webui::MOUSE_CLICK: return " MOUSE_CLICK" ;
44+ case webui::NAVIGATION: return " NAVIGATION" ;
45+ case webui::CALLBACKS: return " CALLBACKS" ;
46+ default : return " UNKNOWN" ;
47+ }
48+ }
49+
50+ // This method gets called every time the user clicks on "OpenNewWindow"
1551 void show_second_window (webui::window::event* e) {
1652 // Show a new window, and navigate to `/second.html`
1753 // if the window is already opened, then switch in the same window
1854 my_second_window.show (" second.html" );
1955 }
2056
21- // This method gets called every time the
22- // user clicks on "SwitchToSecondPage"
57+ // This method gets called every time the user clicks on "SwitchToSecondPage"
2358 void switch_to_second_page (webui::window::event* e) {
2459 // Switch to `/second.html` in the same opened window.
2560 e->get_window ().show (" second.html" );
2661 }
2762
28- // Example of a simple function (Not a method)
29- // This function receives all events because
30- // it's get bind with an empty HTML ID.
63+ // This method receives all events because it's bind with an empty HTML ID.
3164 void events (webui::window::event* e) {
65+ std::cout << " [events] window=" << e->window
66+ << " type=" << e->get_type ()
67+ << " (" << event_type_to_string (e->get_type ()) << " )"
68+ << " element='" << e->get_element ()
69+ << " ' event=" << e->get_number ()
70+ << std::endl;
71+
3272 if (e->event_type == webui::CONNECTED)
3373 std::cout << " Window Connected." << std::endl;
3474 else if (e->event_type == webui::DISCONNECTED)
@@ -42,50 +82,15 @@ class MyClass {
4282 }
4383 }
4484
45- // Example of a simple function (Not a method)
4685 void exit_app (webui::window::event* e) {
4786 // Close all opened windows
4887 webui::exit ();
4988 }
5089};
5190
52- // -- MyClass C Wrapper ------------------------------------------------------------------------
53- // Because WebUI is written in C, so it can not access `MyClass` directly.
54- // That's why we should create a simple C++ wrapper.
55- MyClass myClassObj;
56- void show_second_window_wrp (webui::window::event* e) { myClassObj.show_second_window (e); }
57- void switch_to_second_page_wrp (webui::window::event* e) { myClassObj.switch_to_second_page (e); }
58- void events_wrp (webui::window::event* e) { myClassObj.events (e); }
59- void exit_app_wrp (webui::window::event* e) { myClassObj.exit_app (e); }
60- // ---------------------------------------------------------------------------------------------
61-
6291int main () {
63-
64- // Print logs (debug build only)
65- std::cout << " Starting..." << std::endl;
66-
67- // Create a new window
68- webui::window my_window;
69-
70- // Bind HTML element IDs with a C functions
71- my_window.bind (" SwitchToSecondPage" , switch_to_second_page_wrp);
72- my_window.bind (" OpenNewWindow" , show_second_window_wrp);
73- my_window.bind (" Exit" , exit_app_wrp);
74- my_second_window.bind (" Exit" , exit_app_wrp);
75-
76- // Bind all events
77- my_window.bind (" " , events_wrp);
78- my_second_window.bind (" " , events_wrp);
79-
80- // Show a new window
81- my_window.show (" index.html" ); // my_window.show_browser("index.html", Chrome);
82-
83- // Wait until all windows get closed
84- webui::wait ();
85-
86- // Print logs (debug build only)
87- std::cout << " Thank you." << std::endl;
88-
92+ ServeAFolderApp app;
93+ app.run ();
8994 return 0 ;
9095}
9196
0 commit comments