All files / core/src/RenderingEngine/helpers/stats RenderModesPanel.ts

0.98% Statements 1/102
0% Branches 0/31
0% Functions 0/12
0.98% Lines 1/102

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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382                                                                                                                                                                                                                                                                                                                                                                                                                                          128x                                                                                                                                                                                                                                                                                                                                                
import type { Panel } from './types';
import { PANEL_CONFIG, PANEL_CONFIGS } from './constants';
import { PanelType } from './enums';
 
/**
 * Text panel that displays each viewport's GenericViewport bindings from its
 * internal debug map plus public actor metadata.
 */
export class RenderModesPanel implements Panel {
  public dom: HTMLDivElement;
  private readonly list: HTMLDivElement;
 
  constructor() {
    const config = PANEL_CONFIGS[PanelType.RENDER_MODES];
    this.dom = document.createElement('div');
    this.dom.style.cssText = `
      width:min(720px, max(${PANEL_CONFIG.WIDE_WIDTH}px, calc(100vw - ${
        PANEL_CONFIG.WIDTH + 24
      }px)));
      min-width:0;
      min-height:${PANEL_CONFIG.HEIGHT}px;
      background:${config.backgroundColor};
      color:${config.foregroundColor};
      font:${PANEL_CONFIG.FONT_SIZE + 2}px ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
      padding:8px;
      box-sizing:border-box;
      max-height:min(520px, calc(100vh - 16px));
      overflow:auto;
      border:1px solid rgba(255, 204, 0, 0.35);
      border-radius:4px;
      box-shadow:0 8px 28px rgba(0, 0, 0, 0.35);
    `;
 
    const title = document.createElement('div');
    title.textContent = config.name;
    title.style.cssText = `
      margin-bottom:8px;
      font-weight:700;
      letter-spacing:0.08em;
      text-transform:uppercase;
    `;
    this.dom.appendChild(title);
 
    this.list = document.createElement('div');
    this.list.style.cssText = `
      display:flex;
      flex-direction:column;
      gap:8px;
      line-height:1.35;
      font-weight:normal;
      color:#fff3bf;
    `;
    this.dom.appendChild(this.list);
  }
 
  /**
   * Implements the Panel numeric signature as a no-op; render-mode content is
   * refreshed via {@link setContent}.
   */
  public update(): void {
    // Content is pushed from StatsOverlay via setContent().
  }
 
  public setContent(
    entries: Array<{
      renderingEngineId: string;
      viewportId: string;
      viewportType: string;
      bindings: RenderModePanelBinding[];
    }>
  ): void {
    this.list.replaceChildren();
 
    if (!entries.length) {
      this.list.appendChild(createEmptyState('(no viewports)'));
      return;
    }
 
    for (const entry of entries) {
      const bindings = orderBindings(entry.bindings);
      const section = document.createElement('div');
      section.style.cssText = `
        padding:7px;
        background:rgba(0, 0, 0, 0.18);
        border:1px solid rgba(255, 204, 0, 0.18);
        border-radius:4px;
      `;
 
      const viewportId = `${entry.renderingEngineId}/${entry.viewportId}`;
      const heading = document.createElement('div');
      heading.title = viewportId;
      heading.style.cssText = `
        display:flex;
        align-items:center;
        justify-content:space-between;
        gap:8px;
        margin-bottom:6px;
        color:#ffd84d;
        font-weight:700;
      `;
 
      const left = document.createElement('div');
      left.style.cssText = `
        display:flex;
        align-items:center;
        gap:6px;
        min-width:0;
      `;
 
      const name = document.createElement('span');
      name.textContent = truncate(viewportId, 72);
      name.style.cssText = `
        min-width:0;
        overflow:hidden;
        text-overflow:ellipsis;
        white-space:nowrap;
      `;
      left.appendChild(name);
 
      if (entry.viewportType) {
        const type = document.createElement('span');
        type.textContent = entry.viewportType;
        type.title = entry.viewportType;
        type.style.cssText = `
          flex:0 0 auto;
          padding:2px 6px;
          background:rgba(255, 204, 0, 0.14);
          border:1px solid rgba(255, 204, 0, 0.4);
          border-radius:3px;
          color:#ffe08a;
          font-size:${PANEL_CONFIG.FONT_SIZE}px;
          font-weight:700;
          letter-spacing:0.03em;
        `;
        left.appendChild(type);
      }
 
      heading.appendChild(left);
 
      const count = document.createElement('span');
      count.textContent = `${bindings.length} binding${bindings.length === 1 ? '' : 's'}`;
      count.style.cssText = `
        flex:0 0 auto;
        color:#c9b05a;
        font-size:${PANEL_CONFIG.FONT_SIZE}px;
        font-weight:600;
      `;
      heading.appendChild(count);
 
      section.appendChild(heading);
 
      if (!bindings.length) {
        section.appendChild(createEmptyState('(no bindings)'));
        this.list.appendChild(section);
        continue;
      }
 
      for (const binding of bindings) {
        section.appendChild(createBindingRow(binding));
      }
 
      this.list.appendChild(section);
    }
  }
}
 
export type RenderModePanelBinding = {
  actorUID?: string;
  dataId: string;
  referencedId?: string;
  renderMode: string;
  role: 'source' | 'overlay' | 'data';
  /** Scalar buffer type backing the actor, e.g. `Uint8Array`. */
  scalarType?: string;
  /** Min/max of the actor's scalar data. */
  scalarRange?: [number, number];
  /** Number of components per voxel (1 for grayscale, 3 for RGB, ...). */
  numberOfComponents?: number;
};
 
