From 5244b07d6aba16a775660f1a60456f5d76b1626d Mon Sep 17 00:00:00 2001 From: Liao Junxuan Date: Mon, 20 Nov 2023 19:20:51 +0800 Subject: [PATCH 1/2] Add support for zoom gesture --- zathura/callbacks.c | 25 +++++++++++++++++++++++++ zathura/callbacks.h | 3 +++ zathura/zathura.c | 7 +++++++ zathura/zathura.h | 8 ++++++++ 4 files changed, 43 insertions(+) diff --git a/zathura/callbacks.c b/zathura/callbacks.c index 5c1c039..fd9b743 100644 --- a/zathura/callbacks.c +++ b/zathura/callbacks.c @@ -808,3 +808,28 @@ cb_window_update_icon(ZathuraRenderRequest* GIRARA_UNUSED(request), cairo_surfac gtk_window_set_icon(GTK_WINDOW(zathura->ui.session->gtk.window), pixbuf); g_object_unref(pixbuf); } + +void +cb_gesture_zoom_begin(GtkGesture* UNUSED(self), GdkEventSequence* UNUSED(sequence), void* data) +{ + zathura_t* zathura = data; + if (zathura == NULL || zathura->document == NULL) { + return; + } + const double current_zoom = zathura_document_get_zoom(zathura->document); + zathura->gesture.initial_zoom = current_zoom; +} + +void +cb_gesture_zoom_scale_changed(GtkGestureZoom* UNUSED(self), gdouble scale, void* data) +{ + zathura_t* zathura = data; + if (zathura == NULL || zathura->document == NULL) { + return; + } + + const double next_zoom = zathura->gesture.initial_zoom * scale; + zathura_document_set_zoom(zathura->document, next_zoom); + render_all(zathura); + refresh_view(zathura); +} diff --git a/zathura/callbacks.h b/zathura/callbacks.h index 8cbaaf8..c8520ee 100644 --- a/zathura/callbacks.h +++ b/zathura/callbacks.h @@ -281,5 +281,8 @@ void update_visible_pages(zathura_t* zathura); */ void cb_window_update_icon(ZathuraRenderRequest* request, cairo_surface_t* surface, void* data); +void cb_gesture_zoom_begin(GtkGesture* self, GdkEventSequence* sequence, void* data); + +void cb_gesture_zoom_scale_changed(GtkGestureZoom* self, gdouble scale, void* data); #endif // CALLBACKS_H diff --git a/zathura/zathura.c b/zathura/zathura.c index 033cb95..2370f0b 100644 --- a/zathura/zathura.c +++ b/zathura/zathura.c @@ -221,6 +221,13 @@ init_ui(zathura_t* zathura) zathura->ui.session->events.buffer_changed = cb_buffer_changed; zathura->ui.session->events.unknown_command = cb_unknown_command; + /* gestures */ + + GtkGesture* zoom = gtk_gesture_zoom_new(GTK_WIDGET(zathura->ui.session->gtk.view)); + g_signal_connect(zoom, "scale-changed", G_CALLBACK(cb_gesture_zoom_scale_changed), zathura); + g_signal_connect(zoom, "begin", G_CALLBACK(cb_gesture_zoom_begin), zathura); + gtk_event_controller_set_propagation_phase(GTK_EVENT_CONTROLLER(zathura->ui.session->gtk.view), GTK_PHASE_BUBBLE); + /* zathura signals */ zathura->signals.refresh_view = g_signal_new( "refresh-view", GTK_TYPE_WIDGET, G_SIGNAL_RUN_LAST, 0, NULL, NULL, diff --git a/zathura/zathura.h b/zathura/zathura.h index a9cf41e..55f44f2 100644 --- a/zathura/zathura.h +++ b/zathura/zathura.h @@ -226,6 +226,14 @@ struct zathura_s } toggle_presentation_mode; } shortcut; + /** + * Storage for gestures. + */ + struct { + double initial_zoom; + } gesture; + + /** * Context for MIME type detection */ From 1419beccd013be576303550746d841036837ee6c Mon Sep 17 00:00:00 2001 From: Liao Junxuan Date: Mon, 20 Nov 2023 21:23:16 +0800 Subject: [PATCH 2/2] Apply zoom limits to the zoom gesture --- zathura/callbacks.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/zathura/callbacks.c b/zathura/callbacks.c index fd9b743..36bf2fd 100644 --- a/zathura/callbacks.c +++ b/zathura/callbacks.c @@ -829,7 +829,8 @@ cb_gesture_zoom_scale_changed(GtkGestureZoom* UNUSED(self), gdouble scale, void* } const double next_zoom = zathura->gesture.initial_zoom * scale; - zathura_document_set_zoom(zathura->document, next_zoom); + const double corrected_zoom = zathura_correct_zoom_value(zathura->ui.session, next_zoom); + zathura_document_set_zoom(zathura->document, corrected_zoom); render_all(zathura); refresh_view(zathura); }