public static async globalHandles()

in packages/app/client/src/utils/eventHandlers.ts [67:174]


  public static async globalHandles(event: KeyboardEvent): Promise<any> {
    // Meta corresponds to 'Command' on Mac
    const ctrlOrCmdPressed = event.ctrlKey || event.metaKey;
    const shiftPressed = event.shiftKey;
    const key = event.key.toLowerCase();
    const keyCode = event.keyCode;
    const {
      Commands: {
        Electron: { ToggleDevTools },
        UI: { ShowBotCreationDialog, ShowOpenBotDialog },
        Notifications: { Add },
      },
    } = SharedConstants;
    let awaitable: Promise<any>;
    // Ctrl+O
    if (ctrlOrCmdPressed && key === 'o') {
      awaitable = EventHandlers.commandService.call(ShowOpenBotDialog);
    }
    // Ctrl+N
    if (ctrlOrCmdPressed && key === 'n') {
      awaitable = EventHandlers.commandService.call(ShowBotCreationDialog);
    }
    // Ctrl+0
    if (ctrlOrCmdPressed && key === '0') {
      remote.getCurrentWebContents().setZoomLevel(0);
    }
    // Ctrl+= or Ctrl+Shift+=
    if (ctrlOrCmdPressed && (key === '=' || key === '+')) {
      const webContents = remote.getCurrentWebContents();
      const zoomFactor = webContents.getZoomFactor();
      const newZoomFactor = zoomFactor + 0.1;
      if (newZoomFactor >= maxZoomFactor) {
        webContents.setZoomFactor(maxZoomFactor);
      } else {
        webContents.setZoomFactor(newZoomFactor);
      }
    }
    // Ctrl+- or Ctrl+Shift+-
    if (ctrlOrCmdPressed && (key === '-' || key === '_')) {
      const webContents = remote.getCurrentWebContents();
      const zoomFactor = webContents.getZoomFactor();
      const newZoomFactor = zoomFactor - 0.1;
      if (newZoomFactor <= minZoomFactor) {
        webContents.setZoomFactor(minZoomFactor);
      } else {
        webContents.setZoomFactor(newZoomFactor);
      }
    }
    // F11
    if (key === 'f11') {
      const currentWindow = remote.getCurrentWindow();
      currentWindow.setFullScreen(!currentWindow.isFullScreen());
      if (currentWindow.isFullScreen()) {
        await EventHandlers.commandService.remoteCall(Electron.ShowMessageBox, false, {
          message: 'Entering full screen.',
          title: 'Full screen mode',
        });
      } else {
        await EventHandlers.commandService.remoteCall(Electron.ShowMessageBox, false, {
          message: 'Exiting full screen.',
          title: 'Full screen mode',
        });
      }
    }
    // Ctrl+Shift+I
    if (ctrlOrCmdPressed && shiftPressed && key === 'i') {
      awaitable = EventHandlers.commandService.remoteCall(ToggleDevTools);
    }
    if (awaitable) {
      // Prevents the char from showing up if an input is focused
      event.preventDefault();
      event.stopPropagation();
      try {
        await awaitable;
      } catch (e) {
        await EventHandlers.commandService.call(Add, {
          message: '' + e,
          type: NotificationType.Error,
        } as Notification);
      }
    }

    if (isMac() || isLinux()) {
      const tabPressed: boolean = key === 'tab';

      if (tabPressed) {
        const lastDecendants = EventHandlers.getLastDecendants(document.querySelector('main'));
        // TODO: More generalized approach to finding first and last focusable elements. This seems brittle.
        let firstElement: HTMLElement;
        if (isLinux()) {
          firstElement = document.querySelector('[class*="app-menu"] button');
        } else {
          firstElement = document.querySelector('nav').firstElementChild as HTMLElement;
        }
        const lastElement = lastDecendants[lastDecendants.length - 1] as HTMLElement;
        const isFirstElement: boolean = document.activeElement === firstElement;
        const isLastElement: boolean = document.activeElement === lastElement;

        if (shiftPressed && isFirstElement) {
          lastElement.focus();
          event.preventDefault();
        } else if (!shiftPressed && isLastElement) {
          firstElement.focus();
          event.preventDefault();
        }
      }
    }
  }