Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonAlling committed Mar 18, 2017
2 parents 88a1add + d9edd45 commit c511cce
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
17 changes: 15 additions & 2 deletions js/Zatacka.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ const Zatacka = ((window, document) => {
description: TEXT.pref_label_description_confirm_quit,
default: true,
},
{
type: BooleanPreference,
key: STRINGS.pref_key_confirm_reload,
label: TEXT.pref_label_confirm_reload,
description: TEXT.pref_label_description_confirm_reload,
default: true,
},
{
type: MultichoicePreference,
key: STRINGS.pref_key_cursor,
Expand Down Expand Up @@ -385,8 +392,14 @@ const Zatacka = ((window, document) => {
} else {
quitGame();
}
} else if (pressedKey === KEY_RELOAD && game.shouldShowReloadConfirmationOnReloadKey() && !(guiController.isShowingDialog())) {
guiController.showDialog(config.dialogs.confirmation_reload, reload);
} else if (pressedKey === KEY_RELOAD) {
if (preferenceManager.getCached(STRINGS.pref_key_confirm_reload) === true) {
if (game.shouldShowReloadConfirmationOnReloadKey() && !(guiController.isShowingDialog())) {
guiController.showDialog(config.dialogs.confirmation_reload, reload);
}
} else {
reload();
}
}
}

Expand Down
24 changes: 20 additions & 4 deletions js/lib/preferences/PreferenceManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@ function PreferenceManager(preferencesData) {
CACHED_PREFERENCES_WITH_VALUES.forEach((preferenceWithValue) => {
const key = preferenceWithValue.preference.key;
try {
preferenceWithValue.value = getSaved(key);
const savedValue = getSaved(key);
const defaultValue = preferenceWithValue.preference.getDefaultValue();
if (savedValue === null) {
log(`Using default value '${defaultValue}' for preference '${key}' since there was no saved value.`);
}
preferenceWithValue.value = savedValue !== null ? savedValue : defaultValue;
} catch(e) {
logWarning(`Using default value '${preferenceWithValue.preference.getDefaultValue()}' for preference '${key}' since no saved value could be loaded from localStorage.`);
if (e instanceof TypeError) {
logWarning(`Using default value '${defaultValue}' for preference '${key}' since the saved value in localStorage was not a valid one.`);
} else {
logWarning(`Using default value '${defaultValue}' for preference '${key}' since no saved value could be loaded from localStorage.`);
}
}
});

Expand Down Expand Up @@ -113,7 +122,7 @@ function PreferenceManager(preferencesData) {
}
}

function getSaved(key) { // throws SecurityError etc
function getSaved(key) { // throws SecurityError, TypeError etc
if (!preferenceExists(key)) {
throw new Error(`There is no preference with key '${key}'.`);
}
Expand All @@ -125,7 +134,14 @@ function PreferenceManager(preferencesData) {
logError(`Failed to load saved value for preference '${key}' from localStorage. The following error was thrown:\n${e}`);
throw e; // likely a SecurityError, but could be others as well
}
return isValidPreferenceValue(key, pref.constructor.parse(savedValue)) ? pref.constructor.parse(savedValue) : getDefaultValue(key);
if (savedValue === null) {
// There was no saved value.
return null;
} else if (isValidPreferenceValue(key, pref.constructor.parse(savedValue))) {
return pref.constructor.parse(savedValue);
} else {
throw new TypeError(`'${savedValue}' could not be parsed to a valid value for preference '${pref}'.`);
}
}

function getCached(key) {
Expand Down
3 changes: 3 additions & 0 deletions js/locales/Zatacka.en_US.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,8 @@ const TEXT = (() => {

pref_label_confirm_quit: `Confirm quit`,
pref_label_description_confirm_quit: `Ask for confirmation before quitting to the main menu.`,

pref_label_confirm_reload: `Confirm reload`,
pref_label_description_confirm_reload: `Ask for confirmation before reloading the application.`,
});
})();
1 change: 1 addition & 0 deletions js/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ const STRINGS = (() => Object.freeze({
pref_value_hints_none: "none",

pref_key_confirm_quit: "confirm_quit",
pref_key_confirm_reload: "confirm_reload",
pref_key_prevent_spawnkill: "prevent_spawnkill",
}))();

0 comments on commit c511cce

Please sign in to comment.