Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

2.4 KB
Raw Blame
import type { CanvasToolCapability } from '@workbench/canvas-engine/capabilities';
import type { Tool, ToolContext } from '@workbench/canvas-engine/tools/tool';
import type { ToolId } from '@workbench/canvas-engine/types';

export interface InteractionControllerOptions {
  readonly initialToolId?: ToolId;
  readonly getTool: (toolId: ToolId) => Tool | undefined;
  readonly getToolContext: () => ToolContext;
  /** Cancels an in-flight gesture through the still-active outgoing tool. */
  readonly cancelGesture?: () => void;
  readonly beforeSwitch?: (from: ToolId, to: ToolId, options?: { temporary?: boolean }) => void | boolean;
  readonly publishActiveTool: (toolId: ToolId) => void;
  readonly updateCursor: () => void;
  readonly invalidateOverlay: () => void;
  readonly stepBrushSize: (direction: 1 | -1) => void;
}

/** Owns active-tool transitions and the public tool command boundary. */
export class InteractionController {
  readonly tools: CanvasToolCapability;
  private activeToolId: ToolId;
  private disposed = false;

  constructor(private readonly options: InteractionControllerOptions) {
    this.activeToolId = options.initialToolId ?? 'view';
    this.tools = {
      setTool: (toolId, switchOptions) => this.setTool(toolId, switchOptions),
      stepBrushSize: (direction) => {
        if (!this.disposed) {
          options.stepBrushSize(direction);
        }
      },
    };
  }

  getActiveToolId(): ToolId {
    return this.activeToolId;
  }

  getActiveTool(): Tool | undefined {
    return this.options.getTool(this.activeToolId);
  }

  setTool(toolId: ToolId, switchOptions?: { temporary?: boolean }): void {
    if (this.disposed || toolId === this.activeToolId) {
      return;
    }
    if (!switchOptions?.temporary) {
      this.options.cancelGesture?.();
      // Ending a held temporary tool may already have restored the requested tool.
      if (toolId === this.activeToolId) {
        return;
      }
    }
    const previous = this.activeToolId;
    if (this.options.beforeSwitch?.(previous, toolId, switchOptions) === false) {
      return;
    }
    this.options.getTool(previous)?.onDeactivate?.(this.options.getToolContext(), switchOptions);
    this.activeToolId = toolId;
    this.options.getTool(toolId)?.onActivate?.(this.options.getToolContext(), switchOptions);
    this.options.publishActiveTool(toolId);
    this.options.updateCursor();
    this.options.invalidateOverlay();
  }

  dispose(): void {
    this.disposed = true;
  }
}