Thanks to John Foerch for pointing out that a simple enable/disable
was insufficient.
---
 content/minibuffer-annotation.css          |    3 ++
 content/minibuffer.css                     |    4 --
 modules/minibuffer.js                      |    6 ++++
 modules/stylesheet.js                      |   45 ++++++++++++++++++++++++++++
 tests/simple/registered-stylesheet-mode.js |   28 +++++++++++++++++
 5 files changed, 82 insertions(+), 4 deletions(-)
 create mode 100644 content/minibuffer-annotation.css
 create mode 100644 tests/simple/registered-stylesheet-mode.js

diff --git a/content/minibuffer-annotation.css 
b/content/minibuffer-annotation.css
new file mode 100644
index 0000000..4c5b5e8
--- /dev/null
+++ b/content/minibuffer-annotation.css
@@ -0,0 +1,3 @@
+#minibuffer-input {
+    -moz-binding: 
url("chrome://conkeror-gui/content/annotated-textbox.xml#annotated-textbox");
+}
diff --git a/content/minibuffer.css b/content/minibuffer.css
index 71105fd..17ee1b2 100644
--- a/content/minibuffer.css
+++ b/content/minibuffer.css
@@ -13,7 +13,3 @@
 #minibuffer[minibuffermode="input"] #minibuffer-mode-indicator {
   visibility: collapse;
 }
-
-#minibuffer-input {
-    -moz-binding: 
url("chrome://conkeror-gui/content/annotated-textbox.xml#annotated-textbox");
-}
diff --git a/modules/minibuffer.js b/modules/minibuffer.js
index 6b19305..1c296f9 100644
--- a/modules/minibuffer.js
+++ b/modules/minibuffer.js
@@ -447,4 +447,10 @@ function minibuffer_abort (window) {
 interactive("minibuffer-abort", null, function (I) { 
minibuffer_abort(I.window); });
 
 
+// Mode to provide an extra label during minibuffer input.
+
+const minibuffer_annotation_stylesheet = 
"chrome://conkeror-gui/content/minibuffer-annotation.css";
+
+var minibuffer_annotation_mode = new 
registered_stylesheet_mode(minibuffer_annotation_stylesheet);
+
 provide("minibuffer");
diff --git a/modules/stylesheet.js b/modules/stylesheet.js
index 518df3f..99bee28 100644
--- a/modules/stylesheet.js
+++ b/modules/stylesheet.js
@@ -89,4 +89,49 @@ function make_css_data_uri (rules) {
     return make_uri("data:text/css,"+escape(rules));
 }
 
+/**
+ * Construct a mode to keep a stylesheet registered while its users
+ * need it.
+ */
+
+function registered_stylesheet_mode (url) {
+    this.url = url;
+    this.users = [];
+    this.enabled = false;
+}
+registered_stylesheet_mode.prototype = {
+    constructor: registered_stylesheet_mode,
+
+    register: function (user) {
+        this.users.push(user);
+        this._switch_if_needed();
+    },
+
+    unregister: function (user) {
+        var i = this.users.indexOf(user);
+        if (i > -1)
+            this.users.splice(i, 1);
+        this._switch_if_needed();
+    },
+
+    _switch_if_needed: function (user) {
+        if (this.enabled && this.users.length == 0)
+            this._disable();
+        if (!this.enabled && this.users.length != 0)
+            this._enable();
+    },
+
+    _enable: function () {
+        if (this.url)
+            register_user_stylesheet(this.url);
+        this.enabled = true;
+    },
+
+    _disable: function () {
+        if (this.url)
+            unregister_user_stylesheet(this.url);
+        this.enabled = false;
+    }
+}
+
 provide("stylesheet");
diff --git a/tests/simple/registered-stylesheet-mode.js 
b/tests/simple/registered-stylesheet-mode.js
new file mode 100644
index 0000000..fb151ad
--- /dev/null
+++ b/tests/simple/registered-stylesheet-mode.js
@@ -0,0 +1,28 @@
+require('walnut.js');
+
+walnut_run({
+    suite_setup: function () {
+        this.mode = new registered_stylesheet_mode(null);
+    },
+    test_registered_stylesheet_mode_1: function () {
+        this.mode.register(this);
+        assert_equals(this.mode.users.length, 1);
+        assert(this.mode.enabled);
+    },
+    test_registered_stylesheet_mode_2: function () {
+        this.mode.register("foo");
+        assert_equals(this.mode.users.length, 2);
+        assert(this.mode.enabled);
+    },
+    test_registered_stylesheet_mode_3: function () {
+        this.mode.unregister(this);
+        assert_equals(this.mode.users.length, 1);
+        assert(this.mode.enabled);
+    },
+    test_registered_stylesheet_mode_4: function () {
+        this.mode.unregister("foo");
+        assert_equals(this.mode.users.length, 0);
+        assert(!this.mode.enabled);
+    }
+});
+
-- 
1.7.9.1

_______________________________________________
Conkeror mailing list
[email protected]
https://www.mozdev.org/mailman/listinfo/conkeror

Reply via email to