Refactor handling of link hints

This commit is contained in:
Joe Nahmias 2012-02-05 16:09:02 -05:00 committed by Christian Dywan
parent e8cbe34aeb
commit e1fe41f3d7

View file

@ -1930,33 +1930,19 @@ midori_view_inspector_window_key_press_event_cb (GtkWidget* window,
return FALSE; return FALSE;
} }
static gboolean static void
gtk_widget_key_press_event_cb (WebKitWebView* web_view, handle_link_hints (WebKitWebView* web_view,
GdkEventKey* event, GdkEventKey* event,
MidoriView* view) MidoriView* view)
{ {
guint character;
gint digit = g_ascii_digit_value (event->keyval); gint digit = g_ascii_digit_value (event->keyval);
gchar* result = NULL;
event->state = event->state & MIDORI_KEYS_MODIFIER_MASK;
/* Handle oddities in Russian keyboard layouts */
if (event->hardware_keycode == ';' || event->hardware_keycode == '=')
event->keyval = ',';
else if (event->hardware_keycode == '<')
event->keyval = '.';
/* Find links by number: . to show links, type number, Return to go */
if (event->keyval == '.'
|| (view->find_links > -1
&& (digit != -1 || event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_Escape)))
{
WebKitWebFrame* web_frame = webkit_web_view_get_main_frame (web_view); WebKitWebFrame* web_frame = webkit_web_view_get_main_frame (web_view);
JSContextRef js_context = webkit_web_frame_get_global_context (web_frame); JSContextRef js_context = webkit_web_frame_get_global_context (web_frame);
gchar* result;
if (view->find_links == -1) if (view->find_links < 0)
{ {
/* Links are currently off, turn them on */
result = sokoke_js_script_eval (js_context, result = sokoke_js_script_eval (js_context,
" var style_func = (function (selector, rule) { " " var style_func = (function (selector, rule) { "
" var style = document.createElement ('style');" " var style = document.createElement ('style');"
@ -1978,7 +1964,7 @@ gtk_widget_key_press_event_cb (WebKitWebView* web_view,
" border:1px solid gray; padding:0 0.1em 0.2em 0.1em !important;" " border:1px solid gray; padding:0 0.1em 0.2em 0.1em !important;"
" position:absolute; display:inline !important; }');" " position:absolute; display:inline !important; }');"
" var label_count = 0;" " var label_count = 0;"
" for (var i in document.links) {" " for (i in document.links) {"
" if (document.links[i].href && document.links[i].insertBefore) {" " if (document.links[i].href && document.links[i].insertBefore) {"
" var child = document.createElement ('span');" " var child = document.createElement ('span');"
" if (document.links[i].accessKey && isNaN (document.links[i].accessKey)) {" " if (document.links[i].accessKey && isNaN (document.links[i].accessKey)) {"
@ -1991,48 +1977,14 @@ gtk_widget_key_press_event_cb (WebKitWebView* web_view,
" }" " }"
" document.links[i].insertBefore (child); } }", " document.links[i].insertBefore (child); } }",
NULL); NULL);
view->find_links = 0; view->find_links = 0; /* Links are now on */
}
else if (digit != -1 && event->keyval != GDK_KEY_Return && event->keyval != GDK_KEY_Escape)
{
if (view->find_links > -1)
view->find_links *= 10;
view->find_links += digit;
}
else if (event->keyval == GDK_KEY_Escape)
{
view->find_links = 0;
}
else if (event->keyval == GDK_KEY_Return)
{
gchar* script;
script = g_strdup_printf (
"var links = document.getElementsByClassName ('midoriHKD87346');"
"var i = %d; var return_key = %d;"
"if (return_key) {"
" if (typeof links[i] != 'undefined')"
" links[i].parentNode.href; }",
view->find_links, event->keyval == GDK_KEY_Return
);
result = sokoke_js_script_eval (js_context, script, NULL);
if (midori_uri_is_location (result))
{
if (MIDORI_MOD_NEW_TAB (event->state))
{
gboolean background = view->open_tabs_in_the_background;
if (MIDORI_MOD_BACKGROUND (event->state))
background = !background;
g_signal_emit (view, signals[NEW_TAB], 0, result, background);
}
else
midori_view_set_uri (view, result);
}
g_free (script);
g_free (result); g_free (result);
view->find_links = 0; return;
} }
else
if (event->keyval == '.')
{ {
/* Pressed '.' with links on, so turn them off */
result = sokoke_js_script_eval (js_context, result = sokoke_js_script_eval (js_context,
"var links = document.getElementsByClassName ('midoriHKD87346');" "var links = document.getElementsByClassName ('midoriHKD87346');"
"for (var i = links.length - 1; i >= 0; i--) {" "for (var i = links.length - 1; i >= 0; i--) {"
@ -2049,7 +2001,83 @@ gtk_widget_key_press_event_cb (WebKitWebView* web_view,
NULL); NULL);
g_free (result); g_free (result);
view->find_links = -1; view->find_links = -1;
return;
} }
/* Links are already on at this point, so process the input character */
if (digit != -1 && event->keyval != GDK_KEY_Return && event->keyval != GDK_KEY_Escape)
{
/* Got a digit, add it to the link count/ number */
if (view->find_links > 0)
view->find_links *= 10;
view->find_links += digit;
return;
}
if (event->keyval == GDK_KEY_Escape)
{
// Clear the link count/number
view->find_links = 0;
return;
}
if (event->keyval == GDK_KEY_Return)
{
/* Return pressed, grab URI if we have a link with the entered number */
gchar* script = g_strdup_printf (
"var links = document.getElementsByClassName ('midoriHKD87346');"
"var i = %d; var return_key = %d;"
"if (return_key) {"
" if (typeof links[i] != 'undefined')"
" links[i].parentNode.href; }",
view->find_links, event->keyval == GDK_KEY_Return
);
result = sokoke_js_script_eval (js_context, script, NULL);
g_free (script);
}
/* Check the URI we grabbed to see if it's valid, if so go there */
if (midori_uri_is_location (result))
{
if (MIDORI_MOD_NEW_TAB (event->state))
{
gboolean background = view->open_tabs_in_the_background;
if (MIDORI_MOD_BACKGROUND (event->state))
background = !background;
g_signal_emit (view, signals[NEW_TAB], 0, result, background);
}
else
midori_view_set_uri (view, result);
view->find_links = -1; /* Turn off link mode */
}
else /* Invalid URI, start over... */
view->find_links = 0;
if (result)
g_free (result);
return;
}
static gboolean
gtk_widget_key_press_event_cb (WebKitWebView* web_view,
GdkEventKey* event,
MidoriView* view)
{
guint character;
event->state = event->state & MIDORI_KEYS_MODIFIER_MASK;
/* Handle oddities in Russian keyboard layouts */
if (event->hardware_keycode == ';' || event->hardware_keycode == '=')
event->keyval = ',';
else if (event->hardware_keycode == '<')
event->keyval = '.';
/* Find links by number: . to show links, type number, Return to go */
if ( event->keyval == '.' || view->find_links > -1 )
{
handle_link_hints (web_view, event, view);
return FALSE; return FALSE;
} }