From d19aee697472477c0e53c12a75303121e47f5b64 Mon Sep 17 00:00:00 2001 From: Colomban Wendling Date: Wed, 22 Nov 2023 00:39:43 +0100 Subject: [PATCH] webhelper: Add support for loading the current file in the web view This is a somewhat popular request, which is easy enough to implement and can be handy when working on static HTML files. --- webhelper/src/gwh-browser.c | 51 +++++++++++++++++++++++++++++++++ webhelper/src/gwh-browser.h | 5 ++++ webhelper/src/gwh-keybindings.h | 1 + webhelper/src/gwh-plugin.c | 10 +++++++ 4 files changed, 67 insertions(+) diff --git a/webhelper/src/gwh-browser.c b/webhelper/src/gwh-browser.c index 8ed1e74ad..36390487a 100644 --- a/webhelper/src/gwh-browser.c +++ b/webhelper/src/gwh-browser.c @@ -31,6 +31,8 @@ #include #include +#include + #include "gwh-utils.h" #include "gwh-settings.h" #include "gwh-keybindings.h" @@ -271,6 +273,36 @@ on_url_entry_icon_press (GtkEntry *entry, } } +static void +on_item_load_current_file_activate (GtkMenuItem *item, + GwhBrowser *self) +{ + gwh_browser_set_uri_from_document (self, document_get_current ()); +} + +static void +on_url_entry_populate_popup (GtkEntry *entry, + GtkWidget *menu, + GwhBrowser *self) +{ + GtkWidget *item; + GeanyDocument *doc = document_get_current (); + + /* separator */ + item = gtk_separator_menu_item_new (); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); + gtk_widget_show (item); + + /* load current file item */ + item = gtk_menu_item_new_with_mnemonic (_("_Load current file")); + gtk_widget_set_sensitive (item, doc && doc->real_path); + g_signal_connect (item, "activate", + G_CALLBACK (on_item_load_current_file_activate), self); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); + gtk_widget_show (item); + item_show_accelerator (item, GWH_KB_LOAD_CURRENT_FILE); +} + static gboolean on_entry_completion_match_selected (GtkEntryCompletion *comp, GtkTreeModel *model, @@ -773,6 +805,8 @@ create_toolbar (GwhBrowser *self) G_CALLBACK (on_url_entry_activate), self); g_signal_connect (G_OBJECT (self->priv->url_entry), "icon-press", G_CALLBACK (on_url_entry_icon_press), self); + g_signal_connect (G_OBJECT (self->priv->url_entry), "populate-popup", + G_CALLBACK (on_url_entry_populate_popup), self); g_signal_connect (G_OBJECT (self->priv->url_combo), "notify::active", G_CALLBACK (on_url_combo_active_notify), self); g_signal_connect (G_OBJECT (comp), "match-selected", @@ -972,6 +1006,23 @@ gwh_browser_set_uri (GwhBrowser *self, g_free (real_uri); } +gboolean +gwh_browser_set_uri_from_document (GwhBrowser *self, + GeanyDocument *doc) +{ + gchar *uri; + + /* document must exist on disk */ + if (! doc || ! doc->real_path) + return FALSE; + + uri = g_strconcat ("file://", doc->file_name, NULL); + gwh_browser_set_uri (self, uri); + g_free (uri); + + return TRUE; +} + const gchar * gwh_browser_get_uri (GwhBrowser *self) { diff --git a/webhelper/src/gwh-browser.h b/webhelper/src/gwh-browser.h index faa12e9b8..8432d5a35 100644 --- a/webhelper/src/gwh-browser.h +++ b/webhelper/src/gwh-browser.h @@ -24,6 +24,8 @@ #include #include +#include + G_BEGIN_DECLS @@ -69,6 +71,9 @@ G_GNUC_INTERNAL void gwh_browser_set_uri (GwhBrowser *self, const gchar *uri); G_GNUC_INTERNAL +gboolean gwh_browser_set_uri_from_document (GwhBrowser *self, + GeanyDocument *doc); +G_GNUC_INTERNAL const gchar *gwh_browser_get_uri (GwhBrowser *self); G_GNUC_INTERNAL GtkToolbar *gwh_browser_get_toolbar (GwhBrowser *self); diff --git a/webhelper/src/gwh-keybindings.h b/webhelper/src/gwh-keybindings.h index b768776d2..39431a30b 100644 --- a/webhelper/src/gwh-keybindings.h +++ b/webhelper/src/gwh-keybindings.h @@ -33,6 +33,7 @@ enum { GWH_KB_TOGGLE_INSPECTOR, GWH_KB_SHOW_HIDE_SEPARATE_WINDOW, GWH_KB_TOGGLE_BOOKMARK, + GWH_KB_LOAD_CURRENT_FILE, GWH_KB_COUNT }; diff --git a/webhelper/src/gwh-plugin.c b/webhelper/src/gwh-plugin.c index 68bba3fa2..d65236ce1 100644 --- a/webhelper/src/gwh-plugin.c +++ b/webhelper/src/gwh-plugin.c @@ -300,6 +300,13 @@ on_kb_toggle_bookmark (guint key_id) } } +static void +on_kb_load_current_file (guint key_id) +{ + gwh_browser_set_uri_from_document (GWH_BROWSER (G_browser), + document_get_current ()); +} + static gchar * get_config_filename (void) @@ -460,6 +467,9 @@ plugin_init (GeanyData *data) keybindings_set_item (gwh_keybindings_get_group (), GWH_KB_TOGGLE_BOOKMARK, on_kb_toggle_bookmark, 0, 0, "toggle_bookmark", _("Toggle bookmark for the current website"), NULL); + keybindings_set_item (gwh_keybindings_get_group (), GWH_KB_LOAD_CURRENT_FILE, + on_kb_load_current_file, 0, 0, "load_current_file", + _("Load the current file in the web view"), NULL); } void