adji

Adji's a Decisive and Joyful Internet browser
git clone https://noxz.tech/git/adji.git
adji

commit: 90bc012c2898665fe927c138be1c36e7bb2d9dde
parent: 5b9eb99d161fe0d5cf428ed054fb5923170f3a89
author: Chris Noxz <chris@noxz.tech>
date:   Tue, 25 Apr 2023 15:01:05 +0200
generalize context menu item creation
Mbrowser.c79++++++++++----------
Mbrowser.h2+
2 files changed, 40 insertions(+), 41 deletions(-)
diff --git a/browser.c b/browser.c
@@ -220,7 +220,6 @@ create_context_menu(struct Client              *c,
                     WebKitContextMenu          *context_menu,
                     WebKitHitTestResult        *hit_test_result)
 {
-	GAction                *a;                  /* context menu item action */
 	guint                   x;                  /* hit test result context */
 
 	x = webkit_hit_test_result_get_context(hit_test_result);
@@ -231,56 +230,54 @@ create_context_menu(struct Client              *c,
 
 	/* if document is the only context */
 	if (x == WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT) {
-		CB(
-		    (a = (GAction*)g_simple_action_new("open-external", NULL)),
-		    "activate",
-		    cb_open_external,
-		    c
-		);
-		webkit_context_menu_prepend(
+		create_context_menu_item(
+		    c,
 		    context_menu,
-		    webkit_context_menu_item_new_from_gaction(
-		        a, "Open Page Externally", NULL
-		    )
+		    "open-external",
+		    "Open Page Externally",
+		    cb_open_external
 		);
-		g_object_unref(a);
 		return; /* no need to check further */
 	}
 
-	if (x & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
-		CB(
-		    (a = (GAction*)g_simple_action_new("open-external", NULL)),
-		    "activate",
-		    cb_open_external,
-		    c
-		);
-		webkit_context_menu_prepend(
+	if (x & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK)
+		create_context_menu_item(
+		    c,
 		    context_menu,
-		    webkit_context_menu_item_new_from_gaction(
-		        a, "Open Link Externally", NULL
-		    )
+		    "open-external",
+		    "Open Link Externally",
+		    cb_open_external
 		);
-		g_object_unref(a);
-	}
 
-	if (x & WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION) {
-		/* requires javascript for DOM access from here on */
-		if (!webkit_settings_get_enable_javascript(c->settings))
-			return;
-		CB(
-		    (a = (GAction*)g_simple_action_new("selection-search", NULL)),
-		    "activate",
-		    cb_selection_search,
-		    c
-		);
-		webkit_context_menu_prepend(
+	/* requires javascript for DOM access from here on */
+	if (!webkit_settings_get_enable_javascript(c->settings))
+		return;
+
+	if (x & WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION)
+		create_context_menu_item(
+		    c,
 		    context_menu,
-		    webkit_context_menu_item_new_from_gaction(
-		        a, "Search Selection", NULL
-		    )
+		    "selection-search",
+		    "Search Selection",
+		    cb_selection_search
 		);
-		g_object_unref(a);
-	}
+}
+
+void
+create_context_menu_item(struct Client          *c,
+                         WebKitContextMenu      *context_menu,
+                         const gchar            *name,
+                         const gchar            *label,
+                         void                   *action)
+{
+	GAction                *a = (GAction*)g_simple_action_new(name, NULL);
+
+	CB(a, "activate", action, c);
+	webkit_context_menu_prepend(
+	    context_menu,
+	    webkit_context_menu_item_new_from_gaction(a, label, NULL)
+	);
+	g_object_unref(a);
 }
 
 void
diff --git a/browser.h b/browser.h
@@ -209,6 +209,8 @@ static void client_destroy(struct Client *);
 static gboolean command(struct Client *, const gchar*);
 static void create_context_menu(struct Client *, WebKitContextMenu *,
                                 WebKitHitTestResult *);
+static void create_context_menu_item(struct Client *, WebKitContextMenu *,
+                                     const gchar *, const gchar *, void *);
 static void die(const char *);
 static gboolean ipc_request(GIOChannel *, GIOCondition, gpointer);
 static ssize_t ipc_send(char *);