Compare commits

...

2 Commits

Author SHA1 Message Date
204508bd0d formatting pass 2024-03-23 23:29:26 -03:00
a988ce965c simplify rosenrot.c further 2024-03-23 23:22:00 -03:00
3 changed files with 94 additions and 159 deletions

View File

@ -5,7 +5,7 @@ This branch contains a bare-bones version of rosenrot, a webkit and gtk based br
- similar in many ways to the [original version of rose](https://github.com/mini-rose/rose-browser/tree/60173b6f5b562861b11dea17e3869ad6c3462bbb/src).
- But with the up to date libwebkit2gtk-4.1!
- compilable with [tcc](https://bellard.org/tcc/)! (also with gcc/clang)
- *One c file*, 390 lines (~356 excluding comments)
- *One c file*, 320 lines (~256 excluding comments & extraneous newlines)
- Still some minimal niceties: zoom, parsing more than one url from the command line, max number of tabs, plenty of shortcuts.
- Missing many quality of life features
- Meant for developers seeking to understand or replicate rosenrot

View File

@ -38,6 +38,10 @@ depsdebian:
STYLE_BLUEPRINT="{BasedOnStyle: webkit, AllowShortIfStatementsOnASingleLine: true, IndentCaseLabels: true, AllowShortEnumsOnASingleLine: true}"
FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT)
stats:
cat rosenrot.c | wc -l
gcc -fpreprocessed -dD -E -P rosenrot.c | wc -l
format: $(SRC)
$(FORMATTER) $(SRC)

View File

@ -29,60 +29,10 @@
// GTK
#define GTK_SETTINGS_CONFIG_H "gtk-application-prefer-dark-theme", 0, "gtk-enable-animations", 0
#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 CTRL 1 << 2
#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 */
static GtkNotebook* notebook;
static GtkWindow* window;
@ -221,75 +171,68 @@ void handle_signal_bar_press_enter(GtkEntry* self, GtkNotebook* notebook)
}
/* 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 bool is_fullscreen = 0;
if (event_state & CTRL) {
WebKitWebView* view = notebook_get_webview(notebook);
switch (id) {
case goback:
switch (event_keyval) {
case KEY(h): // go back
webkit_web_view_go_back(view);
break;
case goforward:
case KEY(j): // go forward
webkit_web_view_go_forward(view);
break;
case refresh:
case KEY(r): // reload
webkit_web_view_reload(view);
break;
case refresh_force:
case KEY(R): // force reload
webkit_web_view_reload_bypass_cache(view);
break;
case back_to_home:
case KEY(H): // back to home
load_uri(view, HOME);
break;
case zoomin:
webkit_web_view_set_zoom_level(view,
(zoom += ZOOM_VAL));
case KEY(equal): // zoom in
webkit_web_view_set_zoom_level(view, (zoom += ZOOM_VAL));
break;
case zoomout:
webkit_web_view_set_zoom_level(view,
(zoom -= ZOOM_VAL));
case KEY(minus): // zoom out
webkit_web_view_set_zoom_level(view, (zoom -= ZOOM_VAL));
break;
case zoom_reset:
webkit_web_view_set_zoom_level(view,
(zoom = ZOOM));
case KEY(0): // restore zoom
webkit_web_view_set_zoom_level(view, (zoom = ZOOM));
break;
case prev_tab:; // declarations aren't statements
// <https://stackoverflow.com/questions/92396/why-cant-variables-be-declared-in-a-switch-statement>
case KEY(KP_Page_Up): {
int n = gtk_notebook_get_n_pages(notebook);
int k = gtk_notebook_get_current_page(notebook);
int l = (n + k - 1) % n;
gtk_notebook_set_current_page(notebook, l);
break;
case next_tab:;
} // previous tab
case KEY(KP_Page_Down): { // next tab
int m = gtk_notebook_get_n_pages(notebook);
int i = gtk_notebook_get_current_page(notebook);
int j = (i + 1) % m;
gtk_notebook_set_current_page(notebook, j);
break;
case close_tab:
}
case KEY(w): // close tab
gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook));
num_tabs -= 1;
if (gtk_notebook_get_n_pages(notebook) == 0) {
exit(0);
}
break;
case toggle_fullscreen:
if (is_fullscreen)
gtk_window_unfullscreen(window);
else
gtk_window_fullscreen(window);
is_fullscreen = !is_fullscreen;
break;
case show_searchbar: {
case KEY(l): // show search bar
case KEY(semicolon): { // fallthrough; same
bar.entry_mode = _SEARCH;
const char* url = webkit_web_view_get_uri(notebook_get_webview(notebook));
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));
break;
}
case show_finder: {
case KEY(f): { // 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)));
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_window_set_focus(window, GTK_WIDGET(bar.line));
break;
}
case finder_next:
} break;
case KEY(n): // find next
webkit_find_controller_search_next(webkit_web_view_get_find_controller(view));
break;
case finder_prev:
case KEY(N): // find previous
webkit_find_controller_search_previous(webkit_web_view_get_find_controller(view));
break;
case new_tab:
case KEY(t): // new tab
notebook_create_new_tab(notebook, NULL);
break;
}
return 1;
} 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;
}
// 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;
}