Removed runtime styling for hints (#40).
authorDaniel Carl <danielcarl@gmx.de>
Thu, 11 Jul 2013 20:29:18 +0000 (22:29 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Thu, 11 Jul 2013 21:26:22 +0000 (23:26 +0200)
There is no need to change the hints style properties during runtime. So this
settings are removed. But on the other side all hinted elements and hint label
have now classes to style them via user style. Following style is the default
used for the hints.

._hintLabel {
    position: absolute;
    z-index: 100000;
    font-family: monospace;
    font-weight: bold;
    font-size: 10px;
    color: #000;
    background-color: #fff;
    margin: 0;
    padding: 0px 1px;
    border: 1px solid #444;
    opacity: 0.7
}
._hintElem {
    background-color: #ff0 !important;
    color: #000 !important
}
._hintElem._hintFocus{
    background-color: #8f0 !important
}
._hintLabel._hintFocus{
    opacity: 1
}

src/config.h
src/hints.c
src/hints.js
src/main.h
src/setting.c

index 1858d66..75cfe35 100644 (file)
@@ -137,10 +137,6 @@ const char *default_config[] = {
     "set completion-fg-active=#fff",
     "set completion-bg-normal=#656565",
     "set completion-bg-active=#777",
-    "set hint-bg=#ff0",
-    "set hint-bg-focus=#8f0",
-    "set hint-fg=#000",
-    "set hint-style=position:absolute;z-index:100000;font-family:monospace;font-weight:bold;font-size:10px;color:#000;background-color:#fff;margin:0;padding:0px 1px;border:1px solid #444;opacity:0.7;",
     "set ca-bundle=/etc/ssl/certs/ca-certificates.crt",
     "set home-page=https://github.com/fanglingsu/vimb",
     "set download-path=",
index 54100c5..701c4e6 100644 (file)
@@ -72,7 +72,6 @@ void hints_create(const char *input, guint mode, const guint prefixLength)
     if (!(vb.state.mode & VB_MODE_HINTING)) {
         vb_set_mode(VB_MODE_COMMAND | VB_MODE_HINTING, false);
 
-        Style *style = &vb.style;
         hints.prefixLength = prefixLength;
         hints.mode         = mode;
         hints.num          = 0;
@@ -96,13 +95,8 @@ void hints_create(const char *input, guint mode, const guint prefixLength)
         }
 
         js = g_strdup_printf(
-            "%s.init('%c', '%c', '%s', '%s', '%s', '%s', %d);",
-            HINT_VAR, type, usage,
-            style->hint_bg,
-            style->hint_bg_focus,
-            style->hint_fg,
-            style->hint_style,
-            MAXIMUM_HINTS
+            "%s.init('%c', '%c', %d);",
+            HINT_VAR, type, usage, MAXIMUM_HINTS
         );
 
         observe_input(true);
index 4bdab0c..5b8219d 100644 (file)
@@ -1,32 +1,19 @@
 var VbHint = (function(){
     'use strict';
 
-    var hClass      = "__hint",
-        hClassFocus = "__hintFocus",
-        hConts      = [],
-        hints       = [],
-        focusNum    = 1,
-        config      = {};
-
-    /* mode: l - links, i - images, e - editables */
-    /* usage: O - open, T - open in new window, U - use source */
-    function init(mode, usage, bg, bgf, fg, style, maxHints) {
-        config      = {
-            mode:     mode,
-            usage:    usage,
-            bg:       bg,
-            bgf:      bgf,
-            fg:       fg,
-            style:    style,
-            maxHints: maxHints
-        };
-    }
+    var hConts   = [],               /* holds the hintcontainers of the different documents */
+        hints    = [],               /* holds all hint data (hinted element, label, number) */
+        focusNum = 1,                /* number of current focused hint */
+        cId      = "_hintContainer", /* id of the conteiner holding the hint lables */
+        lClass   = "_hintLabel",     /* class used on the hint labels with the hint numbers */
+        hClass   = "_hintElem",      /* marks hinted elements */
+        fClass   = "_hintFocus",     /* marks focused element and focued hint */
+        config;
 
     function create(inputText) {
-        var topwin     = window,
-            top_height = topwin.innerHeight,
-            top_width  = topwin.innerWidth,
-            hCount     = 0;
+        var topWinH = window.innerHeight,
+            topWinW = window.innerWidth,
+            count   = 0;
 
         clear();
 
@@ -46,20 +33,21 @@ var VbHint = (function(){
                 ),
 
                 /* generate basic hint element which will be cloned and updated later */
-                hintSpan = doc.createElement("span"),
+                labelTmpl = doc.createElement("span"),
                 /* Bounds */
                 minX = offsetX < 0 ? -offsetX : 0,
                 minY = offsetY < 0 ? -offsetY : 0,
-                maxX = offsetX + win.width > top_width ? top_width - offsetX : top_width,
-                maxY = offsetY + win.height > top_height ? top_height - offsetY : top_height,
+                maxX = offsetX + win.width > topWinW ? topWinW - offsetX : topWinW,
+                maxY = offsetY + win.height > topWinH ? topWinH - offsetY : topWinH,
                 rect, e, i;
 
-            hintSpan.setAttribute("class", hClass);
-            hintSpan.style.cssText = config.style;
+            labelTmpl.className = lClass;
+
+            createStyle(doc);
 
             /* due to the different XPath result type, we will need two counter variables */
             for (i = 0; i < res.snapshotLength; i++) {
-                if (hCount >= config.maxHints) {
+                if (count >= config.maxHints) {
                     break;
                 }
 
@@ -69,44 +57,39 @@ var VbHint = (function(){
                     continue;
                 }
 
-                var cStyle = topwin.getComputedStyle(e, "");
+                var cStyle = window.getComputedStyle(e, "");
                 if (cStyle.display === "none" || cStyle.visibility !== "visible") {
                     continue;
                 }
 
-                /* making this block DOM compliant */
-                var hint        = hintSpan.cloneNode(false);
-                hint.style.left = Math.max((rect.left + win.scrollX), win.scrollX) - 3 + "px";
-                hint.style.top  = Math.max((rect.top + win.scrollY), win.scrollY) - 3 + "px";
-                hint.className  = hClass;
-                hint.appendChild(doc.createTextNode(hCount + 1));
+                /* create the hint label with number */
+                var label        = labelTmpl.cloneNode(false);
+                label.style.left = Math.max((rect.left + win.scrollX), win.scrollX) - 3 + "px";
+                label.style.top  = Math.max((rect.top + win.scrollY), win.scrollY) - 3 + "px";
+                label.innerText  = count + 1;
+
+                fragment.appendChild(label);
 
-                fragment.appendChild(hint);
+                /* add the hint class to the hinted element */
+                e.classList.add(hClass);
 
-                hCount++;
+                count++;
                 hints.push({
-                    e:    e,
-                    num:  hCount,
-                    span: hint,
-                    bg:   e.style.background,
-                    fg:   e.style.color
+                    e:     e,
+                    num:   count,
+                    label: label
                 });
-
-                /* change the foreground and background colors of the hinted items */
-                e.style.color = config.fg;
-                e.style.background = config.bg;
             }
 
             var hDiv = doc.createElement("div");
-            hDiv.id  = "hint_container";
-
+            hDiv.id  = cId;
             hDiv.appendChild(fragment);
             doc.documentElement.appendChild(hDiv);
 
             hConts.push(hDiv);
 
             /* recurse into any iframe or frame element */
-            var frameTags = ["frame","iframe"],
+            var frameTags = ["frame", "iframe"],
                 frames, n, f, i;
             for (f = 0; f < frameTags.length; ++f) {
                 frames = doc.getElementsByTagName(frameTags[f]);
@@ -121,14 +104,49 @@ var VbHint = (function(){
             }
         }
 
-        helper(topwin, 0, 0);
+        helper(window, 0, 0);
 
-        if (hCount <= 1) {
+        if (count <= 1) {
             return fire(1);
         }
         return focusHint(1);
     }
 
+    function createStyle(doc)
+    {
+        if (doc.hasStyle) {
+            return;
+        }
+        var e = doc.createElement("style");
+        e.innerHTML += "." + lClass + "{" +
+            "position:absolute;" +
+            "z-index:100000;" +
+            "font-family:monospace;" +
+            "font-weight:bold;" +
+            "font-size:10px;" +
+            "color:#000;" +
+            "background-color:#fff;" +
+            "margin:0;" +
+            "padding:0px 1px;" +
+            "border:1px solid #444;" +
+            "opacity:0.7" +
+            "}" +
+            "." + hClass + "{" +
+            "background-color:#ff0 !important;" +
+            "color:#000 !important" +
+            "}" +
+            "." + hClass + "." + fClass + "{" +
+            "background-color:#8f0 !important" +
+            "}" +
+            "." + lClass + "." + fClass + "{" +
+            "opacity:1" +
+            "}";
+
+        doc.head.appendChild(e);
+        /* prevent us from adding the style multiple times */
+        doc.hasStyle = true;
+    }
+
     function focus(back) {
         var num,
             i = getHintId(focusNum);
@@ -175,11 +193,9 @@ var VbHint = (function(){
         for (i = 0; i < hints.length; ++i) {
             hint = hints[i];
             if (hint.e) {
-                hint.e.style.background = hint.bg;
-                hint.e.style.color = hint.fg;
-                hint.e.classList.remove(hClassFocus);
+                hint.e.classList.remove(fClass);
                 hint.e.classList.remove(hClass);
-                hint.span.parentNode.removeChild(hint.span);
+                hint.label.parentNode.removeChild(hint.label);
             }
         }
         hints = [];
@@ -282,8 +298,9 @@ var VbHint = (function(){
         /* reset previous focused hint */
         var hint = getHint(focusNum);
         if (hint) {
-            hint.e.classList.remove(hClassFocus);
-            hint.e.style.background = config.bg;
+            hint.e.classList.remove(fClass);
+            hint.label.classList.remove(fClass);
+
             mouseEvent(hint.e, "mouseout");
         }
 
@@ -292,11 +309,12 @@ var VbHint = (function(){
         /* mark new hint as focused */
         hint = getHint(focusNum);
         if (hint) {
-            hint.e.classList.add(hClassFocus);
-            hint.e.style.background = config.bgf;
-            var source              = getSrc(hint.e);
+            hint.e.classList.add(fClass);
+            hint.label.classList.add(fClass);
+
             mouseEvent(hint.e, "mouseover");
 
+            var source = getSrc(hint.e);
             return "OVER:" + (source ? source : "");
         }
     }
@@ -327,9 +345,9 @@ var VbHint = (function(){
             return;
         }
         var hint = hints[i];
-        hint.e.style.background = hint.bg;
-        hint.e.style.color      = hint.fg;
-        hint.span.parentNode.removeChild(hint.span);
+        hint.e.classList.remove(fClass);
+        hint.e.classList.remove(hClass);
+        hint.label.parentNode.removeChild(hint.label);
 
         /* remove hints from all hints */
         hints.splice(i, 1);
@@ -406,7 +424,15 @@ var VbHint = (function(){
 
     /* the api */
     return {
-        init:   init,
+        /* mode: l - links, i - images, e - editables */
+        /* usage: O - open, T - open in new window, U - use source */
+        init: function init(mode, usage, maxHints) {
+            config = {
+                mode:     mode,
+                usage:    usage,
+                maxHints: maxHints
+            };
+        },
         create: create,
         update: update,
         clear:  clear,
index 609aaad..196f7b5 100644 (file)
@@ -287,11 +287,6 @@ typedef struct {
     VbColor              comp_fg[VB_COMP_LAST];
     VbColor              comp_bg[VB_COMP_LAST];
     PangoFontDescription *comp_font;
-    /* hint style */
-    char                 *hint_bg;
-    char                 *hint_bg_focus;
-    char                 *hint_fg;
-    char                 *hint_style;
     /* status bar */
     VbColor              status_bg[VB_STATUS_LAST];
     VbColor              status_fg[VB_STATUS_LAST];
index 88df890..fa147fb 100644 (file)
@@ -34,7 +34,6 @@ static gboolean status_color_fg(const Setting *s, const SettingType type);
 static gboolean status_font(const Setting *s, const SettingType type);
 static gboolean input_style(const Setting *s, const SettingType type);
 static gboolean completion_style(const Setting *s, const SettingType type);
-static gboolean hint_style(const Setting *s, const SettingType type);
 static gboolean strict_ssl(const Setting *s, const SettingType type);
 static gboolean ca_bundle(const Setting *s, const SettingType type);
 static gboolean home_page(const Setting *s, const SettingType type);
@@ -99,10 +98,6 @@ static Setting default_settings[] = {
     {NULL, "completion-fg-active", TYPE_COLOR, completion_style, {0}},
     {NULL, "completion-bg-normal", TYPE_COLOR, completion_style, {0}},
     {NULL, "completion-bg-active", TYPE_COLOR, completion_style, {0}},
-    {NULL, "hint-bg", TYPE_CHAR, hint_style, {0}},
-    {NULL, "hint-bg-focus", TYPE_CHAR, hint_style, {0}},
-    {NULL, "hint-fg", TYPE_CHAR, hint_style, {0}},
-    {NULL, "hint-style", TYPE_CHAR, hint_style, {0}},
     {NULL, "ca-bundle", TYPE_CHAR, ca_bundle, {0}},
     {NULL, "home-page", TYPE_CHAR, home_page, {0}},
     {NULL, "download-path", TYPE_CHAR, download_path, {0}},
@@ -519,38 +514,6 @@ static gboolean completion_style(const Setting *s, const SettingType type)
     return true;
 }
 
-static gboolean hint_style(const Setting *s, const SettingType type)
-{
-    Style *style = &vb.style;
-    if (!g_strcmp0(s->name, "hint-bg")) {
-        if (type == SETTING_GET) {
-            print_value(s, style->hint_bg);
-        } else {
-            OVERWRITE_STRING(style->hint_bg, s->arg.s)
-        }
-    } else if (!g_strcmp0(s->name, "hint-bg-focus")) {
-        if (type == SETTING_GET) {
-            print_value(s, style->hint_bg_focus);
-        } else {
-            OVERWRITE_STRING(style->hint_bg_focus, s->arg.s)
-        }
-    } else if (!g_strcmp0(s->name, "hint-fg")) {
-        if (type == SETTING_GET) {
-            print_value(s, style->hint_fg);
-        } else {
-            OVERWRITE_STRING(style->hint_fg, s->arg.s)
-        }
-    } else {
-        if (type == SETTING_GET) {
-            print_value(s, style->hint_style);
-        } else {
-            OVERWRITE_STRING(style->hint_style, s->arg.s);
-        }
-    }
-
-    return true;
-}
-
 static gboolean strict_ssl(const Setting *s, const SettingType type)
 {
     gboolean value;