Skip to content

Commit

Permalink
Fix clone() on TextLineImpl and LineImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
sedwards2009 committed Apr 13, 2024
1 parent 33a3ca2 commit 9cc30d5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
29 changes: 21 additions & 8 deletions packages/term-api-lineimpl/src/LineImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,32 @@ import { AspectRatioMode, QImage, QSize, TransformationMode } from "@nodegui/nod
*/
export class LineImpl extends TextLineImpl {

#embeddedImageMap: EmbeddedImageMap = null;
_embeddedImageMap: EmbeddedImageMap = null;

clone(): LineImpl {
const newInstance = new LineImpl(this.width, this.palette);
this.cloneInto(newInstance);
return newInstance;
}

cloneInto(target: LineImpl): void {
super.cloneInto(target);
if (this._embeddedImageMap != null) {
target._embeddedImageMap = new Map(this._embeddedImageMap);
}
}

addImage(imageId: number, image: QImage, cellWidthPx: number, cellHeightPx: number): void {
if (this.#embeddedImageMap == null) {
this.#embeddedImageMap = new Map<number, EmbeddedImage>();
if (this._embeddedImageMap == null) {
this._embeddedImageMap = new Map<number, EmbeddedImage>();
} else {
this.#garbageCollectLineImages();
// Note: Ideally this would happen *after* the line is updated with the
// new image. So we might be hanging onto images longer than strictly
// necessary. But in the case of an image being constantly overwritten
// (i.e. animation or live updates) this is good enough.
}
this.#embeddedImageMap.set(imageId, {
this._embeddedImageMap.set(imageId, {
sourceImage: image,
sourceCellWidthPx: cellWidthPx,
sourceCellHeightPx: cellHeightPx,
Expand All @@ -38,7 +51,7 @@ export class LineImpl extends TextLineImpl {
* collect the images.
*/
#garbageCollectLineImages(): void {
const imageMap = this.#embeddedImageMap;
const imageMap = this._embeddedImageMap;
const ids = Array.from(imageMap.keys());
const cols = this.width;
for (const id of ids) {
Expand All @@ -56,11 +69,11 @@ export class LineImpl extends TextLineImpl {
}

getEmbeddedImageMap(newCellWidthPx: number, newCellHeightPx: number): EmbeddedImageMap {
if (this.#embeddedImageMap == null) {
if (this._embeddedImageMap == null) {
return null;
}

for (const [_, imageEntry] of this.#embeddedImageMap) {
for (const [_, imageEntry] of this._embeddedImageMap) {
if (imageEntry.cellWidthPx !== newCellWidthPx || imageEntry.cellHeightPx !== newCellHeightPx) {
if (imageEntry.sourceCellHeightPx === newCellWidthPx && imageEntry.sourceCellHeightPx === newCellHeightPx) {
imageEntry.image = imageEntry.sourceImage;
Expand All @@ -78,6 +91,6 @@ export class LineImpl extends TextLineImpl {
imageEntry.cellHeightPx = newCellHeightPx;
}
}
return this.#embeddedImageMap;
return this._embeddedImageMap;
}
}
16 changes: 9 additions & 7 deletions packages/text-term-api-lineimpl/src/TextLineImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,18 @@ export class TextLineImpl extends CharCellLine implements Line {
}

clone(): Line {
const grid = new TextLineImpl(this.width, this.palette);
this.cloneInto(grid);
const newInstance = new TextLineImpl(this.width, this.palette);
this.cloneInto(newInstance);
return newInstance;
}

cloneInto(target: TextLineImpl): void {
super.cloneInto(target);
if (this._hyperlinkIDToURLMapping != null) {
grid._hyperlinkIDCounter = this._hyperlinkIDCounter;
grid._hyperlinkIDToURLMapping = new Map(this._hyperlinkIDToURLMapping);
grid._hyperlinkURLToIDMapping = this._hyperlinkURLToIDMapping.copy();
target._hyperlinkIDCounter = this._hyperlinkIDCounter;
target._hyperlinkIDToURLMapping = new Map(this._hyperlinkIDToURLMapping);
target._hyperlinkURLToIDMapping = this._hyperlinkURLToIDMapping.copy();
}

return grid;
}

clear(): void {
Expand Down

0 comments on commit 9cc30d5

Please sign in to comment.