Skip to content

Commit

Permalink
BasicTextEditor and TextEditor Part I: Performance improvements
Browse files Browse the repository at this point in the history
Code clean up in ColorManager (reference #9)
  • Loading branch information
piotrzarzycki21 committed Oct 20, 2017
1 parent e9949c0 commit 059ce44
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
////////////////////////////////////////////////////////////////////////////////
package actionScripts.ui.editor
{
import flash.display.DisplayObject;
import actionScripts.events.GlobalEventDispatcher;

import flash.display.DisplayObject;
import flash.events.Event;

import mx.containers.Canvas;
Expand Down Expand Up @@ -63,6 +65,7 @@ package actionScripts.ui.editor
private var pop:FileSavePopup;
protected var model:IDEModel = IDEModel.getInstance();

private var dispatcher:GlobalEventDispatcher = GlobalEventDispatcher.getInstance();
private var selectProjectPopup:SelectOpenedFlexProject;
private var _isChanged:Boolean;

Expand Down Expand Up @@ -216,25 +219,17 @@ package actionScripts.ui.editor
loadingFile = false;
// Get data from file
text = data;
if (tempScrollTo > 0)
{
scrollTo(tempScrollTo);
tempScrollTo = -1;
}
scrollToTempValue();
}

protected function openHandler(event:Event):void
{
loadingFile = false;
// Get data from file
text = file.fileBridge.data.toString();

if (tempScrollTo > 0)
{
scrollTo(tempScrollTo);
tempScrollTo = -1;
}


scrollToTempValue();

file.fileBridge.getFile.removeEventListener(Event.COMPLETE, openHandler);
}

Expand All @@ -254,7 +249,7 @@ package actionScripts.ui.editor
updateChangeStatus();

// Tell the world we've changed
GlobalEventDispatcher.getInstance().dispatchEvent(
dispatcher.dispatchEvent(
new SaveFileEvent(SaveFileEvent.FILE_SAVED, file, this)
);
}
Expand All @@ -265,21 +260,22 @@ package actionScripts.ui.editor
updateChangeStatus();

// Tell the world we've changed
GlobalEventDispatcher.getInstance().dispatchEvent(
dispatcher.dispatchEvent(
new SaveFileEvent(SaveFileEvent.FILE_SAVED, file, this)
);
}
else if (!ConstantsCoreVO.IS_AIR)
{
GlobalEventDispatcher.getInstance().dispatchEvent(new ConsoleOutputEvent(file.fileBridge.name +": Saving in process..."));
loader = new DataAgent(URLDescriptorVO.FILE_MODIFY, onSaveSuccess, onSaveFault, {path:file.fileBridge.nativePath,text:text});
dispatcher.dispatchEvent(new ConsoleOutputEvent(file.fileBridge.name +": Saving in process..."));
loader = new DataAgent(URLDescriptorVO.FILE_MODIFY, onSaveSuccess, onSaveFault,
{path:file.fileBridge.nativePath,text:text});
}
}

private function onSaveFault(message:String):void
{
//Alert.show("Save Fault"+message);
GlobalEventDispatcher.getInstance().dispatchEvent(new ConsoleOutputEvent(file.fileBridge.name +": Save error!"));
dispatcher.dispatchEvent(new ConsoleOutputEvent(file.fileBridge.name +": Save error!"));
loader = null;
}

Expand All @@ -289,10 +285,8 @@ package actionScripts.ui.editor
loader = null;
editor.save();
updateChangeStatus();
GlobalEventDispatcher.getInstance().dispatchEvent(new ConsoleOutputEvent(file.fileBridge.name +": Saving successful."));
GlobalEventDispatcher.getInstance().dispatchEvent(
new SaveFileEvent(SaveFileEvent.FILE_SAVED, file, this)
);
dispatcher.dispatchEvent(new ConsoleOutputEvent(file.fileBridge.name +": Saving successful."));
dispatcher.dispatchEvent(new SaveFileEvent(SaveFileEvent.FILE_SAVED, file, this));
}

