From 3d12dbc51f943a6094c1a8f02622ff77685ba502 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Mon, 10 Sep 2018 23:07:42 +0200 Subject: [PATCH] Add option to update window icon based on first page of a document (fixes pwmt/zathura#8) --- doc/man/zathurarc.5.rst | 8 ++++++++ zathura/callbacks.c | 15 +++++++++++++++ zathura/config.c | 2 ++ zathura/zathura.c | 26 +++++++++++++++++++++++++- zathura/zathura.h | 1 + 5 files changed, 51 insertions(+), 1 deletion(-) diff --git a/doc/man/zathurarc.5.rst b/doc/man/zathurarc.5.rst index 0b7d3a4..3b20b27 100644 --- a/doc/man/zathurarc.5.rst +++ b/doc/man/zathurarc.5.rst @@ -1069,6 +1069,14 @@ Some features are disabled when using strict sandbox mode: No feature regressions are expected when using normal sandbox mode. +window-icon-document +^^^^^^^^^^^^^^^^^^^^ +Defines whether the window document should be updated based on the first page of +a dcument. + +* Value type: Boolean +* default value: false + SEE ALSO ======== diff --git a/zathura/callbacks.c b/zathura/callbacks.c index 04cd268..2e814f6 100644 --- a/zathura/callbacks.c +++ b/zathura/callbacks.c @@ -758,3 +758,18 @@ cb_page_widget_scaled_button_release(ZathuraPage* page_widget, GdkEventButton* e synctex_edit(editor, page, event->x, event->y); g_free(editor); } + +void +cb_window_update_icon(ZathuraRenderRequest* GIRARA_UNUSED(request), cairo_surface_t* surface, void* data) +{ + zathura_t* zathura = data; + + girara_debug("updating window icon"); + GdkPixbuf* pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, cairo_image_surface_get_width(surface), cairo_image_surface_get_height(surface)); + if (pixbuf == NULL) { + girara_error("Unable to convert cairo surface to Gdk Pixbuf."); + } + + gtk_window_set_icon(GTK_WINDOW(zathura->ui.session->gtk.window), pixbuf); + g_object_unref(pixbuf); +} diff --git a/zathura/config.c b/zathura/config.c index dc17b3e..db5f39b 100644 --- a/zathura/config.c +++ b/zathura/config.c @@ -273,6 +273,8 @@ config_load_default(zathura_t* zathura) bool_value = false; girara_setting_add(gsession, "window-title-page", &bool_value, BOOLEAN, false, _("Display the page number in the window title"), cb_window_statbusbar_changed, NULL); bool_value = false; + girara_setting_add(gsession, "window-icon-document", &bool_value, BOOLEAN, false, _("Use first page of a document as window icon"), NULL, NULL); + bool_value = false; girara_setting_add(gsession, "statusbar-basename", &bool_value, BOOLEAN, false, _("Use basename of the file in the statusbar"), cb_window_statbusbar_changed, NULL); bool_value = false; girara_setting_add(gsession, "statusbar-home-tilde", &bool_value, BOOLEAN, false, _("Use ~ instead of $HOME in the filename in the statusbar"), cb_window_statbusbar_changed, NULL); diff --git a/zathura/zathura.c b/zathura/zathura.c index bba38f3..1d3b38b 100644 --- a/zathura/zathura.c +++ b/zathura/zathura.c @@ -650,7 +650,8 @@ zathura_set_argv(zathura_t* zathura, char** argv) } static bool -setup_renderer(zathura_t* zathura, zathura_document_t* document) { +setup_renderer(zathura_t* zathura, zathura_document_t* document) +{ /* page cache size */ int cache_size = 0; girara_setting_get(zathura->ui.session, "page-cache-size", &cache_size); @@ -660,6 +661,7 @@ setup_renderer(zathura_t* zathura, zathura_document_t* document) { cache_size = ZATHURA_PAGE_CACHE_DEFAULT_SIZE; } + girara_debug("starting renderer with cache size %d", cache_size); ZathuraRenderer* renderer = zathura_renderer_new(cache_size); if (renderer == NULL) { return false; @@ -684,6 +686,18 @@ setup_renderer(zathura_t* zathura, zathura_document_t* document) { zathura->sync.render_thread = renderer; + /* create render request to render window icon */ + bool window_icon = false; + girara_setting_get(zathura->ui.session, "window-icon-document", &window_icon); + if (window_icon == true) { + girara_debug("starting render request for window icon"); + ZathuraRenderRequest* request = zathura_render_request_new(renderer, zathura_document_get_page(document, 0)); + g_signal_connect(request, "completed", G_CALLBACK(cb_window_update_icon), zathura); + zathura_render_request_set_render_plain(request, true); + zathura_render_request(request, 0); + zathura->window_icon_render_request = request; + } + return true; } @@ -1351,8 +1365,17 @@ document_close(zathura_t* zathura, bool keep_monitor) return false; } + /* reset window icon */ + if (zathura->ui.session != NULL && zathura->window_icon_render_request != NULL) { + char* window_icon = NULL; + girara_setting_get(zathura->ui.session, "window-icon", &window_icon); + girara_setting_set(zathura->ui.session, "window-icon", window_icon); + g_free(window_icon); + } + /* stop rendering */ zathura_renderer_stop(zathura->sync.render_thread); + g_clear_object(&zathura->window_icon_render_request); /* remove monitor */ if (keep_monitor == false) { @@ -1375,6 +1398,7 @@ document_close(zathura_t* zathura, bool keep_monitor) save_fileinfo_to_db(zathura); } + /* remove jump list */ girara_list_iterator_free(zathura->jumplist.cur); zathura->jumplist.cur = NULL; girara_list_free(zathura->jumplist.list); diff --git a/zathura/zathura.h b/zathura/zathura.h index 014e755..6d8fc50 100644 --- a/zathura/zathura.h +++ b/zathura/zathura.h @@ -183,6 +183,7 @@ struct zathura_s GtkWidget** pages; /**< The page widgets */ zathura_database_t* database; /**< The database */ ZathuraDbus* dbus; /**< D-Bus service */ + ZathuraRenderRequest* window_icon_render_request; /**< Render request for window icon */ /** * File monitor