Set a preference for the importation of resources for projects stored on the computer (#6318)

This commit is contained in:
Aurélien Vivet
2024-02-09 09:33:58 +01:00
committed by GitHub
parent 29747690c2
commit 0982424d0d
6 changed files with 78 additions and 7 deletions

View File

@@ -1,7 +1,10 @@
// @flow
import { Trans } from '@lingui/macro';
import * as React from 'react';
import type { ResourceKind } from '../../ResourcesList/ResourceSource';
import type {
ResourceKind,
ResourceImportationBehavior,
} from '../../ResourcesList/ResourceSource';
import { type EditorMosaicNode } from '../../UI/EditorMosaic';
import { type FileMetadataAndStorageProviderName } from '../../ProjectsStorage';
import { type ShortcutMap } from '../../KeyboardShortcuts/DefaultShortcuts';
@@ -204,6 +207,7 @@ export type PreferencesValues = {|
isMenuBarHiddenInPreview: boolean,
isAlwaysOnTopInPreview: boolean,
backdropClickBehavior: 'nothing' | 'apply' | 'cancel',
resourcesImporationBehavior: ResourceImportationBehavior,
eventsSheetCancelInlineParameter: 'cancel' | 'apply',
showCommunityExtensions: boolean,
showGetStartedSectionByDefault: boolean,
@@ -276,6 +280,7 @@ export type Preferences = {|
getIsMenuBarHiddenInPreview: () => boolean,
setIsMenuBarHiddenInPreview: (enabled: boolean) => void,
setBackdropClickBehavior: (value: string) => void,
setResourcesImporationBehavior: (value: string) => void,
getIsAlwaysOnTopInPreview: () => boolean,
setIsAlwaysOnTopInPreview: (enabled: boolean) => void,
setEventsSheetCancelInlineParameter: (value: string) => void,
@@ -345,6 +350,7 @@ export const initialPreferences = {
isMenuBarHiddenInPreview: true,
isAlwaysOnTopInPreview: false,
backdropClickBehavior: 'nothing',
resourcesImporationBehavior: 'ask',
eventsSheetCancelInlineParameter: 'apply',
showCommunityExtensions: false,
showGetStartedSectionByDefault: true,
@@ -401,6 +407,7 @@ export const initialPreferences = {
getIsMenuBarHiddenInPreview: () => true,
setIsMenuBarHiddenInPreview: () => {},
setBackdropClickBehavior: () => {},
setResourcesImporationBehavior: () => {},
getIsAlwaysOnTopInPreview: () => true,
setIsAlwaysOnTopInPreview: () => {},
setEventsSheetCancelInlineParameter: () => {},

View File

@@ -62,6 +62,7 @@ const PreferencesDialog = ({ i18n, onClose }: Props) => {
setShortcutForCommand,
setIsMenuBarHiddenInPreview,
setBackdropClickBehavior,
setResourcesImporationBehavior,
setIsAlwaysOnTopInPreview,
setEventsSheetCancelInlineParameter,
setShowCommunityExtensions,
@@ -226,6 +227,30 @@ const PreferencesDialog = ({ i18n, onClose }: Props) => {
<SelectOption value="apply" label={t`Apply changes`} />
<SelectOption value="nothing" label={t`Do nothing`} />
</SelectField>
{!!electron && (
<SelectField
floatingLabelText={
<Trans>
Importing resources outside from the project folder
</Trans>
}
value={values.resourcesImporationBehavior}
onChange={(e, i, value: string) =>
setResourcesImporationBehavior(value)
}
fullWidth
>
<SelectOption
value="import"
label={t`Copy them into the project folder`}
/>
<SelectOption
value="relative"
label={t`Keep their original location`}
/>
<SelectOption value="ask" label={t`Ask every time`} />
</SelectField>
)}
<Text size="block-title">
<Trans>Updates</Trans>
</Text>

View File

@@ -12,7 +12,10 @@ import {
type PreferencesValues,
type EditorMosaicName,
} from './PreferencesContext';
import type { ResourceKind } from '../../ResourcesList/ResourceSource';
import type {
ResourceKind,
ResourceImportationBehavior,
} from '../../ResourcesList/ResourceSource';
import { type EditorMosaicNode } from '../../UI/EditorMosaic';
import { type FileMetadataAndStorageProviderName } from '../../ProjectsStorage';
import defaultShortcuts from '../../KeyboardShortcuts/DefaultShortcuts';
@@ -135,6 +138,9 @@ export default class PreferencesProvider extends React.Component<Props, State> {
getIsMenuBarHiddenInPreview: this._getIsMenuBarHiddenInPreview.bind(this),
setIsMenuBarHiddenInPreview: this._setIsMenuBarHiddenInPreview.bind(this),
setBackdropClickBehavior: this._setBackdropClickBehavior.bind(this),
setResourcesImporationBehavior: this._setResourcesImporationBehavior.bind(
this
),
getIsAlwaysOnTopInPreview: this._getIsAlwaysOnTopInPreview.bind(this),
setIsAlwaysOnTopInPreview: this._setIsAlwaysOnTopInPreview.bind(this),
setEventsSheetCancelInlineParameter: this._setEventsSheetCancelInlineParameter.bind(
@@ -812,6 +818,17 @@ export default class PreferencesProvider extends React.Component<Props, State> {
);
}
_setResourcesImporationBehavior(
resourcesImporationBehavior: ResourceImportationBehavior
) {
this.setState(
state => ({
values: { ...state.values, resourcesImporationBehavior },
}),
() => this._persistValuesToLocalStorage(this.state)
);
}
_getIsAlwaysOnTopInPreview() {
return this.state.values.isAlwaysOnTopInPreview;
}

View File

@@ -65,6 +65,7 @@ const localResourceSources: Array<ResourceSource> = [
setLastUsedPath,
project,
options,
resourcesImporationBehavior,
}: ChooseResourceProps) => {
if (!dialog)
throw new Error('Electron dialog not supported in this environment.');
@@ -142,11 +143,19 @@ const localResourceSources: Array<ResourceSource> = [
const newToOldFilePaths = new Map<string, string>();
let filesWithMappedResources = new Map<string, MappedResources>();
if (hasFilesOutsideProjectFolder) {
const answer = Window.showConfirmDialog(
i18n._(
t`This/these file(s) are outside the project folder. Would you like to make a copy of them in your project folder first (recommended)?`
)
);
let answer: boolean;
if (resourcesImporationBehavior === 'relative') {
answer = false;
} else if (resourcesImporationBehavior === 'import') {
answer = true;
} else {
answer = Window.showConfirmDialog(
i18n._(
t`This/these file(s) are outside the project folder. Would you like to make a copy of them in your project folder first (recommended)?`
)
);
}
if (answer) {
filePaths = await copyAllToProjectFolder(
@@ -225,6 +234,8 @@ const localResourceSources: Array<ResourceSource> = [
getLastUsedPath: props.getLastUsedPath,
setLastUsedPath: props.setLastUsedPath,
options: props.options,
resourcesImporationBehavior:
props.resourcesImporationBehavior,
});
props.onChooseResources(resources);

View File

@@ -102,6 +102,8 @@ export const NewResourceDialog = ({
getStorageProvider,
getLastUsedPath: preferences.getLastUsedPath,
setLastUsedPath: preferences.setLastUsedPath,
resourcesImporationBehavior:
preferences.values.resourcesImporationBehavior,
});
onChooseResources(resources);
} catch (error) {
@@ -171,6 +173,8 @@ export const NewResourceDialog = ({
getLastUsedPath: preferences.getLastUsedPath,
setLastUsedPath: preferences.setLastUsedPath,
onChooseResources,
resourcesImporationBehavior:
preferences.values.resourcesImporationBehavior,
});
})}
{currentTab === 'import' ? (
@@ -188,6 +192,8 @@ export const NewResourceDialog = ({
getLastUsedPath: preferences.getLastUsedPath,
setLastUsedPath: preferences.setLastUsedPath,
onChooseResources,
resourcesImporationBehavior:
preferences.values.resourcesImporationBehavior,
// Ask the component to try to automatically open the dialog to import file(s),
// but only if tabs were not changed, meaning the user navigated out of it already.
@@ -210,6 +216,8 @@ export const NewResourceDialog = ({
getLastUsedPath: preferences.getLastUsedPath,
setLastUsedPath: preferences.setLastUsedPath,
onChooseResources,
resourcesImporationBehavior:
preferences.values.resourcesImporationBehavior,
})}
</React.Fragment>
))}

View File

@@ -116,6 +116,8 @@ export type ChooseResourceOptions = {|
resourceKind: ResourceKind,
|};
export type ResourceImportationBehavior = 'import' | 'relative' | 'ask';
export type ChooseResourceProps = {|
i18n: I18nType,
project: gdProject,
@@ -129,6 +131,7 @@ export type ChooseResourceProps = {|
) => void,
options: ChooseResourceOptions,
automaticallyOpenIfPossible?: boolean,
resourcesImporationBehavior: ResourceImportationBehavior,
|};
export type ResourceSourceComponentProps = {|