@@ -31,14 +31,59 @@ define(function (require, exports, module) {
3131 CommandManager = require ( "command/CommandManager" ) ,
3232 Strings = require ( "strings" ) ,
3333 ProjectManager = require ( "project/ProjectManager" ) ,
34- EditorManager = require ( "editor/EditorManager" ) ;
34+ EditorManager = require ( "editor/EditorManager" ) ,
35+ PreferencesManager = require ( "preferences/PreferencesManager" ) ,
36+ DocumentManager = require ( "document/DocumentManager" ) ,
37+ AppInit = require ( "utils/AppInit" ) ;
3538
3639 /**
3740 * @const
3841 * @type {string }
3942 */
4043 var DYNAMIC_FONT_STYLE_ID = "codemirror-dynamic-fonts" ;
4144
45+ /**
46+ * @const
47+ * @private
48+ * Unique PreferencesManager clientID
49+ * @type {string }
50+ */
51+ var PREFERENCES_CLIENT_ID = "com.adobe.brackets." + module . id ;
52+
53+ /**
54+ * @const
55+ * @private
56+ * The smallest font size in pixels
57+ * @type {int }
58+ */
59+ var MIN_FONT_SIZE = 1 ;
60+
61+ /**
62+ * @const
63+ * @private
64+ * The largest font size in pixels
65+ * @type {int }
66+ */
67+ var MAX_FONT_SIZE = 72 ;
68+
69+ /**
70+ * @private
71+ * @type {PreferenceStorage }
72+ */
73+ var _prefs = { } ;
74+
75+ /**
76+ * @private
77+ * @type {PreferenceStorage }
78+ */
79+ var _defaultPrefs = { fontSizeAdjustment : 0 } ;
80+
81+ /**
82+ * @private
83+ * @type {boolean }
84+ */
85+ var _fontSizePrefsLoaded = false ;
86+
4287 function _removeDynamicFontSize ( refresh ) {
4388 $ ( "#" + DYNAMIC_FONT_STYLE_ID ) . remove ( ) ;
4489 if ( refresh ) {
@@ -49,9 +94,10 @@ define(function (require, exports, module) {
4994 /**
5095 * @private
5196 * Increases or decreases the editor's font size.
52- * @param {number } -1 to make the font smaller; 1 to make it bigger.
97+ * @param {number } negative number to make the font smaller; positive number to make it bigger.
98+ * @return {boolean } true if adjustment occurred, false if it did not occur
5399 */
54- function _adjustFontSize ( direction ) {
100+ function _adjustFontSize ( adjustment ) {
55101 var styleId = "codemirror-dynamic-fonts" ;
56102
57103 var fsStyle = $ ( ".CodeMirror" ) . css ( "font-size" ) ;
@@ -62,7 +108,7 @@ define(function (require, exports, module) {
62108 // Make sure the font size and line height are expressed in terms
63109 // we can handle (px or em). If not, simply bail.
64110 if ( fsStyle . search ( validFont ) === - 1 || lhStyle . search ( validFont ) === - 1 ) {
65- return ;
111+ return false ;
66112 }
67113
68114 // Guaranteed to work by the validation above.
@@ -72,25 +118,27 @@ define(function (require, exports, module) {
72118 var fsOld = parseFloat ( fsStyle . substring ( 0 , fsStyle . length - 2 ) ) ;
73119 var lhOld = parseFloat ( lhStyle . substring ( 0 , lhStyle . length - 2 ) ) ;
74120
75- var fsDelta = ( fsUnits === "px" ) ? 1 : 0.1 ;
76- var lhDelta = ( lhUnits === "px" ) ? 1 : 0.1 ;
77-
78- if ( direction === - 1 ) {
79- fsDelta *= - 1 ;
80- lhDelta *= - 1 ;
81- }
121+ var fsDelta = ( fsUnits === "px" ) ? adjustment : ( 0.1 * adjustment ) ;
122+ var lhDelta = ( lhUnits === "px" ) ? adjustment : ( 0.1 * adjustment ) ;
82123
83124 var fsNew = fsOld + fsDelta ;
84125 var lhNew = lhOld + lhDelta ;
85126
86127 var fsStr = fsNew + fsUnits ;
87128 var lhStr = lhNew + lhUnits ;
88129
89- // Don't let the fonts get too small.
90- if ( direction === - 1 && ( ( fsUnits === "px" && fsNew <= 1 ) || ( fsUnits === "em" && fsNew <= 0.1 ) ) ) {
91- return ;
130+ // Don't let the font size get too small.
131+ if ( ( fsUnits === "px" && fsNew < MIN_FONT_SIZE ) ||
132+ ( fsUnits === "em" && fsNew < ( MIN_FONT_SIZE * 0.1 ) ) ) {
133+ return false ;
92134 }
93-
135+
136+ // Don't let the font size get too large.
137+ if ( ( fsUnits === "px" && fsNew > MAX_FONT_SIZE ) ||
138+ ( fsUnits === "em" && fsNew > ( MAX_FONT_SIZE * 0.1 ) ) ) {
139+ return false ;
140+ }
141+
94142 // It's necessary to inject a new rule to address all editors.
95143 _removeDynamicFontSize ( false ) ;
96144 var style = $ ( "<style type='text/css'></style>" ) . attr ( "id" , DYNAMIC_FONT_STYLE_ID ) ;
@@ -108,25 +156,74 @@ define(function (require, exports, module) {
108156 var scrollPos = editor . getScrollPos ( ) ;
109157 var scrollDeltaX = Math . round ( scrollPos . x / lhOld ) ;
110158 var scrollDeltaY = Math . round ( scrollPos . y / lhOld ) ;
111- editor . setScrollPos ( scrollPos . x + ( scrollDeltaX * direction ) ,
112- scrollPos . y + ( scrollDeltaY * direction ) ) ;
159+
160+ scrollDeltaX = ( adjustment >= 0 ? scrollDeltaX : - scrollDeltaX ) ;
161+ scrollDeltaY = ( adjustment >= 0 ? scrollDeltaY : - scrollDeltaY ) ;
162+
163+ editor . setScrollPos ( ( scrollPos . x + scrollDeltaX ) ,
164+ ( scrollPos . y + scrollDeltaY ) ) ;
113165 }
114-
166+
167+ return true ;
115168 }
116169
117170 function _handleIncreaseFontSize ( ) {
118- _adjustFontSize ( 1 ) ;
171+ if ( _adjustFontSize ( 1 ) ) {
172+ _prefs . setValue ( "fontSizeAdjustment" , _prefs . getValue ( "fontSizeAdjustment" ) + 1 ) ;
173+ }
119174 }
120175
121176 function _handleDecreaseFontSize ( ) {
122- _adjustFontSize ( - 1 ) ;
177+ if ( _adjustFontSize ( - 1 ) ) {
178+ _prefs . setValue ( "fontSizeAdjustment" , _prefs . getValue ( "fontSizeAdjustment" ) - 1 ) ;
179+ }
123180 }
124181
125182 function _handleRestoreFontSize ( ) {
126183 _removeDynamicFontSize ( true ) ;
184+ _prefs . setValue ( "fontSizeAdjustment" , 0 ) ;
127185 }
128186
187+ /**
188+ * @private
189+ * Updates the user interface appropriately based on whether or not a document is
190+ * currently open in the editor.
191+ */
192+ function _updateUI ( ) {
193+ if ( DocumentManager . getCurrentDocument ( ) !== null ) {
194+ if ( ! CommandManager . get ( Commands . VIEW_INCREASE_FONT_SIZE ) . getEnabled ( ) ) {
195+ // If one is disabled then they all are disabled, so enable them all
196+ CommandManager . get ( Commands . VIEW_INCREASE_FONT_SIZE ) . setEnabled ( true ) ;
197+ CommandManager . get ( Commands . VIEW_DECREASE_FONT_SIZE ) . setEnabled ( true ) ;
198+ CommandManager . get ( Commands . VIEW_RESTORE_FONT_SIZE ) . setEnabled ( true ) ;
199+ }
200+
201+ // Font Size preferences only need to be loaded one time
202+ if ( ! _fontSizePrefsLoaded ) {
203+ _removeDynamicFontSize ( false ) ;
204+ _adjustFontSize ( _prefs . getValue ( "fontSizeAdjustment" ) ) ;
205+ _fontSizePrefsLoaded = true ;
206+ }
207+
208+ } else {
209+ // No current document so disable all of the Font Size commands
210+ CommandManager . get ( Commands . VIEW_INCREASE_FONT_SIZE ) . setEnabled ( false ) ;
211+ CommandManager . get ( Commands . VIEW_DECREASE_FONT_SIZE ) . setEnabled ( false ) ;
212+ CommandManager . get ( Commands . VIEW_RESTORE_FONT_SIZE ) . setEnabled ( false ) ;
213+ }
214+ }
215+
216+ // Register command handlers
129217 CommandManager . register ( Strings . CMD_INCREASE_FONT_SIZE , Commands . VIEW_INCREASE_FONT_SIZE , _handleIncreaseFontSize ) ;
130218 CommandManager . register ( Strings . CMD_DECREASE_FONT_SIZE , Commands . VIEW_DECREASE_FONT_SIZE , _handleDecreaseFontSize ) ;
131219 CommandManager . register ( Strings . CMD_RESTORE_FONT_SIZE , Commands . VIEW_RESTORE_FONT_SIZE , _handleRestoreFontSize ) ;
220+
221+ // Init PreferenceStorage
222+ _prefs = PreferencesManager . getPreferenceStorage ( PREFERENCES_CLIENT_ID , _defaultPrefs ) ;
223+
224+ // Update UI when opening or closing a document
225+ $ ( DocumentManager ) . on ( "currentDocumentChange" , _updateUI ) ;
226+
227+ // Update UI when Brackets finishes loading
228+ AppInit . appReady ( _updateUI ) ;
132229} ) ;
0 commit comments