1#ifndef WEBVIEW_CALLBACKS_H
2#define WEBVIEW_CALLBACKS_H
3
4#include <gtk/gtk.h>
5#include <webkit/webkit.h>
6#include <glib-object.h>
7
8// Forward declarations for Go callbacks
9extern void goOnTitleChanged(unsigned long id, char* title);
10extern void goOnURIChanged(unsigned long id, char* uri);
11extern void goOnThemeChanged(unsigned long id, int prefer_dark);
12extern void* goResolveURIScheme(char* uri, size_t* out_len, char** out_mime);
13extern void goQuitMainLoop();
14
15// Static inline functions to avoid multiple definition errors
16static inline void on_title_notify(GObject* obj, GParamSpec* pspec, gpointer user_data) {
17 (void)pspec;
18 WebKitWebView* view = WEBKIT_WEB_VIEW(obj);
19 const gchar* title = webkit_web_view_get_title(view);
20 GtkWindow* win = GTK_WINDOW(user_data);
21 if (title && win) {
22 gtk_window_set_title(win, title);
23 }
24}
25
26static inline void on_title_notify_id(GObject* obj, GParamSpec* pspec, gpointer user_data) {
27 (void)pspec; (void)obj;
28 const gchar* title = webkit_web_view_get_title(WEBKIT_WEB_VIEW(obj));
29 if (title) { goOnTitleChanged((unsigned long)user_data, (char*)title); }
30}
31
32static inline void on_uri_notify(GObject* obj, GParamSpec* pspec, gpointer user_data) {
33 (void)pspec; (void)obj;
34 const gchar* uri = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(obj));
35 if (uri) { goOnURIChanged((unsigned long)user_data, (char*)uri); }
36}
37
38static inline void on_theme_changed(GObject* obj, GParamSpec* pspec, gpointer user_data) {
39 (void)pspec;
40 GtkSettings* settings = GTK_SETTINGS(obj);
41 if (!settings) return;
42 gboolean prefer = FALSE;
43 g_object_get(settings, "gtk-application-prefer-dark-theme", &prefer, NULL);
44 goOnThemeChanged((unsigned long)user_data, prefer ? 1 : 0);
45}
46
47static inline void on_uri_scheme(WebKitURISchemeRequest* request, gpointer user_data) {
48 (void)user_data;
49 const gchar* uri = webkit_uri_scheme_request_get_uri(request);
50 size_t n = 0;
51 char* mime = NULL;
52 void* buf = goResolveURIScheme((char*)uri, &n, &mime);
53 if (buf && n > 0 && mime) {
54 GInputStream* stream = g_memory_input_stream_new_from_data(buf, (gssize)n, g_free);
55 webkit_uri_scheme_request_finish(request, stream, (gint64)n, mime);
56 g_object_unref(stream);
57 g_free(mime);
58 } else {
59 GError* err = g_error_new_literal(g_quark_from_string("dumber"), 404, "Not found");
60 webkit_uri_scheme_request_finish_error(request, err);
61 g_error_free(err);
62 }
63}
64
65// GTK4 close-request signal handler
66static inline gboolean on_close_request(GtkWindow* window, gpointer user_data) {
67 (void)window; (void)user_data;
68 goQuitMainLoop();
69 return FALSE; // Allow the window to close
70}
71
72#endif // WEBVIEW_CALLBACKS_H