hide brush cost gauges if they're irrelevant

This commit is contained in:
りき萌 2025-06-17 00:18:48 +02:00
parent 8b464d50f4
commit 9b82b211b4
2 changed files with 41 additions and 4 deletions

View file

@ -1,9 +1,10 @@
class Gauge extends HTMLElement { class Gauge extends HTMLElement {
constructor(iconName, label) { constructor(iconName, label, description) {
super(); super();
this.iconName = iconName; this.iconName = iconName;
this.label = label; this.label = label;
this.description = description;
} }
connectedCallback() { connectedCallback() {
@ -12,12 +13,23 @@ class Gauge extends HTMLElement {
this.full = this.appendChild(document.createElement("div")); this.full = this.appendChild(document.createElement("div"));
this.full.classList.add("full", "icon", `icon-${this.iconName}-white`); this.full.classList.add("full", "icon", `icon-${this.iconName}-white`);
this.normalizedValue = 0;
this.updateVisibility();
} }
setValue(value, valueMax) { setValue(value, valueMax) {
let clampedNormalized = Math.max(0, Math.min(1, value / valueMax)); let clampedNormalized = Math.max(0, Math.min(1, value / valueMax));
this.normalizedValue = clampedNormalized;
this.style.setProperty("--progress", `${clampedNormalized * 100}%`); this.style.setProperty("--progress", `${clampedNormalized * 100}%`);
this.title = `${this.label}: ${value} / ${valueMax} (${Math.ceil(clampedNormalized * 100)}%)`; this.title = `${this.label}: ${value} / ${valueMax} (${Math.ceil(clampedNormalized * 100)}%)\n${this.description}`;
this.updateVisibility();
}
updateVisibility() {
this.visible = this.normalizedValue > 0.25;
if (!this.visible) this.classList.add("hidden");
else this.classList.remove("hidden");
} }
} }
@ -34,6 +46,10 @@ export class BrushCostGauges extends HTMLElement {
className: "code-size", className: "code-size",
iconName: "brackets", iconName: "brackets",
label: "Code size", label: "Code size",
description:
"How large your code is.\n" +
"The more syntactic elements there are, the bigger it gets!\n" +
"Note that this is not the same as the character count.",
}), }),
); );
this.fuelGauge = this.appendChild( this.fuelGauge = this.appendChild(
@ -41,6 +57,10 @@ export class BrushCostGauges extends HTMLElement {
className: "fuel", className: "fuel",
iconName: "droplet", iconName: "droplet",
label: "Fuel", label: "Fuel",
description:
"How long your code runs.\n" +
"The longer it runs, the more fuel it consumes!\n" +
"Remember that built-in functions are cheaper than ones you write yourself.",
}), }),
); );
this.memoryGauge = this.appendChild( this.memoryGauge = this.appendChild(
@ -48,6 +68,10 @@ export class BrushCostGauges extends HTMLElement {
className: "memory", className: "memory",
iconName: "memory", iconName: "memory",
label: "Memory", label: "Memory",
description:
"How much memory your code uses.\n" +
"This includes a baseline amount of memory for temporary data.\n" +
"Excessive list transformations can fill up your memory very quickly!",
}), }),
); );
@ -60,13 +84,18 @@ export class BrushCostGauges extends HTMLElement {
this.codeSizeGauge.setValue(stats.astSize, stats.astSizeMax); this.codeSizeGauge.setValue(stats.astSize, stats.astSizeMax);
this.fuelGauge.setValue(stats.fuel, stats.fuelMax); this.fuelGauge.setValue(stats.fuel, stats.fuelMax);
this.memoryGauge.setValue(stats.memory, stats.memoryMax); this.memoryGauge.setValue(stats.memory, stats.memoryMax);
let visible =
this.codeSizeGauge.visible || this.fuelGauge.visible || this.memoryGauge.visible;
if (!visible) this.classList.add("hidden");
else this.classList.remove("hidden");
} }
} }
customElements.define("rkgk-brush-cost-gauges", BrushCostGauges); customElements.define("rkgk-brush-cost-gauges", BrushCostGauges);
function createGauge({ className, iconName, label }) { function createGauge({ className, iconName, label, description }) {
let gauge = new Gauge(iconName, label); let gauge = new Gauge(iconName, label, description);
gauge.classList.add(className); gauge.classList.add(className);
return gauge; return gauge;
} }

View file

@ -425,11 +425,19 @@ rkgk-brush-cost-gauges.rkgk-panel {
overflow: clip; /* clip corners */ overflow: clip; /* clip corners */
&.hidden {
display: none;
}
& > rkgk-brush-cost-gauge { & > rkgk-brush-cost-gauge {
display: block; display: block;
height: var(--gauge-size); height: var(--gauge-size);
flex-grow: 1; flex-grow: 1;
&.hidden {
display: none;
}
& > .full { & > .full {
width: 100%; width: 100%;
height: 100%; height: 100%;