1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-09 19:18:44 +02:00

Fixed the scroll bar of an empty panel being positioned incorrectly

This commit is contained in:
Ell 2022-09-24 11:04:23 +02:00
parent 74b3c426b8
commit d6a51776e5
2 changed files with 21 additions and 17 deletions

View file

@ -46,6 +46,7 @@ Fixes
- Fixed UiMarkdownParser not parsing formatting in headings and blockquotes
- Fixed Element.OnChildAdded and Element.OnChildRemoved being called for grandchildren when a child is added
- Fixed an exception when trying to force-update the area of an element without a ui system
- Fixed the scroll bar of an empty panel being positioned incorrectly
### MLEM.Data
Additions

View file

@ -84,8 +84,9 @@ namespace MLEM.Ui.Elements {
return;
if (e == null || !e.GetParentTree().Contains(this))
return;
var firstChild = this.Children.First(c => c != this.ScrollBar);
this.ScrollBar.CurrentValue = (e.Area.Center.Y - this.Area.Height / 2 - firstChild.Area.Top) / e.Scale + this.ChildPadding.Value.Height / 2;
var firstChild = this.Children.FirstOrDefault(c => c != this.ScrollBar);
if (firstChild != null)
this.ScrollBar.CurrentValue = (e.Area.Center.Y - this.Area.Height / 2 - firstChild.Area.Top) / e.Scale + this.ChildPadding.Value.Height / 2;
};
this.AddChild(this.ScrollBar);
}
@ -231,28 +232,30 @@ namespace MLEM.Ui.Elements {
protected virtual void ScrollSetup() {
if (!this.scrollOverflow || this.IsHidden)
return;
// if there is only one child, then we have just the scroll bar
if (this.Children.Count == 1)
return;
// the "real" first child is the scroll bar, which we want to ignore
var firstChild = this.Children.First(c => c != this.ScrollBar);
var lowestChild = this.GetLowestChild(c => c != this.ScrollBar && !c.IsHidden);
var childrenHeight = lowestChild.Area.Bottom - firstChild.Area.Top;
float childrenHeight;
if (this.Children.Count > 1) {
var firstChild = this.Children.FirstOrDefault(c => c != this.ScrollBar);
var lowestChild = this.GetLowestChild(c => c != this.ScrollBar && !c.IsHidden);
childrenHeight = lowestChild.Area.Bottom - firstChild.Area.Top;
} else {
// if we only have one child (the scroll bar), then the children take up no visual height
childrenHeight = 0;
}
// the max value of the scrollbar is the amount of non-scaled pixels taken up by overflowing components
// the max value of the scroll bar is the amount of non-scaled pixels taken up by overflowing components
var scrollBarMax = (childrenHeight - this.ChildPaddedArea.Height) / this.Scale;
if (!this.ScrollBar.MaxValue.Equals(scrollBarMax, Element.Epsilon)) {
this.ScrollBar.MaxValue = scrollBarMax;
this.relevantChildrenDirty = true;
}
// update child padding based on whether the scroll bar is visible
var childOffset = this.ScrollBar.IsHidden ? 0 : this.ScrollerSize.Value.X + this.ScrollBarOffset;
if (!this.scrollBarChildOffset.Equals(childOffset, Element.Epsilon)) {
this.ChildPadding += new Padding(0, -this.scrollBarChildOffset + childOffset, 0, 0);
this.scrollBarChildOffset = childOffset;
this.SetAreaDirty();
}
// update child padding based on whether the scroll bar is visible
var childOffset = this.ScrollBar.IsHidden ? 0 : this.ScrollerSize.Value.X + this.ScrollBarOffset;
if (!this.scrollBarChildOffset.Equals(childOffset, Element.Epsilon)) {
this.ChildPadding += new Padding(0, -this.scrollBarChildOffset + childOffset, 0, 0);
this.scrollBarChildOffset = childOffset;
this.SetAreaDirty();
}
// the scroller height has the same relation to the scroll bar height as the visible area has to the total height of the panel's content