Repository: 4ian/GDevelop
File: newIDE/app/src/Utils/ApplyProjectSettings.js (JavaScript)

Code before:
// @flow
import { type ProjectSettings } from './ProjectSettingsReader';
import { type Preferences } from '../MainFrame/Preferences/PreferencesContext';

/**
 * Applies project-specific settings from a settings.ini file to the editor preferences.
 * This allows projects to override certain editor preferences when they are opened.
 *
 * @param settings - The parsed project settings from settings.ini
 * @param preferences - The preferences context with setters
 */
export const applyProjectSettings = (
  settings: ProjectSettings,
  preferences: Preferences
): void => {
  // At launch settings
  if (typeof settings.showCreateSectionByDefault === 'boolean') {
    preferences.setShowCreateSectionByDefault(
      settings.showCreateSectionByDefault
    );
  }
  if (typeof settings.autoOpenMostRecentProject === 'boolean') {
    preferences.setAutoOpenMostRecentProject(
      settings.autoOpenMostRecentProject
    );
  }

  // Preview settings
  if (typeof settings.autosaveOnPreview === 'boolean') {
    preferences.setAutosaveOnPreview(settings.autosaveOnPreview);
  }
  if (typeof settings.fetchPlayerTokenForPreviewAutomatically === 'boolean') {
    preferences.setFetchPlayerTokenForPreviewAutomatically(
      settings.fetchPlayerTokenForPreviewAutomatically
    );
  }
  if (typeof settings.openDiagnosticReportAutomatically === 'boolean') {
    preferences.setOpenDiagnosticReportAutomatically(
      settings.openDiagnosticReportAutomatically
    );
  }

Review comment (suggestion):
This is fairly verbose. I wonder if we could reduce everything to a few key concepts:

- ProjectSettings renamed to "ProjectSpecificPreferences". It can be a "Partial<Preferences>" (or in Flow, I think it's `$Shape<Preferences>`).
- An "allowlist" of names of preferences that can be overwritten.
- When a project is loaded, preferences are loaded from it and applied without needing to code the calls to the setters.

This would reduce this function to something that iterates on the allowlist. 

Code after:
// @flow
import { type ParsedProjectSettings } from './ProjectSettingsReader';
import {
  type Preferences,
  type ProjectSpecificPreferencesValues,
} from '../MainFrame/Preferences/PreferencesContext';

/** Allowlist of preference keys that can be overridden per-project. */
const allowedPreferenceKeys: $ReadOnlyArray<
  $Keys<ProjectSpecificPreferencesValues>
> = [
  'language',
  'autoDownloadUpdates',
  'themeName',
  'codeEditorThemeName',
  'autoDisplayChangelog',
  'lastLaunchedVersion',
  'eventsSheetShowObjectThumbnails',
  'autosaveOnPreview',
  'useGDJSDevelopmentWatcher',
  'eventsSheetUseAssignmentOperators',
  'eventsSheetIndentScale',
  'eventsSheetZoomLevel',
  'showEffectParameterNames',
  'autoOpenMostRecentProject',
  'hasProjectOpened',
  'newObjectDialogDefaultTab',
  'shareDialogDefaultTab',
  'isMenuBarHiddenInPreview',
  'isAlwaysOnTopInPreview',
  'backdropClickBehavior',
  'eventsSheetCancelInlineParameter',
  'showExperimentalExtensions',
  'showCreateSectionByDefault',
  'showInAppTutorialDeveloperMode',
  'showDeprecatedInstructionWarning',
  'openDiagnosticReportAutomatically',
  'use3DEditor',
  'showBasicProfilingCounters',
  'newProjectsDefaultFolder',
  'newProjectsDefaultStorageProviderName',