Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/LewisStructuresColors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ const LewisStructuresColors = {
projector: "#f5f5f5",
}),

/** End-of-round panel fill (the vegas LevelCompletedNode colour, kept in both profiles). */
roundCompletedFillColorProperty: new ProfileColorProperty(LewisStructuresNamespace, "roundCompletedFill", {
default: "rgb( 180, 205, 255 )",
projector: "rgb( 180, 205, 255 )",
}),

/** End-of-round panel border. */
roundCompletedStrokeColorProperty: new ProfileColorProperty(LewisStructuresNamespace, "roundCompletedStroke", {
default: "black",
projector: "black",
}),

/**
* Border/stroke color for control panels and dialogs.
* Teal-navy in default mode; medium gray in projector mode.
Expand Down
54 changes: 25 additions & 29 deletions src/challenge/view/ChallengeScreenView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CHALLENGE_SCREEN_VIEW_SETTINGS } from "../../LewisStructuresConstants.j
* bar (level, problem number, score, Start Over). Each problem names a species to draw from scratch
* with the palette and board; Hint reveals the next hint, Check grades the drawing, Next moves on.
* The electron count stays hidden until the first hint, since counting is part of the task. A
* LevelCompletedNode shows the score at the end of the round.
* RoundCompletedPanel shows the score at the end of the round.
*
* ┌ status bar ─────────────────────────────────────────────┐
* │ task ┌ hints ─────┐
Expand All @@ -23,7 +23,6 @@ import { ScreenView, type ScreenViewOptions } from "scenerystack/sim";
import { Tandem } from "scenerystack/tandem";
import {
FiniteStatusBar,
LevelCompletedNode,
LevelSelectionButtonGroup,
type LevelSelectionButtonGroupItem,
ScoreDisplayStars,
Expand Down Expand Up @@ -62,6 +61,7 @@ import lewisStructuresQueryParameters from "../../preferences/lewisStructuresQue
import { type ChallengeModel, HINTS, LEVEL_COUNT, NUMBER_OF_STARS, PERFECT_SCORE } from "../model/ChallengeModel.js";
import { ChallengeScreenSummaryContent } from "./ChallengeScreenSummaryContent.js";
import { hintText } from "./challengeHints.js";
import { RoundCompletedPanel } from "./RoundCompletedPanel.js";

const STATUS_BAR_HEIGHT: number = CHALLENGE_SCREEN_VIEW_SETTINGS.STATUS_BAR_HEIGHT;
const TASK_TOP = STATUS_BAR_HEIGHT + 8;
Expand All @@ -77,8 +77,6 @@ const LEVEL_ICON_SIZE: number = CHALLENGE_SCREEN_VIEW_SETTINGS.LEVEL_ICON_SIZE;
const LEVEL_ICON_SPECIES = ["water", "carbonDioxide", "sulfurHexafluoride"];

export class ChallengeScreenView extends ScreenView {
private levelCompletedNode: LevelCompletedNode | null = null;

public constructor(model: ChallengeModel, preferences: LewisStructuresPreferencesModel, options?: ScreenViewOptions) {
super({ screenSummaryContent: new ChallengeScreenSummaryContent(model), ...options });

Expand Down Expand Up @@ -390,31 +388,6 @@ export class ChallengeScreenView extends ScreenView {
resetAllButton.visible = level === null;
});

model.roundCompleteProperty.link((complete) => {
this.levelCompletedNode?.dispose();
this.levelCompletedNode = null;
if (complete) {
this.levelCompletedNode = new LevelCompletedNode(
model.levelNumberProperty.value,
model.scoreProperty.value,
PERFECT_SCORE,
NUMBER_OF_STARS,
false,
0,
0,
false,
() => model.leaveLevel(),
{
cornerRadius: PANEL_CORNER_RADIUS,
buttonFont: new PhetFont(CHALLENGE_SCREEN_VIEW_SETTINGS.LEVEL_COMPLETED_NODE_FONT_SIZE),
center: this.layoutBounds.center,
contentMaxWidth: 400,
},
);
this.addChild(this.levelCompletedNode);
}
});

this.addChild(
new Node({
pdomOrder: [
Expand All @@ -433,6 +406,29 @@ export class ChallengeScreenView extends ScreenView {
],
}),
);

// One end-of-round panel, shown when the round is complete. Rebuilding vegas'
// LevelCompletedNode each round leaked every panel (see RoundCompletedPanel).
const roundCompletedPanel = new RoundCompletedPanel(
model.levelNumberProperty,
model.scoreProperty,
PERFECT_SCORE,
NUMBER_OF_STARS,
() => model.leaveLevel(),
{
cornerRadius: PANEL_CORNER_RADIUS,
buttonFont: new PhetFont(CHALLENGE_SCREEN_VIEW_SETTINGS.LEVEL_COMPLETED_NODE_FONT_SIZE),
contentMaxWidth: 400,
visible: false,
},
);
roundCompletedPanel.localBoundsProperty.link(() => {
roundCompletedPanel.center = this.layoutBounds.center;
});
model.roundCompleteProperty.link((complete) => {
roundCompletedPanel.visible = complete;
});
this.addChild(roundCompletedPanel);
}
}

Expand Down
114 changes: 114 additions & 0 deletions src/challenge/view/RoundCompletedPanel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* RoundCompletedPanel.ts
*
* The end-of-round panel on the Challenge screen: a title that depends on the score, the stars,
* "Level N", "Score: X out of Y" and a Continue button. It looks like vegas' LevelCompletedNode
* and uses the vegas strings, but it is built once and shown or hidden, with its text derived
* from the model's level and score.
*
* Why not LevelCompletedNode: it is built per round, and its "Level N" DerivedProperty is never
* disposed (SceneryStack 3.0.0). That Property stays linked to the vegas level string, and the
* shared closure context keeps every disposed panel's subtree alive, so each completed round
* leaked a whole panel (the headless soak's retained-heap climb).
*/

