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.

11.1 KB
Raw Blame
import type { GalleryItem, GalleryItemKey, GalleryItemRef } from '@features/gallery/core/items';
import type { GalleryNavigationDirection, GalleryNavigationEntry } from '@features/gallery/core/selection';

import { toGalleryItemKey, toGalleryItemRef } from '@features/gallery/core/items';
import { getGalleryNavigationCursor, getGalleryNavigationStep } from '@features/gallery/core/selection';
import { useEffect, useEffectEvent, useRef } from 'react';
import { useTranslation } from 'react-i18next';

import { useGalleryUi } from './GalleryUiContext';
import { useGalleryWidget } from './GalleryWidgetContext';

/**
 * How an arrow moves: `select` replaces the selection with the next tile, `extend` selects the range from the anchor to
 * it, and `focus` moves keyboard focus alone, so a toggle can then build a discontiguous selection.
 */
export type GalleryNavigationMode = 'extend' | 'focus' | 'select';

export interface GalleryUnloadedNavigationRequest {
  anchorKey: GalleryItemKey | null;
  mode: GalleryNavigationMode;
  onResolved: (itemKey: GalleryItemKey) => void;
}

const GALLERY_HOTKEYS = [
  ['gallery.selectAllOnPage', 'widgets.gallery.commands.selectAllOnPage', null, ['mod+a']],
  ['gallery.clearSelection', 'widgets.gallery.commands.clearSelection', null, []],
  ['gallery.galleryNavUp', 'widgets.gallery.commands.navigationUp', ['up', 'select'], ['arrowup']],
  ['gallery.galleryNavRight', 'widgets.gallery.commands.navigationRight', ['right', 'select'], ['arrowright']],
  ['gallery.galleryNavDown', 'widgets.gallery.commands.navigationDown', ['down', 'select'], ['arrowdown']],
  ['gallery.galleryNavLeft', 'widgets.gallery.commands.navigationLeft', ['left', 'select'], ['arrowleft']],
  ['gallery.galleryNavUpAlt', 'widgets.gallery.commands.navigationUp', ['up', 'select'], ['alt+arrowup']],
  ['gallery.galleryNavRightAlt', 'widgets.gallery.commands.navigationRight', ['right', 'select'], ['alt+arrowright']],
  ['gallery.galleryNavDownAlt', 'widgets.gallery.commands.navigationDown', ['down', 'select'], ['alt+arrowdown']],
  ['gallery.galleryNavLeftAlt', 'widgets.gallery.commands.navigationLeft', ['left', 'select'], ['alt+arrowleft']],
  ['gallery.extendSelectionUp', 'widgets.gallery.commands.extendSelectionUp', ['up', 'extend'], ['shift+arrowup']],
  [
    'gallery.extendSelectionRight',
    'widgets.gallery.commands.extendSelectionRight',
    ['right', 'extend'],
    ['shift+arrowright'],
  ],
  [
    'gallery.extendSelectionDown',
    'widgets.gallery.commands.extendSelectionDown',
    ['down', 'extend'],
    ['shift+arrowdown'],
  ],
  [
    'gallery.extendSelectionLeft',
    'widgets.gallery.commands.extendSelectionLeft',
    ['left', 'extend'],
    ['shift+arrowleft'],
  ],
  ['gallery.moveFocusUp', 'widgets.gallery.commands.moveFocusUp', ['up', 'focus'], ['mod+arrowup']],
  ['gallery.moveFocusRight', 'widgets.gallery.commands.moveFocusRight', ['right', 'focus'], ['mod+arrowright']],
  ['gallery.moveFocusDown', 'widgets.gallery.commands.moveFocusDown', ['down', 'focus'], ['mod+arrowdown']],
  ['gallery.moveFocusLeft', 'widgets.gallery.commands.moveFocusLeft', ['left', 'focus'], ['mod+arrowleft']],
  ['gallery.toggleFocusedInSelection', 'widgets.gallery.commands.toggleFocusedInSelection', null, []],
  ['gallery.deleteSelection', 'widgets.gallery.commands.deleteSelection', null, ['delete', 'backspace']],
  ['gallery.starImage', 'widgets.gallery.commands.toggleStarImage', null, ['.']],
  ['gallery.toggleStarredOnly', 'widgets.gallery.commands.toggleStarredOnly', null, []],
] as const satisfies readonly (readonly [
  string,
  string,
  readonly [GalleryNavigationDirection, GalleryNavigationMode] | null,
  readonly string[],
])[];

