From a6026caeeaca22234514244a89fb344320fff4f8 Mon Sep 17 00:00:00 2001 From: Simon Alling Date: Sat, 18 Mar 2017 16:51:03 +0100 Subject: [PATCH 1/2] Add confirm reload option --- js/Zatacka.js | 17 +++++++++++++++-- js/locales/Zatacka.en_US.js | 3 +++ js/strings.js | 1 + 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/js/Zatacka.js b/js/Zatacka.js index 3e2521d..7a8b6dd 100644 --- a/js/Zatacka.js +++ b/js/Zatacka.js @@ -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, @@ -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(); + } } } diff --git a/js/locales/Zatacka.en_US.js b/js/locales/Zatacka.en_US.js index bbabdae..9e0f793 100644 --- a/js/locales/Zatacka.en_US.js +++ b/js/locales/Zatacka.en_US.js @@ -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.`, }); })(); diff --git a/js/strings.js b/js/strings.js index f845075..f1c25c5 100644 --- a/js/strings.js +++ b/js/strings.js @@ -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", }))(); From d9edd45cab8d053bd5833a3b9639f6f0f3866ad9 Mon Sep 17 00:00:00 2001 From: Simon Alling Date: Sat, 18 Mar 2017 17:18:08 +0100 Subject: [PATCH 2/2] Fix bug with settings with no saved value If the user had never saved any value for some preference, it would be read as null from localStorage, and, for BooleanPreferences, we would interpret that as false. This led to all BooleanPreferences defaulting to false, even if they should not. --- js/lib/preferences/PreferenceManager.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/js/lib/preferences/PreferenceManager.js b/js/lib/preferences/PreferenceManager.js index 57b579e..3822cba 100644 --- a/js/lib/preferences/PreferenceManager.js +++ b/js/lib/preferences/PreferenceManager.js @@ -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.`); + } } }); @@ -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}'.`); } @@ -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) {