Tidy up SectionsContainer class layout/ordering

This commit is contained in:
Dean Herbert 2020-04-13 20:12:51 +09:00
parent 4c5d01a611
commit 0be2dc9b2d
1 changed files with 43 additions and 39 deletions

View File

@ -17,12 +17,7 @@ namespace osu.Game.Graphics.Containers
public class SectionsContainer<T> : Container<T>
where T : Drawable
{
private Drawable expandableHeader, fixedHeader, footer, headerBackground;
private readonly OsuScrollContainer scrollContainer;
private readonly Container headerBackgroundContainer;
private FlowContainer<T> scrollContentContainer;
protected override Container<T> Content => scrollContentContainer;
public Bindable<T> SelectedSection { get; } = new Bindable<T>();
public Drawable ExpandableHeader
{
@ -84,6 +79,7 @@ public Drawable HeaderBackground
headerBackgroundContainer.Clear();
headerBackground = value;
if (value == null) return;
headerBackgroundContainer.Add(headerBackground);
@ -92,37 +88,17 @@ public Drawable HeaderBackground
}
}
public Bindable<T> SelectedSection { get; } = new Bindable<T>();
protected override Container<T> Content => scrollContentContainer;
protected virtual FlowContainer<T> CreateScrollContentContainer()
=> new FillFlowContainer<T>
{
Direction = FillDirection.Vertical,
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
};
public override void Add(T drawable)
{
base.Add(drawable);
lastKnownScroll = float.NaN;
headerHeight = float.NaN;
footerHeight = float.NaN;
}
private readonly OsuScrollContainer scrollContainer;
private readonly Container headerBackgroundContainer;
private readonly MarginPadding originalSectionsMargin;
private Drawable expandableHeader, fixedHeader, footer, headerBackground;
private FlowContainer<T> scrollContentContainer;
private float headerHeight, footerHeight;
private readonly MarginPadding originalSectionsMargin;
private void updateSectionsMargin()
{
if (!Children.Any()) return;
var newMargin = originalSectionsMargin;
newMargin.Top += headerHeight;
newMargin.Bottom += footerHeight;
scrollContentContainer.Margin = newMargin;
}
private float lastKnownScroll;
public SectionsContainer()
{
@ -133,22 +109,41 @@ public SectionsContainer()
s.RelativeSizeAxes = Axes.Both;
s.Masking = true;
s.ScrollbarVisible = false;
s.Children = new Drawable[] { scrollContentContainer = CreateScrollContentContainer() };
s.Child = scrollContentContainer = CreateScrollContentContainer();
}),
headerBackgroundContainer = new Container
{
RelativeSizeAxes = Axes.X
}
});
originalSectionsMargin = scrollContentContainer.Margin;
}
public override void Add(T drawable)
{
base.Add(drawable);
lastKnownScroll = float.NaN;
headerHeight = float.NaN;
footerHeight = float.NaN;
}
public void ScrollTo(Drawable section) =>
scrollContainer.ScrollTo(scrollContainer.GetChildPosInContent(section) - (FixedHeader?.BoundingBox.Height ?? 0));
public void ScrollToTop() => scrollContainer.ScrollTo(0);
[NotNull]
protected virtual OsuScrollContainer CreateScrollContainer() => new OsuScrollContainer();
public void ScrollTo(Drawable section) => scrollContainer.ScrollTo(scrollContainer.GetChildPosInContent(section) - (FixedHeader?.BoundingBox.Height ?? 0));
public void ScrollToTop() => scrollContainer.ScrollTo(0);
[NotNull]
protected virtual FlowContainer<T> CreateScrollContentContainer() =>
new FillFlowContainer<T>
{
Direction = FillDirection.Vertical,
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
};
protected override bool OnInvalidate(Invalidation invalidation, InvalidationSource source)
{
@ -163,8 +158,6 @@ protected override bool OnInvalidate(Invalidation invalidation, InvalidationSour
return result;
}
private float lastKnownScroll;
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
@ -215,5 +208,16 @@ protected override void UpdateAfterChildren()
SelectedSection.Value = bestMatch;
}
}
private void updateSectionsMargin()
{
if (!Children.Any()) return;
var newMargin = originalSectionsMargin;
newMargin.Top += headerHeight;
newMargin.Bottom += footerHeight;
scrollContentContainer.Margin = newMargin;
}
}
}