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;
32 register: function (cssfile) {
33 register_agent_stylesheet(this.location+cssfile);
35 unregister: function (cssfile) {
36 unregister_agent_stylesheet(this.location+cssfile);
38 registered_p: function (cssfile) {
39 return agent_stylesheet_registered_p(this.location+cssfile);
43 function theme_find (name) {
44 if (loaded_themes[name])
45 return loaded_themes[name];
46 for each (var path in theme_load_paths) {
48 if (path instanceof Ci.nsIURI) {
50 } else if (path instanceof Ci.nsIFile) {
51 url = make_uri(path).spec;
52 } else if (typeof(path) == "string") {
53 if (path.substr(0, 7) == 'chrome:')
56 url = make_uri(make_file(path)).spec;
58 if (url.substr(-1) != '/')
61 let def = get_contents_synchronously(url+'theme.json');
64 def = eval('('+def+')');
65 loaded_themes[name] = new theme(url, def.sheets);
66 return loaded_themes[name];
71 function theme_load (name) {
72 var th = theme_find(name);
73 if (! th) throw new Error("theme "+name+" not found.");
74 for each (var cssfile in th.sheets) {
75 let module = theme_cssfile_module(cssfile);
76 if (! themes[module]) {
78 if (! loaded(module+".js"))
79 call_after_load(module+".js",
80 function () { theme_module(module); });
82 if (! themes[module][cssfile])
83 themes[module][cssfile] = [];
84 if (themes[module][cssfile].length > 0)
85 themes[module][cssfile][0].unregister(cssfile);
86 themes[module][cssfile].unshift(th);
87 if (loaded(module+".js"))
92 function theme_unload (name) {
93 var th = theme_find(name);
94 for each (var cssfile in th.sheets) {
95 let module = theme_cssfile_module(cssfile);
96 themes[module][cssfile] =
97 themes[module][cssfile].filter(function (x) { return x !== th; });
98 if (th.registered_p(cssfile)) {
99 th.unregister(cssfile);
100 if (themes[module][cssfile].length > 0)
101 themes[module][cssfile][0].register(cssfile);
104 delete loaded_themes[name];
107 function theme_module (module) {
108 for (var cssfile in themes[module]) {
109 if (themes[module][cssfile].length > 0)
110 themes[module][cssfile][0].register(cssfile);
115 theme_load("default");