Repository: jiahaog/nativefier
File: app/src/main.ts (TypeScript)

Code before:
  });

  app.on('ready', () => {
    mainWindow = createMainWindow(appArgs, app.quit.bind(this), setDockBadge);
    createTrayIcon(appArgs, mainWindow);

    // Register global shortcuts
    if (appArgs.globalShortcuts) {
      appArgs.globalShortcuts.forEach((shortcut) => {
        globalShortcut.register(shortcut.key, () => {
          shortcut.inputEvents.forEach((inputEvent) => {
            mainWindow.webContents.sendInputEvent(inputEvent);
          });
        });
      });

      if (isOSX() && appArgs.accessibilityPrompt) {
        const mediaKeys = [
          'MediaPlayPause',
          'MediaNextTrack',
          'MediaPreviousTrack',
          'MediaStop',
        ];
        const globalShortcutsKeys = appArgs.globalShortcuts.map((g) => g.key);
        const mediaKeyWasSet =
          globalShortcutsKeys.filter((g) => mediaKeys.includes(g)).length > 0;
        if (
          mediaKeyWasSet &&
          !systemPreferences.isTrustedAccessibilityClient(false)
        ) {
          // Since we're tryig to set global keyboard shortcuts for media keys, we need to prompt
          // the user for permission on Mac.
          // For reference:
          // https://www.electronjs.org/docs/api/global-shortcut?q=MediaPlayPause#globalshortcutregisteraccelerator-callback
          //
          const accessibilityPromptResult = dialog.showMessageBoxSync(null, {
            type: 'question',
            message: 'Accessibility Permissions Needed',
            buttons: ['Yes', 'No', 'Never Ask Me Again'],
            defaultId: 0,
            detail:
              `${appArgs.name} would like to use one or more of your keyboard's media keys (start, stop, next track, or previous track) to control it.\n\n` +
              `Would you like Mac OS to ask for your permission to do so?\n\n` +
              `If so, you will need to restart ${appArgs.name} after granting permissions for these keyboard shortcuts to begin working.`,
          });
          switch (accessibilityPromptResult) {
            // User clicked Yes, prompt for accessibility
            case 0:
              systemPreferences.isTrustedAccessibilityClient(true);
              break;
            // User cliecked Never Ask Me Again, save that info

Review comment (suggestion):
Nit: no need to look at the whole array, let's stop at the first match:

```suggestion
        const mediaKeyWasSet =
          globalShortcutsKeys.find((g) => mediaKeys.includes(g));
```

Code after:
      mainWindow.focus();
    }
  });

  app.on('ready', () => {
    mainWindow = createMainWindow(appArgs, app.quit.bind(this), setDockBadge);
    createTrayIcon(appArgs, mainWindow);

    // Register global shortcuts
    if (appArgs.globalShortcuts) {
      appArgs.globalShortcuts.forEach((shortcut) => {
        globalShortcut.register(shortcut.key, () => {
          shortcut.inputEvents.forEach((inputEvent) => {
            mainWindow.webContents.sendInputEvent(inputEvent);
          });
        });
      });

      if (isOSX() && appArgs.accessibilityPrompt) {
        const mediaKeys = [
          'MediaPlayPause',
          'MediaNextTrack',
          'MediaPreviousTrack',
          'MediaStop',
        ];
        const globalShortcutsKeys = appArgs.globalShortcuts.map((g) => g.key);
        const mediaKeyWasSet = globalShortcutsKeys.find((g) =>
          mediaKeys.includes(g),
        );
        if (
          mediaKeyWasSet &&
          !systemPreferences.isTrustedAccessibilityClient(false)
        ) {
          // Since we're trying to set global keyboard shortcuts for media keys, we need to prompt
          // the user for permission on Mac.
          // For reference:
          // https://www.electronjs.org/docs/api/global-shortcut?q=MediaPlayPause#globalshortcutregisteraccelerator-callback
          const accessibilityPromptResult = dialog.showMessageBoxSync(null, {
            type: 'question',
            message: 'Accessibility Permissions Needed',
            buttons: ['Yes', 'No', 'No and never ask again'],
            defaultId: 0,
            detail:
              `${appArgs.name} would like to use one or more of your keyboard's media keys (start, stop, next track, or previous track) to control it.\n\n` +
              `Would you like Mac OS to ask for your permission to do so?\n\n` +
              `If so, you will need to restart ${appArgs.name} after granting permissions for these keyboard shortcuts to begin working.`,
          });
          switch (accessibilityPromptResult) {
            // User clicked Yes, prompt for accessibility
            case 0:
              systemPreferences.isTrustedAccessibilityClient(true);