-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Set the font size before opening the documents #7185
Changes from 1 commit
79ccbe9
a5a6394
ebb6b7a
8d0abbd
25ee232
bb3e75d
3959dd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,12 +78,6 @@ define(function (require, exports, module) { | |
| */ | ||
| var LINE_HEIGHT = 1.25; | ||
|
|
||
| /** | ||
| * @private | ||
| * @type {boolean} | ||
| */ | ||
| var _fontSizePrefsLoaded = false; | ||
|
|
||
|
|
||
| /** | ||
| * @private | ||
|
|
@@ -95,39 +89,44 @@ define(function (require, exports, module) { | |
|
|
||
| /** | ||
| * @private | ||
| * Sets the font size and restores the scroll position as best as possible. | ||
| * @param {string} fontSizeStyle A string with the font size and the size unit | ||
| * @param {string} lineHeightStyle A string with the line height and a the size unit | ||
| * Add the styles used to update the font size | ||
| * @param {string} fontSizeStyle A string with the font size and the size unit | ||
| * @param {string} lineHeightStyle A string with the line height and a the size unit | ||
| */ | ||
| function _setSizeAndRestoreScroll(fontSizeStyle, lineHeightStyle) { | ||
| var editor = EditorManager.getCurrentFullEditor(), | ||
| oldWidth = editor._codeMirror.defaultCharWidth(), | ||
| oldHeight = editor.getTextHeight(), | ||
| scrollPos = editor.getScrollPos(); | ||
|
|
||
| function _addDynamicFontSize(fontSizeStyle, lineHeightStyle) { | ||
| // It's necessary to inject a new rule to address all editors. | ||
| _removeDynamicFontSize(); | ||
| var style = $("<style type='text/css'></style>").attr("id", DYNAMIC_FONT_STYLE_ID); | ||
| style.html(".CodeMirror {" + | ||
| "font-size: " + fontSizeStyle + " !important;" + | ||
| "line-height: " + lineHeightStyle + " !important;}"); | ||
| $("head").append(style); | ||
| } | ||
|
|
||
| /** | ||
| * @private | ||
| * Sets the font size and restores the scroll position as best as possible. | ||
| * @param {string} fontSizeStyle A string with the font size and the size unit | ||
| * @param {string} lineHeightStyle A string with the line height and a the size unit | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Old typo: |
||
| */ | ||
| function _setSizeAndRestoreScroll(fontSizeStyle, lineHeightStyle) { | ||
| var editor = EditorManager.getCurrentFullEditor(), | ||
| oldWidth = editor._codeMirror.defaultCharWidth(), | ||
| oldHeight = editor.getTextHeight(), | ||
| scrollPos = editor.getScrollPos(); | ||
|
|
||
| _addDynamicFontSize(fontSizeStyle, lineHeightStyle); | ||
| editor.refreshAll(); | ||
|
|
||
| // Scroll the document back to its original position, but not on the first load since the position | ||
| // was saved with the new height and already been restored. | ||
| if (_fontSizePrefsLoaded) { | ||
| // Calculate the new scroll based on the old font sizes and scroll position | ||
| var newWidth = editor._codeMirror.defaultCharWidth(), | ||
| newHeight = editor.getTextHeight(), | ||
| deltaX = scrollPos.x / oldWidth, | ||
| deltaY = scrollPos.y / oldHeight, | ||
| scrollPosX = scrollPos.x + Math.round(deltaX * (newWidth - oldWidth)), | ||
| scrollPosY = scrollPos.y + Math.round(deltaY * (newHeight - oldHeight)); | ||
| // Calculate the new scroll based on the old font sizes and scroll position | ||
| var newWidth = editor._codeMirror.defaultCharWidth(), | ||
| newHeight = editor.getTextHeight(), | ||
| deltaX = scrollPos.x / oldWidth, | ||
| deltaY = scrollPos.y / oldHeight, | ||
| scrollPosX = scrollPos.x + Math.round(deltaX * (newWidth - oldWidth)), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not related to the two issues you're fixing, but I wonder this calculation is always correct for a long line that can wrap the horizontal scroll position to a new line with Word Wrap on.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if Word Wrap is on, you can't scroll horizontally, so even trying to make ti scroll horizontally, it will always stay at 0.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, you're right! |
||
| scrollPosY = scrollPos.y + Math.round(deltaY * (newHeight - oldHeight)); | ||
|
|
||
| editor.setScrollPos(scrollPosX, scrollPosY); | ||
| } | ||
| editor.setScrollPos(scrollPosX, scrollPosY); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -158,6 +157,7 @@ define(function (require, exports, module) { | |
|
|
||
| var fsNew = fsOld + (delta * adjustment); | ||
| var lhNew = lhOld; | ||
|
|
||
| if (fsUnits === lhUnits) { | ||
| lhNew = fsNew * LINE_HEIGHT; | ||
| if (lhUnits === "px") { | ||
|
|
@@ -177,6 +177,9 @@ define(function (require, exports, module) { | |
|
|
||
| _setSizeAndRestoreScroll(fsStr, lhStr); | ||
|
|
||
| PreferencesManager.setViewState("fontSizeString", fsStr); | ||
| PreferencesManager.setViewState("lineHeightString", lhStr); | ||
|
|
||
| $(exports).triggerHandler("fontSizeChange", [adjustment, fsStr, lhStr]); | ||
| return true; | ||
| } | ||
|
|
@@ -215,14 +218,6 @@ define(function (require, exports, module) { | |
| CommandManager.get(Commands.VIEW_DECREASE_FONT_SIZE).setEnabled(true); | ||
| CommandManager.get(Commands.VIEW_RESTORE_FONT_SIZE).setEnabled(true); | ||
| } | ||
|
|
||
| // Font Size preferences only need to be loaded one time | ||
| if (!_fontSizePrefsLoaded) { | ||
| _removeDynamicFontSize(); | ||
| _adjustFontSize(PreferencesManager.getViewState("fontSizeAdjustment")); | ||
| _fontSizePrefsLoaded = true; | ||
| } | ||
|
|
||
| } else { | ||
| // No current document so disable all of the Font Size commands | ||
| CommandManager.get(Commands.VIEW_INCREASE_FONT_SIZE).setEnabled(false); | ||
|
|
@@ -231,6 +226,18 @@ define(function (require, exports, module) { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Restores the Font Size and Line Height using the saved strings | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: We can take out Line Height here. |
||
| */ | ||
| function restoreFontSize() { | ||
| var fsStr = PreferencesManager.getViewState("fontSizeString"), | ||
| lhStr = PreferencesManager.getViewState("lineHeightString"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we may not need |
||
|
|
||
| if (fsStr && lhStr) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With these new view states Update: Maybe we can refactor the code in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original font size is
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. I'll use LINE_HEIGHT in place of |
||
| _addDynamicFontSize(fsStr, lhStr); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /** | ||
|
|
@@ -336,4 +343,6 @@ define(function (require, exports, module) { | |
|
|
||
| // Update UI when Brackets finishes loading | ||
| AppInit.appReady(_updateUI); | ||
|
|
||
| exports.restoreFontSize = restoreFontSize; | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo:
a the size unit. should removea.