Skip to content

Commit 8773f63

Browse files
author
David Kutugata
authored
check the position of the scroll in the interactive (#7198) (#7236)
* check the position of the scroll in the interactive window, if it is at the bottom, the screen stays at the bottom, and if not, the screen stays in place. Throttling of the scroll was increased to help with consistency on rapidly appearing plots. * handle the scroll in the MainPanel, tested to work with: - regular code, -dataframes, -big and regular plots. * Added some comments.
1 parent 557fbaf commit 8773f63

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

news/2 Fixes/6580.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Changed the way scrolling is treated. Now we only check for the position of the scroll, the size of the cell won't matter.
2+
Still the interactive window will snap to the bottom if you already are at the bottom, and will stay in place if you are not. Like a chat window.
3+
Tested to work with:
4+
- regular code
5+
- dataframes
6+
- big and regular plots

src/datascience-ui/history-react/MainPanel.tsx

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export class MainPanel extends React.Component<IMainPanelProps, IMainPanelState>
4141
private stackLimit = 10;
4242
private updateCount = 0;
4343
private renderCount = 0;
44+
private internalScrollCount = 0;
4445
private editCellRef: Cell | null = null;
4546
private mainPanel: HTMLDivElement | null = null;
4647
private variableExplorerRef: React.RefObject<VariableExplorer>;
@@ -68,7 +69,8 @@ export class MainPanel extends React.Component<IMainPanelProps, IMainPanelState>
6869
editorOptions: this.computeEditorOptions(),
6970
currentExecutionCount: 0,
7071
debugging: false,
71-
enableGather: getSettings && getSettings().enableGather
72+
enableGather: getSettings && getSettings().enableGather,
73+
isAtBottom: true
7274
};
7375

7476
// Add test state if necessary
@@ -151,7 +153,7 @@ export class MainPanel extends React.Component<IMainPanelProps, IMainPanelState>
151153
<section id='main-panel-variable' aria-label={getLocString('DataScience.collapseVariableExplorerLabel', 'Variables')}>
152154
{this.renderVariablePanel(baseTheme)}
153155
</section>
154-
<main id='main-panel-content'>
156+
<main id='main-panel-content' onScroll={this.handleScroll}>
155157
{this.renderContentPanel(baseTheme)}
156158
</main>
157159
<section id='main-panel-footer' aria-label={getLocString('DataScience.editSection', 'Input new cells here')}>
@@ -161,6 +163,26 @@ export class MainPanel extends React.Component<IMainPanelProps, IMainPanelState>
161163
);
162164
}
163165

166+
// This handles the scrolling. Its called from the props of contentPanel.
167+
// We only scroll when the state indicates we are at the bottom of the interactive window,
168+
// otherwise it sometimes scrolls when the user wasn't at the bottom.
169+
public scrollDiv = (div: HTMLDivElement) => {
170+
if (this.state.isAtBottom) {
171+
this.internalScrollCount += 1;
172+
div.scrollIntoView({ behavior: 'auto', block: 'start', inline: 'nearest' });
173+
}
174+
}
175+
176+
public handleScroll = (e: React.UIEvent<HTMLDivElement>) => {
177+
if (this.internalScrollCount > 0) {
178+
this.internalScrollCount -= 1;
179+
} else {
180+
this.setState({
181+
isAtBottom: e.currentTarget.scrollHeight - e.currentTarget.scrollTop === e.currentTarget.clientHeight
182+
});
183+
}
184+
}
185+
164186
// tslint:disable-next-line:no-any cyclomatic-complexity max-func-body-length
165187
public handleMessage = (msg: string, payload?: any) => {
166188
switch (msg) {
@@ -445,7 +467,8 @@ export class MainPanel extends React.Component<IMainPanelProps, IMainPanelState>
445467
openLink: this.openLink,
446468
expandImage: this.showPlot,
447469
gatherCode: this.gatherCode,
448-
enableGather: this.state.enableGather
470+
enableGather: this.state.enableGather,
471+
scrollToBottom: this.scrollDiv
449472
};
450473
}
451474
private getToolbarProps = (baseTheme: string): IToolbarPanelProps => {

src/datascience-ui/history-react/contentPanel.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export interface IContentPanelProps {
3333
onCodeCreated(code: string, file: string, cellId: string, modelId: string): void;
3434
openLink(uri: monacoEditor.Uri): void;
3535
expandImage(imageHtml: string): void;
36+
scrollToBottom(div: HTMLDivElement): void;
3637
}
3738

3839
export class ContentPanel extends React.Component<IContentPanelProps> {
@@ -122,13 +123,8 @@ export class ContentPanel extends React.Component<IContentPanelProps> {
122123
private scrollIntoView() {
123124
// Force auto here as smooth scrolling can be canceled by updates to the window
124125
// from elsewhere (and keeping track of these would make this hard to maintain)
125-
if (this.bottomRef.current
126-
// This condition prevents window to scroll down when user is not near the bottom.
127-
// We increase window.innerHeight by 20% to still snap to the bottom when creating
128-
// multiple default plots. User needs to do about 3 'scrolls' up from the bottom
129-
// to prevent the window to snap to the bottom.
130-
&& this.bottomRef.current.getBoundingClientRect().bottom <= window.innerHeight * 1.2) {
131-
this.bottomRef.current.scrollIntoView({ behavior: 'auto', block: 'start', inline: 'nearest' });
126+
if (this.bottomRef.current) {
127+
this.props.scrollToBottom(this.bottomRef.current);
132128
}
133129
}
134130

src/datascience-ui/history-react/mainPanelState.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export interface IMainPanelState {
3232
currentExecutionCount: number;
3333
debugging: boolean;
3434
enableGather?: boolean;
35+
isAtBottom: boolean;
3536
}
3637

3738
// tslint:disable-next-line: no-multiline-string
@@ -63,7 +64,8 @@ export function generateTestState(inputBlockToggled: (id: string) => void, fileP
6364
editorOptions: {},
6465
currentExecutionCount: 0,
6566
debugging: false,
66-
enableGather: false
67+
enableGather: false,
68+
isAtBottom: true
6769
};
6870
}
6971

0 commit comments

Comments
 (0)