Skip to content

Commit

Permalink
webhelper: Add support for loading the current file in the web view
Browse files Browse the repository at this point in the history
This is a somewhat popular request, which is easy enough to implement
and can be handy when working on static HTML files.
  • Loading branch information
b4n committed Nov 21, 2023
1 parent fc54c57 commit d19aee6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
51 changes: 51 additions & 0 deletions webhelper/src/gwh-browser.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <gtk/gtk.h>
#include <webkit2/webkit2.h>

#include <document.h>

#include "gwh-utils.h"
#include "gwh-settings.h"
#include "gwh-keybindings.h"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
{
Expand Down
5 changes: 5 additions & 0 deletions webhelper/src/gwh-browser.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <gtk/gtk.h>
#include <webkit2/webkit2.h>

#include <document.h>

G_BEGIN_DECLS


Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions webhelper/src/gwh-keybindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
10 changes: 10 additions & 0 deletions webhelper/src/gwh-plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d19aee6

Please sign in to comment.