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

Persisting font size between Brackets Sessions #3027

Merged
merged 15 commits into from
Mar 11, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 117 additions & 20 deletions src/view/ViewCommandHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,59 @@ define(function (require, exports, module) {
CommandManager = require("command/CommandManager"),
Strings = require("strings"),
ProjectManager = require("project/ProjectManager"),
EditorManager = require("editor/EditorManager");
EditorManager = require("editor/EditorManager"),
PreferencesManager = require("preferences/PreferencesManager"),
DocumentManager = require("document/DocumentManager"),
AppInit = require("utils/AppInit");

/**
* @const
* @type {string}
*/
var DYNAMIC_FONT_STYLE_ID = "codemirror-dynamic-fonts";

/**
* @const
* @private
* Unique PreferencesManager clientID
* @type {string}
*/
var PREFERENCES_CLIENT_ID = "com.adobe.brackets." + module.id;

/**
* @const
* @private
* The smallest font size in pixels
* @type {int}
*/
var MIN_FONT_SIZE = 1;

/**
* @const
* @private
* The largest font size in pixels
* @type {int}
*/
var MAX_FONT_SIZE = 72;

/**
* @private
* @type {PreferenceStorage}
*/
var _prefs = {};

/**
* @private
* @type {PreferenceStorage}
*/
var _defaultPrefs = { fontSizeAdjustment: 0 };

/**
* @private
* @type {boolean}
*/
var _fontSizePrefsLoaded = false;

function _removeDynamicFontSize(refresh) {
$("#" + DYNAMIC_FONT_STYLE_ID).remove();
if (refresh) {
Expand All @@ -49,9 +94,10 @@ define(function (require, exports, module) {
/**
* @private
* Increases or decreases the editor's font size.
* @param {number} -1 to make the font smaller; 1 to make it bigger.
* @param {number} negative number to make the font smaller; positive number to make it bigger.
* @return {boolean} true if adjustment occurred, false if it did not occur
*/
function _adjustFontSize(direction) {
function _adjustFontSize(adjustment) {
var styleId = "codemirror-dynamic-fonts";

var fsStyle = $(".CodeMirror").css("font-size");
Expand All @@ -62,7 +108,7 @@ define(function (require, exports, module) {
// Make sure the font size and line height are expressed in terms
// we can handle (px or em). If not, simply bail.
if (fsStyle.search(validFont) === -1 || lhStyle.search(validFont) === -1) {
return;
return false;
}

// Guaranteed to work by the validation above.
Expand All @@ -72,25 +118,27 @@ define(function (require, exports, module) {
var fsOld = parseFloat(fsStyle.substring(0, fsStyle.length - 2));
var lhOld = parseFloat(lhStyle.substring(0, lhStyle.length - 2));

var fsDelta = (fsUnits === "px") ? 1 : 0.1;
var lhDelta = (lhUnits === "px") ? 1 : 0.1;

if (direction === -1) {
fsDelta *= -1;
lhDelta *= -1;
}
var fsDelta = (fsUnits === "px") ? adjustment : (0.1 * adjustment);
var lhDelta = (lhUnits === "px") ? adjustment : (0.1 * adjustment);

var fsNew = fsOld + fsDelta;
var lhNew = lhOld + lhDelta;

var fsStr = fsNew + fsUnits;
var lhStr = lhNew + lhUnits;

// Don't let the fonts get too small.
if (direction === -1 && ((fsUnits === "px" && fsNew <= 1) || (fsUnits === "em" && fsNew <= 0.1))) {
return;
// Don't let the font size get too small.
if ((fsUnits === "px" && fsNew < MIN_FONT_SIZE) ||
(fsUnits === "em" && fsNew < (MIN_FONT_SIZE * 0.1))) {
return false;
}


// Don't let the font size get too large.
if ((fsUnits === "px" && fsNew > MAX_FONT_SIZE) ||
(fsUnits === "em" && fsNew > (MAX_FONT_SIZE * 0.1))) {
return false;
}

// It's necessary to inject a new rule to address all editors.
_removeDynamicFontSize(false);
var style = $("<style type='text/css'></style>").attr("id", DYNAMIC_FONT_STYLE_ID);
Expand All @@ -108,25 +156,74 @@ define(function (require, exports, module) {
var scrollPos = editor.getScrollPos();
var scrollDeltaX = Math.round(scrollPos.x / lhOld);
var scrollDeltaY = Math.round(scrollPos.y / lhOld);
editor.setScrollPos(scrollPos.x + (scrollDeltaX * direction),
scrollPos.y + (scrollDeltaY * direction));

scrollDeltaX = (adjustment >= 0 ? scrollDeltaX : -scrollDeltaX);
scrollDeltaY = (adjustment >= 0 ? scrollDeltaY : -scrollDeltaY);

editor.setScrollPos((scrollPos.x + scrollDeltaX),
(scrollPos.y + scrollDeltaY));
}


return true;
}

function _handleIncreaseFontSize() {
_adjustFontSize(1);
if (_adjustFontSize(1)) {
_prefs.setValue("fontSizeAdjustment", _prefs.getValue("fontSizeAdjustment") + 1);
}
}

function _handleDecreaseFontSize() {
_adjustFontSize(-1);
if (_adjustFontSize(-1)) {
_prefs.setValue("fontSizeAdjustment", _prefs.getValue("fontSizeAdjustment") - 1);
}
}

function _handleRestoreFontSize() {
_removeDynamicFontSize(true);
_prefs.setValue("fontSizeAdjustment", 0);
}

/**
* @private
* Updates the user interface appropriately based on whether or not a document is
* currently open in the editor.
*/
function _updateUI() {
if (DocumentManager.getCurrentDocument() !== null) {
if (!CommandManager.get(Commands.VIEW_INCREASE_FONT_SIZE).getEnabled()) {
// If one is disabled then they all are disabled, so enable them all
CommandManager.get(Commands.VIEW_INCREASE_FONT_SIZE).setEnabled(true);
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(false);
_adjustFontSize(_prefs.getValue("fontSizeAdjustment"));
_fontSizePrefsLoaded = true;
}

} else {
// No current document so disable all of the Font Size commands
CommandManager.get(Commands.VIEW_INCREASE_FONT_SIZE).setEnabled(false);
CommandManager.get(Commands.VIEW_DECREASE_FONT_SIZE).setEnabled(false);
CommandManager.get(Commands.VIEW_RESTORE_FONT_SIZE).setEnabled(false);
}
}

// Register command handlers
CommandManager.register(Strings.CMD_INCREASE_FONT_SIZE, Commands.VIEW_INCREASE_FONT_SIZE, _handleIncreaseFontSize);
CommandManager.register(Strings.CMD_DECREASE_FONT_SIZE, Commands.VIEW_DECREASE_FONT_SIZE, _handleDecreaseFontSize);
CommandManager.register(Strings.CMD_RESTORE_FONT_SIZE, Commands.VIEW_RESTORE_FONT_SIZE, _handleRestoreFontSize);

// Init PreferenceStorage
_prefs = PreferencesManager.getPreferenceStorage(PREFERENCES_CLIENT_ID, _defaultPrefs);

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

// Update UI when Brackets finishes loading
AppInit.appReady(_updateUI);
});