Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,20 @@ if (WEBUI_BUILD_EXAMPLES)
add_executable(minimal ${CMAKE_CURRENT_SOURCE_DIR}/examples/C++/minimal/main.cpp)
add_executable(call_js_from_cpp ${CMAKE_CURRENT_SOURCE_DIR}/examples/C++/call_js_from_cpp/main.cpp)
add_executable(call_js_from_cpp_class ${CMAKE_CURRENT_SOURCE_DIR}/examples/C++/call_js_from_cpp_class/main.cpp)
add_executable(serve_a_folder ${CMAKE_CURRENT_SOURCE_DIR}/examples/C++/serve_a_folder/main.cpp)
add_executable(call_js_from_c ${CMAKE_CURRENT_SOURCE_DIR}/examples/C/call_js_from_c/main.c)

target_link_libraries(minimal webui)
target_link_libraries(call_js_from_cpp webui)
target_link_libraries(call_js_from_cpp_class webui)
target_link_libraries(serve_a_folder webui)
target_link_libraries(call_js_from_c webui)

if (MSVC)
set_target_properties(minimal PROPERTIES LINK_FLAGS "/SubSystem:\"Windows\"" VS_DPI_AWARE "ON")
set_target_properties(call_js_from_cpp PROPERTIES LINK_FLAGS "/SubSystem:\"Windows\"" VS_DPI_AWARE "ON")
set_target_properties(call_js_from_cpp_class PROPERTIES LINK_FLAGS "/SubSystem:\"Windows\"" VS_DPI_AWARE "ON")
set_target_properties(serve_a_folder PROPERTIES LINK_FLAGS "/SubSystem:\"Windows\"" VS_DPI_AWARE "ON")
set_target_properties(call_js_from_c PROPERTIES LINK_FLAGS "/SubSystem:\"Windows\"" VS_DPI_AWARE "ON")
endif()

Expand Down
2 changes: 1 addition & 1 deletion examples/C++/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The only requirement to build the examples is a a C++11 compiler.
- `call_c_from_js`: Calls C++ from JavaScript.
- `call_js_from_c`: Calls JavaScript from C++.
- `call_js_from_cpp_class`: Calls JavaScript from C++ using class methods and member-function bind.
- `serve_a_folder`: Uses WebUI to serve a folder that contains multiple files.
- `serve_a_folder`: Uses WebUI to serve a folder with multiple files (class-based example using member-function bind).

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

Expand Down
103 changes: 54 additions & 49 deletions examples/C++/serve_a_folder/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,71 @@
// Include C++ STD
#include <iostream>

// Making this object global so show_second_window() can access it.
webui::window my_second_window;

// Example of a simple Class
class MyClass {
class ServeAFolderApp {
public:
// This method gets called every time the
// user clicks on "OpenNewWindow"
ServeAFolderApp() {
// Bind HTML element IDs with class methods
my_window.bind("SwitchToSecondPage", this, &ServeAFolderApp::switch_to_second_page);
my_window.bind("OpenNewWindow", this, &ServeAFolderApp::show_second_window);
my_window.bind("Exit", this, &ServeAFolderApp::exit_app);
my_second_window.bind("Exit", this, &ServeAFolderApp::exit_app);

// Bind all events
my_window.bind("", this, &ServeAFolderApp::events);
my_second_window.bind("", this, &ServeAFolderApp::events);
}

void run() {
// Print logs (debug build only)
std::cout << "Starting..." << std::endl;

// Show a new window
my_window.show("index.html"); // my_window.show_browser("index.html", Chrome);

// Wait until all windows get closed
webui::wait();

// Print logs (debug build only)
std::cout << "Thank you." << std::endl;
}

private:
webui::window my_window;
webui::window my_second_window;

const char* event_type_to_string(size_t type) const {
switch (type) {
case webui::DISCONNECTED: return "DISCONNECTED";
case webui::CONNECTED: return "CONNECTED";
case webui::MOUSE_CLICK: return "MOUSE_CLICK";
case webui::NAVIGATION: return "NAVIGATION";
case webui::CALLBACKS: return "CALLBACKS";
default: return "UNKNOWN";
}
}

// This method gets called every time the user clicks on "OpenNewWindow"
void show_second_window(webui::window::event* e) {
// Show a new window, and navigate to `/second.html`
// if the window is already opened, then switch in the same window
my_second_window.show("second.html");
}

// This method gets called every time the
// user clicks on "SwitchToSecondPage"
// This method gets called every time the user clicks on "SwitchToSecondPage"
void switch_to_second_page(webui::window::event* e) {
// Switch to `/second.html` in the same opened window.
e->get_window().show("second.html");
}

// Example of a simple function (Not a method)
// This function receives all events because
// it's get bind with an empty HTML ID.
// This method receives all events because it's bind with an empty HTML ID.
void events(webui::window::event* e) {
std::cout << "[events] window=" << e->window
<< " type=" << e->get_type()
<< " (" << event_type_to_string(e->get_type()) << ")"
<< " element='" << e->get_element()
<< "' event=" << e->get_number()
<< std::endl;

if (e->event_type == webui::CONNECTED)
std::cout << "Window Connected." << std::endl;
else if (e->event_type == webui::DISCONNECTED)
Expand All @@ -42,50 +82,15 @@ class MyClass {
}
}

// Example of a simple function (Not a method)
void exit_app(webui::window::event* e) {
// Close all opened windows
webui::exit();
}
};

// -- MyClass C Wrapper ------------------------------------------------------------------------
// Because WebUI is written in C, so it can not access `MyClass` directly.
// That's why we should create a simple C++ wrapper.
MyClass myClassObj;
void show_second_window_wrp(webui::window::event* e) { myClassObj.show_second_window(e); }
void switch_to_second_page_wrp(webui::window::event* e) { myClassObj.switch_to_second_page(e); }
void events_wrp(webui::window::event* e) { myClassObj.events(e); }
void exit_app_wrp(webui::window::event* e) { myClassObj.exit_app(e); }
// ---------------------------------------------------------------------------------------------

int main() {

// Print logs (debug build only)
std::cout << "Starting..." << std::endl;

// Create a new window
webui::window my_window;

// Bind HTML element IDs with a C functions
my_window.bind("SwitchToSecondPage", switch_to_second_page_wrp);
my_window.bind("OpenNewWindow", show_second_window_wrp);
my_window.bind("Exit", exit_app_wrp);
my_second_window.bind("Exit", exit_app_wrp);

// Bind all events
my_window.bind("", events_wrp);
my_second_window.bind("", events_wrp);

// Show a new window
my_window.show("index.html"); // my_window.show_browser("index.html", Chrome);

// Wait until all windows get closed
webui::wait();

// Print logs (debug build only)
std::cout << "Thank you." << std::endl;

ServeAFolderApp app;
app.run();
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion include/webui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ namespace webui {

// Get current window object pointer
webui::window& get_window() {
return event::handler::get_window(window);
return event::handler::get_window(bind_id);
}

// Get event type
Expand Down
8 changes: 3 additions & 5 deletions src/webui.c
Original file line number Diff line number Diff line change
Expand Up @@ -1610,11 +1610,9 @@ void webui_navigate(size_t window, const char* url) {
_webui_webview_update(win);
}
#else
_webui_free_mem((void*) win->webView->url);
char* url_cp = _webui_str_dup(url);
win->webView->url = url_cp;
win->webView->navigate = true;
_webui_webview_update(win);
// In WebKitGTK, navigation can be triggered from navigation-policy callback.
// Route this path through show(), which already applies in_show guard.
(void)webui_show(window, url);
#endif
}
}
Expand Down
Loading