From 805fd6d45afe6b647e089f251fc4f40c05f903dd Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sat, 20 Jul 2024 22:19:16 -0400 Subject: [PATCH] GTK4: add top bar --- rosenrot4_greenfield.c | 90 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 85 insertions(+), 5 deletions(-) diff --git a/rosenrot4_greenfield.c b/rosenrot4_greenfield.c index 17cf690..33635fe 100644 --- a/rosenrot4_greenfield.c +++ b/rosenrot4_greenfield.c @@ -24,17 +24,88 @@ static int custom_style_enabled = 1; void toggle_bar(GtkNotebook* notebook, Bar_entry_mode mode); void notebook_create_new_tab(GtkNotebook* notebook, const char* uri); +/* Utils */ +WebKitWebView* notebook_get_webview(GtkNotebook* notebook) +{ + return WEBKIT_WEB_VIEW(gtk_notebook_get_nth_page(notebook, gtk_notebook_get_current_page(notebook))); +} + /* Load content */ void load_uri(WebKitWebView* view, const char* uri) { if (strlen(uri) == 0) { webkit_web_view_load_uri(view, ""); - // toggle_bar(notebook, _SEARCH); + toggle_bar(notebook, _SEARCH); } else if (g_str_has_prefix(uri, "http://") || g_str_has_prefix(uri, "https://") || g_str_has_prefix(uri, "file://") || g_str_has_prefix(uri, "about:")) { webkit_web_view_load_uri(view, uri); + } else if (strstr(uri, ".com") || strstr(uri, ".org")) { + char tmp[strlen("https://") + strlen(uri) + 1]; + snprintf(tmp, sizeof(tmp) + 1, "https://%s", uri); + webkit_web_view_load_uri(view, tmp); + } else { + // Check for shortcuts + int l = SHORTCUT_N + strlen(uri) + 1; + char uri_expanded[l]; + str_init(uri_expanded, l); + int check = shortcut_expand(uri, uri_expanded); + if (check == 2) { + webkit_web_view_load_uri(view, uri_expanded); + } else { + // Feed into search engine. + char tmp[strlen(uri) + strlen(SEARCH)]; + snprintf(tmp, sizeof(tmp), SEARCH, uri); + webkit_web_view_load_uri(view, tmp); + } } } +/* Top bar */ +void toggle_bar(GtkNotebook* notebook, Bar_entry_mode mode) +{ + bar.entry_mode = mode; + switch (bar.entry_mode) { + case _SEARCH: { + const char* url = webkit_web_view_get_uri(notebook_get_webview(notebook)); + gtk_entry_set_placeholder_text(bar.line, "Search"); + gtk_entry_buffer_set_text(bar.line_text, url, strlen(url)); + gtk_widget_show(GTK_WIDGET(bar.widget)); + gtk_window_set_focus(window, GTK_WIDGET(bar.line)); + break; + } + case _FIND: { + const char* search_text = webkit_find_controller_get_search_text( + webkit_web_view_get_find_controller(notebook_get_webview(notebook))); + + if (search_text != NULL) + gtk_entry_buffer_set_text(bar.line_text, search_text, strlen(search_text)); + + gtk_entry_set_placeholder_text(bar.line, "Find"); + gtk_widget_show(GTK_WIDGET(bar.widget)); + gtk_window_set_focus(window, GTK_WIDGET(bar.line)); + break; + } + case _HIDDEN: + gtk_widget_hide(GTK_WIDGET(bar.widget)); + } +} + +// Handle what happens when the user is on the bar and presses enter +void handle_signal_bar_press_enter(GtkEntry* self, GtkNotebook* notebook) +{ + if (bar.entry_mode == _SEARCH) + load_uri(notebook_get_webview(notebook), gtk_entry_buffer_get_text(bar.line_text)); + else if (bar.entry_mode == _FIND) + webkit_find_controller_search( + webkit_web_view_get_find_controller(notebook_get_webview(notebook)), + gtk_entry_buffer_get_text(bar.line_text), + WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE | WEBKIT_FIND_OPTIONS_WRAP_AROUND, + G_MAXUINT); + + gtk_widget_hide(GTK_WIDGET(bar.widget)); +} + + + /* Create new tabs */ WebKitWebView* create_new_webview() { @@ -172,12 +243,22 @@ int main(int argc, char** argv) window = gtk_window_new(); gtk_window_set_default_size(GTK_WINDOW(window), WIDTH, HEIGHT_4); - // Set up our GUI elements - + // 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(GTK_WINDOW(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); + // g_signal_connect(bar.line, "activate", G_CALLBACK(handle_signal_bar_press_enter), notebook); + + 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 @@ -189,7 +270,6 @@ int main(int argc, char** argv) // Show the application window gtk_window_present(GTK_WINDOW(window)); - gtk_window_set_child(GTK_WINDOW(window), GTK_WIDGET(notebook)); char* first_uri = argc > 1 ? argv[1] : HOME; notebook_create_new_tab(notebook, first_uri);