From 2b9b8a9c7046f14f64cc2416d396540c4ce23123 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Mon, 4 Dec 2023 23:01:34 +0100 Subject: [PATCH 1/3] Add callback that clears highlighted links when inputbar is hidden Since hiding the inputbar marks the end of the `follow a link` action, clearing all links at that event ensures that it will work in all possible situations. --- zathura/callbacks.c | 21 +++++++++++++++++++++ zathura/callbacks.h | 8 ++++++++ 2 files changed, 29 insertions(+) diff --git a/zathura/callbacks.c b/zathura/callbacks.c index 6fa1cfc..9a2d38b 100644 --- a/zathura/callbacks.c +++ b/zathura/callbacks.c @@ -834,3 +834,24 @@ cb_gesture_zoom_scale_changed(GtkGestureZoom* UNUSED(self), gdouble scale, void* render_all(zathura); refresh_view(zathura); } + +void cb_hide_links(GtkWidget* widget, gpointer data) { + g_return_if_fail(widget != NULL); + g_return_if_fail(data != NULL); + + /* disconnect from signal */ + gulong handler_id = GPOINTER_TO_UINT(g_object_steal_data(G_OBJECT(widget), "handler_id")); + g_signal_handler_disconnect(G_OBJECT(widget), handler_id); + + zathura_t* zathura = data; + unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document); + for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) { + zathura_page_t* page = zathura_document_get_page(zathura->document, page_id); + if (page == NULL) { + continue; + } + + GtkWidget* page_widget = zathura_page_get_widget(zathura, page); + g_object_set(G_OBJECT(page_widget), "draw-links", FALSE, NULL); + } +} diff --git a/zathura/callbacks.h b/zathura/callbacks.h index c8520ee..786a8e4 100644 --- a/zathura/callbacks.h +++ b/zathura/callbacks.h @@ -285,4 +285,12 @@ void cb_gesture_zoom_begin(GtkGesture* self, GdkEventSequence* sequence, void* d void cb_gesture_zoom_scale_changed(GtkGestureZoom* self, gdouble scale, void* data); +/** + * Clears all highlighted links when the inputbar gets closed + * + * @param GtkWidget* Inputbar widget + * @param data The zathura instance + */ +void cb_hide_links(GtkWidget* widget, gpointer data); + #endif // CALLBACKS_H From a6859512b5ee60776319a5c98494dcd4f43445ea Mon Sep 17 00:00:00 2001 From: Andreas Perstinger Date: Thu, 20 Apr 2023 14:48:36 +0200 Subject: [PATCH 2/3] Refactor link shortcut functions The three link shortcut functions `sc_follow`, `sc_display_link` and `sc_copy_link` were almost identical. --- zathura/shortcuts.c | 72 ++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 44 deletions(-) diff --git a/zathura/shortcuts.c b/zathura/shortcuts.c index 2975db1..8836cd2 100644 --- a/zathura/shortcuts.c +++ b/zathura/shortcuts.c @@ -21,7 +21,7 @@ #include "adjustment.h" #include -/* Helper function; see sc_display_link and sc_follow. */ +/* Helper function for highlighting the links */ static bool draw_links(zathura_t* zathura) { @@ -55,6 +55,30 @@ draw_links(zathura_t* zathura) return show_links; } +/* Common code for sc_follow, sc_display_link and sc_copy_link */ +static bool +link_shortcuts(zathura_t* zathura, girara_callback_inputbar_activate_t callback, + const char* text) +{ + if (zathura->document == NULL || zathura->ui.session == NULL) { + return false; + } + + bool show_links = draw_links(zathura); + + /* ask for input */ + if (show_links == true) { + GtkWidget *inputbar = zathura->ui.session->gtk.inputbar; + gulong handler_id = g_signal_connect(inputbar, "hide", G_CALLBACK(cb_hide_links), zathura); + g_object_set_data(G_OBJECT(inputbar), "handler_id", GUINT_TO_POINTER(handler_id)); + + zathura_document_set_adjust_mode(zathura->document, ZATHURA_ADJUST_INPUTBAR); + girara_dialog(zathura->ui.session, text, FALSE, NULL, callback, zathura->ui.session); + } + + return false; +} + bool sc_abort(girara_session_t* session, girara_argument_t* UNUSED(argument), girara_event_t* UNUSED(event), unsigned int UNUSED(t)) @@ -133,21 +157,7 @@ sc_display_link(girara_session_t* session, girara_argument_t* UNUSED(argument), g_return_val_if_fail(session->global.data != NULL, false); zathura_t* zathura = session->global.data; - if (zathura->document == NULL || zathura->ui.session == NULL) { - return false; - } - - bool show_links = draw_links(zathura); - - /* ask for input */ - if (show_links) { - zathura_document_set_adjust_mode(zathura->document, ZATHURA_ADJUST_INPUTBAR); - girara_dialog(zathura->ui.session, "Display link:", FALSE, NULL, - cb_sc_display_link, - zathura->ui.session); - } - - return false; + return link_shortcuts(zathura, cb_sc_display_link, "Display Link: "); } bool @@ -158,21 +168,7 @@ sc_copy_link(girara_session_t* session, girara_argument_t* UNUSED(argument), g_return_val_if_fail(session->global.data != NULL, false); zathura_t* zathura = session->global.data; - if (zathura->document == NULL || zathura->ui.session == NULL) { - return false; - } - - bool show_links = draw_links(zathura); - - /* ask for input */ - if (show_links) { - zathura_document_set_adjust_mode(zathura->document, ZATHURA_ADJUST_INPUTBAR); - girara_dialog(zathura->ui.session, "Copy link:", FALSE, NULL, - cb_sc_copy_link, - zathura->ui.session); - } - - return false; + return link_shortcuts(zathura, cb_sc_copy_link, "Copy Link: "); } bool @@ -272,19 +268,7 @@ sc_follow(girara_session_t* session, girara_argument_t* UNUSED(argument), g_return_val_if_fail(session->global.data != NULL, false); zathura_t* zathura = session->global.data; - if (zathura->document == NULL || zathura->ui.session == NULL) { - return false; - } - - bool show_links = draw_links(zathura); - - /* ask for input */ - if (show_links == true) { - zathura_document_set_adjust_mode(zathura->document, ZATHURA_ADJUST_INPUTBAR); - girara_dialog(zathura->ui.session, "Follow link:", FALSE, NULL, cb_sc_follow, zathura->ui.session); - } - - return false; + return link_shortcuts(zathura, cb_sc_follow, "Follow Link: "); } bool From bb84bb4eaa257bb701ec93c4c051007368494b9c Mon Sep 17 00:00:00 2001 From: Andreas Perstinger Date: Thu, 20 Apr 2023 14:55:14 +0200 Subject: [PATCH 3/3] Refactor handle_links As soon as we found the link, we do not need to look any further. --- zathura/callbacks.c | 57 ++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 32 deletions(-) diff --git a/zathura/callbacks.c b/zathura/callbacks.c index 9a2d38b..4c23148 100644 --- a/zathura/callbacks.c +++ b/zathura/callbacks.c @@ -408,49 +408,42 @@ handle_link(GtkEntry* entry, girara_session_t* session, index = index - 1; } - /* set pages to draw links */ - bool invalid_index = true; + /* find the link*/ + zathura_link_t* link = NULL; unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document); for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) { zathura_page_t* page = zathura_document_get_page(zathura->document, page_id); - if (page == NULL || zathura_page_get_visibility(page) == false) { + if (page == NULL || zathura_page_get_visibility(page) == false || eval == false) { continue; } - GtkWidget* page_widget = zathura_page_get_widget(zathura, page); - g_object_set(G_OBJECT(page_widget), "draw-links", FALSE, NULL); - - if (eval == FALSE) { - /* nothing to evaluate */ - continue; - } - - zathura_link_t* link = zathura_page_widget_link_get(ZATHURA_PAGE(page_widget), index); + link = zathura_page_widget_link_get(ZATHURA_PAGE(page_widget), index); if (link != NULL) { - invalid_index = false; - switch (action) { - case ZATHURA_LINK_ACTION_FOLLOW: - zathura_link_evaluate(zathura, link); - break; - case ZATHURA_LINK_ACTION_DISPLAY: - zathura_link_display(zathura, link); - break; - case ZATHURA_LINK_ACTION_COPY: { - GdkAtom* selection = get_selection(zathura); - if (selection == NULL) { - break; - } - - zathura_link_copy(zathura, link, selection); - g_free(selection); - break; - } - } + break; } } - if (eval == TRUE && invalid_index == true) { + if (eval == TRUE && link == NULL) { girara_notify(session, GIRARA_WARNING, _("Invalid index '%s' given."), input); + } else { + switch (action) { + case ZATHURA_LINK_ACTION_FOLLOW: + zathura_link_evaluate(zathura, link); + break; + case ZATHURA_LINK_ACTION_DISPLAY: + zathura_link_display(zathura, link); + break; + case ZATHURA_LINK_ACTION_COPY: { + GdkAtom* selection = get_selection(zathura); + if (selection == NULL) { + break; + } + + zathura_link_copy(zathura, link, selection); + g_free(selection); + break; + } + } } g_free(input);