GTK4: try to isolate signal bug

This commit is contained in:
NunoSempere 2024-07-21 09:14:14 -04:00
parent 42d09714a8
commit 7f1074b318
3 changed files with 170 additions and 3 deletions

View File

@ -20,7 +20,7 @@ LIBS_4=`pkg-config --libs ${DEPS_4}` `pkg-config --libs gtk4`
# DEPRECATION_FLAGS=-DGDK_DISABLE_DEPRECATED -DGTK_DISABLE_DEPRECATED
# Dependencies starting from scratchpad
SRC_4_greenfield=rosenrot4_greenfield.c
SRC_4_greenfield=rosenrot4_greenfield_minimal.c
DEPS_4_greenfield='webkitgtk-6.0'
INCS_4_greenfield=`pkg-config --cflags ${DEPS_4_greenfield}` `pkg-config --cflags gtk4`
LIBS_4_greenfield=`pkg-config --libs ${DEPS_4_greenfield}` `pkg-config --libs gtk4`
@ -81,7 +81,7 @@ user_cache:
runtime_files:
@echo
sudo mkdir -p /opt/rosenrot/
sudo cp style-gtk4.css /opt/rosenrot/style.css
sudo cp style-gtk3.css /opt/rosenrot/style.css
sudo cp -r images/flower-imgs /opt/rosenrot/
sudo cp plugins/style/style.js /opt/rosenrot/
sudo cp plugins/readability/readability.js /opt/rosenrot/

View File

@ -420,7 +420,7 @@ int main(int argc, char** argv)
g_signal_connect_object(event_controller_keypress, "key-pressed", G_CALLBACK(handle_signal_keypress), window, G_CONNECT_DEFAULT);
gtk_widget_add_controller(GTK_WIDGET(window), event_controller_keypress);
g_signal_connect(bar.line, "activate", G_CALLBACK(handle_signal_bar_press_enter), notebook);
g_signal_connect(bar.line, "activate", G_CALLBACK(handle_signal_bar_press_enter), NULL);
// I suspect there is something wonky going on here
// GtkEventController* event_controller_bar_press_enter;
// event_controller_bar_press_enter = gtk_event_controller_key_new();

View File

