From 6ad244502d1c3c09a26012c5e7fad901c95cc38e Mon Sep 17 00:00:00 2001 From: Arslaan Pathan Date: Fri, 1 May 2026 21:38:49 +1200 Subject: Fix the buttons and implement hooks for future webkit bindings to build on top of --- tests/test_hooks.c | 36 ++++++++++++++++++++++++++++++++++++ tests/test_main.c | 9 ++++++--- 2 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 tests/test_hooks.c (limited to 'tests') diff --git a/tests/test_hooks.c b/tests/test_hooks.c new file mode 100644 index 0000000..ad56aa9 --- /dev/null +++ b/tests/test_hooks.c @@ -0,0 +1,36 @@ +#include "saffron_api.h" +#include +#include +#include + +bool hook_callback(SDL_Event* event) { + printf("got an event!\n"); + return false; // we didnt consume the event +} + +bool hook_callback_2(SDL_Event* event) { + printf("second hook got an event!\n"); + return true; // we consumed the event +} + +bool hook_callback_3(SDL_Event* event) { + printf("you should never see this, a hook with higher priority (hook_callback_2) should have consumed this event\n"); + return false; +} + +int main() { + saffron_init(); + + SaffronWindow* window = saffron_window_new("Saffron hooks test", 800, 600); + + saffron_hook_sdl_all_events(window, hook_callback, 999); + saffron_hook_sdl_all_events(window, hook_callback_2, 2); + saffron_hook_sdl_all_events(window, hook_callback_3, 1); + + saffron_window_main(window); + + saffron_window_free(window); + saffron_quit(); + + return 0; +} diff --git a/tests/test_main.c b/tests/test_main.c index cb92189..087b32a 100644 --- a/tests/test_main.c +++ b/tests/test_main.c @@ -31,6 +31,10 @@ void my_test_onclick(SaffronWidget* self) { printf("clicked!\n"); } +static void button_click_handler(SaffronButton* self) { + printf("button clicked!\n"); +} + int main(void) { saffron_init(); @@ -61,13 +65,12 @@ int main(void) { TTF_Font* font = TTF_OpenFont("/usr/share/fonts/fantasque-sans-mono/FantasqueSansMono-Regular.otf", 24); SaffronText* test3 = saffron_text_new("Mangoes", font); - /* become lunatic, add custom clickhandler to.. text????????????? */ - ((SaffronWidget*)test3)->on_click = my_test_onclick; + SaffronButton* btn = saffron_button_new_with_text(test3, true, &button_click_handler, 200, 150); saffron_widget_add_child(window->root, test); saffron_widget_add_child(window->root, box); saffron_widget_add_child(box, test2); - saffron_widget_add_child(box, (SaffronWidget*)test3); + saffron_widget_add_child(window->root, (SaffronWidget*)btn); saffron_window_main(window); -- cgit v1.2.3