Skip to content

add class-based call_js_from_cpp example and member-function bind overload#684

Merged
AlbertShown merged 1 commit intowebui-dev:mainfrom
triuk:main
Feb 17, 2026
Merged

add class-based call_js_from_cpp example and member-function bind overload#684
AlbertShown merged 1 commit intowebui-dev:mainfrom
triuk:main

Conversation

@triuk
Copy link
Contributor

@triuk triuk commented Feb 16, 2026

Adds ergonomic class-member binding in the C++ API and introduces a class-based example.
The previous callback signature made C++ class usage too complex.
Now you can do just this in the constructor and you are good to go:

my_window.bind("my_function_count", this, &CounterApp::my_function_count);

This update enables direct, readable binding of member functions without workarounds. Just like in the Python-WebUI.

Changes

  • include/webui.hpp

    • switched callback storage in window::event::handler to std::function<void(event*)> (from raw function pointers)
    • updated internal callback bookkeeping to map + mutex
    • added overload: bind(element, instance, &Class::method)
  • examples/C++/call_js_from_cpp_class/main.cpp

    • new example equivalent to call_js_from_cpp, but implemented with a class
  • CMakeLists.txt

    • added call_js_from_cpp_class target
  • examples/C++/README.md

    • listed the new example

@AlbertShown
Copy link
Member

Thank you @triuk for this PR.
C to C++ class method calls was in the todo list for a while.
Thank you for fixing this, very useful.

After marge, I will replace the old example call_js_from_cpp with this new one.

@AlbertShown AlbertShown merged commit b36c04e into webui-dev:main Feb 17, 2026
26 checks passed
@triuk
Copy link
Contributor Author

triuk commented Feb 17, 2026

@AlbertShown Hi, I just found serve_a_folder example, which uses class by default. I did not notice it as it was not a build target in CMakeFiles.txt.
I tried to rewrite it with the new class bind to have up-to-date code, but I found a bug. I am on it, probably solved. I will do another PR after more testing.

@AlbertShown
Copy link
Member

Sounds good @triuk, Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants