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;
34 register: function (cssfile) {
35 register_agent_stylesheet(this.location+cssfile);
37 unregister: function (cssfile) {
38 unregister_agent_stylesheet(this.location+cssfile);
40 registered_p: function (cssfile) {
41 return agent_stylesheet_registered_p(this.location+cssfile);
45 function theme_find (name) {
46 if (loaded_themes[name])
47 return loaded_themes[name];
48 for each (var path in theme_load_paths) {
50 if (path instanceof Ci.nsIURI) {
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:')
58 url = make_uri(make_file(path)).spec;
60 if (url.substr(-1) != '/')
63 let def = get_contents_synchronously(url+'theme.json');
66 def = eval('('+def+')');
67 loaded_themes[name] = new theme(url, def.sheets);
68 return loaded_themes[name];
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]) {
80 if (! featurep(module))
81 call_after_load(module,
82 function () { theme_module(module); });
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);
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);
106 delete loaded_themes[name];
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);
117 theme_load("default");