From 83dd9d3360a51aee5c279f43997028252af7552d Mon Sep 17 00:00:00 2001 From: Marcel Gerber Date: Mon, 1 Jun 2015 00:40:40 +0200 Subject: [PATCH 1/2] Make font settings react to pref changes --- src/view/ViewCommandHandlers.js | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/view/ViewCommandHandlers.js b/src/view/ViewCommandHandlers.js index 59e4d8dccb0..43cef009e39 100644 --- a/src/view/ViewCommandHandlers.js +++ b/src/view/ViewCommandHandlers.js @@ -49,6 +49,10 @@ define(function (require, exports, module) { var prefs = PreferencesManager.getExtensionPrefs("fonts"); + // These variables contain the preference values. They are used to no-op in case nothing changed + // and updated *after* the call to setFontSize/setFontFamily. + var fontSizeValue, fontFamilyValue; + /** * @const * @type {string} @@ -194,9 +198,7 @@ define(function (require, exports, module) { * @param {string} fontSize The font size with size unit as 'px' or 'em' */ function setFontSize(fontSize) { - var oldValue = prefs.get("fontSize"); - - if (oldValue === fontSize) { + if (fontSizeValue === fontSize) { return; } @@ -214,7 +216,7 @@ define(function (require, exports, module) { } }); - exports.trigger("fontSizeChange", fontSize, oldValue); + exports.trigger("fontSizeChange", fontSize, fontSizeValue); prefs.set("fontSize", fontSize); } @@ -232,10 +234,9 @@ define(function (require, exports, module) { * @param {string} fontFamily The font family to be set. It can be a string with multiple comma separated fonts */ function setFontFamily(fontFamily) { - var editor = EditorManager.getCurrentFullEditor(), - oldValue = prefs.get("fontFamily"); + var editor = EditorManager.getCurrentFullEditor(); - if (oldValue === fontFamily) { + if (fontFamilyValue === fontFamily) { return; } @@ -244,7 +245,7 @@ define(function (require, exports, module) { _addDynamicFontFamily(fontFamily); } - exports.trigger("fontFamilyChange", fontFamily, oldValue); + exports.trigger("fontFamilyChange", fontFamily, fontFamilyValue); prefs.set("fontFamily", fontFamily); if (editor) { @@ -333,8 +334,10 @@ define(function (require, exports, module) { * Initializes the different settings that need to loaded */ function init() { - _addDynamicFontFamily(prefs.get("fontFamily")); - _addDynamicFontSize(prefs.get("fontSize")); + fontFamilyValue = prefs.get("fontFamily"); + _addDynamicFontFamily(fontFamilyValue); + fontSizeValue = prefs.get("fontSize"); + _addDynamicFontSize(fontSizeValue); _updateUI(); } @@ -488,8 +491,14 @@ define(function (require, exports, module) { PreferencesManager.convertPreferences(module, {"fontSizeAdjustment": "user"}, true, _convertToNewViewState); - prefs.definePreference("fontSize", "string", DEFAULT_FONT_SIZE + "px"); - prefs.definePreference("fontFamily", "string", DEFAULT_FONT_FAMILY); + prefs.definePreference("fontSize", "string", DEFAULT_FONT_SIZE + "px").on("change", function () { + setFontSize(prefs.get("fontSize")); + fontSizeValue = prefs.get("fontSize"); + }); + prefs.definePreference("fontFamily", "string", DEFAULT_FONT_FAMILY).on("change", function () { + setFontFamily(prefs.get("fontFamily")); + fontFamilyValue = prefs.get("fontFamily"); + }); // Update UI when opening or closing a document MainViewManager.on("currentFileChange", _updateUI); From 3c01d66636b8bcd2654edf51ac48b77c57db893d Mon Sep 17 00:00:00 2001 From: Marcel Gerber Date: Mon, 1 Jun 2015 15:44:24 +0200 Subject: [PATCH 2/2] Add proper JSDoc, make sure methods are only called once --- src/view/ViewCommandHandlers.js | 37 +++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/view/ViewCommandHandlers.js b/src/view/ViewCommandHandlers.js index 43cef009e39..0a114a8e656 100644 --- a/src/view/ViewCommandHandlers.js +++ b/src/view/ViewCommandHandlers.js @@ -49,9 +49,20 @@ define(function (require, exports, module) { var prefs = PreferencesManager.getExtensionPrefs("fonts"); - // These variables contain the preference values. They are used to no-op in case nothing changed - // and updated *after* the call to setFontSize/setFontFamily. - var fontSizeValue, fontFamilyValue; + + /** + * @private + * The currently present font size. Used to detect no-op changes. + * @type {string} + */ + var currFontSize; + + /** + * @private + * The currently present font family. Used to detect no-op changes. + * @type {string} + */ + var currFontFamily; /** * @const @@ -198,7 +209,7 @@ define(function (require, exports, module) { * @param {string} fontSize The font size with size unit as 'px' or 'em' */ function setFontSize(fontSize) { - if (fontSizeValue === fontSize) { + if (currFontSize === fontSize) { return; } @@ -216,7 +227,8 @@ define(function (require, exports, module) { } }); - exports.trigger("fontSizeChange", fontSize, fontSizeValue); + exports.trigger("fontSizeChange", fontSize, currFontSize); + currFontSize = fontSize; prefs.set("fontSize", fontSize); } @@ -236,7 +248,7 @@ define(function (require, exports, module) { function setFontFamily(fontFamily) { var editor = EditorManager.getCurrentFullEditor(); - if (fontFamilyValue === fontFamily) { + if (currFontFamily === fontFamily) { return; } @@ -245,7 +257,8 @@ define(function (require, exports, module) { _addDynamicFontFamily(fontFamily); } - exports.trigger("fontFamilyChange", fontFamily, fontFamilyValue); + exports.trigger("fontFamilyChange", fontFamily, currFontFamily); + currFontFamily = fontFamily; prefs.set("fontFamily", fontFamily); if (editor) { @@ -334,10 +347,10 @@ define(function (require, exports, module) { * Initializes the different settings that need to loaded */ function init() { - fontFamilyValue = prefs.get("fontFamily"); - _addDynamicFontFamily(fontFamilyValue); - fontSizeValue = prefs.get("fontSize"); - _addDynamicFontSize(fontSizeValue); + currFontFamily = prefs.get("fontFamily"); + _addDynamicFontFamily(currFontFamily); + currFontSize = prefs.get("fontSize"); + _addDynamicFontSize(currFontSize); _updateUI(); } @@ -493,11 +506,9 @@ define(function (require, exports, module) { prefs.definePreference("fontSize", "string", DEFAULT_FONT_SIZE + "px").on("change", function () { setFontSize(prefs.get("fontSize")); - fontSizeValue = prefs.get("fontSize"); }); prefs.definePreference("fontFamily", "string", DEFAULT_FONT_FAMILY).on("change", function () { setFontFamily(prefs.get("fontFamily")); - fontFamilyValue = prefs.get("fontFamily"); }); // Update UI when opening or closing a document