Skip to content

Commit dde44f8

Browse files
committed
try a more permissive scroll bar max history implementation
1 parent a56fd6c commit dde44f8

File tree

1 file changed

+9
-23
lines changed

1 file changed

+9
-23
lines changed

MLEM.Ui/Elements/Panel.cs

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public StyleProp<float> ScrollBarOffset {
5757

5858
private readonly List<Element> relevantChildren = new List<Element>();
5959
private readonly HashSet<Element> scrolledChildren = new HashSet<Element>();
60-
private readonly float[] scrollBarMaxHistory;
60+
private readonly List<float> scrollBarMaxHistory = new List<float>();
6161
private readonly bool scrollOverflow;
6262

6363
private RenderTarget2D renderTarget;
@@ -66,7 +66,6 @@ public StyleProp<float> ScrollBarOffset {
6666
private StyleProp<float> scrollBarOffset;
6767
private float lastScrollOffset;
6868
private bool childrenDirtyForScroll;
69-
private bool scrollBarMaxHistoryDirty;
7069

7170
/// <summary>
7271
/// Creates a new panel with the given settings.
@@ -85,10 +84,6 @@ public Panel(Anchor anchor, Vector2 size, Vector2 positionOffset, bool setHeight
8584
base.CanBeSelected = false;
8685

8786
if (scrollOverflow) {
88-
this.scrollBarMaxHistory = new float[3];
89-
this.scrollBarMaxHistoryDirty = true;
90-
this.ResetScrollBarMaxHistory();
91-
9287
this.ScrollBar = new ScrollBar(Anchor.TopRight, Vector2.Zero, 0, 0) {
9388
OnValueChanged = (element, value) => this.ScrollChildren(),
9489
CanAutoAnchorsAttach = false,
@@ -153,7 +148,7 @@ public override void RemoveChild(Element element) {
153148
throw new NotSupportedException("A panel that scrolls overflow cannot have its scroll bar removed from its list of children");
154149
base.RemoveChild(element);
155150

156-
this.ResetScrollBarMaxHistory();
151+
this.scrollBarMaxHistory.Clear();
157152

158153
// when removing children, our scroll bar might have to be hidden
159154
// if we don't do this before adding children again, they might incorrectly assume that the scroll bar will still be visible and adjust their size accordingly
@@ -166,7 +161,7 @@ public override T AddChild<T>(T element, int index = -1) {
166161
if (this.childrenDirtyForScroll && this.System != null)
167162
this.ScrollSetup();
168163

169-
this.ResetScrollBarMaxHistory();
164+
this.scrollBarMaxHistory.Clear();
170165

171166
return base.AddChild(element, index);
172167
}
@@ -180,7 +175,7 @@ public override void RemoveChildren(Func<Element, bool> condition = null) {
180175
public override void Update(GameTime time) {
181176
// reset the scroll bar's max history when an update happens, at which point we know that any scroll bar recursion has "settled"
182177
// (this reset ensures that the max history is recursion-internal and old values aren't reused when elements get modified later)
183-
this.ResetScrollBarMaxHistory();
178+
this.scrollBarMaxHistory.Clear();
184179
base.Update(time);
185180
}
186181

@@ -340,13 +335,12 @@ protected virtual void ScrollSetup() {
340335
// the max value of the scroll bar is the amount of non-scaled pixels taken up by overflowing components
341336
var scrollBarMax = Math.Max(0, (childrenHeight - this.ChildPaddedArea.Height) / this.Scale);
342337
// avoid an infinite show/hide oscillation that occurs while updating our area by simply using the maximum recent height in that case
343-
if (this.scrollBarMaxHistory[0].Equals(this.scrollBarMaxHistory[2], Element.Epsilon) && this.scrollBarMaxHistory[1].Equals(scrollBarMax, Element.Epsilon))
344-
scrollBarMax = Math.Max(scrollBarMax, this.scrollBarMaxHistory.Max());
338+
if (this.scrollBarMaxHistory.Count(v => v.Equals(scrollBarMax, Element.Epsilon)) >= 2)
339+
scrollBarMax = this.scrollBarMaxHistory.Max();
345340
if (!this.ScrollBar.MaxValue.Equals(scrollBarMax, Element.Epsilon)) {
346-
this.scrollBarMaxHistory[0] = this.scrollBarMaxHistory[1];
347-
this.scrollBarMaxHistory[1] = this.scrollBarMaxHistory[2];
348-
this.scrollBarMaxHistory[2] = scrollBarMax;
349-
this.scrollBarMaxHistoryDirty = true;
341+
this.scrollBarMaxHistory.Add(scrollBarMax);
342+
if (this.scrollBarMaxHistory.Count > 8)
343+
this.scrollBarMaxHistory.RemoveAt(0);
350344

351345
this.ScrollBar.MaxValue = scrollBarMax;
352346
this.relevantChildrenDirty = true;
@@ -431,13 +425,5 @@ private void ScrollChildren() {
431425
this.relevantChildrenDirty = true;
432426
}
433427

434-
private void ResetScrollBarMaxHistory() {
435-
if (this.scrollOverflow && this.scrollBarMaxHistoryDirty) {
436-
for (var i = 0; i < this.scrollBarMaxHistory.Length; i++)
437-
this.scrollBarMaxHistory[i] = -1;
438-
this.scrollBarMaxHistoryDirty = false;
439-
}
440-
}
441-
442428
}
443429
}

0 commit comments

Comments
 (0)