respect the strict-focus variable
authorBenjamin Petrenko <benjamin.petrenko@gmail.com>
Mon, 20 Jul 2015 07:08:02 +0000 (10:08 +0300)
committerDaniel Carl <danielcarl@gmx.de>
Mon, 20 Jul 2015 10:46:37 +0000 (12:46 +0200)
src/dom.c
src/dom.h
src/main.c
src/main.h

index 0e9e3c6..7fc24bb 100644 (file)
--- a/src/dom.c
+++ b/src/dom.c
@@ -30,33 +30,27 @@ static gboolean editable_focus_cb(Element *element, Event *event);
 static Element *get_active_element(Document *doc);
 
 
-void dom_check_auto_insert(Document *doc)
+void dom_auto_insert_unless_strict_focus(Document *doc)
 {
-    Element *active;
-    HtmlElement *element;
+    Element *active = webkit_dom_html_document_get_active_element(WEBKIT_DOM_HTML_DOCUMENT(doc));
 
-    /* First check for current active element that becomes focused before we
-     * could add the evnet observers. */
-    active = webkit_dom_html_document_get_active_element(WEBKIT_DOM_HTML_DOCUMENT(doc));
     if (active) {
         if (!vb.config.strict_focus) {
             auto_insert(active);
         } else if (vb.mode->id != 'i') {
-            /* If strict-focus is enabled and the editable element becomes
-             * focus, we explicitely remove the focus. But only if vim isn't
-             * in input mode at the time. This prevents from leaving input
-             * mode that was started by user interaction like click to
-             * editable element, or the gi normal mode command. */
             webkit_dom_element_blur(active);
         }
     }
+}
+
+void dom_install_focus_blur_callbacks(Document *doc)
+{
+    HtmlElement *element = webkit_dom_document_get_body(doc);
 
-    element = webkit_dom_document_get_body(doc);
     if (!element) {
         element = WEBKIT_DOM_HTML_ELEMENT(webkit_dom_document_get_document_element(doc));
     }
     if (element) {
-        /* add event listener to track focus and blur events on the document */
         webkit_dom_event_target_add_event_listener(
             WEBKIT_DOM_EVENT_TARGET(element), "blur", G_CALLBACK(editable_blur_cb), true, NULL
         );
@@ -66,6 +60,12 @@ void dom_check_auto_insert(Document *doc)
     }
 }
 
+void dom_check_auto_insert(Document *doc)
+{
+    dom_auto_insert_unless_strict_focus(doc);
+    dom_install_focus_blur_callbacks(doc);
+}
+
 /**
  * Remove focus from active and editable elements.
  */
@@ -281,7 +281,9 @@ static gboolean editable_blur_cb(Element *element, Event *event)
 
 static gboolean editable_focus_cb(Element *element, Event *event)
 {
-    auto_insert((Element*)webkit_dom_event_get_target(event));
+    if (vb.state.done_loading_page || !vb.config.strict_focus) {
+        auto_insert((Element*)webkit_dom_event_get_target(event));
+    }
 
     return false;
 }
index d2bb3b9..db3331a 100644 (file)
--- a/src/dom.h
+++ b/src/dom.h
@@ -32,6 +32,8 @@
 #define HtmlInputElement    WebKitDOMHTMLInputElement
 #define HtmlTextareaElement WebKitDOMHTMLTextAreaElement
 
+void dom_install_focus_blur_callbacks(Document *doc);
+void dom_auto_insert_unless_strict_focus(Document *doc);
 void dom_check_auto_insert(Document *doc);
 void dom_clear_focus(WebKitWebView *view);
 gboolean dom_focus_input(Document *doc);
index e5614c3..efc3f42 100644 (file)
@@ -752,6 +752,7 @@ static void webview_load_status_cb(WebKitWebView *view, GParamSpec *pspec)
 
             /* clear possible set marks */
             marks_clear();
+
             break;
 
         case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT:
@@ -765,7 +766,8 @@ static void webview_load_status_cb(WebKitWebView *view, GParamSpec *pspec)
             }
 
             WebKitWebFrame *frame = webkit_web_view_get_main_frame(view);
-            dom_check_auto_insert(webkit_web_frame_get_dom_document(frame));
+            dom_install_focus_blur_callbacks(webkit_web_frame_get_dom_document(frame));
+            vb.state.done_loading_page = false;
 
             break;
 
@@ -1149,6 +1151,7 @@ static void setup_signals()
         "signal::should-show-delete-interface-for-element", G_CALLBACK(gtk_false), NULL,
         "signal::resource-request-starting", G_CALLBACK(webview_request_starting_cb), NULL,
         "signal::navigation-policy-decision-requested", G_CALLBACK(navigation_decision_requested_cb), NULL,
+        "signal::onload-event", G_CALLBACK(onload_event_cb), NULL,
         NULL
     );
 
@@ -1426,6 +1429,14 @@ static gboolean navigation_decision_requested_cb(WebKitWebView *view,
     return false;
 }
 
+static void onload_event_cb(WebKitWebView *view, WebKitWebFrame *frame,
+    gpointer user_data)
+{
+    Document *doc = webkit_web_frame_get_dom_document(frame);
+    dom_auto_insert_unless_strict_focus(doc);
+    vb.state.done_loading_page = true;
+}
+
 static void hover_link_cb(WebKitWebView *webview, const char *title, const char *link)
 {
     char *message;
index f0c4921..ddd8870 100644 (file)
@@ -315,6 +315,7 @@ typedef struct {
     char            *fifo_path;             /* holds the path to the control fifo */
     char            *socket_path;           /* holds the path to the control socket */
     char            *pid_str;               /* holds the pid as string */
+    gboolean        done_loading_page;
 } State;
 
 typedef struct {