2 * (C) Copyright 2008 John J. Foerch
4 * Use, modification, and distribution are subject to the terms specified in the
10 let loaded_themes = {};
12 define_variable('theme_load_paths', ['chrome://conkeror-gui/skin/'],
13 "List of directories and chrome urls within which themes "+
14 "may be found. The items can be represented as strings, "+
15 "nsIURI's, or nsIFile's. Chrome urls must be given as "+
18 function theme_cssfile_module (cssfile) {
19 cssfile = cssfile.substring(0, cssfile.indexOf('.'));
20 var sep = cssfile.indexOf('--');
24 return cssfile.substring(0, sep);
27 function theme (location, sheets) {
28 this.location = location;
33 register: function (cssfile) {
34 register_agent_stylesheet(this.location+cssfile);
36 unregister: function (cssfile) {
37 unregister_agent_stylesheet(this.location+cssfile);
39 registered_p: function (cssfile) {
40 return agent_stylesheet_registered_p(this.location+cssfile);
44 function theme_find (name) {
45 if (loaded_themes[name])
46 return loaded_themes[name];
47 for each (var path in theme_load_paths) {
49 if (path instanceof Ci.nsIURI) {
51 } else if (path instanceof Ci.nsIFile) {
52 url = make_uri(path).spec;
53 } else if (typeof(path) == "string") {
54 if (path.substr(0, 7) == 'chrome:')
57 url = make_uri(make_file(path)).spec;
59 if (url.substr(-1) != '/')
62 let def = get_contents_synchronously(url+'theme.json');
65 def = JSON.parse(def);
66 loaded_themes[name] = new theme(url, def.sheets);
67 return loaded_themes[name];
72 function theme_load (name) {
73 var th = theme_find(name);
74 if (! th) throw new Error("theme "+name+" not found.");
75 for each (var cssfile in th.sheets) {
76 let module = theme_cssfile_module(cssfile);
77 if (! themes[module]) {
79 if (! featurep(module))
80 call_after_load(module,
81 function () { theme_module(module); });
83 if (! themes[module][cssfile])
84 themes[module][cssfile] = [];
85 if (themes[module][cssfile].length > 0)
86 themes[module][cssfile][0].unregister(cssfile);
87 themes[module][cssfile].unshift(th);
93 function theme_unload (name) {
94 var th = theme_find(name);
95 for each (var cssfile in th.sheets) {
96 let module = theme_cssfile_module(cssfile);
97 themes[module][cssfile] =
98 themes[module][cssfile].filter(function (x) { return x !== th; });
99 if (th.registered_p(cssfile)) {
100 th.unregister(cssfile);
101 if (themes[module][cssfile].length > 0)
102 themes[module][cssfile][0].register(cssfile);
105 delete loaded_themes[name];
108 function theme_module (module) {
109 for (var cssfile in themes[module]) {
110 if (themes[module][cssfile].length > 0)
111 themes[module][cssfile][0].register(cssfile);
116 theme_load("default");