diff --git a/zathura/callbacks.c b/zathura/callbacks.c index c7ba706..bd25819 100644 --- a/zathura/callbacks.c +++ b/zathura/callbacks.c @@ -216,6 +216,44 @@ cb_refresh_view(GtkWidget* GIRARA_UNUSED(view), gpointer data) statusbar_page_number_update(zathura); } +void +cb_monitors_changed(GdkScreen* screen, gpointer data) +{ + girara_debug("signal received"); + + zathura_t* zathura = data; + if (screen == NULL || zathura == NULL) { + return; + } + + zathura_update_view_dpi(zathura); +} + +void +cb_widget_screen_changed(GtkWidget* widget, GdkScreen* UNUSED(previous_screen), gpointer data) +{ + girara_debug("signal received"); + + zathura_t* zathura = data; + if (widget == NULL || zathura == NULL) { + return; + } + + if (gtk_widget_has_screen(widget)) { + GdkScreen* screen = gtk_widget_get_screen(widget); + + /* disconnect signal on previous screen */ + g_signal_handlers_disconnect_matched(screen, G_SIGNAL_MATCH_FUNC, 0, 0, + NULL, (gpointer) cb_monitors_changed, zathura); + + /* connect to new screen */ + g_signal_connect(G_OBJECT(screen), + "monitors-changed", G_CALLBACK(cb_monitors_changed), zathura); + } + + zathura_update_view_dpi(zathura); +} + void cb_scale_factor(GObject* object, GParamSpec* UNUSED(pspec), gpointer data) { @@ -235,6 +273,7 @@ cb_scale_factor(GObject* object, GParamSpec* UNUSED(pspec), gpointer data) fabs(new_factor - current.y) >= DBL_EPSILON) { zathura_document_set_device_factors(zathura->document, new_factor, new_factor); girara_debug("New device scale factor: %d", new_factor); + zathura_update_view_dpi(zathura); render_all(zathura); } } diff --git a/zathura/callbacks.h b/zathura/callbacks.h index 95ee0fe..ab0210c 100644 --- a/zathura/callbacks.h +++ b/zathura/callbacks.h @@ -79,6 +79,30 @@ void cb_view_vadjustment_changed(GtkAdjustment *adjustment, gpointer data); */ void cb_refresh_view(GtkWidget* view, gpointer data); +/** + * This function gets called when the monitors associated with the GdkScreen + * change. + * + * It udpates the stored value for the monitor DPI. + * + * @param screen The GDK screen + * @param gpointer The zathura instance + */ +void cb_monitors_changed(GdkScreen* screen, gpointer data); + +/** + * This function gets called when the screen associated with the view widget + * changes. + * + * It udpates updates the connection on the monitors-changed ignal and the + * stored value for the monitor DPI. + * + * @param widget The view widget + * @param previous_screen The widget's previous screen + * @param gpointer The zathura instance + */ +void cb_widget_screen_changed(GtkWidget* widget, GdkScreen* previous_screen, gpointer data); + /** * This function gets called when the view widget scale factor changes (e.g. * when moving from a regular to a HiDPI screen). diff --git a/zathura/zathura.c b/zathura/zathura.c index e4c5f45..1e7fc67 100644 --- a/zathura/zathura.c +++ b/zathura/zathura.c @@ -217,6 +217,9 @@ init_ui(zathura_t* zathura) g_signal_connect(G_OBJECT(zathura->ui.session->gtk.view), "notify::scale-factor", G_CALLBACK(cb_scale_factor), zathura); + g_signal_connect(G_OBJECT(zathura->ui.session->gtk.view), + "screen-changed", G_CALLBACK(cb_widget_screen_changed), zathura); + /* page view */ zathura->ui.page_widget = gtk_grid_new(); gtk_grid_set_row_homogeneous(GTK_GRID(zathura->ui.page_widget), TRUE); @@ -1025,6 +1028,9 @@ document_open(zathura_t* zathura, const char* path, const char* uri, const char* zathura_update_view_dpi(zathura); + /* call screen-changed callback to connect monitors-changed signal on initial screen */ + cb_widget_screen_changed(zathura->ui.session->gtk.view, NULL, zathura); + /* get initial device scale */ int device_factor = gtk_widget_get_scale_factor(zathura->ui.session->gtk.view); zathura_document_set_device_factors(zathura->document, device_factor, device_factor);