Skip to content

Keep event sheet scroll location when switching functions #7279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions newIDE/app/src/EventsFunctionsExtensionEditor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export default class EventsFunctionsExtensionEditor extends React.Component<
onAddEventsBasedObjectCb: null,
};
editor: ?EventsSheetInterface;
_editorScrollPositions = new Map<number, number>();
eventsFunctionList: ?EventsFunctionsListInterface;
_editorMosaic: ?EditorMosaicInterface;
_editorNavigator: ?EditorNavigatorInterface;
Expand Down Expand Up @@ -588,6 +589,14 @@ export default class EventsFunctionsExtensionEditor extends React.Component<
selectedEventsBasedBehavior: ?gdEventsBasedBehavior,
selectedEventsBasedObject: ?gdEventsBasedObject
) => {
const previousEditor = this.editor;
const previousFunction = this.state.selectedEventsFunction;
if (previousEditor && previousFunction) {
this._editorScrollPositions.set(
previousFunction.ptr,
previousEditor.getScrollPosition()
);
}
this._editBehavior(selectedEventsBasedBehavior);
this._editObject(selectedEventsBasedObject);
};
Expand Down Expand Up @@ -1427,6 +1436,9 @@ export default class EventsFunctionsExtensionEditor extends React.Component<
hotReloadPreviewButtonProps={
this.props.hotReloadPreviewButtonProps
}
initialScrollPosition={this._editorScrollPositions.get(
selectedEventsFunction.ptr
)}
/>
</Background>
) : selectedEventsBasedBehavior &&
Expand Down
42 changes: 40 additions & 2 deletions newIDE/app/src/EventsSheet/EventsTree/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ type EventsTreeProps = {|
onEventMoved: (previousRowIndex: number, nextRowIndex: number) => void,
onEndEditingEvent: (event: gdBaseEvent) => void,
onScroll?: () => void,
initialScrollPosition?: number,

screenType: ScreenType,
windowSize: WindowSizeType,
Expand Down Expand Up @@ -399,6 +400,7 @@ export default class ThemableEventsTree extends Component<
DropTarget = makeDropTarget<SortableTreeNode>(eventsSheetEventsDnDType);
temporaryUnfoldedNodes: Array<SortableTreeNode>;
_hoverTimerId: ?TimeoutID;
_isForcedToInitialScroll: boolean;

constructor(props: EventsTreeProps) {
super(props);
Expand All @@ -413,6 +415,7 @@ export default class ThemableEventsTree extends Component<
isScrolledTop: true,
isScrolledBottom: false,
};
this._isForcedToInitialScroll = !!this.props.initialScrollPosition;
}

componentDidMount() {
Expand Down Expand Up @@ -448,8 +451,12 @@ export default class ThemableEventsTree extends Component<
*/
onHeightsChanged(cb: ?() => void) {
this.forceUpdate(() => {
if (this._list && this._list.wrappedInstance.current) {
this._list.wrappedInstance.current.recomputeRowHeights();
const currentList = this._list;
if (currentList) {
const listWrapper = currentList.wrappedInstance.current;
if (listWrapper) {
listWrapper.recomputeRowHeights();
}
}
if (cb) cb();
});
Expand Down Expand Up @@ -484,6 +491,15 @@ export default class ThemableEventsTree extends Component<
}
}

getScrollPosition(): number {
const currentList = this._list;
if (!currentList) {
return 0;
}
const listWrapper = currentList.wrappedInstance.current;
return listWrapper ? listWrapper.Grid.state.scrollTop : 0;
}

/**
* Unfold events so that the given one is visible
*/
Expand Down Expand Up @@ -999,6 +1015,22 @@ export default class ThemableEventsTree extends Component<
const treeData = this.state.treeData ? [...this.state.treeData] : null;
const zoomLevel = this.props.fontSize || 14;

if (this._isForcedToInitialScroll) {
if (this.props.searchResults) {
this._isForcedToInitialScroll = false;
}
else {
const currentList = this._list;
if (currentList) {
const listWrapper = currentList.wrappedInstance.current;
if (listWrapper && listWrapper.Grid.state.scrollTop !== this.props.initialScrollPosition) {
// For some reason the List scroll is reset to 0 twice when the component is mounted.
listWrapper.scrollToPosition(this.props.initialScrollPosition);
}
}
}
}

return (
<div
style={{
Expand Down Expand Up @@ -1053,6 +1085,12 @@ export default class ThemableEventsTree extends Component<
reactVirtualizedListProps={{
ref: list => (this._list = list),
onScroll: event => {
if (
event.scrollTop !== 0 &&
event.scrollTop !== this.props.initialScrollPosition
) {
this._isForcedToInitialScroll = false;
}
this.props.onScroll && this.props.onScroll();
this.setState({
isScrolledTop: event.scrollTop === 0,
Expand Down
11 changes: 11 additions & 0 deletions newIDE/app/src/EventsSheet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ type Props = {|
unsavedChanges?: ?UnsavedChanges,
isActive: boolean,
hotReloadPreviewButtonProps: HotReloadPreviewButtonProps,
initialScrollPosition?: number,
|};

type ComponentProps = {|
Expand Down Expand Up @@ -351,6 +352,10 @@ export class EventsSheetComponentWithoutHandle extends React.Component<
if (this._eventsTree) this._eventsTree.forceEventsUpdate();
};

getScrollPosition = (): number => {
return this._eventsTree ? this._eventsTree.getScrollPosition() : 0;
};

updateToolbar() {
if (!this.props.setToolbar) return;

Expand Down Expand Up @@ -1921,6 +1926,7 @@ export class EventsSheetComponentWithoutHandle extends React.Component<
key={events.ptr}
indentScale={preferences.values.eventsSheetIndentScale}
onScroll={this._ensureFocused}
initialScrollPosition={this.props.initialScrollPosition}
events={events}
project={project}
scope={scope}
Expand Down Expand Up @@ -2184,6 +2190,7 @@ export class EventsSheetComponentWithoutHandle extends React.Component<
export type EventsSheetInterface = {|
updateToolbar: () => void,
onResourceExternallyChanged: ({| identifier: string |}) => void,
getScrollPosition: () => number,
|};

// EventsSheet is a wrapper so that the component can use multiple
Expand All @@ -2192,6 +2199,7 @@ const EventsSheet = (props, ref) => {
React.useImperativeHandle(ref, () => ({
updateToolbar,
onResourceExternallyChanged,
getScrollPosition,
}));

const component = React.useRef<?EventsSheetComponentWithoutHandle>(null);
Expand All @@ -2202,6 +2210,9 @@ const EventsSheet = (props, ref) => {
if (component.current)
component.current.onResourceExternallyChanged(resourceInfo);
};
const getScrollPosition = (): number => {
return component.current ? component.current.getScrollPosition() : 0;
};

const authenticatedUser = React.useContext(AuthenticatedUserContext);
const preferences = React.useContext(PreferencesContext);
Expand Down
Loading