Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 8971aed

Browse files
committed
Merge pull request #7185 from adobe/tom/issue-7093
Set the font size before opening the documents
2 parents db59b6f + 3959dd0 commit 8971aed

4 files changed

Lines changed: 103 additions & 90 deletions

File tree

src/brackets.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ define(function (require, exports, module) {
100100
ColorUtils = require("utils/ColorUtils"),
101101
CodeInspection = require("language/CodeInspection"),
102102
NativeApp = require("utils/NativeApp"),
103+
ViewCommandHandlers = require("view/ViewCommandHandlers"),
103104
_ = require("thirdparty/lodash");
104105

105106
// Load modules that self-register and just need to get included in the main project
@@ -108,7 +109,6 @@ define(function (require, exports, module) {
108109
require("editor/EditorStatusBar");
109110
require("editor/EditorCommandHandlers");
110111
require("editor/EditorOptionHandlers");
111-
require("view/ViewCommandHandlers");
112112
require("help/HelpCommandHandlers");
113113
require("search/FindInFiles");
114114
require("search/FindReplace");
@@ -221,6 +221,7 @@ define(function (require, exports, module) {
221221
// Load the initial project after extensions have loaded
222222
extensionLoaderPromise.always(function () {
223223
// Finish UI initialization
224+
ViewCommandHandlers.restoreFontSize();
224225
var initialProjectPath = ProjectManager.getInitialProjectPath();
225226
ProjectManager.openProject(initialProjectPath).always(function () {
226227
_initTest();

src/preferences/PreferenceStorage.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ define(function (require, exports, module) {
218218
var rule = rules[key];
219219
if (!rule && prefCheckCallback) {
220220
rule = prefCheckCallback(key);
221+
} else if (prefCheckCallback) {
222+
// Check whether we have a new preference key-value pair
223+
// for an old preference.
224+
var newRule = prefCheckCallback(key, prefs[key]);
225+
if (newRule) {
226+
rule = _.cloneDeep(newRule);
227+
}
221228
}
222229
if (!rule) {
223230
console.warn("Preferences conversion for ", self._clientID, " has no rule for", key);
@@ -238,6 +245,11 @@ define(function (require, exports, module) {
238245
manager.set(newKey, prefs[key], options);
239246
convertedKeys.push(key);
240247
}
248+
} else if (_.isObject(rule)) {
249+
Object.keys(rule).forEach(function (ruleKey) {
250+
manager.set(ruleKey, rule[ruleKey]);
251+
});
252+
convertedKeys.push(key);
241253
} else {
242254
complete = false;
243255
}

src/styles/brackets_theme_default.less

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,8 @@
144144
*/
145145
.code-font() {
146146
color: @content-color;
147-
// line-height must be specified in px not em because the code font and line number font sizes are different.
148-
// Sizing via em will cause the code and line numbers to misalign
149-
line-height: 15px;
150147
font-size: 12px;
148+
line-height: 1.25;
151149
font-family: "SourceCodePro-Medium", "MS ゴシック", "MS Gothic", monospace;
152150
}
153151

src/view/ViewCommandHandlers.js

Lines changed: 88 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
* Increase Font Size, Decrease Font Size, or Restore Font Size commands.
3131
* The 2nd arg to the listener is the amount of the change. The 3rd arg
3232
* is a string containing the new font size after applying the change.
33-
* The 4th arg is a string containing the new line height after applying
34-
* the change.
3533
*/
3634

3735
define(function (require, exports, module) {
@@ -58,31 +56,25 @@ define(function (require, exports, module) {
5856
* @const
5957
* @private
6058
* The smallest font size in pixels
61-
* @type {int}
59+
* @type {number}
6260
*/
6361
var MIN_FONT_SIZE = 1;
6462

6563
/**
6664
* @const
6765
* @private
6866
* The largest font size in pixels
69-
* @type {int}
67+
* @type {number}
7068
*/
7169
var MAX_FONT_SIZE = 72;
7270

7371
/**
7472
* @const
7573
* @private
76-
* The ratio of line-height to font-size when they use the same units
77-
* @type {float}
74+
* The default font size used only to convert the old fontSizeAdjustment view state to the new fontSizeStyle
75+
* @type {number}
7876
*/
79-
var LINE_HEIGHT = 1.25;
80-
81-
/**
82-
* @private
83-
* @type {boolean}
84-
*/
85-
var _fontSizePrefsLoaded = false;
77+
var DEFAULT_FONT_SIZE = 12;
8678

8779

8880
/**
@@ -93,112 +85,92 @@ define(function (require, exports, module) {
9385
$("#" + DYNAMIC_FONT_STYLE_ID).remove();
9486
}
9587

88+
/**
89+
* @private
90+
* Add the styles used to update the font size
91+
* @param {string} fontSizeStyle A string with the font size and the size unit
92+
*/
93+
function _addDynamicFontSize(fontSizeStyle) {
94+
var style = $("<style type='text/css'></style>").attr("id", DYNAMIC_FONT_STYLE_ID);
95+
style.html(".CodeMirror { font-size: " + fontSizeStyle + " !important; }");
96+
$("head").append(style);
97+
}
98+
9699
/**
97100
* @private
98101
* Sets the font size and restores the scroll position as best as possible.
99-
* @param {string} fontSizeStyle A string with the font size and the size unit
100-
* @param {string} lineHeightStyle A string with the line height and a the size unit
102+
* @param {string=} fontSizeStyle A string with the font size and the size unit
101103
*/
102-
function _setSizeAndRestoreScroll(fontSizeStyle, lineHeightStyle) {
104+
function _setSizeAndRestoreScroll(fontSizeStyle) {
103105
var editor = EditorManager.getCurrentFullEditor(),
104106
oldWidth = editor._codeMirror.defaultCharWidth(),
105-
oldHeight = editor.getTextHeight(),
106-
scrollPos = editor.getScrollPos();
107+
scrollPos = editor.getScrollPos(),
108+
line = editor._codeMirror.lineAtHeight(scrollPos.y, "local");
107109

108-
// It's necessary to inject a new rule to address all editors.
109110
_removeDynamicFontSize();
110-
var style = $("<style type='text/css'></style>").attr("id", DYNAMIC_FONT_STYLE_ID);
111-
style.html(".CodeMirror {" +
112-
"font-size: " + fontSizeStyle + " !important;" +
113-
"line-height: " + lineHeightStyle + " !important;}");
114-
$("head").append(style);
115-
111+
if (fontSizeStyle) {
112+
_addDynamicFontSize(fontSizeStyle);
113+
}
116114
editor.refreshAll();
117115

118-
// Scroll the document back to its original position, but not on the first load since the position
119-
// was saved with the new height and already been restored.
120-
if (_fontSizePrefsLoaded) {
121-
// Calculate the new scroll based on the old font sizes and scroll position
122-
var newWidth = editor._codeMirror.defaultCharWidth(),
123-
newHeight = editor.getTextHeight(),
124-
deltaX = scrollPos.x / oldWidth,
125-
deltaY = scrollPos.y / oldHeight,
126-
scrollPosX = scrollPos.x + Math.round(deltaX * (newWidth - oldWidth)),
127-
scrollPosY = scrollPos.y + Math.round(deltaY * (newHeight - oldHeight));
128-
129-
editor.setScrollPos(scrollPosX, scrollPosY);
130-
}
116+
// Calculate the new scroll based on the old font sizes and scroll position
117+
var newWidth = editor._codeMirror.defaultCharWidth(),
118+
deltaX = scrollPos.x / oldWidth,
119+
scrollPosX = scrollPos.x + Math.round(deltaX * (newWidth - oldWidth)),
120+
scrollPosY = editor._codeMirror.heightAtLine(line, "local");
121+
122+
editor.setScrollPos(scrollPosX, scrollPosY);
131123
}
132124

133125
/**
134126
* @private
135127
* Increases or decreases the editor's font size.
136-
* @param {number} adjustment Negative number to make the font smaller; positive number to make it bigger
128+
* @param {number} adjustment Negative number to make the font smaller; positive number to make it bigger
137129
* @return {boolean} true if adjustment occurred, false if it did not occur
138130
*/
139131
function _adjustFontSize(adjustment) {
140-
var fsStyle = $(".CodeMirror").css("font-size");
141-
var lhStyle = $(".CodeMirror").css("line-height");
142-
143-
var validFont = /^[\d\.]+(px|em)$/;
132+
var fsStyle = $(".CodeMirror").css("font-size"),
133+
validFont = /^[\d\.]+(px|em)$/;
144134

145-
// Make sure the font size and line height are expressed in terms
146-
// we can handle (px or em). If not, simply bail.
147-
if (fsStyle.search(validFont) === -1 || lhStyle.search(validFont) === -1) {
135+
// Make sure that the font size is expressed in terms we can handle (px or em). If not, simply bail.
136+
if (fsStyle.search(validFont) === -1) {
148137
return false;
149138
}
150139

151140
// Guaranteed to work by the validation above.
152-
var fsUnits = fsStyle.substring(fsStyle.length - 2, fsStyle.length);
153-
var lhUnits = lhStyle.substring(lhStyle.length - 2, lhStyle.length);
154-
var delta = (fsUnits === "px") ? 1 : 0.1;
155-
156-
var fsOld = parseFloat(fsStyle.substring(0, fsStyle.length - 2));
157-
var lhOld = parseFloat(lhStyle.substring(0, lhStyle.length - 2));
158-
159-
var fsNew = fsOld + (delta * adjustment);
160-
var lhNew = lhOld;
161-
if (fsUnits === lhUnits) {
162-
lhNew = fsNew * LINE_HEIGHT;
163-
if (lhUnits === "px") {
164-
// Use integer px value to avoid rounding differences
165-
lhNew = Math.ceil(lhNew);
166-
}
167-
}
168-
169-
var fsStr = fsNew + fsUnits;
170-
var lhStr = lhNew + lhUnits;
141+
var fsUnits = fsStyle.substring(fsStyle.length - 2, fsStyle.length),
142+
delta = fsUnits === "px" ? 1 : 0.1,
143+
fsOld = parseFloat(fsStyle.substring(0, fsStyle.length - 2)),
144+
fsNew = fsOld + (delta * adjustment),
145+
fsStr = fsNew + fsUnits;
171146

172147
// Don't let the font size get too small or too large. The minimum font size is 1px or 0.1em
173148
// and the maximum font size is 72px or 7.2em depending on the unit used
174149
if (fsNew < MIN_FONT_SIZE * delta || fsNew > MAX_FONT_SIZE * delta) {
175150
return false;
176151
}
177152

178-
_setSizeAndRestoreScroll(fsStr, lhStr);
153+
_setSizeAndRestoreScroll(fsStr);
154+
PreferencesManager.setViewState("fontSizeStyle", fsStr);
179155

180-
$(exports).triggerHandler("fontSizeChange", [adjustment, fsStr, lhStr]);
156+
$(exports).triggerHandler("fontSizeChange", [adjustment, fsStr]);
181157
return true;
182158
}
183159

184160
/** Increases the font size by 1 */
185161
function _handleIncreaseFontSize() {
186-
if (_adjustFontSize(1)) {
187-
PreferencesManager.setViewState("fontSizeAdjustment", PreferencesManager.getViewState("fontSizeAdjustment") + 1);
188-
}
162+
_adjustFontSize(1);
189163
}
190164

191165
/** Decreases the font size by 1 */
192166
function _handleDecreaseFontSize() {
193-
if (_adjustFontSize(-1)) {
194-
PreferencesManager.setViewState("fontSizeAdjustment", PreferencesManager.getViewState("fontSizeAdjustment") - 1);
195-
}
167+
_adjustFontSize(-1);
196168
}
197169

198170
/** Restores the font size to the original size */
199171
function _handleRestoreFontSize() {
200-
_adjustFontSize(-PreferencesManager.getViewState("fontSizeAdjustment"));
201-
PreferencesManager.setViewState("fontSizeAdjustment", 0);
172+
_setSizeAndRestoreScroll();
173+
PreferencesManager.setViewState("fontSizeStyle");
202174
}
203175

204176

@@ -215,14 +187,6 @@ define(function (require, exports, module) {
215187
CommandManager.get(Commands.VIEW_DECREASE_FONT_SIZE).setEnabled(true);
216188
CommandManager.get(Commands.VIEW_RESTORE_FONT_SIZE).setEnabled(true);
217189
}
218-
219-
// Font Size preferences only need to be loaded one time
220-
if (!_fontSizePrefsLoaded) {
221-
_removeDynamicFontSize();
222-
_adjustFontSize(PreferencesManager.getViewState("fontSizeAdjustment"));
223-
_fontSizePrefsLoaded = true;
224-
}
225-
226190
} else {
227191
// No current document so disable all of the Font Size commands
228192
CommandManager.get(Commands.VIEW_INCREASE_FONT_SIZE).setEnabled(false);
@@ -231,6 +195,31 @@ define(function (require, exports, module) {
231195
}
232196
}
233197

198+
/**
199+
* Restores the font size using the saved style and migrates the old fontSizeAdjustment
200+
* view state to the new fontSizeStyle, when required
201+
*/
202+
function restoreFontSize() {
203+
var fsStyle = PreferencesManager.getViewState("fontSizeStyle"),
204+
fsAdjustment = PreferencesManager.getViewState("fontSizeAdjustment");
205+
206+
if (fsAdjustment) {
207+
// Always remove the old view state even if we also have the new view state.
208+
PreferencesManager.setViewState("fontSizeAdjustment");
209+
210+
if (!fsStyle) {
211+
// Migrate the old view state to the new one.
212+
fsStyle = (DEFAULT_FONT_SIZE + fsAdjustment) + "px";
213+
PreferencesManager.setViewState("fontSizeStyle", fsStyle);
214+
}
215+
}
216+
217+
if (fsStyle) {
218+
_removeDynamicFontSize();
219+
_addDynamicFontSize(fsStyle);
220+
}
221+
}
222+
234223

235224

236225
/**
@@ -319,6 +308,19 @@ define(function (require, exports, module) {
319308
_scrollLine(1);
320309
}
321310

311+
/**
312+
* @private
313+
* Convert the old "fontSizeAdjustment" preference to the new view state.
314+
*
315+
* @param {string} key The key of the preference to be examined for migration
316+
* of old preferences. Not used since we only have one in this module.
317+
* @param {string} value The value of "fontSizeAdjustment" preference
318+
* @return {Object} JSON object for the new view state equivalent to
319+
* the old "fontSizeAdjustment" preference.
320+
*/
321+
function _convertToNewViewState(key, value) {
322+
return { "fontSizeStyle": (DEFAULT_FONT_SIZE + value) + "px" };
323+
}
322324

323325
// Register command handlers
324326
CommandManager.register(Strings.CMD_INCREASE_FONT_SIZE, Commands.VIEW_INCREASE_FONT_SIZE, _handleIncreaseFontSize);
@@ -327,13 +329,13 @@ define(function (require, exports, module) {
327329
CommandManager.register(Strings.CMD_SCROLL_LINE_UP, Commands.VIEW_SCROLL_LINE_UP, _handleScrollLineUp);
328330
CommandManager.register(Strings.CMD_SCROLL_LINE_DOWN, Commands.VIEW_SCROLL_LINE_DOWN, _handleScrollLineDown);
329331

330-
// Initialize the default font size
331-
PreferencesManager.stateManager.definePreference("fontSizeAdjustment", "number", 0);
332-
PreferencesManager.convertPreferences(module, {"fontSizeAdjustment": "user"}, true);
332+
PreferencesManager.convertPreferences(module, {"fontSizeAdjustment": "user"}, true, _convertToNewViewState);
333333

334334
// Update UI when opening or closing a document
335335
$(DocumentManager).on("currentDocumentChange", _updateUI);
336336

337337
// Update UI when Brackets finishes loading
338338
AppInit.appReady(_updateUI);
339+
340+
exports.restoreFontSize = restoreFontSize;
339341
});

0 commit comments

Comments
 (0)