-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Set the font size before opening the documents #7185
Changes from 2 commits
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,46 @@ 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 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 the size unit | ||
*/ | ||
function _setSizeAndRestoreScroll(fontSizeStyle, lineHeightStyle) { | ||
var editor = EditorManager.getCurrentFullEditor(), | ||
oldWidth = editor._codeMirror.defaultCharWidth(), | ||
scrollPos = editor.getScrollPos(), | ||
line = editor._codeMirror.lineAtHeight(scrollPos.y, "local"); | ||
console.log(line); | ||
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. You forgot to remove two console.log in this function. 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. Woops. I forgot to recheck the code before pushing. |
||
if (fontSizeStyle && lineHeightStyle) { | ||
_addDynamicFontSize(fontSizeStyle, lineHeightStyle); | ||
} else { | ||
_removeDynamicFontSize(); | ||
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. You can move 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. Good suggestion. |
||
} | ||
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)); | ||
|
||
editor.setScrollPos(scrollPosX, scrollPosY); | ||
} | ||
// Calculate the new scroll based on the old font sizes and scroll position | ||
var newWidth = editor._codeMirror.defaultCharWidth(), | ||
deltaX = scrollPos.x / oldWidth, | ||
scrollPosX = scrollPos.x + Math.round(deltaX * (newWidth - oldWidth)), | ||
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. 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. 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 = editor._codeMirror.heightAtLine(line, "local"); | ||
console.log(scrollPosY); | ||
editor.setScrollPos(scrollPosX, scrollPosY); | ||
} | ||
|
||
/** | ||
|
@@ -158,6 +159,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,28 +179,28 @@ define(function (require, exports, module) { | |
|
||
_setSizeAndRestoreScroll(fsStr, lhStr); | ||
|
||
PreferencesManager.setViewState("fontSizeStyle", fsStr); | ||
PreferencesManager.setViewState("lineHeightStyle", lhStr); | ||
|
||
$(exports).triggerHandler("fontSizeChange", [adjustment, fsStr, lhStr]); | ||
return true; | ||
} | ||
|
||
/** Increases the font size by 1 */ | ||
function _handleIncreaseFontSize() { | ||
if (_adjustFontSize(1)) { | ||
PreferencesManager.setViewState("fontSizeAdjustment", PreferencesManager.getViewState("fontSizeAdjustment") + 1); | ||
} | ||
_adjustFontSize(1); | ||
} | ||
|
||
/** Decreases the font size by 1 */ | ||
function _handleDecreaseFontSize() { | ||
if (_adjustFontSize(-1)) { | ||
PreferencesManager.setViewState("fontSizeAdjustment", PreferencesManager.getViewState("fontSizeAdjustment") - 1); | ||
} | ||
_adjustFontSize(-1); | ||
} | ||
|
||
/** Restores the font size to the original size */ | ||
function _handleRestoreFontSize() { | ||
_adjustFontSize(-PreferencesManager.getViewState("fontSizeAdjustment")); | ||
PreferencesManager.setViewState("fontSizeAdjustment", 0); | ||
_setSizeAndRestoreScroll(); | ||
PreferencesManager.setViewState("fontSizeStyle", undefined); | ||
PreferencesManager.setViewState("lineHeightStyle", undefined); | ||
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 know you're explicitly using 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. Right, using |
||
} | ||
|
||
|
||
|
@@ -215,14 +217,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 +225,18 @@ define(function (require, exports, module) { | |
} | ||
} | ||
|
||
/** | ||
* Restores the Font Size and Line Height using the saved strings | ||
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("fontSizeStyle"), | ||
lhStr = PreferencesManager.getViewState("lineHeightStyle"); | ||
|
||
if (fsStr && lhStr) { | ||
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 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
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 +342,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.
cmv4 is already landed in master and I wonder
lineAtHeight
andheightAtLine
are still the same in cmv4. Can you double check or can you merge master to your pull request?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.
Sure will try, hopefully it should behave the same way.
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.
Actually I made this PR after cmv4 landed.
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.
Really, but I notice that CM submodule is different.
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.
Yes, I can use multiple cursors in this branch.
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.
Ok.