Skip to content

Commit

Permalink
Fixing issues with configuration and renamed globals.js to settings.js
Browse files Browse the repository at this point in the history
  • Loading branch information
kbuffington committed Jan 7, 2021
1 parent 27e7536 commit c45664b
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 85 deletions.
4 changes: 2 additions & 2 deletions georgia-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ includeFiles([
'js\\configuration.js', // reads/write from config file. The actual configuration values are specified in globals.js
'js\\helpers.js',
'js\\CaTRoX_QWR\\Common.js',
'js\\hyperlinks.js', // used in globals.js
'js\\globals.js', // if we stop using PanelProperties can move this above Common.js
'js\\hyperlinks.js', // used in settings.js
'js\\settings.js', // must be below hyperlinks.js and Common.js
'js\\CaTRoX_QWR\\Utility_LinkedList.js',
'js\\CaTRoX_QWR\\Control_ContextMenu.js',
'js\\CaTRoX_QWR\\Control_Scrollbar.js',
Expand Down
42 changes: 32 additions & 10 deletions js/configuration.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const ConfigurationObjectType = {
Array: 'array',
Object: 'object',
Value: 'value' // not currently handled
};

/**
Expand All @@ -13,7 +14,7 @@ class ConfigurationObjectSchema {
/**
* @param {string} name The name to be used for the object in the configuration file. i.e. if the object is `grid: {}`, then name should be `'grid'`
* @param {string} container The type of container for the object. Should be of ConfigurationObjectType.
* @param {Array<FieldDefinition>=} fields The fields for each entry in the object. If undefined, uses key/value pairs.
* @param {Array<FieldDefinition>=} fields The fields for each entry in the object. If undefined, uses key/value pairs for objects, or comma separated values for arrays.
* @param {string=} comment Adds a '//' field as first entry in the object. Used for explaining things to the user.
*/
constructor(name, container, fields = undefined, comment = undefined) {
Expand Down Expand Up @@ -112,7 +113,8 @@ class Configuration {
return config;
}
catch (e) {
throw new ThemeError(`<ERROR: Could not read from ${this.path}, or JSON may be invalid.>`)
throw new ThemeError(`<ERROR: Could not read from ${this.path}, or JSON may be invalid. ` +
`If file exists please delete or restore from a backup.>`)
}
}

Expand All @@ -127,11 +129,24 @@ class Configuration {
p.WriteLine(' on the next reload. To ensure changes are not lost, reload the theme immediately');
p.WriteLine(' after manually changing values. */');
p.WriteLine('{');
p.WriteLine(`\t"version": "${currentVersion}",`)
this._configuration.forEach((conf, i) => {
const container = conf.definition.container === ConfigurationObjectType.Array ? '[' : '{';
p.WriteLine(`\t"${conf.definition.name}": ${container}`);
if (conf.definition.comment) {
p.WriteLine(`\t\t// ${conf.definition.comment}`);
let line = conf.definition.comment;
let done = false;
while (!done) {
const lineLen = 100;
if (line.length < lineLen) {
p.WriteLine(`\t\t// ${line.trim()}`);
done = true;
} else {
const idx = line.lastIndexOf(' ', lineLen);
p.WriteLine(`\t\t// ${line.substr(0, idx).trim()}`);
line = line.substr(idx);
}
}
}
if (conf.definition.fields) {
// array of fields
Expand All @@ -150,13 +165,20 @@ class Configuration {
p.WriteLine(`\t\t${entry}${i < conf.values.length - 1 ? ',' : ''}${comment}`);
}
} else {
// object with key/value pairs
const keys = Object.keys(conf.values);
keys.forEach((key, i) => {
const comment = conf.comments[key] ? ` // ${conf.comments[key]}` : '';
const quotes = typeof conf.values[key] === 'string' ? '"': '';
p.WriteLine(`\t\t"${key}": ${quotes}${conf.values[key]}${quotes}${i < keys.length - 1 ? ',' : ''}${comment}`)
});
if (conf.definition.container === ConfigurationObjectType.Array) {
// array of comma separated entries
conf.values.forEach((val, i) => {
p.WriteLine(`\t\t"${val.replace(/\\/g, '\\\\')}"${i < conf.values.length - 1 ? ',' : ''}`);
})
} else {
// object with key/value pairs
const keys = Object.keys(conf.values);
keys.forEach((key, i) => {
const comment = conf.comments[key] ? ` // ${conf.comments[key]}` : '';
const quotes = typeof conf.values[key] === 'string' ? '"': '';
p.WriteLine(`\t\t"${key}": ${quotes}${conf.values[key]}${quotes}${i < keys.length - 1 ? ',' : ''}${comment}`)
});
}
}
const closeContainer = conf.definition.container === ConfigurationObjectType.Array ? ']' : '}';
p.WriteLine(`\t${closeContainer}${i < this._configuration.length - 1 ? ',' : ''}`);
Expand Down
38 changes: 18 additions & 20 deletions js/georgia-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1159,10 +1159,10 @@ function onOptionsMenu(x, y) {
menu.addSeparator();

const debugMenu = new Menu('Debug Settings');
debugMenu.addToggleItem('Enable debug output', pref, 'show_debug_log');
debugMenu.addItem('Enable theme debug output', pref.show_theme_log, () => {
pref.show_theme_log = !pref.show_theme_log;
if (pref.show_theme_log) {
debugMenu.addToggleItem('Enable debug output', settings, 'showDebugLog');
debugMenu.addItem('Enable theme debug output', settings.showThemeLog, () => {
settings.showThemeLog = !settings.showThemeLog;
if (settings.showThemeLog) {
albumart = null;
on_playback_new_track(fb.GetNowPlaying());
}
Expand All @@ -1175,7 +1175,7 @@ function onOptionsMenu(x, y) {

menu.addSeparator();

menu.addToggleItem('Lock right click...', pref, 'locked');
menu.addToggleItem('Lock right click...', settings, 'locked');
menu.addItem('Restart foobar', false, () => { fb.RunMainMenuCommand("File/Restart"); });

var idx = menu.trackPopupMenu(x, y);
Expand Down Expand Up @@ -1497,19 +1497,19 @@ function on_metadb_changed(handle_list, fromhook) {
str.length = (h > 0 ? h + ":" + (m < 10 ? "0" : '') + m : m) + ":" + (s < 10 ? "0" : '') + s;

str.grid = [];
for (let k = 0; k < tf.grid.length; k++) {
let val = $(tf.grid[k].val);
if (val && tf.grid[k].label) {
if (tf.grid[k].age) {
for (let k = 0; k < metadataGrid.length; k++) {
let val = $(metadataGrid[k].val);
if (val && metadataGrid[k].label) {
if (metadataGrid[k].age) {
val = $('$date(' + val + ')'); // never show time
var age = calcAgeDateString(val);
if (age) {
val += ' (' + age + ')';
}
}
str.grid.push({
age: tf.grid[k].age,
label: tf.grid[k].label,
age: metadataGrid[k].age,
label: metadataGrid[k].label,
val: val,
});
}
Expand Down Expand Up @@ -1673,7 +1673,7 @@ function on_mouse_rbtn_up(x, y, m) {
trace_call && console.log(qwr_utils.function_name());
return library.on_mouse_rbtn_up(x, y, m);
} else
return pref.locked;
return settings.locked;
}

function on_mouse_move(x, y, m) {
Expand All @@ -1683,7 +1683,7 @@ function on_mouse_move(x, y, m) {
state.mouse_x = x;
state.mouse_y = y;

if (settings.hide_cursor) {
if (settings.hideCursor) {
clearTimeout(hideCursorTimer);
hideCursorTimer = setTimeout(() => {
// if there's a menu id (i.e. a menu is down) we don't want the cursor to ever disappear
Expand Down Expand Up @@ -1962,7 +1962,7 @@ function on_playback_stop(reason) {
}

function on_playback_starting(cmd, is_paused) {
if (settings.hide_cursor) {
if (settings.hideCursor) {
window.SetCursor(-1); // hide cursor
}
refreshPlayButton();
Expand Down Expand Up @@ -2537,13 +2537,11 @@ function fetchNewArtwork(metadb) {
getThemeColors(albumart);
ResizeArtwork(true);
} else {
for (let k = 0; k < tf.glob_paths.length; k++) {
aa_list = aa_list.concat(utils.Glob($(tf.glob_paths[k])));
}
const pattern = new RegExp('(cd|vinyl|' + settings.cdart_basename + ')([0-9]*|[a-h])\.png', 'i');
const imageTest = /jpg|png$/i;
aa_list = tf.imgPaths.map(path => utils.Glob($(path))).flat();
const pattern = new RegExp('(cd|vinyl|' + settings.cdArtBasename + ')([0-9]*|[a-h])\.png', 'i');
const imageType = /jpg|png$/i;
// remove duplicates and cd/vinyl art and make sure all files are jpg or pngs
aa_list = [... new Set(aa_list)].filter(path => !pattern.test(path) && imageTest.test(path));
aa_list = [... new Set(aa_list)].filter(path => !pattern.test(path) && imageType.test(path));

if (aa_list.length) {
noArtwork = false;
Expand Down
2 changes: 1 addition & 1 deletion js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function getMetaValues(name, metadb = undefined) {
* @type {function(...*):void} var_args
*/
const debugLog = (var_args) => {
if (pref.show_debug_log) console.log(var_args);
if (settings.showDebugLog) console.log(var_args);
}

/**
Expand Down
Loading

0 comments on commit c45664b

Please sign in to comment.