lazy-widgets
Version:
Typescript retained mode GUI for the HTML canvas API
77 lines • 2.32 kB
JavaScript
import { BaseLabel } from './BaseLabel.js';
/**
* A widget which displays a line of text.
*
* Similar to {@link Label}, except the observable attached to this label is
* watched; when its value changes, the displayed text is updated. The
* observable can also be swapped via the {@link LiveLabel#textSource}
* accessor.
*
* @category Widget
*/
export class LiveLabel extends BaseLabel {
/**
* @param textSource - The observable to get the text for the label.
*/
constructor(textSource, properties) {
super(properties);
/**
* The watch callback for the observable. For internal use only, used for
* cleaning up the watcher.
*/
this._textWatcher = null;
/**
* Does the displayed value need to be updated?
*/
this.textDirty = true;
this._textSource = textSource;
}
handlePreLayoutUpdate() {
var _a;
if (this.textDirty) {
this.textDirty = false;
this.textHelper.text = (_a = this.textSource.value) !== null && _a !== void 0 ? _a : '';
}
super.handlePreLayoutUpdate();
}
/** The current text value. */
get currentText() {
return this._textSource.value;
}
/** The observable that is displayed in this label */
set textSource(textSource) {
if (this._textSource !== textSource) {
const oldTextSource = this._textSource;
this._textSource = textSource;
if (this._textWatcher !== null) {
oldTextSource.unwatch(this._textWatcher);
textSource.watch(this._textWatcher, true);
}
}
}
get textSource() {
return this._textSource;
}
handleAttachment() {
this._textWatcher = () => this.textDirty = true;
this._textSource.watch(this._textWatcher, true);
}
handleDetachment() {
if (this._textWatcher) {
this._textSource.unwatch(this._textWatcher);
this._textWatcher = null;
}
}
}
LiveLabel.autoXML = {
name: 'live-label',
inputConfig: [
{
mode: 'value',
name: 'text-source',
validator: 'observable',
optional: true
}
]
};
//# sourceMappingURL=LiveLabel.js.map