Merge changes I04602bec,I8a25c006,Id6f8114f,I5a1d639e,Ieca03602

* changes:
  Make reinitializing cursor explicit
  Move temp scrolling overwrite into cursor init
  Reset scroll listening closer to where its set
  Clarify why we listen to scrolling
  Update tab stops only once when diff changes
This commit is contained in:
Ole Rehmsen
2020-03-31 15:10:21 +00:00
committed by Gerrit Code Review
3 changed files with 46 additions and 30 deletions

View File

@@ -275,7 +275,12 @@ class GrDiffCursor extends mixinBehaviors([Gerrit.FireBehavior],
} }
reInitCursor() { reInitCursor() {
this._updateStops(); if (!this.diffRow) {
// does not scroll during init unless requested
const scrollingBehaviorForInit = this.initialLineNumber ?
ScrollBehavior.KEEP_VISIBLE :
ScrollBehavior.NEVER;
this._scrollBehavior = scrollingBehaviorForInit;
if (this.initialLineNumber) { if (this.initialLineNumber) {
this.moveToLineNumber(this.initialLineNumber, this.side); this.moveToLineNumber(this.initialLineNumber, this.side);
this.initialLineNumber = null; this.initialLineNumber = null;
@@ -283,32 +288,31 @@ class GrDiffCursor extends mixinBehaviors([Gerrit.FireBehavior],
this.moveToFirstChunk(); this.moveToFirstChunk();
} }
} }
this._scrollBehavior = ScrollBehavior.KEEP_VISIBLE;
}
_handleWindowScroll() { _handleWindowScroll() {
if (this._listeningForScroll) { if (this._preventAutoScrollOnManualScroll) {
this._scrollBehavior = ScrollBehavior.NEVER; this._scrollBehavior = ScrollBehavior.NEVER;
this._focusOnMove = false; this._focusOnMove = false;
this._listeningForScroll = false; this._preventAutoScrollOnManualScroll = false;
} }
} }
handleDiffUpdate() { handleDiffUpdate() {
this._updateStops(); this._updateStops();
if (!this.diffRow) {
// does not scroll during init unless requested
const scrollingBehaviorForInit = this.initialLineNumber ?
ScrollBehavior.KEEP_VISIBLE :
ScrollBehavior.NEVER;
this._scrollBehavior = scrollingBehaviorForInit;
this.reInitCursor(); this.reInitCursor();
} }
this._scrollBehavior = ScrollBehavior.KEEP_VISIBLE;
this._focusOnMove = true;
this._listeningForScroll = false;
}
_handleDiffRenderStart() { _handleDiffRenderStart() {
this._listeningForScroll = true; this._preventAutoScrollOnManualScroll = true;
}
_handleDiffRenderContent() {
this._updateStops();
// When done rendering, turn focus on move and automatic scrolling back on
this._focusOnMove = true;
this._preventAutoScrollOnManualScroll = false;
} }
createCommentInPlace() { createCommentInPlace() {
@@ -472,7 +476,8 @@ class GrDiffCursor extends mixinBehaviors([Gerrit.FireBehavior],
i < splice.index + splice.addedCount; i < splice.index + splice.addedCount;
i++) { i++) {
this.listen(this.diffs[i], 'render-start', '_handleDiffRenderStart'); this.listen(this.diffs[i], 'render-start', '_handleDiffRenderStart');
this.listen(this.diffs[i], 'render-content', 'handleDiffUpdate'); this.listen(
this.diffs[i], 'render-content', '_handleDiffRenderContent');
} }
for (i = 0; for (i = 0;
@@ -481,7 +486,7 @@ class GrDiffCursor extends mixinBehaviors([Gerrit.FireBehavior],
this.unlisten(splice.removed[i], this.unlisten(splice.removed[i],
'render-start', '_handleDiffRenderStart'); 'render-start', '_handleDiffRenderStart');
this.unlisten(splice.removed[i], this.unlisten(splice.removed[i],
'render-content', 'handleDiffUpdate'); 'render-content', '_handleDiffRenderContent');
} }
} }
} }

