All files / player-ui/src/components gauge-panel.ts

13.33% Statements 2/15
0% Branches 0/28
0% Functions 0/2
14.28% Lines 2/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100              48x 48x                                                                                                                                                                                      
import { svg, html, nothing, css, type CSSResultGroup, type PropertyDeclarations, type TemplateResult } from 'lit';
import { DataElement } from '../base.ts';
import type { BeMusicGaugeData } from '../models.ts';
import { structureStyles } from '../styles/structure.ts';
import { stats } from '../templates.ts';
 
export class BeMusicGaugePanelElement extends DataElement<BeMusicGaugeData> {
  static override properties: PropertyDeclarations = { ...DataElement.properties, compact: { type: Boolean } };
  static override styles: CSSResultGroup = [
    structureStyles,
    css`
      label {
        display: flex;
        align-items: center;
        gap: 6px;
        font-size: 0.85em;
      }
      .compact {
        display: block;
        position: relative;
        text-align: left;
      }
      .compact .name {
        display: block;
        margin-right: 3em;
        overflow: hidden;
        white-space: nowrap;
      }
      .compact .value {
        position: absolute;
        right: 0;
        top: 0;
      }
      .compact meter {
        display: block;
        width: 100%;
        margin-top: 4px;
      }
      :host([data-cabinet]) .compact {
        height: 100%;
      }
      :host([data-cabinet]) .name,
      :host([data-cabinet]) .value {
        position: absolute;
        top: var(--gauge-caption-y);
        transform: translateY(-50%);
      }
      :host([data-cabinet]) .compact meter {
        position: absolute;
        top: var(--gauge-bar-y);
        height: var(--gauge-bar-height);
        margin-top: 0;
      }
      meter {
        flex: 1;
        min-width: 0;
        height: 12px;
      }
    `,
  ];
  declare compact: boolean;
  protected override render(): TemplateResult | typeof nothing {
    const d = this.data;
    if (!d) return nothing;
    const value = Math.max(0, Math.min(100, d.value));
    const meter = html`<label part="meter"
      ><span>${d.label ?? 'Gauge'} · ${value.toFixed(1)}%</span
      ><meter min="0" max="100" .value=${value} aria-label=${d.label ?? 'Gauge'}></meter
    ></label>`;
    if (this.compact)
      return html`<label class="compact" part="meter"
        ><span class="name">${d.label ?? 'Gauge'}</span><span class="value">${value.toFixed(1)}%</span
        ><meter min="0" max="100" .value=${value} aria-label=${d.label ?? 'Gauge'}></meter
      ></label>`;
    const count = Math.min(96, d.history.length);
    const points = Array.from({ length: count }, (_, i) => {
      const sample = d.history[Math.round((i / Math.max(1, count - 1)) * (d.history.length - 1))]!;
      return `${sample.progress * 220},${44 - Math.max(0, Math.min(100, sample.value)) * 0.44}`;
    }).join(' ');
    const threshold = 44 - Math.max(0, Math.min(100, d.threshold ?? 0)) * 0.44;
    return html`<section class="panel" part="panel">
      <h3>Gauge</h3>
      ${meter}${stats([
        ['Clear threshold', d.survival ? 'Survival' : d.threshold === undefined ? '—' : `${d.threshold}%`],
        ['Status', d.survival ? (value > 0 ? 'Surviving' : 'Failed') : d.cleared ? 'Clear' : 'Below threshold'],
      ])}
      <svg part="graph" viewBox="0 0 220 44" preserveAspectRatio="none" role="img" aria-label="Gauge history">
        ${!d.survival && d.threshold !== undefined ? svg`<line x1="0" x2="220" y1=${threshold} y2=${threshold} stroke="currentColor" stroke-dasharray="3 3"></line>` : nothing}
        <polyline
          fill="none"
          stroke="var(--primary,#edf2fa)"
          stroke-width="2"
          stroke-linejoin="round"
          points=${points}
        ></polyline>
      </svg>
    </section>`;
  }
}