@ -0,0 +1,167 @@
#include <gdk/gdk.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "plugins/plugins.h"
#include <webkit/webkit.h>
static GtkNotebook* notebook;
static GtkWindow* window;
typedef enum { _SEARCH,
_FIND,
_HIDDEN } Bar_entry_mode;
static struct {
GtkHeaderBar* widget;
GtkEntry* line;
GtkEntryBuffer* line_text;
Bar_entry_mode entry_mode;
} bar;
#define NOTNULL(x) \
do { \
if (x == NULL) { \
printf("\nNull found"); \
printf("@ %s (%d): ", __FILE__, __LINE__); \
exit(0); \
} \
} while (0)
/* Utils */
WebKitWebView* notebook_get_webview(GtkNotebook* notebook)
{
WebKitWebView* view = WEBKIT_WEB_VIEW(gtk_notebook_get_nth_page(notebook, gtk_notebook_get_current_page(notebook)));
NOTNULL(view);
return view;
}
/* Handle shortcuts */
int handle_shortcut(func id)
{
WebKitWebView* view = notebook_get_webview(notebook);
NOTNULL(notebook);
NOTNULL(view);
switch (id) {
case show_searchbar:
gtk_widget_set_visible(GTK_WIDGET(bar.widget), 1);
break;
case hide_bar:
gtk_widget_set_visible(GTK_WIDGET(bar.widget), 0);
break;
}
return 1;
}
/* Listen to keypresses */
static gboolean handle_signal_keypress(GtkWidget* w,
guint keyval,
guint keycode,
GdkModifierType state,
GtkEventControllerKey* event_controller_keypress)
{
fprintf(stdout, "New keypress!\n");
if (1) {
printf("Keypress state: %d\n", state);
printf("Keypress value: %d\n", keyval);
}
for (int i = 0; i < sizeof(shortcut) / sizeof(shortcut[0]); i++){
if ((state & shortcut[i].mod || shortcut[i].mod == 0x0) && keyval == shortcut[i].key) {
return handle_shortcut(shortcut[i].id);
}
}
return 0;
}
WebKitWebView* create_new_webview()
{
WebKitWebView* view = g_object_new(WEBKIT_TYPE_WEB_VIEW, NULL);
NOTNULL(view);
return view;
}
void notebook_create_first_tab(GtkNotebook* notebook, const char* uri)
{
WebKitWebView* view = create_new_webview();
int n = gtk_notebook_append_page(notebook, GTK_WIDGET(view), NULL);
gtk_notebook_set_tab_reorderable(notebook, GTK_WIDGET(view), true);
gtk_widget_set_visible(GTK_WIDGET(window), 1);
gtk_widget_hide(GTK_WIDGET(bar.widget));
webkit_web_view_load_uri(view, (uri) ? uri : HOME);
gtk_notebook_set_current_page(notebook, n);
gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(view), "-");
}
int main(int argc, char** argv)
{
/* Initialize GTK in general */
gtk_init();
// Create the main window
window = GTK_WINDOW(gtk_window_new());
gtk_window_set_default_size(window, WIDTH, HEIGHT_4);
// Set up notebook
notebook = GTK_NOTEBOOK(gtk_notebook_new());
gtk_notebook_set_show_tabs(notebook, true);
gtk_notebook_set_show_border(notebook, false);
gtk_window_set_child(window, GTK_WIDGET(notebook));
// Bar
bar.line_text = GTK_ENTRY_BUFFER(gtk_entry_buffer_new("", 0));
bar.line = GTK_ENTRY(gtk_entry_new_with_buffer(bar.line_text));
gtk_entry_set_alignment(bar.line, 0.48);
gtk_widget_set_size_request(GTK_WIDGET(bar.line), BAR_SIZE, -1);
bar.widget = GTK_HEADER_BAR(gtk_header_bar_new());
gtk_header_bar_set_title_widget(bar.widget, GTK_WIDGET(bar.line));
gtk_window_set_titlebar(window, GTK_WIDGET(bar.widget));
// Listen to signals
// g_signal_connect(window, "destroy", G_CALLBACK(exit), notebook);
GtkEventController* event_controller_keypress = gtk_event_controller_key_new();
g_signal_connect_object(event_controller_keypress, "key-pressed", G_CALLBACK(handle_signal_keypress), notebook, G_CONNECT_DEFAULT);
gtk_widget_add_controller(GTK_WIDGET(notebook), event_controller_keypress);
// g_signal_connect(bar.line, "activate", G_CALLBACK(handle_signal_bar_press_enter), NULL);
// I suspect there is something wonky going on here
// GtkEventController* event_controller_bar_press_enter;
// event_controller_bar_press_enter = gtk_event_controller_key_new();
// g_signal_connect_object(event_controller_bar_press_enter, "key-pressed", G_CALLBACK(handle_signal_bar_press_enter), notebook, G_CONNECT_DEFAULT);
// gtk_widget_add_controller(GTK_WIDGET(bar.line), event_controller_bar_press_enter);
// Show the application window
gtk_window_present(window);
char* first_uri = argc > 1 ? argv[1] : HOME;
notebook_create_first_tab(notebook, first_uri);
/* Show to user */
gtk_widget_set_visible(GTK_WIDGET(window), 1);
if (argc != 0) gtk_widget_set_visible(GTK_WIDGET(bar.widget), 0);
/* Deal with more tabs */
if (argc > 2) {
gtk_notebook_set_show_tabs(notebook, true);
for (int i = 2; i < argc; i++) {
notebook_create_first_tab(notebook, argv[i]);
}
}
// Enter the main event loop, and wait for user interaction
printf("\nEntering main loop");
while (g_list_model_get_n_items(gtk_window_get_toplevels()) > 0)
g_main_context_iteration(NULL, TRUE);
return 0;
}