Improve behavior of font size detection when editors have changing visibility

- Allow on-demand checking of text layer character sizes
- Disregard character measurements that result in width or height of 0
This commit is contained in:
Joe Cheng 2011-02-26 09:02:50 +08:00 • committed by Fabian Jakobs
commit 4f2de650ae
2 changed files with 21 additions and 7 deletions

View file

@ -50,7 +50,7 @@ var Text = function(parentEl) {
this.element.className = "ace_layer ace_text-layer";
parentEl.appendChild(this.element);
this.$characterSize = this.$measureSizes();
this.$characterSize = this.$measureSizes() || {width: 0, height: 0};
this.$pollSizeChanges();
};
@ -75,14 +75,18 @@ var Text = function(parentEl) {
return this.$characterSize.width || 1;
};
this.checkForSizeChanges = function() {
var size = this.$measureSizes();
if (size && (this.$characterSize.width !== size.width || this.$characterSize.height !== size.height)) {
this.$characterSize = size;
this._dispatchEvent("changeCharaterSize", {data: size});
}
};
this.$pollSizeChanges = function() {
var self = this;
setInterval(function() {
var size = self.$measureSizes();
if (self.$characterSize.width !== size.width || self.$characterSize.height !== size.height) {
self.$characterSize = size;
self._dispatchEvent("changeCharaterSize", {data: size});
}
self.checkForSizeChanges();
}, 500);
};
@ -92,7 +96,7 @@ var Text = function(parentEl) {
fontWeight : 1,
fontStyle : 1,
lineHeight : 1
},
};
this.$measureSizes = function() {
var n = 1000;
@ -134,6 +138,12 @@ var Text = function(parentEl) {
height: this.$measureNode.offsetHeight,
width: this.$measureNode.offsetWidth / (n * 2)
};
// Size and width can be null if the editor is not visible or
// detached from the document
if (size.width == 0 && size.height == 0)
return null;
return size;
};

View file

@ -192,6 +192,10 @@ var VirtualRenderer = function(container, theme) {
this.$loop.schedule(this.CHANGE_FULL);
};
this.updateFontSize = function() {
this.$textLayer.checkForSizeChanges();
};
/**
* Triggers resize of the editor
*/