2 * (C) Copyright 2008 John J. Foerch
4 * Use, modification, and distribution are subject to the terms specified in the
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 "+
20 function theme_cssfile_module (cssfile) {
21 cssfile = cssfile.substring(0, cssfile.indexOf('.'));
22 var sep = cssfile.indexOf('--');
26 return cssfile.substring(0, sep);
29 function theme (location, sheets) {
30 this.location = location;
35 register: function (cssfile) {
36 register_agent_stylesheet(this.location+cssfile);
38 unregister: function (cssfile) {
39 unregister_agent_stylesheet(this.location+cssfile);
41 registered_p: function (cssfile) {
42 return agent_stylesheet_registered_p(this.location+cssfile);
46 function theme_find (name) {
47 if (loaded_themes[name])
48 return loaded_themes[name];
49 for each (var path in theme_load_paths) {
51 if (path instanceof Ci.nsIURI) {
53 } else if (path instanceof Ci.nsIFile) {
54 url = make_uri(path).spec;
55 } else if (typeof(path) == "string") {
56 if (path.substr(0, 7) == 'chrome:')
59 url = make_uri(make_file(path)).spec;
61 if (url.substr(-1) != '/')
64 let def = get_contents_synchronously(url+'theme.json');
67 def = eval('('+def+')');
68 loaded_themes[name] = new theme(url, def.sheets);
69 return loaded_themes[name];
74 function theme_load (name) {
75 var th = theme_find(name);
76 if (! th) throw new Error("theme "+name+" not found.");
77 for each (var cssfile in th.sheets) {
78 let module = theme_cssfile_module(cssfile);
79 if (! themes[module]) {
81 if (! featurep(module))
82 call_after_load(module,
83 function () { theme_module(module); });
85 if (! themes[module][cssfile])
86 themes[module][cssfile] = [];
87 if (themes[module][cssfile].length > 0)
88 themes[module][cssfile][0].unregister(cssfile);
89 themes[module][cssfile].unshift(th);
95 function theme_unload (name) {
96 var th = theme_find(name);
97 for each (var cssfile in th.sheets) {
98 let module = theme_cssfile_module(cssfile);
99 themes[module][cssfile] =
100 themes[module][cssfile].filter(function (x) { return x !== th; });
101 if (th.registered_p(cssfile)) {
102 th.unregister(cssfile);
103 if (themes[module][cssfile].length > 0)
104 themes[module][cssfile][0].register(cssfile);
107 delete loaded_themes[name];
110 function theme_module (module) {
111 for (var cssfile in themes[module]) {
112 if (themes[module][cssfile].length > 0)
113 themes[module][cssfile][0].register(cssfile);
118 theme_load("default");