Skip to content

Commit

Permalink
second attempt at scroll remembering
Browse files Browse the repository at this point in the history
  • Loading branch information
factubsio committed Mar 12, 2022
1 parent f32ed67 commit 0921941
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
33 changes: 23 additions & 10 deletions BlueprintExplorer/BlueprintControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ public class BlueprintControl : ScrollableControl
{
public delegate void LinkClickedDelegate(string link, bool newTab);
public delegate void PathDelegate(string path);
public delegate void FilterChangedDelegate(string filter);

public event LinkClickedDelegate OnLinkClicked;
public event PathDelegate OnPathHovered;
public event FilterChangedDelegate OnFilterChanged;

private Dictionary<string, int> ScrollPositionCache = new();
private Dictionary<string, (int position, string filter)> HistoryCache = new();

private IDisplayableElementCollection DisplayedObject;

Expand All @@ -34,7 +36,7 @@ public IDisplayableElementCollection Blueprint
if (DisplayedObject == value) return;
if (DisplayedObject != null)
{
ScrollPositionCache[DisplayedObject.GuidText] = VerticalScroll.Value;
HistoryCache[DisplayedObject.GuidText] = (VerticalScroll.Value, _Filter);
}
DisplayedObject = value;
DisplayedObject.EnsureParsed();
Expand Down Expand Up @@ -215,7 +217,7 @@ private void UpdateRowHoverColor()

private int Count => Remap.Count;

private void ValidateFilter()
private void ValidateFilter(int? scrollTo)
{
Remap.Clear();
if (_Filter.Length == 0)
Expand Down Expand Up @@ -268,6 +270,10 @@ private void ValidateFilter()
wantedHeight = Count * RowHeight;
AutoScrollMinSize = new Size(1, wantedHeight);
Invalidate();
if (scrollTo != null)
{
AutoScrollPosition = new Point(0, scrollTo.Value);
}
}

int StringWidthAllowed => Width - NameColumnWidth - 32;
Expand Down Expand Up @@ -424,17 +430,20 @@ private void ValidateBlueprint(bool scroll)
AutoScroll = true;
if (scroll)
{
if (ScrollPositionCache.TryGetValue(DisplayedObject.GuidText, out var scrollPosition))
if (HistoryCache.TryGetValue(DisplayedObject.GuidText, out var history))
{
VerticalScroll.Value = scrollPosition;
_Filter = history.filter;
ValidateFilter(history.position);
OnFilterChanged?.Invoke(_Filter);
}
else
{
VerticalScroll.Value = 0;

_Filter = "";
ValidateFilter(null);
OnFilterChanged?.Invoke(_Filter);
}
}
ValidateFilter();
}

public override Size GetPreferredSize(Size proposedSize) => new(proposedSize.Width, wantedHeight);
Expand All @@ -459,8 +468,12 @@ public string Filter
get => _Filter;
set
{
_Filter = value?.Trim() ?? "";
ValidateFilter();
var newFilter = value?.Trim() ?? "";
if (_Filter != newFilter)
{
_Filter = newFilter;
ValidateFilter(null);
}
}
}

Expand Down Expand Up @@ -647,7 +660,7 @@ private void Toggle(RowElement elem)
else
elem.Collapsed = !elem.Collapsed;

ValidateFilter();
ValidateFilter(null);
}

protected override void OnMouseDoubleClick(MouseEventArgs e)
Expand Down
5 changes: 5 additions & 0 deletions BlueprintExplorer/BlueprintViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public BlueprintViewer()
currentPath.Text = path ?? "-";
};

view.OnFilterChanged += filterValue =>
{
filter.Text = filterValue;
};

filter.TextChanged += (sender, e) => view.Filter = filter.Text;
if (Form1.Dark)
{
Expand Down

0 comments on commit 0921941

Please sign in to comment.