simplify rosenrot.c further

This commit is contained in:
NunoSempere 2024-03-23 23:22:00 -03:00
parent 20c6be16b0
commit a988ce965c

View File

@ -29,60 +29,10 @@
// GTK // GTK
#define GTK_SETTINGS_CONFIG_H "gtk-application-prefer-dark-theme", 0, "gtk-enable-animations", 0 #define GTK_SETTINGS_CONFIG_H "gtk-application-prefer-dark-theme", 0, "gtk-enable-animations", 0
#define KEY(x) GDK_KEY_##x #define KEY(x) GDK_KEY_##x
// Shortcuts
typedef enum {
goback,
goforward,
refresh,
refresh_force,
back_to_home,
toggle_fullscreen,
zoomin,
zoomout,
zoom_reset,
new_tab,
next_tab,
prev_tab,
close_tab,
show_searchbar,
show_finder,
finder_next,
finder_prev,
prettify,
hide_bar
} func;
#define SFT 1 << 0 #define SFT 1 << 0
#define CTRL 1 << 2 #define CTRL 1 << 2
#define ALT 1 << 3 #define ALT 1 << 3
static struct {
unsigned mod;
unsigned key;
func id;
} shortcut[] = {
{ CTRL, KEY(h), goback },
{ CTRL, KEY(j), goforward },
{ CTRL, KEY(r), refresh },
{ CTRL, KEY(R), refresh_force },
{ CTRL, KEY(H), back_to_home },
{ CTRL, KEY(equal), zoomin },
{ CTRL, KEY(minus), zoomout },
{ CTRL, KEY(0), zoom_reset },
{ CTRL, KEY(KP_Page_Up), prev_tab }, /* also try KEY(Page_Up) if this doesn't work on your machine */
{ CTRL, KEY(KP_Page_Down), next_tab }, /* ditto for KEY(Page_Down) */
{ CTRL, KEY(t), new_tab },
{ CTRL, KEY(w), close_tab },
{ 0x0, KEY(F11), toggle_fullscreen },
{ CTRL, KEY(l), show_searchbar },
{ CTRL, KEY(semicolon), hide_bar },
{ CTRL, KEY(f), show_finder },
{ CTRL, KEY(n), finder_next },
{ CTRL, KEY(N), finder_prev },
{ CTRL, KEY(p), prettify }
};
/* Global declarations */ /* Global declarations */
static GtkNotebook* notebook; static GtkNotebook* notebook;
static GtkWindow* window; static GtkWindow* window;
@ -221,75 +171,68 @@ void handle_signal_bar_press_enter(GtkEntry* self, GtkNotebook* notebook)
} }
/* Handle shortcuts */ /* Handle shortcuts */
int handle_shortcut(func id, GtkNotebook* notebook) int handle_signal_keypress(void* self, GdkEvent* event, GtkNotebook* notebook)
{ {
// (void)self;
guint event_keyval = 0;
gdk_event_get_keyval(event, &event_keyval);
GdkModifierType event_state = 0;
gdk_event_get_state(event, &event_state);
static double zoom = ZOOM; static double zoom = ZOOM;
static bool is_fullscreen = 0; static bool is_fullscreen = 0;
if (event_state & CTRL) {
WebKitWebView* view = notebook_get_webview(notebook); WebKitWebView* view = notebook_get_webview(notebook);
switch (event_keyval) {
switch (id) { case KEY(h): // go back
case goback:
webkit_web_view_go_back(view); webkit_web_view_go_back(view);
break; break;
case goforward: case KEY(j): // go forward
webkit_web_view_go_forward(view); webkit_web_view_go_forward(view);
break; break;
case KEY(r): // reload
case refresh:
webkit_web_view_reload(view); webkit_web_view_reload(view);
break; break;
case refresh_force: case KEY(R): // force reload
webkit_web_view_reload_bypass_cache(view); webkit_web_view_reload_bypass_cache(view);
break; break;
case KEY(H): // back to home
case back_to_home:
load_uri(view, HOME); load_uri(view, HOME);
break; break;
case KEY(equal): // zoom in
case zoomin: webkit_web_view_set_zoom_level(view, (zoom += ZOOM_VAL));
webkit_web_view_set_zoom_level(view,
(zoom += ZOOM_VAL));
break; break;
case zoomout: case KEY(minus): // zoom out
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 KEY(0): // restore zoom
webkit_web_view_set_zoom_level(view, webkit_web_view_set_zoom_level(view, (zoom = ZOOM));
(zoom = ZOOM));
break; break;
case KEY(KP_Page_Up):{
case prev_tab:; // declarations aren't statements
// <https://stackoverflow.com/questions/92396/why-cant-variables-be-declared-in-a-switch-statement>
int n = gtk_notebook_get_n_pages(notebook); int n = gtk_notebook_get_n_pages(notebook);
int k = gtk_notebook_get_current_page(notebook); int k = gtk_notebook_get_current_page(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:; } // previous tab
case KEY(KP_Page_Down): { // 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 KEY(w): // 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;
if (gtk_notebook_get_n_pages(notebook) == 0) { if (gtk_notebook_get_n_pages(notebook) == 0) {
exit(0); exit(0);
} }
break; break;
case KEY(l): // show search bar
case toggle_fullscreen: case KEY(semicolon): { // fallthrough; same
if (is_fullscreen)
gtk_window_unfullscreen(window);
else
gtk_window_fullscreen(window);
is_fullscreen = !is_fullscreen;
break;
case show_searchbar: {
bar.entry_mode = _SEARCH; bar.entry_mode = _SEARCH;
const char* url = webkit_web_view_get_uri(notebook_get_webview(notebook)); const char* url = webkit_web_view_get_uri(notebook_get_webview(notebook));
gtk_entry_set_placeholder_text(bar.line, "Search"); gtk_entry_set_placeholder_text(bar.line, "Search");
@ -298,8 +241,7 @@ int handle_shortcut(func id, GtkNotebook* notebook)
gtk_window_set_focus(window, GTK_WIDGET(bar.line)); gtk_window_set_focus(window, GTK_WIDGET(bar.line));
break; break;
} }
case show_finder: { case KEY(f): { // find
bar.entry_mode = _FIND; bar.entry_mode = _FIND;
const char* search_text = webkit_find_controller_get_search_text(webkit_web_view_get_find_controller(notebook_get_webview(notebook))); 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)); if (search_text != NULL) gtk_entry_buffer_set_text(bar.line_text, search_text, strlen(search_text));
@ -307,36 +249,25 @@ int handle_shortcut(func id, GtkNotebook* notebook)
gtk_entry_set_placeholder_text(bar.line, "Find"); gtk_entry_set_placeholder_text(bar.line, "Find");
gtk_window_set_focus(window, GTK_WIDGET(bar.line)); gtk_window_set_focus(window, GTK_WIDGET(bar.line));
break; break;
} } break;
case KEY(n): // find next
case finder_next:
webkit_find_controller_search_next(webkit_web_view_get_find_controller(view)); webkit_find_controller_search_next(webkit_web_view_get_find_controller(view));
break; break;
case finder_prev: case KEY(N): // find previous
webkit_find_controller_search_previous(webkit_web_view_get_find_controller(view)); webkit_find_controller_search_previous(webkit_web_view_get_find_controller(view));
break; break;
case KEY(t): // new tab
case new_tab:
notebook_create_new_tab(notebook, NULL); notebook_create_new_tab(notebook, NULL);
break; break;
} }
} else if(event_state == 0x0 && event_keyval == KEY(F11)){
if (is_fullscreen)
gtk_window_unfullscreen(window);
else
gtk_window_fullscreen(window);
is_fullscreen = !is_fullscreen;
}
return 1;
}
// Listen to key presses and call shortcuts if needed.
int handle_signal_keypress(void* self, GdkEvent* event, GtkNotebook* notebook)
{
(void)self;
guint event_keyval = 0;
gdk_event_get_keyval(event, &event_keyval);
GdkModifierType event_state = 0;
gdk_event_get_state(event, &event_state);
for (int i = 0; i < sizeof(shortcut) / sizeof(shortcut[0]); i++)
if ((event_state & shortcut[i].mod || shortcut[i].mod == 0x0) && event_keyval == shortcut[i].key)
return handle_shortcut(shortcut[i].id, notebook);
// This API is deprecated in GTK4 :(
return 0; return 0;
} }