aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorArslaan Pathan <[email protected]>2026-05-01 21:38:49 +1200
committerArslaan Pathan <[email protected]>2026-05-01 21:38:49 +1200
commit6ad244502d1c3c09a26012c5e7fad901c95cc38e (patch)
treeb0b8106fca78a49e47313f35d8245eeb6980d3ac /src
parentccb8f9316c9bc7aded2dd5d74d2e82445bf2f647 (diff)
downloadsaffron-6ad244502d1c3c09a26012c5e7fad901c95cc38e.tar.xz
saffron-6ad244502d1c3c09a26012c5e7fad901c95cc38e.zip
Fix the buttons and implement hooks for future webkit bindings to build on top of
Diffstat (limited to 'src')
-rw-r--r--src/saffron_button.c37
-rw-r--r--src/saffron_event_hooks.c19
-rw-r--r--src/saffron_window.c6
3 files changed, 56 insertions, 6 deletions
diff --git a/src/saffron_button.c b/src/saffron_button.c
index bef0018..b7db204 100644
--- a/src/saffron_button.c
+++ b/src/saffron_button.c
@@ -7,9 +7,9 @@ static void saffron_button_draw(SaffronWidget* widget, SDL_Renderer* renderer) {
SaffronTheme* theme = widget->theme;
if (!theme) return;
- SaffronColor secondary = theme->secondary;
+ SaffronColor bg = theme->bg;
SaffronColor tertiary = theme->tertiary;
- SDL_SetRenderDrawColor(renderer, secondary.r, secondary.g, secondary.b, secondary.a);
+ SDL_SetRenderDrawColor(renderer, bg.r, bg.g, bg.b, bg.a);
SDL_FRect rect = {widget->x, widget->y, widget->w, widget->h};
SDL_RenderFillRect(renderer, &rect);
@@ -17,16 +17,41 @@ static void saffron_button_draw(SaffronWidget* widget, SDL_Renderer* renderer) {
SDL_RenderRect(renderer, &rect);
}
+static void saffron_button_onclick(SaffronWidget* self) {
+ SaffronButton* btn = (SaffronButton*)self;
+ btn->callback(btn);
+}
+
SaffronButton* saffron_button_new(bool enabled, void (*callback)(SaffronButton* self), int width, int height) {
SaffronButton* button = malloc(sizeof(SaffronButton));
- saffron_widget_init((SaffronWidget*)button);
+ if (!button) return NULL;
+
+ SaffronBox* box = saffron_box_new(SAFFRON_ORIENTATION_HORIZONTAL, SAFFRON_HALIGN_CENTER, SAFFRON_VALIGN_CENTER, 5, 5, 0, width, height);
+ if (!box) {
+ free(button);
+ return NULL;
+ }
+
+ memcpy(&button->base, box, sizeof(SaffronBox));
+ free(box);
- ((SaffronWidget*)button)->type = SAFFRON_WIDGET_BUTTON;
- ((SaffronWidget*)button)->draw = saffron_button_draw;
+ SaffronWidget* widget = (SaffronWidget*)button;
+ // We set it to type BOX because the layout engine needs to treat it as one: I'll remove the BUTTON enum later or make the layout engine check for both
+ widget->type = SAFFRON_WIDGET_BOX;
+ widget->draw = saffron_button_draw;
+ widget->on_click = saffron_button_onclick;
+ widget->children = NULL;
+ widget->child_count = 0;
button->callback = callback;
button->enabled = enabled;
- button->base = *saffron_box_new(SAFFRON_ORIENTATION_HORIZONTAL, SAFFRON_HALIGN_CENTER, SAFFRON_VALIGN_CENTER, 5, 5, 0, width, height);
+
+ return button;
+}
+
+SaffronButton* saffron_button_new_with_text(SaffronText* text, bool enabled, void (*callback)(SaffronButton* self), int width, int height) {
+ SaffronButton* button = saffron_button_new(enabled, callback, width, height);
+ saffron_widget_add_child((SaffronWidget*)button, (SaffronWidget*)text);
return button;
}
diff --git a/src/saffron_event_hooks.c b/src/saffron_event_hooks.c
new file mode 100644
index 0000000..d3a2c43
--- /dev/null
+++ b/src/saffron_event_hooks.c
@@ -0,0 +1,19 @@
+#include "saffron_api.h"
+#include <SDL3/SDL.h>
+#include <saffron.h>
+#include <saffron_event_hooks.h>
+
+void saffron_hook_sdl_all_events(SaffronWindow* window, bool (*callback)(SDL_Event* event), int priority) {
+ window->hooks[window->hook_count].callback = callback;
+ window->hooks[window->hook_count].priority = priority;
+ window->hook_count++;
+ for (int i = 0; i < window->hook_count; i++) {
+ for (int j = i + 1; j < window->hook_count; j++) {
+ if (window->hooks[j].priority > window->hooks[i].priority) {
+ SfInternalEventHook tmp = window->hooks[i];
+ window->hooks[i] = window->hooks[j];
+ window->hooks[j] = tmp;
+ }
+ }
+ }
+}
diff --git a/src/saffron_window.c b/src/saffron_window.c
index 41bc050..34da341 100644
--- a/src/saffron_window.c
+++ b/src/saffron_window.c
@@ -14,6 +14,8 @@ SaffronWindow* saffron_window_new(const char* title, int w, int h) {
window->title = title;
window->w = w;
window->h = h;
+ memset(window->hooks, 0, sizeof(window->hooks));
+ window->hook_count = 0;
Uint32 flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY;
window->sdl_window = SDL_CreateWindow(title, w, h, flags);
@@ -81,6 +83,10 @@ void saffron_window_main(SaffronWindow *window) {
if (event.type == SDL_EVENT_WINDOW_RESIZED) {
handle_window_resized(&event, window);
}
+ for (int i = 0; i < window->hook_count; i++) {
+ bool consumed = window->hooks[i].callback(&event);
+ if (consumed) break;
+ }
}
SDL_SetRenderDrawColor(window->renderer, 0, 0, 0, 255);