Skip to content

Commit 8e55cff

Browse files
authored
Merge pull request #685 from triuk/main
Fix C++ event window lookup and Linux WebKit navigation handling; update serve_a_folder example
2 parents b36c04e + 1c17e9b commit 8e55cff

File tree

5 files changed

+62
-56
lines changed

5 files changed

+62
-56
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,20 @@ if (WEBUI_BUILD_EXAMPLES)
123123
add_executable(minimal ${CMAKE_CURRENT_SOURCE_DIR}/examples/C++/minimal/main.cpp)
124124
add_executable(call_js_from_cpp ${CMAKE_CURRENT_SOURCE_DIR}/examples/C++/call_js_from_cpp/main.cpp)
125125
add_executable(call_js_from_cpp_class ${CMAKE_CURRENT_SOURCE_DIR}/examples/C++/call_js_from_cpp_class/main.cpp)
126+
add_executable(serve_a_folder ${CMAKE_CURRENT_SOURCE_DIR}/examples/C++/serve_a_folder/main.cpp)
126127
add_executable(call_js_from_c ${CMAKE_CURRENT_SOURCE_DIR}/examples/C/call_js_from_c/main.c)
127128

128129
target_link_libraries(minimal webui)
129130
target_link_libraries(call_js_from_cpp webui)
130131
target_link_libraries(call_js_from_cpp_class webui)
132+
target_link_libraries(serve_a_folder webui)
131133
target_link_libraries(call_js_from_c webui)
132134

133135
if (MSVC)
134136
set_target_properties(minimal PROPERTIES LINK_FLAGS "/SubSystem:\"Windows\"" VS_DPI_AWARE "ON")
135137
set_target_properties(call_js_from_cpp PROPERTIES LINK_FLAGS "/SubSystem:\"Windows\"" VS_DPI_AWARE "ON")
136138
set_target_properties(call_js_from_cpp_class PROPERTIES LINK_FLAGS "/SubSystem:\"Windows\"" VS_DPI_AWARE "ON")
139+
set_target_properties(serve_a_folder PROPERTIES LINK_FLAGS "/SubSystem:\"Windows\"" VS_DPI_AWARE "ON")
137140
set_target_properties(call_js_from_c PROPERTIES LINK_FLAGS "/SubSystem:\"Windows\"" VS_DPI_AWARE "ON")
138141
endif()
139142

examples/C++/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The only requirement to build the examples is a a C++11 compiler.
88
- `call_c_from_js`: Calls C++ from JavaScript.
99
- `call_js_from_c`: Calls JavaScript from C++.
1010
- `call_js_from_cpp_class`: Calls JavaScript from C++ using class methods and member-function bind.
11-
- `serve_a_folder`: Uses WebUI to serve a folder that contains multiple files.
11+
- `serve_a_folder`: Uses WebUI to serve a folder with multiple files (class-based example using member-function bind).
1212

1313
To build an example, cd into its directory and run the make command.
1414

examples/C++/serve_a_folder/main.cpp

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,71 @@
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-
6291
int 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

include/webui.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ namespace webui {
209209

210210
// Get current window object pointer
211211
webui::window& get_window() {
212-
return event::handler::get_window(window);
212+
return event::handler::get_window(bind_id);
213213
}
214214

215215
// Get event type

src/webui.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,11 +1610,9 @@ void webui_navigate(size_t window, const char* url) {
16101610
_webui_webview_update(win);
16111611
}
16121612
#else
1613-
_webui_free_mem((void*) win->webView->url);
1614-
char* url_cp = _webui_str_dup(url);
1615-
win->webView->url = url_cp;
1616-
win->webView->navigate = true;
1617-
_webui_webview_update(win);
1613+
// In WebKitGTK, navigation can be triggered from navigation-policy callback.
1614+
// Route this path through show(), which already applies in_show guard.
1615+
(void)webui_show(window, url);
16181616
#endif
16191617
}
16201618
}

0 commit comments

Comments
 (0)