import { DerivedStringProperty, type ReadOnlyProperty, type TReadOnlyProperty } from "scenerystack/axon";
import { optionize } from "scenerystack/phet-core";
import { StringUtils } from "scenerystack/phetcommon";
import { Text, VBox } from "scenerystack/scenery";
import { PhetColorScheme, PhetFont } from "scenerystack/scenery-phet";
import { Panel, type PanelOptions, TextPushButton } from "scenerystack/sun";
import { ScoreDisplayStars, VegasStrings } from "scenerystack/vegas";
import LewisStructuresColors from "../../LewisStructuresColors.js";

/** vegas LevelCompletedNode defaults, kept so the panel looks the same. */
const TITLE_FONT = new PhetFont({ size: 28, weight: "bold" });
const INFO_FONT = new PhetFont({ size: 22, weight: "bold" });
const Y_SPACING = 30;
const STAR_DIAMETER = 62;

type SelfOptions = {
buttonFont?: PhetFont;
contentMaxWidth?: number;
};

export type RoundCompletedPanelOptions = SelfOptions & PanelOptions;

export class RoundCompletedPanel extends Panel {
public constructor(
levelProperty: TReadOnlyProperty<number>,
scoreProperty: ReadOnlyProperty<number>,
perfectScore: number,
numberOfStars: number,
continueFunction: () => void,
providedOptions?: RoundCompletedPanelOptions,
) {
const options = optionize<RoundCompletedPanelOptions, SelfOptions, PanelOptions>()(
{
buttonFont: new PhetFont(26),
contentMaxWidth: 400,
fill: LewisStructuresColors.roundCompletedFillColorProperty,
stroke: LewisStructuresColors.roundCompletedStrokeColorProperty,
lineWidth: 2,
cornerRadius: 35,
xMargin: 20,
yMargin: 20,
},
providedOptions,
);
const maxWidth = options.contentMaxWidth;

// Same thresholds as LevelCompletedNode.
const titleStringProperty = new DerivedStringProperty(
[scoreProperty, VegasStrings.excellent, VegasStrings.great, VegasStrings.good, VegasStrings.keepTrying],
(score, excellent, great, good, keepTrying) => {
const proportion = score / perfectScore;
return proportion > 0.95 ? excellent : proportion > 0.75 ? great : proportion >= 0.5 ? good : keepTrying;
},
);
const levelStringProperty = new DerivedStringProperty([VegasStrings.label.level, levelProperty], (pattern, level) =>
StringUtils.format(pattern, level),
);
const scoreStringProperty = new DerivedStringProperty(
[VegasStrings.label.score.max, scoreProperty],
(pattern, score) => StringUtils.format(pattern, score, perfectScore),
);

const content = new VBox({
spacing: Y_SPACING,
children: [
new Text(titleStringProperty, { font: TITLE_FONT, maxWidth }),
new ScoreDisplayStars(scoreProperty, {
numberOfStars,
perfectScore,
starNodeOptions: { starShapeOptions: { innerRadius: STAR_DIAMETER / 4, outerRadius: STAR_DIAMETER / 2 } },
maxWidth,
}),
new Text(levelStringProperty, { font: INFO_FONT, maxWidth }),
new Text(scoreStringProperty, { font: INFO_FONT, maxWidth }),
new TextPushButton(VegasStrings.continue, {
listener: continueFunction,
font: options.buttonFont,
baseColor: PhetColorScheme.BUTTON_YELLOW,
maxWidth,
}),
],
});

super(content, options);

this.disposeRoundCompletedPanel = () => {
content.disposeSubtree();
titleStringProperty.dispose();
levelStringProperty.dispose();
scoreStringProperty.dispose();
};
}

private readonly disposeRoundCompletedPanel: () => void;

public override dispose(): void {
this.disposeRoundCompletedPanel();
super.dispose();
}
}
32 changes: 30 additions & 2 deletions tests/memory-leak.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@
* tests below using forceGC().
*/