View File

@@ -120,17 +120,20 @@ suite('gr-diff-cursor tests', () => {
}); });
test('cursor scroll behavior', () => { test('cursor scroll behavior', () => {
cursorElement._handleDiffRenderStart();
assert.equal(cursorElement._scrollBehavior, 'keep-visible'); assert.equal(cursorElement._scrollBehavior, 'keep-visible');
cursorElement._handleDiffRenderStart();
assert.isTrue(cursorElement._focusOnMove); assert.isTrue(cursorElement._focusOnMove);
cursorElement._handleWindowScroll(); cursorElement._handleWindowScroll();
assert.equal(cursorElement._scrollBehavior, 'never'); assert.equal(cursorElement._scrollBehavior, 'never');
assert.isFalse(cursorElement._focusOnMove); assert.isFalse(cursorElement._focusOnMove);
cursorElement.handleDiffUpdate(); cursorElement._handleDiffRenderContent();
assert.equal(cursorElement._scrollBehavior, 'keep-visible');
assert.isTrue(cursorElement._focusOnMove); assert.isTrue(cursorElement._focusOnMove);
cursorElement.reInitCursor();
assert.equal(cursorElement._scrollBehavior, 'keep-visible');
}); });
suite('unified diff', () => { suite('unified diff', () => {
@@ -238,6 +241,7 @@ suite('gr-diff-cursor tests', () => {
function renderHandler() { function renderHandler() {
diffElement.removeEventListener('render', renderHandler); diffElement.removeEventListener('render', renderHandler);
cursorElement.reInitCursor();
assert.isFalse(moveToNumStub.called); assert.isFalse(moveToNumStub.called);
assert.isTrue(moveToChunkStub.called); assert.isTrue(moveToChunkStub.called);
assert.equal(scrollBehaviorDuringMove, 'never'); assert.equal(scrollBehaviorDuringMove, 'never');
@@ -255,6 +259,7 @@ suite('gr-diff-cursor tests', () => {
const moveToChunkStub = sandbox.stub(cursorElement, 'moveToFirstChunk'); const moveToChunkStub = sandbox.stub(cursorElement, 'moveToFirstChunk');
function renderHandler() { function renderHandler() {
diffElement.removeEventListener('render', renderHandler); diffElement.removeEventListener('render', renderHandler);
cursorElement.reInitCursor();
assert.isFalse(moveToChunkStub.called); assert.isFalse(moveToChunkStub.called);
assert.isTrue(moveToNumStub.called); assert.isTrue(moveToNumStub.called);
assert.equal(moveToNumStub.lastCall.args[0], 10); assert.equal(moveToNumStub.lastCall.args[0], 10);
@@ -382,11 +387,11 @@ suite('gr-diff-cursor tests', () => {
}); });
test('expand context updates stops', done => { test('expand context updates stops', done => {
sandbox.spy(cursorElement, 'handleDiffUpdate'); sandbox.spy(cursorElement, '_updateStops');
MockInteractions.tap(diffElement.shadowRoot MockInteractions.tap(diffElement.shadowRoot
.querySelector('.showContext')); .querySelector('.showContext'));
flush(() => { flush(() => {
assert.isTrue(cursorElement.handleDiffUpdate.called); assert.isTrue(cursorElement._updateStops.called);
done(); done();
}); });
}); });

View File

@@ -314,6 +314,12 @@ class GrDiffView extends mixinBehaviors( [
this.addEventListener('open-fix-preview', this.addEventListener('open-fix-preview',
this._onOpenFixPreview.bind(this)); this._onOpenFixPreview.bind(this));
this.$.cursor.push('diffs', this.$.diffHost); this.$.cursor.push('diffs', this.$.diffHost);
const onRender = () => {
this.$.diffHost.removeEventListener('render', onRender);
this.$.cursor.reInitCursor();
};
this.$.diffHost.addEventListener('render', onRender);
} }
_getLoggedIn() { _getLoggedIn() {