function orderBindings(
  bindings: RenderModePanelBinding[]
): RenderModePanelBinding[] {
  const rank = {
    source: 0,
    overlay: 1,
    data: 2,
  };
 
  return [...bindings].sort((a, b) => {
    const roleDelta = rank[a.role] - rank[b.role];
 
    if (roleDelta !== 0) {
      return roleDelta;
    }
 
    return a.dataId.localeCompare(b.dataId);
  });
}
 
function truncate(value: string, max: number): string {
  if (value.length <= max) {
    return value;
  }
 
  const head = Math.ceil((max - 1) / 2);
  const tail = Math.floor((max - 1) / 2);
  return `${value.slice(0, head)}...${value.slice(-tail)}`;
}
 
const ROLE_STYLES: Record<
  RenderModePanelBinding['role'],
  { background: string; border: string; color: string; label: string }
> = {
  source: {
    background: 'rgba(0, 104, 126, 0.34)',
    border: 'rgba(92, 225, 255, 0.4)',
    color: '#9decff',
    label: 'SOURCE',
  },
  overlay: {
    background: 'rgba(165, 102, 0, 0.3)',
    border: 'rgba(255, 205, 83, 0.45)',
    color: '#ffd76d',
    label: 'OVERLAY',
  },
  data: {
    background: 'rgba(96, 101, 116, 0.28)',
    border: 'rgba(187, 195, 214, 0.32)',
    color: '#dce5ff',
    label: 'DATA',
  },
};
 
function createBindingRow(binding: RenderModePanelBinding): HTMLDivElement {
  const row = document.createElement('div');
  row.style.cssText = `
    display:grid;
    grid-template-columns:74px minmax(0, 1fr);
    gap:8px;
    padding:6px;
    margin-top:4px;
    background:rgba(255, 255, 255, 0.045);
    border-radius:4px;
  `;
 
  const roleStyle = ROLE_STYLES[binding.role];
  const role = document.createElement('div');
  role.textContent = roleStyle.label;
  role.style.cssText = `
    align-self:start;
    padding:3px 5px;
    background:${roleStyle.background};
    border:1px solid ${roleStyle.border};
    border-radius:3px;
    color:${roleStyle.color};
    font-size:${PANEL_CONFIG.FONT_SIZE}px;
    font-weight:800;
    letter-spacing:0.04em;
    text-align:center;
  `;
  row.appendChild(role);
 
  const fields = document.createElement('div');
  fields.style.cssText = `
    display:flex;
    flex-direction:column;
    gap:3px;
    min-width:0;
  `;
 
  appendField(fields, 'mode', binding.renderMode, 48);
  appendField(fields, 'data', binding.dataId, 76);
 
  if (binding.actorUID && binding.actorUID !== binding.dataId) {
    appendField(fields, 'actor', binding.actorUID, 76);
  }
 
  if (
    binding.referencedId &&
    binding.referencedId !== binding.dataId &&
    binding.referencedId !== binding.actorUID
  ) {
    appendField(fields, 'ref', binding.referencedId, 76);
  }
 
  if (binding.scalarType) {
    appendField(
      fields,
      'type',
      formatScalarType(binding.scalarType, binding.numberOfComponents),
      40
    );
  }
 
  if (binding.scalarRange) {
    appendField(fields, 'range', formatScalarRange(binding.scalarRange), 56);
  }
 
  row.appendChild(fields);
 
  return row;
}
 
/**
 * Renders the scalar buffer type compactly, dropping the `Array` suffix and
 * appending the component count for multi-component data, e.g. `Uint8Array`
 * with 3 components becomes `Uint8 x3`.
 */
function formatScalarType(
  scalarType: string,
  numberOfComponents?: number
): string {
  const friendly = scalarType.replace(/Array$/, '');
 
  return numberOfComponents && numberOfComponents > 1
    ? `${friendly} x${numberOfComponents}`
    : friendly;
}
 
function formatScalarRange([min, max]: [number, number]): string {
  return `${formatScalarValue(min)} .. ${formatScalarValue(max)}`;
}
 
function formatScalarValue(value: number): string {
  if (Number.isInteger(value)) {
    return String(value);
  }
 
  return Number(value.toFixed(2)).toString();
}
 
function appendField(
  parent: HTMLDivElement,
  label: string,
  value: string,
  maxLength: number
): void {
  const field = document.createElement('div');
  field.style.cssText = `
    display:grid;
    grid-template-columns:42px minmax(0, 1fr);
    gap:6px;
    min-width:0;
  `;
 
  const labelNode = document.createElement('span');
  labelNode.textContent = label;
  labelNode.style.cssText = `
    color:#a9954c;
    font-size:${PANEL_CONFIG.FONT_SIZE}px;
    font-weight:700;
    text-transform:uppercase;
  `;
  field.appendChild(labelNode);
 
  const valueNode = document.createElement('span');
  valueNode.textContent = truncate(value, maxLength);
  valueNode.title = value;
  valueNode.style.cssText = `
    min-width:0;
    overflow:hidden;
    text-overflow:ellipsis;
    white-space:nowrap;
    color:#fff7d6;
  `;
  field.appendChild(valueNode);
 
  parent.appendChild(field);
}
 
function createEmptyState(message: string): HTMLDivElement {
  const empty = document.createElement('div');
  empty.textContent = message;
  empty.style.cssText = `
    color:#b7a865;
    font-style:italic;
  `;
 
  return empty;
}