Skip to content

Commit

Permalink
Forbid life-cycle functions to be renamed (#6269)
Browse files Browse the repository at this point in the history
  • Loading branch information
D8H authored Jan 25, 2024
1 parent 10314a1 commit 9b447e0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
5 changes: 0 additions & 5 deletions newIDE/app/src/EventsFunctionsExtensionEditor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1289,11 +1289,6 @@ export default class EventsFunctionsExtensionEditor extends React.Component<
)
}
onDeleteEventsFunction={this._onDeleteEventsFunction}
canRename={(eventsFunction: gdEventsFunction) => {
return !gd.MetadataDeclarationHelper.isExtensionLifecycleEventsFunction(
eventsFunction.getName()
);
}}
onRenameEventsFunction={this._makeRenameFreeEventsFunction(
i18n
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export type EventsFunctionCallbacks = {|
eventsFunction: gdEventsFunction,
cb: (boolean) => void
) => void,
canRename: (eventsFunction: gdEventsFunction) => boolean,
onRenameEventsFunction: (
eventsFunction: gdEventsFunction,
newName: string,
Expand Down Expand Up @@ -79,6 +78,22 @@ export const getEventsFunctionTreeViewItemId = (
return `function-${eventFunction.ptr}`;
};

export const canFunctionBeRenamed = (
eventsFunction: gdEventsFunction,
containerType: 'extension' | 'behavior' | 'object'
) => {
const name = eventsFunction.getName();
if (containerType === 'behavior') {
return !gd.MetadataDeclarationHelper.isBehaviorLifecycleEventsFunction(
name
);
}
if (containerType === 'object') {
return !gd.MetadataDeclarationHelper.isObjectLifecycleEventsFunction(name);
}
return !gd.MetadataDeclarationHelper.isExtensionLifecycleEventsFunction(name);
};

export class EventsFunctionTreeViewItemContent implements TreeViewItemContent {
eventsFunction: gdEventsFunction;
props: EventsFunctionProps;
Expand Down Expand Up @@ -199,15 +214,28 @@ export class EventsFunctionTreeViewItemContent implements TreeViewItemContent {
}

edit(): void {
this.props.editName(this.getId());
if (this.canBeRenamed()) {
this.props.editName(this.getId());
}
}

canBeRenamed() {
return canFunctionBeRenamed(
this.eventsFunction,
this.getEventsBasedBehavior()
? 'behavior'
: this.getEventsBasedObject()
? 'object'
: 'extension'
);
}

buildMenuTemplate(i18n: I18nType, index: number) {
return [
{
label: i18n._(t`Rename`),
click: () => this.edit(),
enabled: this.props.canRename(this.eventsFunction),
enabled: this.canBeRenamed(),
accelerator: 'F2',
},
{
Expand Down
16 changes: 11 additions & 5 deletions newIDE/app/src/EventsFunctionsList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import ErrorBoundary from '../UI/ErrorBoundary';
import {
EventsFunctionTreeViewItemContent,
getEventsFunctionTreeViewItemId,
canFunctionBeRenamed,
type EventFunctionCommonProps,
type EventsFunctionCallbacks,
type EventsFunctionCreationParameters,
Expand Down Expand Up @@ -405,7 +406,6 @@ const EventsFunctionsList = React.forwardRef<
unsavedChanges,
onSelectEventsFunction,
onDeleteEventsFunction,
canRename,
onRenameEventsFunction,
onAddEventsFunction,
onEventsFunctionAdded,
Expand Down Expand Up @@ -567,14 +567,22 @@ const EventsFunctionsList = React.forwardRef<
eventsBasedBehavior,
eventsBasedObject
);
if (canRename(eventsFunction)) {
if (
canFunctionBeRenamed(
eventsFunction,
eventsBasedBehavior
? 'behavior'
: eventsBasedObject
? 'object'
: 'extension'
)
) {
editName(functionItemId);
}
}
);
},
[
canRename,
editName,
eventsFunctionsExtension,
forceUpdate,
Expand Down Expand Up @@ -750,7 +758,6 @@ const EventsFunctionsList = React.forwardRef<
scrollToItem,
onSelectEventsFunction,
onDeleteEventsFunction,
canRename,
onRenameEventsFunction,
onAddEventsFunction,
onEventsFunctionAdded,
Expand All @@ -772,7 +779,6 @@ const EventsFunctionsList = React.forwardRef<
scrollToItem,
onSelectEventsFunction,
onDeleteEventsFunction,
canRename,
onRenameEventsFunction,
onAddEventsFunction,
onEventsFunctionAdded,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export const Default = () => (
eventsFunction.setName(newName);
cb(true);
}}
canRename={() => true}
forceUpdateEditor={action('force editor update')}
/>
</FixedHeightFlexContainer>
Expand Down

0 comments on commit 9b447e0

Please sign in to comment.