formatting & tweaks pass

This commit is contained in:
NunoSempere 2024-03-23 17:48:42 -03:00
parent 978c7ca1cc
commit 5097a1982b
6 changed files with 174 additions and 203 deletions

View File

@ -19,7 +19,7 @@ include plugins/plugins.mk
# PLUGINS=./plugins/stand_in/stand_in.c # PLUGINS=./plugins/stand_in/stand_in.c
## Formatter ## Formatter
STYLE_BLUEPRINT="{BasedOnStyle: webkit, AllowShortIfStatementsOnASingleLine: true}" STYLE_BLUEPRINT="{BasedOnStyle: webkit, AllowShortIfStatementsOnASingleLine: true, IndentCaseLabels: true}"
FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT) FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT)
# Runtime files # Runtime files

View File

@ -1,8 +1,5 @@
#ifndef READABILITY #pragma once
#define READABILITY
#define READABILITY_N 88067 + 1000 #define READABILITY_N 88067 + 1000
void read_readability_js(char* string); void read_readability_js(char* string);
#endif

View File

@ -1,2 +1,4 @@
#pragma once
void str_init(char* str, int n); void str_init(char* str, int n);
int str_replace_start(const char* string, const char* target, const char* replacement, char* output); int str_replace_start(const char* string, const char* target, const char* replacement, char* output);

View File

@ -1,8 +1,5 @@
#ifndef STYLE #pragma once
#define STYLE
#define STYLE_N 7624 + 1000 #define STYLE_N 7624 + 1000
void read_style_js(char* string); void read_style_js(char* string);
#endif

View File

