settings.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. import GLib from 'gi://GLib';
  2. const Signals = imports.signals;
  3. /// An enum non-extensively describing the type of gsettings key.
  4. export const Type = {
  5. B: 'Boolean',
  6. I: 'Integer',
  7. D: 'Double',
  8. S: 'String',
  9. C: 'Color',
  10. AS: 'StringArray',
  11. PIPELINES: 'Pipelines'
  12. };
  13. /// An object to get and manage the gsettings preferences.
  14. ///
  15. /// Should be initialized with an array of keys, for example:
  16. ///
  17. /// let settings = new Settings([
  18. /// { type: Type.I, name: "panel-corner-radius" },
  19. /// { type: Type.B, name: "debug" }
  20. /// ]);
  21. ///
  22. /// Each {type, name} object represents a gsettings key, which must be created
  23. /// in the gschemas.xml file of the extension.
  24. export const Settings = class Settings {
  25. constructor(keys, settings) {
  26. this.settings = settings;
  27. this.keys = keys;
  28. this.keys.forEach(bundle => {
  29. let component = this;
  30. let component_settings = settings;
  31. if (bundle.component !== "general") {
  32. let bundle_component = bundle.component.replaceAll('-', '_');
  33. this[bundle_component] = {
  34. settings: this.settings.get_child(bundle.component)
  35. };
  36. component = this[bundle_component];
  37. component_settings = settings.get_child(bundle.component);
  38. }
  39. bundle.schemas.forEach(key => {
  40. let property_name = this.get_property_name(key.name);
  41. switch (key.type) {
  42. case Type.B:
  43. Object.defineProperty(component, property_name, {
  44. get() {
  45. return component_settings.get_boolean(key.name);
  46. },
  47. set(v) {
  48. component_settings.set_boolean(key.name, v);
  49. }
  50. });
  51. break;
  52. case Type.I:
  53. Object.defineProperty(component, property_name, {
  54. get() {
  55. return component_settings.get_int(key.name);
  56. },
  57. set(v) {
  58. component_settings.set_int(key.name, v);
  59. }
  60. });
  61. break;
  62. case Type.D:
  63. Object.defineProperty(component, property_name, {
  64. get() {
  65. return component_settings.get_double(key.name);
  66. },
  67. set(v) {
  68. component_settings.set_double(key.name, v);
  69. }
  70. });
  71. break;
  72. case Type.S:
  73. Object.defineProperty(component, property_name, {
  74. get() {
  75. return component_settings.get_string(key.name);
  76. },
  77. set(v) {
  78. component_settings.set_string(key.name, v);
  79. }
  80. });
  81. break;
  82. case Type.C:
  83. Object.defineProperty(component, property_name, {
  84. // returns the array [red, blue, green, alpha] with
  85. // values between 0 and 1
  86. get() {
  87. let val = component_settings.get_value(key.name);
  88. return val.deep_unpack();
  89. },
  90. // takes an array [red, blue, green, alpha] with
  91. // values between 0 and 1
  92. set(v) {
  93. let val = new GLib.Variant("(dddd)", v);
  94. component_settings.set_value(key.name, val);
  95. }
  96. });
  97. break;
  98. case Type.AS:
  99. Object.defineProperty(component, property_name, {
  100. get() {
  101. let val = component_settings.get_value(key.name);
  102. return val.deep_unpack();
  103. },
  104. set(v) {
  105. let val = new GLib.Variant("as", v);
  106. component_settings.set_value(key.name, val);
  107. }
  108. });
  109. break;
  110. case Type.PIPELINES:
  111. Object.defineProperty(component, property_name, {
  112. get() {
  113. let pips = component_settings.get_value(key.name).deep_unpack();
  114. Object.keys(pips).forEach(pipeline_id => {
  115. let pipeline = pips[pipeline_id];
  116. if (!('name' in pipeline)) {
  117. this._warn('impossible to get pipelines, pipeline has not name, resetting');
  118. component[property_name + '_reset']();
  119. return component[property_name];
  120. }
  121. let name = pipeline.name.deep_unpack();
  122. if (typeof name !== 'string') {
  123. this._warn('impossible to get pipelines, pipeline name is not a string, resetting');
  124. component[property_name + '_reset']();
  125. return component[property_name];
  126. }
  127. if (!('effects' in pipeline)) {
  128. this._warn('impossible to get pipelines, pipeline has not effects, resetting');
  129. component[property_name + '_reset']();
  130. return component[property_name];
  131. }
  132. let effects = pipeline.effects.deep_unpack();
  133. if (!Array.isArray(effects)) {
  134. this._warn('impossible to get pipelines, pipeline effects is not an array, resetting');
  135. component[property_name + '_reset']();
  136. return component[property_name];
  137. }
  138. effects = effects.map(effect => effect.deep_unpack());
  139. effects.forEach(effect => {
  140. if (!('type' in effect)) {
  141. this._warn('impossible to get pipelines, effect has not type, resetting');
  142. component[property_name + '_reset']();
  143. return component[property_name];
  144. }
  145. let type = effect.type.deep_unpack();
  146. if (typeof type !== 'string') {
  147. this._warn('impossible to get pipelines, effect type is not a string, resetting');
  148. component[property_name + '_reset']();
  149. return component[property_name];
  150. }
  151. if (!('id' in effect)) {
  152. this._warn('impossible to get pipelines, effect has not id, resetting');
  153. component[property_name + '_reset']();
  154. return component[property_name];
  155. }
  156. let id = effect.id.deep_unpack();
  157. if (typeof id !== 'string') {
  158. this._warn('impossible to get pipelines, effect id is not a string, resetting');
  159. component[property_name + '_reset']();
  160. return component[property_name];
  161. }
  162. let params = {};
  163. if ('params' in effect)
  164. params = effect.params.deep_unpack();
  165. if (!(params && typeof params === 'object' && params.constructor === Object)) {
  166. this._warn('impossible to get pipelines, effect params is not an object, resetting');
  167. component[property_name + '_reset']();
  168. return component[property_name];
  169. }
  170. Object.keys(params).forEach(param_key => {
  171. params[param_key] = params[param_key].deep_unpack();
  172. });
  173. effect.type = type;
  174. effect.id = id;
  175. effect.params = params;
  176. });
  177. pipeline.name = name;
  178. pipeline.effects = effects;
  179. });
  180. return pips;
  181. },
  182. set(pips) {
  183. let pipelines = {};
  184. Object.keys(pips).forEach(pipeline_id => {
  185. let pipeline = pips[pipeline_id];
  186. if (!(pipeline && typeof pipeline === 'object' && pipeline.constructor === Object)) {
  187. this._warn('impossible to set pipelines, pipeline is not an object');
  188. return;
  189. }
  190. if (!('name' in pipeline)) {
  191. this._warn('impossible to set pipelines, pipeline has no name');
  192. return;
  193. }
  194. if (typeof pipeline.name !== 'string') {
  195. this._warn('impossible to set pipelines, pipeline name is not a string');
  196. return;
  197. }
  198. if (!('effects' in pipeline)) {
  199. this._warn('impossible to set pipelines, pipeline has no effect');
  200. return;
  201. }
  202. if (!Array.isArray(pipeline.effects)) {
  203. this._warn('impossible to set pipelines, effects is not an array');
  204. return;
  205. }
  206. let gvariant_effects = [];
  207. pipeline.effects.forEach(effect => {
  208. if (!(effect instanceof Object)) {
  209. this._warn('impossible to set pipelines, effect is not an object');
  210. return;
  211. }
  212. if (!('type' in effect)) {
  213. this._warn('impossible to set pipelines, effect has not type');
  214. return;
  215. }
  216. if (typeof effect.type !== 'string') {
  217. this._warn('impossible to set pipelines, effect type is not a string');
  218. return;
  219. }
  220. if (!('id' in effect)) {
  221. this._warn('impossible to set pipelines, effect has not id');
  222. return;
  223. }
  224. if (typeof effect.id !== 'string') {
  225. this._warn('impossible to set pipelines, effect id is not a string');
  226. return;
  227. }
  228. let params = {};
  229. if ('params' in effect) {
  230. params = effect.params;
  231. }
  232. let gvariant_params = {};
  233. Object.keys(params).forEach(param_key => {
  234. let param = params[param_key];
  235. if (typeof param === 'boolean')
  236. gvariant_params[param_key] = GLib.Variant.new_boolean(param);
  237. else if (typeof param === 'number') {
  238. if (Number.isInteger(param))
  239. gvariant_params[param_key] = GLib.Variant.new_int32(param);
  240. else
  241. gvariant_params[param_key] = GLib.Variant.new_double(param);
  242. } else if (typeof param === 'string')
  243. gvariant_params[param_key] = GLib.Variant.new_string(param);
  244. else if (Array.isArray(param) && param.length == 4)
  245. gvariant_params[param_key] = new GLib.Variant("(dddd)", param);
  246. else
  247. this._warn('impossible to set pipeline, effect parameter type is unknown');
  248. });
  249. gvariant_effects.push(
  250. new GLib.Variant("a{sv}", {
  251. type: GLib.Variant.new_string(effect.type),
  252. id: GLib.Variant.new_string(effect.id),
  253. params: new GLib.Variant("a{sv}", gvariant_params)
  254. })
  255. );
  256. });
  257. pipelines[pipeline_id] = {
  258. name: GLib.Variant.new_string(pipeline.name),
  259. effects: new GLib.Variant("av", gvariant_effects)
  260. };
  261. });
  262. let val = new GLib.Variant("a{sa{sv}}", pipelines);
  263. component_settings.set_value(key.name, val);
  264. }
  265. });
  266. break;
  267. }
  268. component[property_name + '_reset'] = function () {
  269. return component_settings.reset(key.name);
  270. };
  271. component[property_name + '_signal_ids'] = [];
  272. component[property_name + '_changed'] = function (cb) {
  273. component[property_name + '_signal_ids'].push(
  274. component_settings.connect('changed::' + key.name, cb)
  275. );
  276. };
  277. component[property_name + '_disconnect'] = function () {
  278. component[property_name + '_signal_ids'].forEach(
  279. id => component_settings.disconnect(id)
  280. );
  281. component[property_name + '_signal_ids'] = [];
  282. };
  283. });
  284. });
  285. };
  286. /// Reset the preferences.
  287. reset() {
  288. this.keys.forEach(bundle => {
  289. let component = this;
  290. if (bundle.component !== "general") {
  291. let bundle_component = bundle.component.replaceAll('-', '_');
  292. component = this[bundle_component];
  293. }
  294. bundle.schemas.forEach(key => {
  295. let property_name = this.get_property_name(key.name);
  296. component[property_name + '_reset']();
  297. });
  298. });
  299. this.emit('reset', true);
  300. }
  301. /// From the gschema name, returns the name of the associated property on
  302. /// the Settings object.
  303. get_property_name(name) {
  304. return name.replaceAll('-', '_').toUpperCase();
  305. }
  306. /// Remove all connections managed by the Settings object, i.e. created with
  307. /// `settings.PROPERTY_changed(callback)`.
  308. disconnect_all_settings() {
  309. this.keys.forEach(bundle => {
  310. let component = this;
  311. if (bundle.component !== "general") {
  312. let bundle_component = bundle.component.replaceAll('-', '_');
  313. component = this[bundle_component];
  314. }
  315. bundle.schemas.forEach(key => {
  316. let property_name = this.get_property_name(key.name);
  317. component[property_name + '_disconnect']();
  318. });
  319. });
  320. }
  321. _warn(str) {
  322. console.warn(`[Blur my Shell > settings] ${str}`);
  323. }
  324. };
  325. Signals.addSignalMethods(Settings.prototype);