File size: 24,494 Bytes
8a29801 | 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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 | /**
* DisplayProcessor
*
* Processes display events with pixel-accurate text wrapping using the same
* Engine display utilities. This ensures the mobile preview matches exactly
* what is shown on the glasses.
*
* Key responsibilities:
* 1. Intercept display events before sending to native SGC
* 2. Wrap text using the correct device profile (G1, etc.)
* 3. Ensure GlassesDisplayMirror shows exactly what glasses will show
*
* @see notes/miniapp-display-api-redesign.md for the current display design
*/
import {
createDisplayToolkit,
G1_PROFILE,
G2_PROFILE,
Z100_PROFILE,
NEX_PROFILE,
NIMO_PROFILE,
TextMeasurer,
TextWrapper,
ColumnComposer,
type DisplayProfile,
type WrapOptions,
type BreakMode,
} from "../utils/display"
import {ISLAND_SETTINGS_KEYS} from "../runtime/config"
import {useGlassesStore} from "../stores/glasses"
import {useSettingsStore} from "../stores/settings"
import {isNimoModelName} from "../types/hardware"
import {isGlassesConnected} from "./GlassesReadiness"
// =============================================================================
// Types
// =============================================================================
/**
* Supported device models for display processing
*/
export type DeviceModel = "g1" | "g2" | "z100" | "nex" | "nimo" | "mach1" | "mentra-live" | "simulated" | "unknown"
/**
* Display event types that we process
*/
export type DisplayLayoutType =
| "text_wall"
| "text_line"
| "text_rows"
| "reference_card"
| "double_text_wall"
| "bitmap_view"
/**
* Raw local display event before device-specific processing
*/
export interface DisplayEvent {
view: "main" | "dashboard"
layoutType?: DisplayLayoutType
layout?: {
layoutType: DisplayLayoutType
text?: string | string[] // string for text_wall/text_line, string[] for text_rows
title?: string
topText?: string
bottomText?: string
breakMode?: BreakMode
data?: string // For bitmap_view
x?: number // bitmap_view container position/size (forwarded to native, used by G2)
y?: number
width?: number
height?: number
[key: string]: unknown
}
text?: string | string[]
title?: string
topText?: string
bottomText?: string
breakMode?: BreakMode
[key: string]: unknown
}
/**
* Processed display event with guaranteed wrapped text
*/
export interface ProcessedDisplayEvent extends DisplayEvent {
/** Marks this event as processed */
_processed: true
/** The device profile used for processing */
_profile: string
/** Pre-split lines for text_wall/text_line (for easy rendering) */
_lines?: string[]
/** Original layout type before processing (e.g., double_text_wall -> text_wall) */
_originalLayoutType?: DisplayLayoutType
/** Column composition metadata for double_text_wall */
_composedColumns?: {
leftLines: string[]
rightLines: string[]
config: {
leftColumnWidthPx: number
rightColumnStartPx: number
rightColumnWidthPx: number
maxLines: number
leftMarginSpaces?: number
}
}
}
/**
* Options for the DisplayProcessor
*/
export interface DisplayProcessorOptions {
/** Default break mode for text wrapping */
breakMode?: BreakMode
/** Whether to log processing details */
debug?: boolean
}
// =============================================================================
// Placeholder Replacement
// =============================================================================
/**
* Placeholder values that can be replaced in display text.
* These match the placeholders used in CoreManager.kt/CoreManager.swift
*/
interface PlaceholderValues {
/** Current time in 12-hour format (e.g., "2:30 PM") */
TIME12: string
/** Current time in 24-hour format (e.g., "14:30") */
TIME24: string
/** Current date (e.g., "1/22") */
DATE: string
/** Glasses battery level (e.g., "85%" or "" if unknown) */
GBATT: string
/** Connection status */
CONNECTION_STATUS: string
}
/**
* Get current placeholder values from device state
*/
function getPlaceholderValues(): PlaceholderValues {
const now = new Date()
// Format time in 12-hour format
const hours12 = now.getHours() % 12 || 12
const minutes = now.getMinutes().toString().padStart(2, "0")
const ampm = now.getHours() >= 12 ? "PM" : "AM"
const TIME12 = `${hours12}:${minutes} ${ampm}`
// Format time in 24-hour format
const hours24 = now.getHours().toString().padStart(2, "0")
const TIME24 = `${hours24}:${minutes}`
// Format date
const month = now.getMonth() + 1
const day = now.getDate()
const DATE = `${month}/${day}`
// Battery + connection read from the engine glasses store directly (was a host
// glassesStatus hook).
const batteryLevel = useGlassesStore.getState().batteryLevel ?? -1
const GBATT = batteryLevel === -1 ? "" : `${batteryLevel}%`
// Connection status
const connected = isGlassesConnected(useGlassesStore.getState().connection)
const CONNECTION_STATUS = connected ? "Connected" : ""
return {TIME12, TIME24, DATE, GBATT, CONNECTION_STATUS}
}
/**
* Replace placeholders in text with actual values.
* Placeholders are in format $NAME$ (e.g., $GBATT$, $TIME12$)
*
* @param text - Text containing placeholders
* @returns Text with placeholders replaced
*/
function replacePlaceholders(text: string): string {
if (!text || !text.includes("$")) {
return text
}
const values = getPlaceholderValues()
return text
.replace(/\$TIME12\$/g, values.TIME12)
.replace(/\$TIME24\$/g, values.TIME24)
.replace(/\$DATE\$/g, values.DATE)
.replace(/\$GBATT\$/g, values.GBATT)
.replace(/\$CONNECTION_STATUS\$/g, values.CONNECTION_STATUS)
.replace(/\$no_datetime\$/g, `${values.DATE}, ${values.TIME12}`)
}
function isBreakMode(value: unknown): value is BreakMode {
return value === "character" || value === "character-no-hyphen" || value === "word" || value === "strict-word"
}
// =============================================================================
// Device Profile Mapping
// =============================================================================
/**
* Map device model names to display profiles
*/
const DEVICE_PROFILES: Record<DeviceModel, DisplayProfile> = {
"g1": G1_PROFILE,
"g2": G2_PROFILE,
"z100": Z100_PROFILE,
"nex": NEX_PROFILE,
"nimo": NIMO_PROFILE,
"mach1": Z100_PROFILE, // Mach1 uses same hardware as Vuzix Z100
"mentra-live": G1_PROFILE, // Mentra Live has no display, uses G1 as fallback
"simulated": G1_PROFILE, // Simulated uses G1 profile
"unknown": G1_PROFILE, // Default to G1
}
/**
* Normalize various model name strings to our DeviceModel type
*/
function normalizeModelName(modelName: string | null | undefined): DeviceModel {
if (!modelName) return "unknown"
const lower = modelName.toLowerCase()
if (isNimoModelName(modelName)) {
return "nimo"
}
if (lower.includes("g2") || lower.includes("even realities g2")) {
return "g2"
}
if (lower.includes("g1") || lower.includes("even realities")) {
return "g1"
}
if (lower.includes("z100") || lower.includes("vuzix")) {
return "z100"
}
if (lower.includes("nex") || lower.includes("mentra display")) {
return "nex"
}
if (lower.includes("mach1") || lower.includes("mach 1")) {
return "mach1"
}
if (lower.includes("mentra live") || lower.includes("mentra-live")) {
return "mentra-live"
}
if (lower.includes("simulated") || lower.includes("simulator")) {
return "simulated"
}
return "unknown"
}
// =============================================================================
// DisplayProcessor Class
// =============================================================================
/**
* Processes display events with pixel-accurate text wrapping.
*
* Usage:
* ```typescript
* const processor = DisplayProcessor.getInstance()
*
* // When glasses connect
* processor.setDeviceModel("Even Realities G1")
*
* // Process display events
* const processed = processor.processDisplayEvent(rawEvent)
* ```
*/
export class DisplayProcessor {
private static instance: DisplayProcessor | null = null
private measurer: TextMeasurer
private wrapper: TextWrapper
private composer: ColumnComposer
private profile: DisplayProfile
private deviceModel: DeviceModel = "unknown"
private options: DisplayProcessorOptions
private runtimeAttached = false
private unsubscribeGlassesStatus: (() => void) | null = null
private constructor(options: DisplayProcessorOptions = {}) {
this.options = {
breakMode: "character-no-hyphen",
debug: false,
...options,
}
// Initialize with default G1 profile
const engine = createDisplayToolkit(G1_PROFILE, {
breakMode: this.options.breakMode,
})
this.measurer = engine.measurer
this.wrapper = engine.wrapper
this.composer = new ColumnComposer(engine.profile, this.options.breakMode)
this.profile = engine.profile
// Engine stores may not be hydrated yet at module-load time; attachToRuntime()
// is called again by the host after app services start.
this.attachToRuntime()
}
/**
* Read the current default wearable and subscribe to engine glasses-status changes.
* Idempotent — safe to call again after app services start.
*/
public attachToRuntime(): void {
if (this.runtimeAttached) {
return
}
// Seed the device profile from the saved default wearable, read off the engine
// settings store directly (was a host settings hook).
const defaultWearable = useSettingsStore.getState().getSetting(ISLAND_SETTINGS_KEYS.defaultWearable) as
| string
| undefined
if (defaultWearable) {
this.setDeviceModel(defaultWearable)
console.log(`DISPLAY_PROCESSOR: Initialized DisplayProcessor with default wearable: ${defaultWearable}`)
}
// Track device-model changes off the engine glasses store directly (was a host
// subscribeGlassesStatus hook).
this.unsubscribeGlassesStatus = useGlassesStore.subscribe(
(s) => s.deviceModel,
(deviceModel) => {
if (deviceModel) this.setDeviceModel(String(deviceModel))
},
)
this.runtimeAttached = true
}
/** Detach runtime subscriptions installed by attachToRuntime(). */
public detachFromRuntime(): void {
this.unsubscribeGlassesStatus?.()
this.unsubscribeGlassesStatus = null
this.runtimeAttached = false
}
/**
* Get the singleton instance
*/
public static getInstance(options?: DisplayProcessorOptions): DisplayProcessor {
if (!DisplayProcessor.instance) {
DisplayProcessor.instance = new DisplayProcessor(options)
}
return DisplayProcessor.instance
}
/**
* Reset the singleton (useful for testing)
*/
public static resetInstance(): void {
DisplayProcessor.instance?.detachFromRuntime()
DisplayProcessor.instance = null
}
// ===========================================================================
// Device Profile Management
// ===========================================================================
/**
* Set the device model and update the display profile accordingly.
* Call this when glasses connect.
*
* @param modelName - The model name from the connected glasses (e.g., "Even Realities G1")
*/
public setDeviceModel(modelName: string | null | undefined): void {
const normalizedModel = normalizeModelName(modelName)
if (normalizedModel === this.deviceModel) {
return // No change needed
}
this.deviceModel = normalizedModel
const newProfile = DEVICE_PROFILES[normalizedModel]
if (newProfile !== this.profile) {
this.updateProfile(newProfile)
}
if (this.options.debug) {
console.log(`[DisplayProcessor] Device model set to: ${normalizedModel} (from: ${modelName})`)
}
}
/**
* Update the display profile and recreate engine
*/
private updateProfile(newProfile: DisplayProfile): void {
const engine = createDisplayToolkit(newProfile, {
breakMode: this.options.breakMode,
})
this.measurer = engine.measurer
this.wrapper = engine.wrapper
this.composer = new ColumnComposer(newProfile, this.options.breakMode)
this.profile = engine.profile
if (this.options.debug) {
console.log(`[DisplayProcessor] Profile updated to: ${newProfile.id}`)
}
}
/**
* Get the current device model
*/
public getDeviceModel(): DeviceModel {
return this.deviceModel
}
/**
* Get the current display profile
*/
public getProfile(): DisplayProfile {
return this.profile
}
// ===========================================================================
// Display Event Processing
// ===========================================================================
/**
* Process a display event, wrapping text as needed.
*
* @param event - Raw display event from WebSocket
* @returns Processed event with wrapped text
*/
public processDisplayEvent(event: DisplayEvent): ProcessedDisplayEvent {
// Already processed? Return as-is
if ((event as ProcessedDisplayEvent)._processed) {
return event as ProcessedDisplayEvent
}
// Get layout type from either root or nested layout object
const layoutType = event.layoutType || event.layout?.layoutType
const layout = event.layout || event
if (!layoutType) {
// No layout type - pass through
return {
...event,
_processed: true,
_profile: this.profile.id,
}
}
switch (layoutType) {
case "text_wall":
case "text_line":
return this.processTextWall(event, layout)
case "text_rows":
return this.processTextRows(event, layout)
case "reference_card":
return this.processReferenceCard(event, layout)
case "double_text_wall":
return this.processDoubleTextWall(event, layout)
case "bitmap_view":
// Bitmap views don't need text processing
return {
...event,
_processed: true,
_profile: this.profile.id,
}
default:
// Unknown layout type - pass through
if (this.options.debug) {
console.log(`[DisplayProcessor] Unknown layout type: ${layoutType}`)
}
return {
...event,
_processed: true,
_profile: this.profile.id,
}
}
}
/**
* Process text_wall or text_line layout
*/
private processTextWall(
event: DisplayEvent,
layout: DisplayEvent | NonNullable<DisplayEvent["layout"]>,
): ProcessedDisplayEvent {
// text_wall/text_line always have string text, not string[]
const rawText = layout.text
const text = typeof rawText === "string" ? rawText : ""
// Replace placeholders BEFORE wrapping (so we measure actual content)
const textWithPlaceholders = replacePlaceholders(text)
const breakMode = this.getEventBreakMode(event, layout)
// Wrap the text
const lines = this.wrapText(textWithPlaceholders, breakMode ? {breakMode} : undefined)
const wrappedText = lines.join("\n")
// Update both root and nested layout if present
const processedLayout = event.layout
? {
...event.layout,
text: wrappedText,
}
: undefined
return {
...event,
// Store original text with placeholders for debugging
_originalText: text,
text: wrappedText,
layout: processedLayout,
_processed: true,
_profile: this.profile.id,
_lines: lines,
}
}
/**
* Process text_rows layout
*/
private processTextRows(
event: DisplayEvent,
layout: DisplayEvent | NonNullable<DisplayEvent["layout"]>,
): ProcessedDisplayEvent {
const textField = layout.text
const rows: string[] = Array.isArray(textField) ? textField : []
// Replace placeholders and wrap each row
const breakMode = this.getEventBreakMode(event, layout)
const wrappedRows = rows.map((row: string) => {
const rowWithPlaceholders = replacePlaceholders(row)
const lines = this.wrapText(rowWithPlaceholders, breakMode ? {breakMode} : undefined)
return lines.join("\n")
})
const processedLayout = event.layout
? {
...event.layout,
text: wrappedRows as string | string[],
}
: undefined
return {
...event,
text: wrappedRows as string | string[],
layout: processedLayout,
_processed: true,
_profile: this.profile.id,
}
}
/**
* Process reference_card layout
*/
private processReferenceCard(
event: DisplayEvent,
layout: DisplayEvent | NonNullable<DisplayEvent["layout"]>,
): ProcessedDisplayEvent {
const title = layout.title || ""
// reference_card text is always string, not string[]
const rawText = layout.text
const text = typeof rawText === "string" ? rawText : ""
// Replace placeholders BEFORE wrapping
const titleWithPlaceholders = replacePlaceholders(title)
const textWithPlaceholders = replacePlaceholders(text)
const breakMode = this.getEventBreakMode(event, layout)
// Wrap title and text separately
// Title typically gets 1 line, text gets remaining lines
const wrappedTitle = this.wrapText(titleWithPlaceholders, {maxLines: 1, ...(breakMode ? {breakMode} : {})})
const wrappedText = this.wrapText(textWithPlaceholders, {
maxLines: this.profile.maxLines - 1,
...(breakMode ? {breakMode} : {}),
})
const processedLayout = event.layout
? {
...event.layout,
title: wrappedTitle.join("\n"),
text: wrappedText.join("\n"),
}
: undefined
return {
...event,
title: wrappedTitle.join("\n"),
text: wrappedText.join("\n"),
layout: processedLayout,
_processed: true,
_profile: this.profile.id,
}
}
/**
* Process double_text_wall layout
*
* Uses ColumnComposer to create a fully composed string with both columns
* merged line-by-line with pixel-precise space alignment. This is the single
* source of truth for double_text_wall composition, replacing duplicate
* implementations in native iOS (G1Text.swift) and Android (G1Text.kt).
*
* The composed text is sent to native as a regular text_wall - native just
* chunks and sends it without any re-wrapping or column composition.
*/
private processDoubleTextWall(
event: DisplayEvent,
layout: DisplayEvent | NonNullable<DisplayEvent["layout"]>,
): ProcessedDisplayEvent {
const leftText = layout.topText || ""
const rightText = layout.bottomText || ""
// Replace placeholders BEFORE composition (so we measure actual content)
const leftTextWithPlaceholders = replacePlaceholders(leftText)
const rightTextWithPlaceholders = replacePlaceholders(rightText)
const breakMode = this.getEventBreakMode(event, layout)
const composer =
breakMode && breakMode !== this.options.breakMode ? new ColumnComposer(this.profile, breakMode) : this.composer
// Use ColumnComposer for pixel-precise column composition
const result = composer.composeDoubleTextWall(leftTextWithPlaceholders, rightTextWithPlaceholders)
if (this.options.debug) {
console.log(`[DisplayProcessor] double_text_wall composed:`)
console.log(` Left lines: ${result.leftLines.length}`)
console.log(` Right lines: ${result.rightLines.length}`)
console.log(` Config: ${JSON.stringify(result.config)}`)
}
// The composed text is a single string with both columns merged
// Native will receive this as a text_wall and just chunk & send it
const processedLayout = event.layout
? {
...event.layout,
// Keep original topText/bottomText for reference but add composed text
topText: leftTextWithPlaceholders,
bottomText: rightTextWithPlaceholders,
// The composed text is what native should actually display
_composedText: result.composedText,
}
: undefined
return {
...event,
// Override the layout type to text_wall since we've pre-composed the columns
// This tells native to use sendTextWall instead of sendDoubleTextWall
layoutType: "text_wall" as DisplayLayoutType,
text: result.composedText,
// Keep original values for debugging/preview (with placeholders replaced)
topText: leftTextWithPlaceholders,
bottomText: rightTextWithPlaceholders,
// Store original values with placeholders for debugging
_originalTopText: leftText,
_originalBottomText: rightText,
layout: processedLayout
? {
...processedLayout,
layoutType: "text_wall" as DisplayLayoutType,
text: result.composedText,
}
: undefined,
_processed: true,
_profile: this.profile.id,
_originalLayoutType: "double_text_wall",
_composedColumns: {
leftLines: result.leftLines,
rightLines: result.rightLines,
config: result.config,
},
}
}
// ===========================================================================
// Text Wrapping Utilities
// ===========================================================================
/**
* Wrap text using the current profile
*
* @param text - Text to wrap
* @param options - Optional override options
* @returns Array of wrapped lines
*/
public wrapText(text: string, options?: WrapOptions): string[] {
if (!text) return [""]
const result = this.wrapper.wrap(text, options)
return result.lines
}
private getEventBreakMode(
event: DisplayEvent,
layout: DisplayEvent | NonNullable<DisplayEvent["layout"]>,
): BreakMode | undefined {
const breakMode = layout.breakMode ?? event.breakMode
return isBreakMode(breakMode) ? breakMode : undefined
}
/**
* Measure the pixel width of text
*
* @param text - Text to measure
* @returns Width in pixels
*/
public measureText(text: string): number {
return this.measurer.measureText(text)
}
/**
* Check if text fits on a single line
*
* @param text - Text to check
* @returns true if text fits without wrapping
*/
public fitsOnSingleLine(text: string): boolean {
return this.measurer.measureText(text) <= this.profile.displayWidthPx
}
/**
* Get the display width in pixels
*/
public getDisplayWidth(): number {
return this.profile.displayWidthPx
}
/**
* Get the maximum number of lines
*/
public getMaxLines(): number {
return this.profile.maxLines
}
/**
* Get detailed text measurement
*/
public measureTextDetailed(text: string) {
return this.measurer.measureTextDetailed(text)
}
// ===========================================================================
// Break Mode Control
// ===========================================================================
/**
* Set the break mode for text wrapping
*
* @param breakMode - 'character' | 'word' | 'strict-word'
*/
public setBreakMode(breakMode: BreakMode): void {
if (this.options.breakMode === breakMode) return
this.options.breakMode = breakMode
this.updateProfile(this.profile) // Recreate wrapper with new break mode
if (this.options.debug) {
console.log(`[DisplayProcessor] Break mode set to: ${breakMode}`)
}
}
/**
* Get the current break mode
*/
public getBreakMode(): BreakMode {
return this.options.breakMode || "character"
}
}
export const displayProcessor = DisplayProcessor.getInstance()
export default displayProcessor
|