@ -41,7 +41,6 @@ void load_uri(WebKitWebView* view, const char* uri)
char tmp[strlen("https://") + strlen(uri)]; char tmp[strlen("https://") + strlen(uri)];
snprintf(tmp, sizeof(tmp), "https://%s", uri); snprintf(tmp, sizeof(tmp), "https://%s", uri);
webkit_web_view_load_uri(view, tmp); webkit_web_view_load_uri(view, tmp);
} else { } else {
// Check for shortcuts // Check for shortcuts
int l = SHORTCUT_N + strlen(uri) + 1; int l = SHORTCUT_N + strlen(uri) + 1;
@ -68,10 +67,7 @@ void redirect_if_annoying(WebKitWebView* view, const char* uri)
str_init(uri_filtered, l); str_init(uri_filtered, l);
int check = libre_redirect(uri, uri_filtered); int check = libre_redirect(uri, uri_filtered);
if (check == 2) webkit_web_view_load_uri(view, uri_filtered);
if (check == 2) {
webkit_web_view_load_uri(view, uri_filtered);
}
} }
} }
void set_custom_style(WebKitWebView* view) void set_custom_style(WebKitWebView* view)
@ -90,16 +86,11 @@ void handle_signal_load_changed(WebKitWebView* self, WebKitLoadEvent load_event,
/* see <https://webkitgtk.org/reference/webkit2gtk/2.5.1/WebKitWebView.html> /* see <https://webkitgtk.org/reference/webkit2gtk/2.5.1/WebKitWebView.html>
*/ */
case WEBKIT_LOAD_STARTED: case WEBKIT_LOAD_STARTED:
case WEBKIT_LOAD_COMMITTED:
set_custom_style(self); set_custom_style(self);
redirect_if_annoying(self, webkit_web_view_get_uri(self));
break;
case WEBKIT_LOAD_REDIRECTED: case WEBKIT_LOAD_REDIRECTED:
redirect_if_annoying(self, webkit_web_view_get_uri(self)); redirect_if_annoying(self, webkit_web_view_get_uri(self));
break; break;
case WEBKIT_LOAD_COMMITTED:
redirect_if_annoying(self, webkit_web_view_get_uri(self));
set_custom_style(self);
break;
case WEBKIT_LOAD_FINISHED: { case WEBKIT_LOAD_FINISHED: {
/* Add gtk tab title */ /* Add gtk tab title */
const char* webpage_title = webkit_web_view_get_title(self); const char* webpage_title = webkit_web_view_get_title(self);
@ -137,11 +128,10 @@ GtkWidget* handle_signal_create_new_tab(WebKitWebView* self,
printf("Creating new window: %s\n", uri); printf("Creating new window: %s\n", uri);
notebook_create_new_tab(notebook, uri); notebook_create_new_tab(notebook, uri);
gtk_notebook_set_show_tabs(notebook, true); gtk_notebook_set_show_tabs(notebook, true);
return NULL;
} else { } else {
webkit_web_view_evaluate_javascript(self, "alert('Too many tabs, not opening a new one')", -1, NULL, "rosenrot-alert-numtabs", NULL, NULL, NULL); webkit_web_view_evaluate_javascript(self, "alert('Too many tabs, not opening a new one')", -1, NULL, "rosenrot-alert-numtabs", NULL, NULL, NULL);
return NULL;
} }
return NULL;
/* /*
WebKitGTK documentation recommends returning the new webview. WebKitGTK documentation recommends returning the new webview.
I imagine that this might allow e.g., to go back in a new tab I imagine that this might allow e.g., to go back in a new tab
@ -171,25 +161,20 @@ WebKitWebView* create_new_webview()
contentmanager = webkit_user_content_manager_new(); contentmanager = webkit_user_content_manager_new();
cookiemanager = webkit_web_context_get_cookie_manager(web_context); cookiemanager = webkit_web_context_get_cookie_manager(web_context);
webkit_cookie_manager_set_persistent_storage( webkit_cookie_manager_set_persistent_storage(cookiemanager, DATA_DIR "/cookies.sqlite", WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE);
cookiemanager, DATA_DIR "/cookies.sqlite",
WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE);
webkit_cookie_manager_set_accept_policy(cookiemanager, webkit_cookie_manager_set_accept_policy(cookiemanager, WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS);
WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS);
if (g_file_get_contents("~/.config/rose/style.css", &style, NULL, NULL)) if (g_file_get_contents("~/.config/rose/style.css", &style, NULL, NULL)) {
webkit_user_content_manager_add_style_sheet( webkit_user_content_manager_add_style_sheet(
contentmanager, webkit_user_style_sheet_new(style, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, WEBKIT_USER_STYLE_LEVEL_USER, NULL, NULL)); contentmanager, webkit_user_style_sheet_new(style, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, WEBKIT_USER_STYLE_LEVEL_USER, NULL, NULL));
}
return g_object_new(WEBKIT_TYPE_WEB_VIEW, "settings", settings, "web-context", return g_object_new(WEBKIT_TYPE_WEB_VIEW, "settings", settings, "web-context", web_context, "user-content-manager", contentmanager, NULL);
web_context, "user-content-manager", contentmanager,
NULL);
} }
void notebook_create_new_tab(GtkNotebook* notebook, const char* uri) void notebook_create_new_tab(GtkNotebook* notebook, const char* uri)
{ {
if (num_tabs < MAX_NUM_TABS || MAX_NUM_TABS == 0) { if (num_tabs < MAX_NUM_TABS || MAX_NUM_TABS == 0) {
WebKitWebView* view = create_new_webview(); WebKitWebView* view = create_new_webview();
g_signal_connect(view, "load_changed", G_CALLBACK(handle_signal_load_changed), notebook); g_signal_connect(view, "load_changed", G_CALLBACK(handle_signal_load_changed), notebook);
@ -208,7 +193,6 @@ void notebook_create_new_tab(GtkNotebook* notebook, const char* uri)
webkit_web_view_set_zoom_level(view, ZOOM); webkit_web_view_set_zoom_level(view, ZOOM);
num_tabs += 1; num_tabs += 1;
} else { } else {
webkit_web_view_evaluate_javascript(notebook_get_webview(notebook), "alert('Too many tabs, not opening a new one')", -1, NULL, "rosenrot-alert-numtabs", NULL, NULL, NULL); webkit_web_view_evaluate_javascript(notebook_get_webview(notebook), "alert('Too many tabs, not opening a new one')", -1, NULL, "rosenrot-alert-numtabs", NULL, NULL, NULL);
} }
} }
@ -241,7 +225,6 @@ void toggle_bar(GtkNotebook* notebook)
// fallthrough // fallthrough
case _HIDDEN: case _HIDDEN:
gtk_widget_hide(GTK_WIDGET(bar.widget)); gtk_widget_hide(GTK_WIDGET(bar.widget));
break; // not needed now, but might reorder
} }
} }
// Handle what happens when the user is on the bar and presses enter // Handle what happens when the user is on the bar and presses enter
@ -292,12 +275,10 @@ int handle_shortcut(func id, GtkNotebook* notebook)
webkit_web_view_set_zoom_level(view, webkit_web_view_set_zoom_level(view,
(zoom += ZOOM_VAL)); (zoom += ZOOM_VAL));
break; break;
case zoomout: case zoomout:
webkit_web_view_set_zoom_level(view, webkit_web_view_set_zoom_level(view,
(zoom -= ZOOM_VAL)); (zoom -= ZOOM_VAL));
break; break;
case zoom_reset: case zoom_reset:
webkit_web_view_set_zoom_level(view, webkit_web_view_set_zoom_level(view,
(zoom = ZOOM)); (zoom = ZOOM));
@ -310,14 +291,12 @@ int handle_shortcut(func id, GtkNotebook* notebook)
int l = (n + k - 1) % n; int l = (n + k - 1) % n;
gtk_notebook_set_current_page(notebook, l); gtk_notebook_set_current_page(notebook, l);
break; break;
case next_tab:; case next_tab:;
int m = gtk_notebook_get_n_pages(notebook); int m = gtk_notebook_get_n_pages(notebook);
int i = gtk_notebook_get_current_page(notebook); int i = gtk_notebook_get_current_page(notebook);
int j = (i + 1) % m; int j = (i + 1) % m;
gtk_notebook_set_current_page(notebook, j); gtk_notebook_set_current_page(notebook, j);
break; break;
case close_tab: case close_tab:
gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook)); gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook));
num_tabs -= 1; num_tabs -= 1;
@ -346,7 +325,6 @@ int handle_shortcut(func id, GtkNotebook* notebook)
bar.entry_mode = _SEARCH; bar.entry_mode = _SEARCH;
toggle_bar(notebook); toggle_bar(notebook);
break; break;
case show_finder: case show_finder:
bar.entry_mode = _FIND; bar.entry_mode = _FIND;
toggle_bar(notebook); toggle_bar(notebook);
@ -356,7 +334,6 @@ int handle_shortcut(func id, GtkNotebook* notebook)
webkit_find_controller_search_next( webkit_find_controller_search_next(
webkit_web_view_get_find_controller(view)); webkit_web_view_get_find_controller(view));
break; break;
case finder_prev: case finder_prev:
webkit_find_controller_search_previous( webkit_find_controller_search_previous(
webkit_web_view_get_find_controller(view)); webkit_web_view_get_find_controller(view));
@ -462,9 +439,7 @@ int main(int argc, char** argv)
/* Show to user */ /* Show to user */
gtk_widget_show_all(GTK_WIDGET(window)); gtk_widget_show_all(GTK_WIDGET(window));
if (argc != 0) { if (argc != 0) gtk_widget_hide(GTK_WIDGET(bar.widget));
gtk_widget_hide(GTK_WIDGET(bar.widget));
}
/* Deal with more tabs */ /* Deal with more tabs */
if (argc > 2) { if (argc > 2) {