Change hover link url on status bar via direct function.
authorDaniel Carl <danielcarl@gmx.de>
Sat, 30 Mar 2019 22:19:55 +0000 (23:19 +0100)
committerDaniel Carl <danielcarl@gmx.de>
Sat, 30 Mar 2019 22:19:55 +0000 (23:19 +0100)
Do not emit fake signal to show the current hovered url from hinting
shown on the left of the statusbar. Instead call a function which writes
the url to the statusbar.

src/hints.c
src/main.c
src/main.h

index d14db78..86c9135 100644 (file)
@@ -306,7 +306,6 @@ static gboolean hint_function_check_result(Client *c, GVariant *return_value)
 {
     gboolean success = FALSE;
     char *value = NULL;
-    WebKitHitTestResult *hitresult;
 
     if (!return_value) {
         goto error;
@@ -323,19 +322,10 @@ static gboolean hint_function_check_result(Client *c, GVariant *return_value)
             /* We get OVER:{I,A}:element-url so we use byte 6 to check for the
              * hinted element type image I or link A. */
             if (*(value + 5) == 'I') {
-                hitresult = g_object_new(WEBKIT_TYPE_HIT_TEST_RESULT,
-                        "context", WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE,
-                        "image-uri", value + 7,
-                        NULL);
+                vb_statusbar_show_hover_url(c, LINK_TYPE_IMAGE, value + 7);
             } else {
-                hitresult = g_object_new(WEBKIT_TYPE_HIT_TEST_RESULT,
-                        "context", WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK,
-                        "link-uri", value + 7,
-                        NULL);
+                vb_statusbar_show_hover_url(c, LINK_TYPE_LINK, value + 7);
             }
-            g_signal_emit_by_name(c->webview, "mouse-target-changed",
-                    WEBKIT_HIT_TEST_RESULT(hitresult), 0);
-            g_object_unref(hitresult);
         } else {
             goto error;
         }
@@ -421,11 +411,7 @@ static gboolean hint_function_check_result(Client *c, GVariant *return_value)
     return TRUE;
 
 error:
-    hitresult = g_object_new(WEBKIT_TYPE_HIT_TEST_RESULT,
-            "context", WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
-            NULL);
-    g_signal_emit_by_name(c->webview, "mouse-target-changed", WEBKIT_HIT_TEST_RESULT(hitresult), 0);
-    g_object_unref(hitresult);
+    vb_statusbar_show_hover_url(c, LINK_TYPE_NONE, NULL);
     return FALSE;
 }
 
index e54d7ac..4a59b60 100644 (file)
@@ -638,6 +638,39 @@ void vb_statusbar_update(Client *c)
     g_string_free(status, TRUE);
 }
 
+/**
+ * Show the given url on the left of statusbar.
+ */
+void vb_statusbar_show_hover_url(Client *c, VbLinkType type, const char *uri)
+{
+    char *sanitized_uri,
+         *msg;
+    const char *type_label;
+
+    /* No uri given - show the current URI. */
+    if (!uri || !*uri) {
+        update_urlbar(c);
+        return;
+    }
+
+    switch (type) {
+        case LINK_TYPE_LINK:
+            type_label = "Link: ";
+            break;
+        case LINK_TYPE_IMAGE:
+            type_label = "Image: ";
+            break;
+        default:
+            return;
+    }
+
+    sanitized_uri = util_sanitize_uri(uri);
+    msg           = g_strconcat(type_label, uri, NULL);
+    gtk_label_set_text(GTK_LABEL(c->statusbar.left), msg);
+    g_free(msg);
+    g_free(sanitized_uri);
+}
+
 /**
  * Destroys given client and removed it from client queue. If no client is
  * there in queue, quit the gtk main loop.
@@ -1429,9 +1462,6 @@ static void on_webview_load_changed(WebKitWebView *webview,
 static void on_webview_mouse_target_changed(WebKitWebView *webview,
         WebKitHitTestResult *result, guint modifiers, Client *c)
 {
-    char *msg;
-    char *uri;
-
     /* Save the hitTestResult to have this later available for events that
      * don't support this. */
     if (c->state.hit_test_result) {
@@ -1440,20 +1470,14 @@ static void on_webview_mouse_target_changed(WebKitWebView *webview,
     c->state.hit_test_result = g_object_ref(result);
 
     if (webkit_hit_test_result_context_is_link(result)) {
-        uri = util_sanitize_uri(webkit_hit_test_result_get_link_uri(result));
-        msg = g_strconcat("Link: ", uri, NULL);
-        gtk_label_set_text(GTK_LABEL(c->statusbar.left), msg);
-        g_free(msg);
-        g_free(uri);
+        vb_statusbar_show_hover_url(c, LINK_TYPE_LINK,
+                webkit_hit_test_result_get_link_uri(result));
     } else if (webkit_hit_test_result_context_is_image(result)) {
-        uri = util_sanitize_uri(webkit_hit_test_result_get_image_uri(result));
-        msg = g_strconcat("Image: ", uri, NULL);
-        gtk_label_set_text(GTK_LABEL(c->statusbar.left), msg);
-        g_free(msg);
-        g_free(uri);
+        vb_statusbar_show_hover_url(c, LINK_TYPE_LINK,
+                webkit_hit_test_result_get_image_uri(result));
     } else {
         /* No link under cursor - show the current URI. */
-        update_urlbar(c);
+        vb_statusbar_show_hover_url(c, LINK_TYPE_NONE, NULL);
     }
 }
 
index 767efb2..9e922c6 100644 (file)
@@ -116,6 +116,12 @@ enum {
     FILES_LAST
 };
 
+typedef enum {
+    LINK_TYPE_NONE,
+    LINK_TYPE_LINK,
+    LINK_TYPE_IMAGE,
+} VbLinkType;
+
 typedef struct Client Client;
 typedef struct State State;
 typedef struct Map Map;
@@ -290,6 +296,7 @@ gboolean vb_quit(Client *c, gboolean force);
 void vb_register_add(Client *c, char buf, const char *value);
 const char *vb_register_get(Client *c, char buf);
 void vb_statusbar_update(Client *c);
+void vb_statusbar_show_hover_url(Client *c, VbLinkType type, const char *uri);
 void vb_gui_style_update(Client *c, const char *name, const char *value);
 
 #endif /* end of include guard: _MAIN_H */