mirror of
https://github.com/DaanVandenBosch/phantasmal-world.git
synced 2025-04-07 08:48:28 +08:00
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import { Widget } from "./Widget";
|
|
import { el } from "./dom";
|
|
import { Resizable } from "./Resizable";
|
|
import { ResizableWidget } from "./ResizableWidget";
|
|
|
|
export class LazyWidget extends ResizableWidget {
|
|
readonly element = el.div({ class: "core_LazyView" });
|
|
|
|
private initialized = false;
|
|
private view: Widget & Resizable | undefined;
|
|
|
|
constructor(private create_view: () => Promise<Widget & Resizable>) {
|
|
super();
|
|
|
|
this.visible.val = false;
|
|
}
|
|
|
|
protected set_visible(visible: boolean): void {
|
|
super.set_visible(visible);
|
|
|
|
if (visible && !this.initialized) {
|
|
this.initialized = true;
|
|
|
|
this.create_view().then(view => {
|
|
if (!this.disposed) {
|
|
this.view = this.disposable(view);
|
|
this.view.resize(this.width, this.height);
|
|
this.element.append(view.element);
|
|
}
|
|
});
|
|
}
|
|
|
|
this.finalize_construction(LazyWidget.prototype);
|
|
}
|
|
|
|
resize(width: number, height: number): this {
|
|
super.resize(width, height);
|
|
|
|
if (this.view) {
|
|
this.view.resize(width, height);
|
|
}
|
|
|
|
return this;
|
|
}
|
|
}
|