Skip to content

A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.

4.7 KB
Raw Blame
/**
 * Lightweight Per-Tab Document Metadata Registry (Phase 6 Native Rust Engine).
 *
 * All tile grids, layer masks, selection masks, undo/redo history, and scratch disk paging
 * live exclusively in `crates/ink-engine` (`DocumentManager`). WebView2 holds ZERO tile grids
 * and ZERO full-document pixel buffers.
 */

import { HistoryItem } from '../types/editor';

export interface DocumentSessionMeta {
  readonly documentId: string;
  history: HistoryItem[];
  historyIndex: number;
  localRedoStack: string[];
  lastAccessedAt: number;
  isActive: boolean;
  isDisposed(): boolean;
  touch(): void;
  resetHistory(): Promise<void>;
  dispose(): Promise<void>;
}

export class NativeDocumentSession implements DocumentSessionMeta {
  public readonly documentId: string;
  public history: HistoryItem[] = [];
  public historyIndex: number = -1;
  public localRedoStack: string[] = [];
  public lastAccessedAt: number;
  public isActive: boolean = false;
  private disposed: boolean = false;

  constructor(documentId: string) {
    this.documentId = documentId;
    this.lastAccessedAt = Date.now();
  }

  public isDisposed(): boolean {
    return this.disposed;
  }

  public touch(): void {
    this.lastAccessedAt = Date.now();
  }

  public async resetHistory(): Promise<void> {
    this.history = [];
    this.historyIndex = -1;
    this.localRedoStack = [];
  }

  public async dispose(): Promise<void> {
    if (this.disposed) return;
    this.disposed = true;
    this.isActive = false;
    this.history = [];
    this.historyIndex = -1;
    this.localRedoStack = [];
  }
}

export class DocumentStore {
  private sessions: Map<string, NativeDocumentSession> = new Map();
  private activeDocumentId: string | null = null;
  private offloadInactiveUndoOnSwitch: boolean = true;

  public getOrCreateSession(documentId: string): NativeDocumentSession {
    let session = this.sessions.get(documentId);
    if (!session || session.isDisposed()) {
      session = new NativeDocumentSession(documentId);
      this.sessions.set(documentId, session);
    }
    session.touch();
    return session;
  }

  public getSession(documentId: string | null | undefined): NativeDocumentSession | null {
    if (!documentId) return null;
    const session = this.sessions.get(documentId);
    return session && !session.isDisposed() ? session : null;
  }

  public hasSession(documentId: string): boolean {
    const s = this.sessions.get(documentId);
    return Boolean(s && !s.isDisposed());
  }

  public getActiveDocumentId(): string | null {
    return this.activeDocumentId;
  }

  public getActiveSession(): NativeDocumentSession | null {
    return this.getSession(this.activeDocumentId);
  }

  public getAllSessions(): NativeDocumentSession[] {
    return Array.from(this.sessions.values()).filter((s) => !s.isDisposed());
  }

  public setActiveDocument(documentId: string | null): NativeDocumentSession | null {
    const prevId = this.activeDocumentId;
    if (prevId && prevId !== documentId) {
      const prev = this.sessions.get(prevId);
      if (prev && !prev.isDisposed()) {
        prev.isActive = false;
        prev.touch();
      }
    }
    this.activeDocumentId = documentId;
    if (!documentId) return null;
    const next = this.getOrCreateSession(documentId);
    next.isActive = true;
    next.touch();
    return next;
  }

  public setTileBudgetMb(_mb: number): void {
    // Managed natively in Rust DocumentManager via setNativeMemoryBudget
  }

  public setSharedUndoRamStepsBudget(_steps: number): void {
    // Managed natively in Rust DocumentManager via setNativeMemoryBudget
  }

  public setOffloadInactiveUndoOnSwitch(enabled: boolean): void {
    this.offloadInactiveUndoOnSwitch = Boolean(enabled);
  }

  public getOffloadInactiveUndoOnSwitch(): boolean {
    return this.offloadInactiveUndoOnSwitch;
  }

  public getActiveSessionIds(): string[] {
    return this.getAllSessions().map((s) => s.documentId);
  }

  public getUndoMemoryStats(): {
    ramSteps: number;
    diskSteps: number;
    compressedRamBytes: number;
  } {
    return { ramSteps: 0, diskSteps: 0, compressedRamBytes: 0 };
  }

  public async purgeTileMemory(): Promise<number> {
    return 0;
  }

  public async purgeUndoMemory(): Promise<number> {
    return 0;
  }

  public async closeSession(documentId: string): Promise<void> {
    const session = this.sessions.get(documentId);
    if (!session) return;
    this.sessions.delete(documentId);
    if (this.activeDocumentId === documentId) {
      this.activeDocumentId = null;
    }
    await session.dispose();
  }

  public async clearAllSessions(): Promise<void> {
    const all = Array.from(this.sessions.values());
    this.sessions.clear();
    this.activeDocumentId = null;
    await Promise.all(all.map((s) => s.dispose()));
  }
}

export const documentStore = new DocumentStore();