From c885716f75f12779b0b224a77ce18ef962aa061d Mon Sep 17 00:00:00 2001 From: Benjamin Petrenko Date: Mon, 20 Jul 2015 10:08:02 +0300 Subject: [PATCH] respect the strict-focus variable --- src/dom.c | 30 ++++++++++++++++-------------- src/dom.h | 2 ++ src/main.c | 13 ++++++++++++- src/main.h | 1 + 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/dom.c b/src/dom.c index 0e9e3c6..7fc24bb 100644 --- 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; } diff --git a/src/dom.h b/src/dom.h index d2bb3b9..db3331a 100644 --- 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); diff --git a/src/main.c b/src/main.c index e5614c3..efc3f42 100644 --- a/src/main.c +++ b/src/main.c @@ -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; diff --git a/src/main.h b/src/main.h index f0c4921..ddd8870 100644 --- a/src/main.h +++ b/src/main.h @@ -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 { -- 2.20.1