Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Merged
Changes from 12 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
125 changes: 98 additions & 27 deletions src/view/ViewCommandHandlers.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*
*/

/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
Expand All @@ -31,14 +31,43 @@ 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;

/**
* @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 +78,9 @@ 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.
*/
function _adjustFontSize(direction) {
function _adjustFontSize(adjustment) {
var styleId = "codemirror-dynamic-fonts";

var fsStyle = $(".CodeMirror").css("font-size");
Expand All @@ -72,13 +101,8 @@ 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;
Expand All @@ -87,10 +111,12 @@ define(function (require, exports, module) {
var lhStr = lhNew + lhUnits;

// Don't let the fonts get too small.
if (direction === -1 && ((fsUnits === "px" && fsNew <= 1) || (fsUnits === "em" && fsNew <= 0.1))) {
if ((fsUnits === "px" && fsNew <= 1) || (fsUnits === "em" && fsNew <= 0.1)) {
// Roll back the font size adjustment value in the persisted data
_prefs.setValue("fontSizeAdjustment", _prefs.getValue("fontSizeAdjustment") + 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had some difficulties in understanding this line. At first, I thought you have "+ 1" by mistake (thinking that you copied this line of code from somewhere else and forgot to remove this part). Later, your comment above this line helped me to realize that your are reverting the exact action done by the caller. So instead of having to know what the caller has done and having to revert the same action here, why don't we make the following changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me. Did we ever agree what that upper bound is going to be or do you want me to pick an arbitrary value and we can tweak it from there?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe no one has suggested the upper bound value. So you have the power to make that decision.

return;
}

// 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 +134,70 @@ 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));
}

}

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

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

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

/**
* @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);
});