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"; this.element.className = "ace_layer ace_text-layer";
parentEl.appendChild(this.element); parentEl.appendChild(this.element);
this.$characterSize = this.$measureSizes(); this.$characterSize = this.$measureSizes() || {width: 0, height: 0};
this.$pollSizeChanges(); this.$pollSizeChanges();
}; };
@ -75,14 +75,18 @@ var Text = function(parentEl) {
return this.$characterSize.width || 1; 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() { this.$pollSizeChanges = function() {
var self = this; var self = this;
setInterval(function() { setInterval(function() {
var size = self.$measureSizes(); self.checkForSizeChanges();
if (self.$characterSize.width !== size.width || self.$characterSize.height !== size.height) {
self.$characterSize = size;
self._dispatchEvent("changeCharaterSize", {data: size});
}
}, 500); }, 500);
}; };
@ -92,7 +96,7 @@ var Text = function(parentEl) {
fontWeight : 1, fontWeight : 1,
fontStyle : 1, fontStyle : 1,
lineHeight : 1 lineHeight : 1
}, };
this.$measureSizes = function() { this.$measureSizes = function() {
var n = 1000; var n = 1000;
@ -134,6 +138,12 @@ var Text = function(parentEl) {
height: this.$measureNode.offsetHeight, height: this.$measureNode.offsetHeight,
width: this.$measureNode.offsetWidth / (n * 2) 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; return size;
}; };

View file

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