fix focus problem http://bugs.conkeror.org/issue263
[conkeror.git] / modules / theme.js
blobaf7d1da2150eb7d89032da3844d1c45c082e1d21
1 /**
2  * (C) Copyright 2008 John J. Foerch
3  *
4  * Use, modification, and distribution are subject to the terms specified in the
5  * COPYING file.
6 **/
8 in_module(null);
11     let themes = {};
12     let loaded_themes = {};
14     define_variable('theme_load_paths', ['chrome://conkeror-gui/skin/'],
15                     "List of directories and chrome urls within which themes "+
16                     "may be found.  The items can be represented as strings, "+
17                     "nsIURI's, or nsIFile's. Chrome urls must be given as "+
18                     "strings.");
20     function theme_cssfile_module (cssfile) {
21         cssfile = cssfile.substring(0, cssfile.indexOf('.'));
22         var sep = cssfile.indexOf('--');
23         if (sep == -1)
24             return cssfile;
25         else
26             return cssfile.substring(0, sep);
27     }
29     function theme (location, sheets) {
30         this.location = location;
31         this.sheets = sheets;
32     }
33     theme.prototype = {
34         register: function (cssfile) {
35             register_agent_stylesheet(this.location+cssfile);
36         },
37         unregister: function (cssfile) {
38             unregister_agent_stylesheet(this.location+cssfile);
39         },
40         registered_p: function (cssfile) {
41             return agent_stylesheet_registered_p(this.location+cssfile);
42         }
43     };
45     function theme_find (name) {
46         if (loaded_themes[name])
47             return loaded_themes[name];
48         for each (var path in theme_load_paths) {
49             var url;
50             if (path instanceof Ci.nsIURI) {
51                 url = path.spec;
52             } else if (path instanceof Ci.nsIFile) {
53                 url = make_uri(path).spec;
54             } else if (typeof(path) == "string") {
55                 if (path.substr(0, 7) == 'chrome:')
56                     url = path;
57                 else
58                     url = make_uri(make_file(path)).spec;
59             }
60             if (url.substr(-1) != '/')
61                 url += '/';
62             url += name + '/';
63             let def = get_contents_synchronously(url+'theme.json');
64             if (def === null)
65                 continue;
66             def = eval('('+def+')');
67             loaded_themes[name] = new theme(url, def.sheets);
68             return loaded_themes[name];
69         }
70         return null;
71     }
73     function theme_load (name) {
74         var th = theme_find(name);
75         if (! th) throw new Error("theme "+name+" not found.");
76         for each (var cssfile in th.sheets) {
77             let module = theme_cssfile_module(cssfile);
78             if (! themes[module]) {
79                 themes[module] = {};
80                 if (! featurep(module))
81                     call_after_load(module,
82                                     function () { theme_module(module); });
83             }
84             if (! themes[module][cssfile])
85                 themes[module][cssfile] = [];
86             if (themes[module][cssfile].length > 0)
87                 themes[module][cssfile][0].unregister(cssfile);
88             themes[module][cssfile].unshift(th);
89             if (featurep(module))
90                 th.register(cssfile);
91         }
92     }
94     function theme_unload (name) {
95         var th = theme_find(name);
96         for each (var cssfile in th.sheets) {
97             let module = theme_cssfile_module(cssfile);
98             themes[module][cssfile] =
99                 themes[module][cssfile].filter(function (x) { return x !== th; });
100             if (th.registered_p(cssfile)) {
101                 th.unregister(cssfile);
102                 if (themes[module][cssfile].length > 0)
103                     themes[module][cssfile][0].register(cssfile);
104             }
105         }
106         delete loaded_themes[name];
107     }
109     function theme_module (module) {
110         for (var cssfile in themes[module]) {
111             if (themes[module][cssfile].length > 0)
112                 themes[module][cssfile][0].register(cssfile);
113         }
114     }
117 theme_load("default");
119 provide("theme");