Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 4f7ceac

Browse files
Maintaining scroll state when history items get added
1 parent f7acb86 commit 4f7ceac

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/HistoryView.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,26 +225,45 @@ private void DrawTimelineRectAroundIconRect(Rect parentRect, Rect iconRect)
225225
public void Load(int loadAhead, List<GitLogEntry> loadEntries)
226226
{
227227
var selectedCommitId = SelectedGitLogEntry.CommitID;
228+
var scrollValue = scroll.y;
229+
230+
var previousCount = entries.Count;
231+
232+
var scrollIndex = (int)(scrollValue / Styles.HistoryEntryHeight);
228233

229234
statusAhead = loadAhead;
230235
entries = loadEntries;
231236

232-
var changed = false;
237+
var selectionPresent = false;
233238
for (var index = 0; index < entries.Count; index++)
234239
{
235240
var gitLogEntry = entries[index];
236241
if (gitLogEntry.CommitID.Equals(selectedCommitId))
237242
{
238243
selectedIndex = index;
239-
changed = true;
244+
selectionPresent = true;
240245
break;
241246
}
242247
}
243248

244-
if (!changed)
249+
if (!selectionPresent)
245250
{
246251
selectedIndex = -1;
247252
}
253+
254+
if (scrollIndex > entries.Count)
255+
{
256+
ScrollTo(0);
257+
}
258+
else
259+
{
260+
var scrollOffset = scrollValue % Styles.HistoryEntryHeight;
261+
262+
var scrollIndexFromBottom = previousCount - scrollIndex;
263+
var newScrollIndex = entries.Count - scrollIndexFromBottom;
264+
265+
ScrollTo(newScrollIndex, scrollOffset);
266+
}
248267
}
249268

250269
private int SelectNext(int index)
@@ -279,9 +298,9 @@ private int SelectPrevious(int index)
279298
return index;
280299
}
281300

282-
public void ScrollTo(int index)
301+
public void ScrollTo(int index, float offset = 0f)
283302
{
284-
scroll.Set(scroll.x, Styles.HistoryEntryHeight * index);
303+
scroll.Set(scroll.x, Styles.HistoryEntryHeight * index + offset);
285304
}
286305
}
287306

0 commit comments

Comments
 (0)