From 6006f94e715f1ca9e0f54ab44fe2da03a07e1644 Mon Sep 17 00:00:00 2001 From: Albert Graef <aggraef@gmail.com> Date: Sun, 13 Sep 2020 00:20:50 +0200 Subject: [PATCH] Add user-defined GUI presets. This discovers user-defined GUI presets (css files) in the css subdirectory of the gui folder, and adds them at the end of the corresponding dropdown menu in the GUI prefs. It is a convenience intended for users and developers who want to experiment with their own GUI styles. The predefined presets are still hardcoded in the dialog_prefs.html file, and are always available (and displayed first in the dropdown menu). If the preset saved in the prefs doesn't exist anymore (e.g., because the user removed its css file), it will be ignored at startup and the default style will be used instead. Note that this isn't entirely foolproof since the fallback may not exist if the user also removed the default.css file. But this could also happen previously when the css directory gets messed up, and in such a case the user simply gets what he or she deserves. --- pd/nw/dialog_prefs.html | 26 +++++++++++++++++++++++--- pd/nw/pdgui.js | 7 ++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/pd/nw/dialog_prefs.html b/pd/nw/dialog_prefs.html index c1f5c934c..e4abcae8d 100644 --- a/pd/nw/dialog_prefs.html +++ b/pd/nw/dialog_prefs.html @@ -1036,9 +1036,29 @@ function autopatch_yoffset_toggle(checked) { function gui_prefs_callback(name, show_grid, save_zoom, browser_doc, browser_path, browser_init, autopatch_yoffset) { - var s = document.getElementById("gui_preset"), - i; - for (i = 0; i < s.options.length; i++) { + var s = document.getElementById("gui_preset"); + + // ag: scan the css subdir for user-defined styles + var predefined = // built-in styles + [ "c64.css", "default.css", "dejavu.css", "extended.css", + "footgun.css", "inverted.css", "solarized.css", + "solarized_inverted.css", "strongbad.css", "subdued.css", + "vanilla.css", "vanilla_inverted.css"]; + var fs = require('fs'); + var base = process.platform === "darwin" ? pdgui.get_lib_dir() : "."; + var files = fs.readdirSync(base + "/css/"); + files.forEach(file => { + var pos = file.lastIndexOf('.'); + var ext = file.substr(pos); + if (ext === ".css" && predefined.indexOf(file) === -1) { + var base = file.substr(0, pos); + var option = document.createElement("option"); + option.text = base; + s.add(option); + } + }); + + for (var i = 0; i < s.options.length; i++) { if (s.options[i].value.toLowerCase() === name.toLowerCase()) { s.selectedIndex = i; break; diff --git a/pd/nw/pdgui.js b/pd/nw/pdgui.js index ffcc1f526..3e176ce5e 100644 --- a/pd/nw/pdgui.js +++ b/pd/nw/pdgui.js @@ -5973,7 +5973,12 @@ var skin = exports.skin = (function () { return dir + preset + ".css"; }, set: function (name) { - preset = name; + // ag: if the preset doesn't exist (e.g., user preset that + // has disappeared), just stick to the default + var base = process.platform === "darwin" ? (lib_dir + "/") : ""; + if (fs.existsSync(base + dir + name + ".css")) { + preset = name; + } for (id in patchwin) { if (patchwin.hasOwnProperty(id) && patchwin[id]) { set_css(patchwin[id].window); -- GitLab