import { Property } from "scenerystack/axon";
import { NumberProperty, Property } from "scenerystack/axon";
import { Vector2 } from "scenerystack/dot";
import { Node } from "scenerystack/scenery";
import { Tandem } from "scenerystack/tandem";
import { VegasStrings } from "scenerystack/vegas";
import { describe, it } from "vitest";
import { ChallengeModel } from "../src/challenge/model/ChallengeModel.js";
import { ChallengeScreenView } from "../src/challenge/view/ChallengeScreenView.js";
import { RoundCompletedPanel } from "../src/challenge/view/RoundCompletedPanel.js";
import { boardBounds, DrawingModel } from "../src/common/model/DrawingModel.js";
import { generateStructures } from "../src/common/model/generateStructures.js";
import { LewisAtom, LewisStructure } from "../src/common/model/LewisStructure.js";
Expand All @@ -29,8 +35,9 @@ import { ShapePreviewNode } from "../src/common/view/ShapePreviewNode.js";
import { ShapeViewNode } from "../src/common/view/ShapeViewNode.js";
import { SpeciesChooserNode } from "../src/common/view/SpeciesChooserNode.js";
import { StructureNode } from "../src/common/view/StructureNode.js";
import { LewisStructuresPreferencesModel } from "../src/preferences/LewisStructuresPreferencesModel.js";
import { FormCardNode } from "../src/resonance/view/FormCardNode.js";
import { describeDisposalLeaks } from "./helpers/memoryLeak.js";
import { describeDisposalLeaks, expectNoListenerGrowth } from "./helpers/memoryLeak.js";

const nitrate = (): StructureData =>
generateStructures(getSpecies("nitrate").skeleton, OctetPolicy.ACCEPT_BOTH).best[0]?.data ??
Expand Down Expand Up @@ -64,6 +71,8 @@ const speciesProperty = new Property<Species | null>(getSpecies("nitrate"));
const chargeProperty = new Property(0);
const listParent = new Node();
const speaker = new Node();
const levelNumberProperty = new NumberProperty(2);
const scoreProperty = new NumberProperty(7);

describeDisposalLeaks([
{
Expand Down Expand Up @@ -131,4 +140,23 @@ describeDisposalLeaks([
return new FormCardNode(form, 1, 136, 118, () => undefined);
},
},
{
name: "RoundCompletedPanel",
create: () => new RoundCompletedPanel(levelNumberProperty, scoreProperty, 10, 3, () => undefined),
keepAlive: () => [levelNumberProperty, scoreProperty],
},
]);

describe("Challenge round end", () => {
// Regression: the view used to build a vegas LevelCompletedNode per completed round, whose
// undisposed "Level N" DerivedProperty kept each panel linked to the vegas level string.
it("does not link new listeners to the vegas strings each round", async () => {
const preferences = new LewisStructuresPreferencesModel(Tandem.OPT_OUT);
const model = new ChallengeModel(preferences.octetPolicyProperty);
new ChallengeScreenView(model, preferences, { tandem: Tandem.OPT_OUT });
await expectNoListenerGrowth([VegasStrings.label.level, VegasStrings.label.score.max, model.scoreProperty], () => {
model.roundCompleteProperty.value = true;
model.roundCompleteProperty.value = false;
});
});
});