public function saveAs(file:FileLocation=null):void
Expand All @@ -303,14 +297,16 @@ package actionScripts.ui.editor
save();
// Update labels
dispatchEvent(new Event('labelChanged'));
GlobalEventDispatcher.getInstance().dispatchEvent(new RefreshTreeEvent(file));
dispatcher.dispatchEvent(new RefreshTreeEvent(file));
return;
}

if (ConstantsCoreVO.IS_AIR)
{
if(this.file)
saveAsPath(this.file.fileBridge.parent.fileBridge.nativePath)
{
saveAsPath(this.file.fileBridge.parent.fileBridge.nativePath);
}
else if (model.projects.length > 1 )
{
if (model.mainView.isProjectViewAdded)
Expand Down Expand Up @@ -366,9 +362,7 @@ package actionScripts.ui.editor
dispatchEvent(new Event('labelChanged'));
editor.save();
updateChangeStatus();
GlobalEventDispatcher.getInstance().dispatchEvent(
new SaveFileEvent(SaveFileEvent.FILE_SAVED, file, this)
);
dispatcher.dispatchEvent(new SaveFileEvent(SaveFileEvent.FILE_SAVED, file, this));
}

protected function handleTextChange(event:ChangeEvent):void
Expand Down Expand Up @@ -398,5 +392,13 @@ package actionScripts.ui.editor
tempSaveAs = null;
}

}
private function scrollToTempValue():void
{
if (tempScrollTo > 0)
{
scrollTo(tempScrollTo);
tempScrollTo = -1;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ package actionScripts.ui.editor.text
import actionScripts.ui.editor.text.change.TextChangeMulti;
import actionScripts.ui.editor.text.change.TextChangeRemove;
import actionScripts.ui.parser.ILineParser;
import actionScripts.valueObjects.Settings;


import actionScripts.valueObjects.Settings;

public class ColorManager
{
private static var charWidthCache:Object = {"\t":7.82666015625*Settings.font.tabWidth};
Expand Down Expand Up @@ -88,7 +87,7 @@ package actionScripts.ui.editor.text
private function invalidate(line:int, addCount:int = 0, silent:Boolean = false):void
{
var merged:Boolean = false;

for (var r:int = ranges.length; r--;)
{
var range:LineRange = ranges[r];
Expand Down Expand Up @@ -128,11 +127,9 @@ package actionScripts.ui.editor.text
private function process(event:Event = null):void
{
//if (!parser) return;

var count:int = model.lines.length;
var timeLimit:int = getTimer() + CHUNK_TIMESPAN;
var lastContext:int = 0;


while (ranges.length)
{
var range:LineRange = ranges[0];
Expand Down Expand Up @@ -201,37 +198,38 @@ package actionScripts.ui.editor.text

public function calculateWidth(text:String):Number
{
var chars:String = "";
var c:String;
var chars:String;
var calculatedChars:String;
var i:int;
var width:Number = 0;
var textLenght:int = text.length;

// Collect uncached characters
for (i = text.length; i--; )
for (i = textLenght; i--; )
{
c = text.charAt(i);
calculatedChars = text.charAt(i);

if (!charWidthCache[c])
if (!charWidthCache[calculatedChars])
{
chars += c;
charWidthCache[c] = -1;
chars += calculatedChars;
charWidthCache[calculatedChars] = -1;
}
}
// Measure uncached characters
if (chars.length > 0)
if (chars)
{
var textLine:TextLine;

textElement.text = chars;
textLine = textBlock.createTextLine()
textLine = textBlock.createTextLine();
for (i = chars.length; i--; )
{
c = chars.charAt(i);
charWidthCache[c] = textLine.getAtomBounds(textLine.getAtomIndexAtCharIndex(i)).width;
calculatedChars = chars.charAt(i);
charWidthCache[calculatedChars] = textLine.getAtomBounds(textLine.getAtomIndexAtCharIndex(i)).width;
}
}
// Calculate line width
for (i = text.length; i--; )
for (i = textLenght; i--; )
{
width += charWidthCache[text.charAt(i)];
}
Expand Down
Loading

0 comments on commit 059ce44

Please sign in to comment.