/** Read current handler state without re-registering commands on selection changes (palette and hotkey churn). */
export const useGalleryGridHotkeys = ({
  actionSelectionRefs,
  columnCount,
  getCursorCandidates,
  getDialogReturnFocus,
  getFirstVisibleTileKey,
  getFocusedItem,
  moveToEntry,
  navigationSections,
  navigateToUnloadedSlot,
  getSelectionPage,
  onNavigationStart,
  selectItemRange,
  shouldStar,
  toggleItem,
}: {
  actionSelectionRefs: GalleryItemRef[];
  columnCount: number;
  /**
   * Where arrows step from, by priority: the focused tile, the followed session, the selection; the first shown wins.
   */
  getCursorCandidates: () => (string | null)[];
  /** Where focus returns from a dialog a command opens; undefined leaves that to the dialog. */
  getDialogReturnFocus: () => (() => HTMLElement | null) | undefined;
  /** The first thumbnail in view: where the arrows land when no cursor is on screen. */
  getFirstVisibleTileKey: () => string | null;
  getFocusedItem: () => GalleryItem | null;
  /**
   * Applies `select` (none for focus-only moves), brings the tile into view, and focuses it while the grid has focus.
   */
  moveToEntry: (entry: GalleryNavigationEntry, select: (() => void) | null) => void;
  /** The arrow-key sections in visual order: the starred strip, in progress, the listing. */
  navigationSections: readonly (readonly GalleryNavigationEntry[])[];
  /** Loads a sparse absolute slot; it becomes selectable after its page hydrates. */
  navigateToUnloadedSlot?: (absoluteIndex: number, request: GalleryUnloadedNavigationRequest) => void;
  /** Supersedes any unfinished sparse navigation before handling this newer arrow command. */
  onNavigationStart?: () => void;
  /** The sparse page stamp for a loaded listing item. */
  getSelectionPage?: (item: GalleryItem) => number | undefined;
  selectItemRange: (
    item: GalleryItem,
    options?: { anchorKey?: GalleryItemKey | null; selectionPage?: number }
  ) => Promise<void>;
  /** Whether the star command stars the action selection: some of it is not starred. */
  shouldStar: boolean;
  toggleItem: (item: GalleryItem) => void;
}) => {
  const { t } = useTranslation();
  const { actions, gallery, itemActions, loadedItems, runtime } = useGalleryWidget();
  const { followProgressSession, gallery: galleryCommands } = useGalleryUi();
  // A run of Shift+arrows keeps the anchor it started from; the range it last reached says whether it is still running.
  const keyboardRangeRef = useRef<{ anchorKey: GalleryItemKey | null; reachedKey: GalleryItemKey } | null>(null);

  // A run continues from its own anchor. A new one starts from the persisted primary, which outlives its tile when a
  // sparse page leaves the viewport; a stored null would let the next step re-anchor on the moved primary.
  const getRangeAnchorKey = (cursorKey: string | null): GalleryItemKey | null => {
    const range = keyboardRangeRef.current;

    return range && range.reachedKey === cursorKey ? range.anchorKey : gallery.primarySelectedItemKey;
  };

  const navigate = useEffectEvent((direction: GalleryNavigationDirection, mode: GalleryNavigationMode) => {
    onNavigationStart?.();
    const cursorKey = getGalleryNavigationCursor(navigationSections, getCursorCandidates());
    // Ranges and focus moves step between items only: in-progress sessions are followed, never selected. They stay in
    // the sections, since one can be where the step starts.
    const firstVisibleKey = cursorKey === null ? getFirstVisibleTileKey() : null;
    // With no cursor on screen the arrows start where the user is looking, not at the top of the sequence.
    const entry =
      (firstVisibleKey === null
        ? null
        : navigationSections
            .flat()
            .find((candidate) => candidate.kind === 'item' && toGalleryItemKey(candidate.item) === firstVisibleKey)) ??
      getGalleryNavigationStep(navigationSections, [cursorKey], direction, columnCount, {
        itemsOnly: mode !== 'select',
      });

    if (!entry) {
      return;
    }

    const unloadedSlotMatch = entry.kind === 'slot' ? /^gallery-unloaded-slot:(\d+)$/.exec(entry.id) : null;

    if (unloadedSlotMatch) {
      const anchorKey = mode === 'extend' ? getRangeAnchorKey(cursorKey) : null;

      navigateToUnloadedSlot?.(Number(unloadedSlotMatch[1]), {
        anchorKey,
        mode,
        onResolved: (itemKey) => {
          keyboardRangeRef.current = mode === 'extend' ? { anchorKey, reachedKey: itemKey } : null;
        },
      });
      return;
    }

    if (entry.kind === 'slot') {
      return;
    }

    if (entry.kind === 'session') {
      moveToEntry(entry, () => followProgressSession(entry.id, { revealPreview: false }));
      return;
    }

    if (mode === 'select') {
      const selectionPage = getSelectionPage?.(entry.item);

      moveToEntry(entry, () =>
        selectionPage === undefined ? actions.selectItem(entry.item) : actions.selectItem(entry.item, selectionPage)
      );
      return;
    }

    if (mode === 'focus') {
      moveToEntry(entry, null);
      return;
    }

    const anchorKey = getRangeAnchorKey(cursorKey);

    keyboardRangeRef.current = { anchorKey, reachedKey: toGalleryItemKey(entry.item) };
    moveToEntry(
      entry,
      () => void selectItemRange(entry.item, { anchorKey, selectionPage: getSelectionPage?.(entry.item) })
    );
  });

  const executeGalleryHotkey = useEffectEvent((commandId: string) => {
    if (commandId === 'gallery.selectAllOnPage') {
      const primaryItem = gallery.items[0];
      const selectionPage = primaryItem ? getSelectionPage?.(primaryItem) : undefined;

      if (primaryItem) {
        if (selectionPage === undefined) {
          actions.selectItemRange(gallery.items.map(toGalleryItemRef), primaryItem);
        } else {
          actions.selectItemRange(gallery.items.map(toGalleryItemRef), primaryItem, selectionPage);
        }
      }
      return;
    }

    if (commandId === 'gallery.toggleFocusedInSelection') {
      const item = getFocusedItem();

      if (item) {
        toggleItem(item);
      }
      return;
    }

    if (commandId === 'gallery.clearSelection') {
      galleryCommands.clearSelection();
      return;
    }

    if (commandId === 'gallery.deleteSelection' && actionSelectionRefs.length > 0) {
      // A member no loaded page holds is off screen: the deletion then always asks and shows what it takes.
      const loadedKeys = new Set(loadedItems.map(toGalleryItemKey));

      void itemActions.deleteItems(actionSelectionRefs, {
        returnFocus: getDialogReturnFocus(),
        targetsOffScreen: actionSelectionRefs.some((ref) => !loadedKeys.has(toGalleryItemKey(ref))),
      });
      return;
    }

    if (commandId === 'gallery.starImage' && actionSelectionRefs.length > 0) {
      void itemActions.setItemsStarred(actionSelectionRefs, shouldStar);
      return;
    }

    if (commandId === 'gallery.toggleStarredOnly' && gallery.semanticImageQuery === null) {
      actions.setStarredOnly(!gallery.starredOnly);
    }
  });

  useEffect(() => {
    const disposers = GALLERY_HOTKEYS.flatMap(([id, titleKey, navigation, defaultKeys]) => [
      runtime.commands.register({
        handler: () => (navigation ? navigate(navigation[0], navigation[1]) : executeGalleryHotkey(id)),
        id,
        title: t(titleKey),
      }),
      runtime.hotkeys.register({
        commandId: id,
        defaultKeys: [...defaultKeys],
        id,
        title: t(titleKey),
      }),
    ]);

    return () => {
      disposers.forEach((dispose) => dispose());
    };
  }, [runtime.commands, runtime.hotkeys, t]);
};