Skip to content

Commit 1747e09

Browse files
committed
Preserve scroll position in AssetBrowser refresh
1 parent f908a87 commit 1747e09

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

game/addons/tools/Code/Editor/AssetBrowser/AssetBrowser.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,46 @@ public AssetListViewMode ViewModeType
126126

127127
private FileSystemWatcher watcher;
128128

129+
int lastKnownScrollPosition = 0;
130+
131+
/// <summary>
132+
/// Retrieves the current vertical scroll position of the asset list.
133+
/// </summary>
134+
private int CaptureScrollPosition()
135+
{
136+
if ( AssetList?.VerticalScrollbar?.IsValid() == true )
137+
{
138+
lastKnownScrollPosition = AssetList.VerticalScrollbar.Value;
139+
}
140+
141+
return lastKnownScrollPosition;
142+
}
143+
144+
/// <summary>
145+
/// Restores the vertical scroll position of the asset list to the specified target value.
146+
/// </summary>
147+
/// <param name="target">The desired scroll position to restore.</param>
148+
private void RestoreScrollPosition( int target )
149+
{
150+
if ( AssetList?.VerticalScrollbar?.IsValid() != true )
151+
return;
152+
153+
void Apply()
154+
{
155+
if ( AssetList?.VerticalScrollbar?.IsValid() != true )
156+
return;
157+
158+
var scrollbar = AssetList.VerticalScrollbar;
159+
var clamped = Math.Clamp( target, scrollbar.Minimum, scrollbar.Maximum );
160+
scrollbar.Value = clamped;
161+
AssetList.SmoothScrollTarget = 0;
162+
lastKnownScrollPosition = clamped;
163+
}
164+
165+
Apply();
166+
MainThread.Queue( Apply );
167+
}
168+
129169
public AssetBrowser( Widget parent ) : this( parent, null )
130170
{
131171

@@ -406,7 +446,7 @@ private async Task<bool> UpdateAssetListAsync( bool recursive, CancellationToken
406446
List<object> items = new List<object>();
407447
var tagCounts = new Dictionary<string, int>();
408448

409-
AssetList.Clear();
449+
var scroll = CaptureScrollPosition();
410450

411451
await Task.Run( () =>
412452
{
@@ -497,6 +537,9 @@ await Task.Run( () =>
497537
return false;
498538

499539
AssetList.SetItems( items );
540+
541+
RestoreScrollPosition( scroll );
542+
500543
if ( !string.IsNullOrEmpty( lastSortColumn ) )
501544
{
502545
SortAssetList( lastSortColumn, lastSortAscending );
@@ -518,6 +561,7 @@ await Task.Run( () =>
518561

519562
private void SortAssetList( string sortBy, bool ascending )
520563
{
564+
var scroll = CaptureScrollPosition();
521565
List<object> items;
522566

523567
switch ( sortBy )
@@ -590,6 +634,7 @@ private void SortAssetList( string sortBy, bool ascending )
590634
items.Reverse();
591635
}
592636
AssetList.SetItems( items );
637+
RestoreScrollPosition( scroll );
593638

594639
lastSortColumn = sortBy;
595640
lastSortAscending = ascending;

0 commit comments

Comments
 (0)